blob: 6cd4f219af5474fade4b66a278bf157ee021f850 [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 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
Gilad Arnold17fe03d2012-10-02 10:05:01 -070028class CommonUtilTest(mox.MoxTestBase):
Frank Farzan37761d12011-12-01 14:29:08 -080029
30 def setUp(self):
Chris Sosaea148d92012-03-06 16:22:04 -080031 mox.MoxTestBase.setUp(self)
Gilad Arnoldc65330c2012-09-20 15:17:48 -070032 self._static_dir = tempfile.mkdtemp('common_util_unittest')
33 self._outside_sandbox_dir = tempfile.mkdtemp('common_util_unittest')
Frank Farzan37761d12011-12-01 14:29:08 -080034
Chris Sosa76e44b92013-01-31 12:11:38 -080035 # Set up some basic existing structure used by GetLatest* tests.
Frank Farzan37761d12011-12-01 14:29:08 -080036 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 15:17:48 -070042 with open(os.path.join(
joychen921e1fb2013-06-28 11:12:20 -070043 build_path, devserver_constants.TEST_IMAGE_FILE), 'w') as f:
Chris Sosa76e44b92013-01-31 12:11:38 -080044 f.write('TEST_IMAGE_FILE')
Chris Sosa47a7d4e2012-03-28 11:26:55 -070045 with open(os.path.join(
joychen121fc9b2013-08-02 14:30:30 -070046 build_path, devserver_constants.STATEFUL_FILE), 'w') as f:
47 f.write('STATEFUL_FILE')
Gilad Arnoldc65330c2012-09-20 15:17:48 -070048 with open(os.path.join(
joychen7c2054a2013-07-25 11:14:07 -070049 build_path, devserver_constants.UPDATE_FILE), 'w') as f:
50 f.write('UPDATE_FILE')
Chris Sosaea148d92012-03-06 16:22:04 -080051
Frank Farzan37761d12011-12-01 14:29:08 -080052 def tearDown(self):
53 shutil.rmtree(self._static_dir)
54 shutil.rmtree(self._outside_sandbox_dir)
55
Chris Sosa76e44b92013-01-31 12:11:38 -080056 def testPathInDir(self):
57 """Various tests around the PathInDir test."""
Frank Farzan37761d12011-12-01 14:29:08 -080058 # Path is in sandbox.
59 self.assertTrue(
Chris Sosa76e44b92013-01-31 12:11:38 -080060 common_util.PathInDir(
Frank Farzan37761d12011-12-01 14:29:08 -080061 self._static_dir, os.path.join(self._static_dir, 'some-board')))
62
63 # Path is sandbox.
64 self.assertFalse(
Chris Sosa76e44b92013-01-31 12:11:38 -080065 common_util.PathInDir(self._static_dir, self._static_dir))
Frank Farzan37761d12011-12-01 14:29:08 -080066
67 # Path is outside the sandbox.
68 self.assertFalse(
Chris Sosa76e44b92013-01-31 12:11:38 -080069 common_util.PathInDir(
Gilad Arnoldc65330c2012-09-20 15:17:48 -070070 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
Frank Farzan37761d12011-12-01 14:29:08 -0800124
125if __name__ == '__main__':
126 unittest.main()