blob: a983bdefdaa2c3bb8b518c03d024528400b7688c [file] [log] [blame]
Frank Farzan37761d12011-12-01 14:29:08 -08001#!/usr/bin/python
2#
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
9import os
10import shutil
11import tempfile
12import unittest
13
Gilad Arnoldabb352e2012-09-23 01:24:27 -070014import mox
15
Gilad Arnoldc65330c2012-09-20 15:17:48 -070016import build_artifact
17import common_util
joychenf3482692013-07-09 11:03:43 -070018import devserver_constants
Frank Farzan37761d12011-12-01 14:29:08 -080019
20
21# Fake Dev Server Layout:
22TEST_LAYOUT = {
23 'test-board-1': ['R17-1413.0.0-a1-b1346', 'R17-18.0.0-a1-b1346'],
Scott Zawalski16954532012-03-20 15:31:36 -040024 'test-board-2': ['R16-2241.0.0-a0-b2', 'R17-2.0.0-a1-b1346'],
25 'test-board-3': []
Frank Farzan37761d12011-12-01 14:29:08 -080026}
27
28
Gilad Arnold17fe03d2012-10-02 10:05:01 -070029class CommonUtilTest(mox.MoxTestBase):
Frank Farzan37761d12011-12-01 14:29:08 -080030
31 def setUp(self):
Chris Sosaea148d92012-03-06 16:22:04 -080032 mox.MoxTestBase.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.
Frank Farzan37761d12011-12-01 14:29:08 -080037 for board, builds in TEST_LAYOUT.iteritems():
38 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(
Chris Sosa76e44b92013-01-31 12:11:38 -080044 build_path, build_artifact.TEST_IMAGE_FILE), 'w') as f:
45 f.write('TEST_IMAGE_FILE')
Chris Sosa47a7d4e2012-03-28 11:26:55 -070046 with open(os.path.join(
Chris Sosa76e44b92013-01-31 12:11:38 -080047 build_path, build_artifact.STATEFUL_UPDATE_FILE), 'w') as f:
48 f.write('STATEFUL_UPDATE_FILE')
Gilad Arnoldc65330c2012-09-20 15:17:48 -070049 with open(os.path.join(
joychenf3482692013-07-09 11:03:43 -070050 build_path, devserver_constants.ROOT_UPDATE_FILE), 'w') as f:
Chris Sosa76e44b92013-01-31 12:11:38 -080051 f.write('ROOT_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.
69 self.assertFalse(
Chris Sosa76e44b92013-01-31 12:11:38 -080070 common_util.PathInDir(
Gilad Arnoldc65330c2012-09-20 15:17:48 -070071 self._static_dir, self._outside_sandbox_dir))
Frank Farzan37761d12011-12-01 14:29:08 -080072
73 # Path contains '..'.
74 self.assertFalse(
Chris Sosa76e44b92013-01-31 12:11:38 -080075 common_util.PathInDir(
Frank Farzan37761d12011-12-01 14:29:08 -080076 self._static_dir, os.path.join(self._static_dir, os.pardir)))
77
78 # Path contains symbolic link references.
79 os.chdir(self._static_dir)
80 os.symlink(os.pardir, 'parent')
81 self.assertFalse(
Chris Sosa76e44b92013-01-31 12:11:38 -080082 common_util.PathInDir(
Frank Farzan37761d12011-12-01 14:29:08 -080083 self._static_dir, os.path.join(self._static_dir, os.pardir)))
84
Frank Farzan37761d12011-12-01 14:29:08 -080085 def testGetLatestBuildVersion(self):
Chris Sosa76e44b92013-01-31 12:11:38 -080086 """Tests that the latest version is correct given our setup."""
Frank Farzan37761d12011-12-01 14:29:08 -080087 self.assertEqual(
Gilad Arnoldc65330c2012-09-20 15:17:48 -070088 common_util.GetLatestBuildVersion(self._static_dir, 'test-board-1'),
Frank Farzan37761d12011-12-01 14:29:08 -080089 'R17-1413.0.0-a1-b1346')
90
Scott Zawalski16954532012-03-20 15:31:36 -040091 def testGetLatestBuildVersionLatest(self):
Gilad Arnold17fe03d2012-10-02 10:05:01 -070092 """Test that we raise CommonUtilError when a build dir is empty."""
93 self.assertRaises(common_util.CommonUtilError,
Gilad Arnoldc65330c2012-09-20 15:17:48 -070094 common_util.GetLatestBuildVersion,
Scott Zawalski16954532012-03-20 15:31:36 -040095 self._static_dir, 'test-board-3')
Frank Farzan37761d12011-12-01 14:29:08 -080096
Scott Zawalski16954532012-03-20 15:31:36 -040097 def testGetLatestBuildVersionUnknownBuild(self):
Gilad Arnold17fe03d2012-10-02 10:05:01 -070098 """Test that we raise CommonUtilError when a build dir does not exist."""
99 self.assertRaises(common_util.CommonUtilError,
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700100 common_util.GetLatestBuildVersion,
Scott Zawalski16954532012-03-20 15:31:36 -0400101 self._static_dir, 'bad-dir')
Frank Farzan37761d12011-12-01 14:29:08 -0800102
Scott Zawalski16954532012-03-20 15:31:36 -0400103 def testGetLatestBuildVersionMilestone(self):
104 """Test that we can get builds based on milestone."""
105 expected_build_str = 'R16-2241.0.0-a0-b2'
106 milestone = 'R16'
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700107 build_str = common_util.GetLatestBuildVersion(
Scott Zawalski16954532012-03-20 15:31:36 -0400108 self._static_dir, 'test-board-2', milestone)
109 self.assertEqual(expected_build_str, build_str)
Frank Farzan37761d12011-12-01 14:29:08 -0800110
Frank Farzan37761d12011-12-01 14:29:08 -0800111 def testGetControlFile(self):
Chris Sosa76e44b92013-01-31 12:11:38 -0800112 """Creates a fake control file and verifies that we can get it."""
Frank Farzan37761d12011-12-01 14:29:08 -0800113 control_file_dir = os.path.join(
Chris Sosaea148d92012-03-06 16:22:04 -0800114 self._static_dir, 'test-board-1', 'R17-1413.0.0-a1-b1346', 'autotest',
115 'server', 'site_tests', 'network_VPN')
Frank Farzan37761d12011-12-01 14:29:08 -0800116 os.makedirs(control_file_dir)
117 with open(os.path.join(control_file_dir, 'control'), 'w') as f:
118 f.write('hello!')
119
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700120 control_content = common_util.GetControlFile(
Chris Sosaea148d92012-03-06 16:22:04 -0800121 self._static_dir, 'test-board-1/R17-1413.0.0-a1-b1346',
Frank Farzan37761d12011-12-01 14:29:08 -0800122 os.path.join('server', 'site_tests', 'network_VPN', 'control'))
123 self.assertEqual(control_content, 'hello!')
124
Frank Farzan37761d12011-12-01 14:29:08 -0800125
126if __name__ == '__main__':
127 unittest.main()