blob: 9cc0165a2e932e2bc1d68cba7885d17c05f5e6fd [file] [log] [blame]
Frank Farzan37761d12011-12-01 22:29:081#!/usr/bin/python
2#
Chris Sosa781ba6d2012-04-11 19:44:433# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Frank Farzan37761d12011-12-01 22:29:084# 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 22:17:487"""Unit tests for common_util module."""
Frank Farzan37761d12011-12-01 22:29:088
9import os
10import shutil
11import tempfile
12import unittest
13
Gilad Arnoldabb352e2012-09-23 08:24:2714import mox
15
Gilad Arnoldc65330c2012-09-20 22:17:4816import common_util
joychenf3482692013-07-09 18:03:4317import devserver_constants
Frank Farzan37761d12011-12-01 22:29:0818
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 19:31:3623 'test-board-2': ['R16-2241.0.0-a0-b2', 'R17-2.0.0-a1-b1346'],
24 'test-board-3': []
Frank Farzan37761d12011-12-01 22:29:0825}
26
27
Gilad Arnold17fe03d2012-10-02 17:05:0128class CommonUtilTest(mox.MoxTestBase):
Frank Farzan37761d12011-12-01 22:29:0829
30 def setUp(self):
Chris Sosaea148d92012-03-07 00:22:0431 mox.MoxTestBase.setUp(self)
Gilad Arnoldc65330c2012-09-20 22:17:4832 self._static_dir = tempfile.mkdtemp('common_util_unittest')
33 self._outside_sandbox_dir = tempfile.mkdtemp('common_util_unittest')
Frank Farzan37761d12011-12-01 22:29:0834
Chris Sosa76e44b92013-01-31 20:11:3835 # Set up some basic existing structure used by GetLatest* tests.
Frank Farzan37761d12011-12-01 22:29:0836 for board, builds in TEST_LAYOUT.iteritems():
37 board_path = os.path.join(self._static_dir, board)
38 os.mkdir(board_path)
39 for build in builds:
40 build_path = os.path.join(board_path, build)
41 os.mkdir(build_path)
Gilad Arnoldc65330c2012-09-20 22:17:4842 with open(os.path.join(
joychen921e1fb2013-06-28 18:12:2043 build_path, devserver_constants.TEST_IMAGE_FILE), 'w') as f:
Chris Sosa76e44b92013-01-31 20:11:3844 f.write('TEST_IMAGE_FILE')
Chris Sosa47a7d4e2012-03-28 18:26:5545 with open(os.path.join(
joychen121fc9b2013-08-02 21:30:3046 build_path, devserver_constants.STATEFUL_FILE), 'w') as f:
47 f.write('STATEFUL_FILE')
Gilad Arnoldc65330c2012-09-20 22:17:4848 with open(os.path.join(
joychen7c2054a2013-07-25 18:14:0749 build_path, devserver_constants.UPDATE_FILE), 'w') as f:
50 f.write('UPDATE_FILE')
Chris Sosaea148d92012-03-07 00:22:0451
Frank Farzan37761d12011-12-01 22:29:0852 def tearDown(self):
53 shutil.rmtree(self._static_dir)
54 shutil.rmtree(self._outside_sandbox_dir)
55
Chris Sosa76e44b92013-01-31 20:11:3856 def testPathInDir(self):
57 """Various tests around the PathInDir test."""
Frank Farzan37761d12011-12-01 22:29:0858 # Path is in sandbox.
59 self.assertTrue(
Chris Sosa76e44b92013-01-31 20:11:3860 common_util.PathInDir(
Frank Farzan37761d12011-12-01 22:29:0861 self._static_dir, os.path.join(self._static_dir, 'some-board')))
62
63 # Path is sandbox.
64 self.assertFalse(
Chris Sosa76e44b92013-01-31 20:11:3865 common_util.PathInDir(self._static_dir, self._static_dir))
Frank Farzan37761d12011-12-01 22:29:0866
67 # Path is outside the sandbox.
68 self.assertFalse(
Chris Sosa76e44b92013-01-31 20:11:3869 common_util.PathInDir(
Gilad Arnoldc65330c2012-09-20 22:17:4870 self._static_dir, self._outside_sandbox_dir))
Frank Farzan37761d12011-12-01 22:29:0871
72 # Path contains '..'.
73 self.assertFalse(
Chris Sosa76e44b92013-01-31 20:11:3874 common_util.PathInDir(
Frank Farzan37761d12011-12-01 22:29:0875 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 20:11:3881 common_util.PathInDir(
Frank Farzan37761d12011-12-01 22:29:0882 self._static_dir, os.path.join(self._static_dir, os.pardir)))
83
Frank Farzan37761d12011-12-01 22:29:0884 def testGetLatestBuildVersion(self):
Chris Sosa76e44b92013-01-31 20:11:3885 """Tests that the latest version is correct given our setup."""
Frank Farzan37761d12011-12-01 22:29:0886 self.assertEqual(
Gilad Arnoldc65330c2012-09-20 22:17:4887 common_util.GetLatestBuildVersion(self._static_dir, 'test-board-1'),
Frank Farzan37761d12011-12-01 22:29:0888 'R17-1413.0.0-a1-b1346')
89
Scott Zawalski16954532012-03-20 19:31:3690 def testGetLatestBuildVersionLatest(self):
Gilad Arnold17fe03d2012-10-02 17:05:0191 """Test that we raise CommonUtilError when a build dir is empty."""
92 self.assertRaises(common_util.CommonUtilError,
Gilad Arnoldc65330c2012-09-20 22:17:4893 common_util.GetLatestBuildVersion,
Scott Zawalski16954532012-03-20 19:31:3694 self._static_dir, 'test-board-3')
Frank Farzan37761d12011-12-01 22:29:0895
Scott Zawalski16954532012-03-20 19:31:3696 def testGetLatestBuildVersionUnknownBuild(self):
Gilad Arnold17fe03d2012-10-02 17:05:0197 """Test that we raise CommonUtilError when a build dir does not exist."""
98 self.assertRaises(common_util.CommonUtilError,
Gilad Arnoldc65330c2012-09-20 22:17:4899 common_util.GetLatestBuildVersion,
Scott Zawalski16954532012-03-20 19:31:36100 self._static_dir, 'bad-dir')
Frank Farzan37761d12011-12-01 22:29:08101
Scott Zawalski16954532012-03-20 19:31:36102 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 22:17:48106 build_str = common_util.GetLatestBuildVersion(
Scott Zawalski16954532012-03-20 19:31:36107 self._static_dir, 'test-board-2', milestone)
108 self.assertEqual(expected_build_str, build_str)
Frank Farzan37761d12011-12-01 22:29:08109
Frank Farzan37761d12011-12-01 22:29:08110 def testGetControlFile(self):
Chris Sosa76e44b92013-01-31 20:11:38111 """Creates a fake control file and verifies that we can get it."""
Frank Farzan37761d12011-12-01 22:29:08112 control_file_dir = os.path.join(
Chris Sosaea148d92012-03-07 00:22:04113 self._static_dir, 'test-board-1', 'R17-1413.0.0-a1-b1346', 'autotest',
114 'server', 'site_tests', 'network_VPN')
Frank Farzan37761d12011-12-01 22:29:08115 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 22:17:48119 control_content = common_util.GetControlFile(
Chris Sosaea148d92012-03-07 00:22:04120 self._static_dir, 'test-board-1/R17-1413.0.0-a1-b1346',
Frank Farzan37761d12011-12-01 22:29:08121 os.path.join('server', 'site_tests', 'network_VPN', 'control'))
122 self.assertEqual(control_content, 'hello!')
123
Alex Deymo3e2d4952013-09-04 04:49:41124 def testSymlinkFile(self):
125 link_fd, link_base = tempfile.mkstemp(prefix="common-symlink-test")
126 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
Frank Farzan37761d12011-12-01 22:29:08149
150if __name__ == '__main__':
151 unittest.main()