blob: c637d95a33901ff22386e5e4a4469bfaeb6ab77f [file] [log] [blame]
Mike Frysingera7f08bc2019-08-27 15:16:33 -04001#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
Chris Sosa781ba6d2012-04-11 12:44:43 -07003# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Frank Farzan37761d12011-12-01 14:29:08 -08004# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
Gilad Arnoldc65330c2012-09-20 15:17:48 -07007"""Unit tests for common_util module."""
Frank Farzan37761d12011-12-01 14:29:08 -08008
Achuith Bhandarkar1a8a7f92019-09-26 15:35:23 +02009from __future__ import print_function
10
Frank Farzan37761d12011-12-01 14:29:08 -080011import os
12import shutil
13import tempfile
14import unittest
15
Gilad Arnoldc65330c2012-09-20 15:17:48 -070016import common_util
joychenf3482692013-07-09 11:03:43 -070017import devserver_constants
Frank Farzan37761d12011-12-01 14:29:08 -080018
19
20# Fake Dev Server Layout:
21TEST_LAYOUT = {
22 'test-board-1': ['R17-1413.0.0-a1-b1346', 'R17-18.0.0-a1-b1346'],
Scott Zawalski16954532012-03-20 15:31:36 -040023 'test-board-2': ['R16-2241.0.0-a0-b2', 'R17-2.0.0-a1-b1346'],
24 'test-board-3': []
Frank Farzan37761d12011-12-01 14:29:08 -080025}
26
27
Achuith Bhandarkar1a8a7f92019-09-26 15:35:23 +020028
29class CommonUtilTest(unittest.TestCase):
30 """Base class for CommonUtil tests."""
Frank Farzan37761d12011-12-01 14:29:08 -080031
32 def setUp(self):
Gilad Arnoldc65330c2012-09-20 15:17:48 -070033 self._static_dir = tempfile.mkdtemp('common_util_unittest')
34 self._outside_sandbox_dir = tempfile.mkdtemp('common_util_unittest')
Frank Farzan37761d12011-12-01 14:29:08 -080035
Chris Sosa76e44b92013-01-31 12:11:38 -080036 # Set up some basic existing structure used by GetLatest* tests.
Achuith Bhandarkar1a8a7f92019-09-26 15:35:23 +020037 for board, builds in TEST_LAYOUT.items():
Frank Farzan37761d12011-12-01 14:29:08 -080038 board_path = os.path.join(self._static_dir, board)
39 os.mkdir(board_path)
40 for build in builds:
41 build_path = os.path.join(board_path, build)
42 os.mkdir(build_path)
Gilad Arnoldc65330c2012-09-20 15:17:48 -070043 with open(os.path.join(
Achuith Bhandarkar1a8a7f92019-09-26 15:35:23 +020044 build_path, devserver_constants.TEST_IMAGE_FILE), 'w') as f:
Chris Sosa76e44b92013-01-31 12:11:38 -080045 f.write('TEST_IMAGE_FILE')
Chris Sosa47a7d4e2012-03-28 11:26:55 -070046 with open(os.path.join(
Achuith Bhandarkar1a8a7f92019-09-26 15:35:23 +020047 build_path, devserver_constants.STATEFUL_FILE), 'w') as f:
joychen121fc9b2013-08-02 14:30:30 -070048 f.write('STATEFUL_FILE')
Gilad Arnoldc65330c2012-09-20 15:17:48 -070049 with open(os.path.join(
Achuith Bhandarkar1a8a7f92019-09-26 15:35:23 +020050 build_path, devserver_constants.UPDATE_FILE), 'w') as f:
joychen7c2054a2013-07-25 11:14:07 -070051 f.write('UPDATE_FILE')
Chris Sosaea148d92012-03-06 16:22:04 -080052
Frank Farzan37761d12011-12-01 14:29:08 -080053 def tearDown(self):
54 shutil.rmtree(self._static_dir)
55 shutil.rmtree(self._outside_sandbox_dir)
56
Chris Sosa76e44b92013-01-31 12:11:38 -080057 def testPathInDir(self):
58 """Various tests around the PathInDir test."""
Frank Farzan37761d12011-12-01 14:29:08 -080059 # Path is in sandbox.
60 self.assertTrue(
Chris Sosa76e44b92013-01-31 12:11:38 -080061 common_util.PathInDir(
Frank Farzan37761d12011-12-01 14:29:08 -080062 self._static_dir, os.path.join(self._static_dir, 'some-board')))
63
64 # Path is sandbox.
65 self.assertFalse(
Chris Sosa76e44b92013-01-31 12:11:38 -080066 common_util.PathInDir(self._static_dir, self._static_dir))
Frank Farzan37761d12011-12-01 14:29:08 -080067
68 # Path is outside the sandbox.
Achuith Bhandarkar1a8a7f92019-09-26 15:35:23 +020069 self.assertFalse(common_util.PathInDir(
70 self._static_dir, self._outside_sandbox_dir))
Frank Farzan37761d12011-12-01 14:29:08 -080071
72 # Path contains '..'.
73 self.assertFalse(
Chris Sosa76e44b92013-01-31 12:11:38 -080074 common_util.PathInDir(
Frank Farzan37761d12011-12-01 14:29:08 -080075 self._static_dir, os.path.join(self._static_dir, os.pardir)))
76
77 # Path contains symbolic link references.
78 os.chdir(self._static_dir)
79 os.symlink(os.pardir, 'parent')
80 self.assertFalse(
Chris Sosa76e44b92013-01-31 12:11:38 -080081 common_util.PathInDir(
Frank Farzan37761d12011-12-01 14:29:08 -080082 self._static_dir, os.path.join(self._static_dir, os.pardir)))
83
Frank Farzan37761d12011-12-01 14:29:08 -080084 def testGetLatestBuildVersion(self):
Chris Sosa76e44b92013-01-31 12:11:38 -080085 """Tests that the latest version is correct given our setup."""
Frank Farzan37761d12011-12-01 14:29:08 -080086 self.assertEqual(
Gilad Arnoldc65330c2012-09-20 15:17:48 -070087 common_util.GetLatestBuildVersion(self._static_dir, 'test-board-1'),
Frank Farzan37761d12011-12-01 14:29:08 -080088 'R17-1413.0.0-a1-b1346')
89
Scott Zawalski16954532012-03-20 15:31:36 -040090 def testGetLatestBuildVersionLatest(self):
Gilad Arnold17fe03d2012-10-02 10:05:01 -070091 """Test that we raise CommonUtilError when a build dir is empty."""
92 self.assertRaises(common_util.CommonUtilError,
Gilad Arnoldc65330c2012-09-20 15:17:48 -070093 common_util.GetLatestBuildVersion,
Scott Zawalski16954532012-03-20 15:31:36 -040094 self._static_dir, 'test-board-3')
Frank Farzan37761d12011-12-01 14:29:08 -080095
Scott Zawalski16954532012-03-20 15:31:36 -040096 def testGetLatestBuildVersionUnknownBuild(self):
Gilad Arnold17fe03d2012-10-02 10:05:01 -070097 """Test that we raise CommonUtilError when a build dir does not exist."""
98 self.assertRaises(common_util.CommonUtilError,
Gilad Arnoldc65330c2012-09-20 15:17:48 -070099 common_util.GetLatestBuildVersion,
Scott Zawalski16954532012-03-20 15:31:36 -0400100 self._static_dir, 'bad-dir')
Frank Farzan37761d12011-12-01 14:29:08 -0800101
Scott Zawalski16954532012-03-20 15:31:36 -0400102 def testGetLatestBuildVersionMilestone(self):
103 """Test that we can get builds based on milestone."""
104 expected_build_str = 'R16-2241.0.0-a0-b2'
105 milestone = 'R16'
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700106 build_str = common_util.GetLatestBuildVersion(
Scott Zawalski16954532012-03-20 15:31:36 -0400107 self._static_dir, 'test-board-2', milestone)
108 self.assertEqual(expected_build_str, build_str)
Frank Farzan37761d12011-12-01 14:29:08 -0800109
Frank Farzan37761d12011-12-01 14:29:08 -0800110 def testGetControlFile(self):
Chris Sosa76e44b92013-01-31 12:11:38 -0800111 """Creates a fake control file and verifies that we can get it."""
Frank Farzan37761d12011-12-01 14:29:08 -0800112 control_file_dir = os.path.join(
Chris Sosaea148d92012-03-06 16:22:04 -0800113 self._static_dir, 'test-board-1', 'R17-1413.0.0-a1-b1346', 'autotest',
114 'server', 'site_tests', 'network_VPN')
Frank Farzan37761d12011-12-01 14:29:08 -0800115 os.makedirs(control_file_dir)
116 with open(os.path.join(control_file_dir, 'control'), 'w') as f:
117 f.write('hello!')
118
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700119 control_content = common_util.GetControlFile(
Chris Sosaea148d92012-03-06 16:22:04 -0800120 self._static_dir, 'test-board-1/R17-1413.0.0-a1-b1346',
Frank Farzan37761d12011-12-01 14:29:08 -0800121 os.path.join('server', 'site_tests', 'network_VPN', 'control'))
122 self.assertEqual(control_content, 'hello!')
123
Alex Deymo3e2d4952013-09-03 21:49:41 -0700124 def testSymlinkFile(self):
Achuith Bhandarkar1a8a7f92019-09-26 15:35:23 +0200125 link_fd, link_base = tempfile.mkstemp(prefix='common-symlink-test')
Alex Deymo3e2d4952013-09-03 21:49:41 -0700126 link_a = link_base + '-link-a'
127 link_b = link_base + '-link-b'
128
129 # Create the "link a" --> "base".
130 common_util.SymlinkFile(link_base, link_a)
131 self.assertTrue(os.path.lexists(link_a))
132 self.assertEqual(os.readlink(link_a), link_base)
133
134 # Create the "link b" --> "base".
135 common_util.SymlinkFile(link_base, link_b)
136 self.assertTrue(os.path.lexists(link_b))
137 self.assertEqual(os.readlink(link_b), link_base)
138
139 # Replace the existing "link b" to point to "link a".
140 common_util.SymlinkFile(link_a, link_b)
141 self.assertTrue(os.path.lexists(link_b))
142 self.assertEqual(os.readlink(link_b), link_a)
143
144 os.close(link_fd)
145 os.unlink(link_b)
146 os.unlink(link_a)
147 os.unlink(link_base)
148
Aseda Aboagyeb6ba5492016-10-14 13:20:09 -0700149 def testExtractTarballMissingFile(self):
150 """Verify that stderr from tar is printed if in encounters an error."""
151 tarball = 'a-tarball-which-does-not-exist.tar.gz'
152 dest = self._static_dir
153
154 try:
155 common_util.ExtractTarball(tarball, dest)
156 except common_util.CommonUtilError as e:
157 # Check to see that tar's error message is printed in the exception.
158 self.assertTrue('Cannot open: No such file or directory' in e.args[0],
Achuith Bhandarkar1a8a7f92019-09-26 15:35:23 +0200159 ("tar's stderr is missing from the exception.\n%s" %
Aseda Aboagyeb6ba5492016-10-14 13:20:09 -0700160 e.args[0]))
161
Frank Farzan37761d12011-12-01 14:29:08 -0800162
163if __name__ == '__main__':
164 unittest.main()