1 #!/usr/bin/env python 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 import os 7 import shutil 8 import sys 9 import tempfile 10 import unittest 11 12 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) 13 TOOLS_DIR = os.path.dirname(SCRIPT_DIR) 14 CHROME_SRC = os.path.dirname(os.path.dirname(os.path.dirname(TOOLS_DIR))) 15 MOCK_DIR = os.path.join(CHROME_SRC, "third_party", "pymock") 16 17 # For the mock library 18 sys.path.append(MOCK_DIR) 19 import mock 20 21 # For getos, the module under test 22 sys.path.append(TOOLS_DIR) 23 import getos 24 import oshelpers 25 26 27 class TestCaseExtended(unittest.TestCase): 28 """Monkey patch some 2.7-only TestCase features.""" 29 # TODO(sbc): remove this once we switch to python2.7 everywhere 30 def assertIn(self, expr1, expr2, msg=None): 31 if hasattr(super(TestCaseExtended, self), 'assertIn'): 32 return super(TestCaseExtended, self).assertIn(expr1, expr2, msg) 33 if expr1 not in expr2: 34 self.fail(msg or '%r not in %r' % (expr1, expr2)) 35 36 37 class TestGetos(TestCaseExtended): 38 def setUp(self): 39 self.patch1 = mock.patch.dict('os.environ', 40 {'NACL_SDK_ROOT': os.path.dirname(TOOLS_DIR)}) 41 self.patch1.start() 42 self.patch2 = mock.patch.object(oshelpers, 'FindExeInPath', 43 return_value='/bin/ls') 44 self.patch2.start() 45 46 def tearDown(self): 47 self.patch1.stop() 48 self.patch2.stop() 49 50 def testGetSDKPath(self): 51 """honors environment variable.""" 52 with mock.patch.dict('os.environ', {'NACL_SDK_ROOT': 'dummy'}): 53 self.assertEqual(getos.GetSDKPath(), 'dummy') 54 55 def testGetSDKPathDefault(self): 56 """defaults to relative path.""" 57 del os.environ['NACL_SDK_ROOT'] 58 self.assertEqual(getos.GetSDKPath(), os.path.dirname(TOOLS_DIR)) 59 60 def testGetPlatform(self): 61 """returns a valid platform.""" 62 platform = getos.GetPlatform() 63 self.assertIn(platform, ('mac', 'linux', 'win')) 64 65 def testGetSystemArch(self): 66 """returns a valid architecture.""" 67 arch = getos.GetSystemArch(getos.GetPlatform()) 68 self.assertIn(arch, ('x86_64', 'x86_32', 'arm')) 69 70 def testGetChromePathEnv(self): 71 """honors CHROME_PATH environment.""" 72 with mock.patch.dict('os.environ', {'CHROME_PATH': '/dummy/file'}): 73 expect = "Invalid CHROME_PATH.*/dummy/file" 74 platform = getos.GetPlatform() 75 if hasattr(self, 'assertRaisesRegexp'): 76 with self.assertRaisesRegexp(getos.Error, expect): 77 getos.GetChromePath(platform) 78 else: 79 # TODO(sbc): remove this path once we switch to python2.7 everywhere 80 self.assertRaises(getos.Error, getos.GetChromePath, platform) 81 82 def testGetChromePathCheckExists(self): 83 """checks that existence of explicitly CHROME_PATH is checked.""" 84 mock_location = '/bin/ls' 85 platform = getos.GetPlatform() 86 if platform == 'win': 87 mock_location = 'c:\\nowhere' 88 with mock.patch.dict('os.environ', {'CHROME_PATH': mock_location}): 89 with mock.patch('os.path.exists') as mock_exists: 90 chrome = getos.GetChromePath(platform) 91 self.assertEqual(chrome, mock_location) 92 mock_exists.assert_called_with(chrome) 93 94 def testGetNaClArch(self): 95 """returns a valid architecture.""" 96 platform = getos.GetPlatform() 97 # Since the unix implementation of GetNaClArch will run objdump on the 98 # chrome binary, and we want to be able to run this test without chrome 99 # installed we mock the GetChromePath call to return a known system binary, 100 # which objdump will work with. 101 with mock.patch('getos.GetChromePath') as mock_chrome_path: 102 mock_chrome_path.return_value = '/bin/ls' 103 arch = getos.GetNaClArch(platform) 104 self.assertIn(arch, ('x86_64', 'x86_32', 'arm')) 105 106 107 class TestGetosWithTempdir(TestCaseExtended): 108 def setUp(self): 109 self.tempdir = tempfile.mkdtemp("_sdktest") 110 self.patch = mock.patch.dict('os.environ', 111 {'NACL_SDK_ROOT': self.tempdir}) 112 self.patch.start() 113 114 def tearDown(self): 115 shutil.rmtree(self.tempdir) 116 self.patch.stop() 117 118 def testGetSDKVersion(self): 119 """correctly parses README to find SDK version.""" 120 expected_version = (16, 100, 'f00baacabba6e-refs/heads/master@{#100}') 121 with open(os.path.join(self.tempdir, 'README'), 'w') as out: 122 out.write('Version: %d\n' % expected_version[0]) 123 out.write('Chrome Revision: %d\n' % expected_version[1]) 124 out.write('Chrome Commit Position: %s\n' % expected_version[2]) 125 126 version = getos.GetSDKVersion() 127 self.assertEqual(version, expected_version) 128 129 def testParseVersion(self): 130 """correctly parses a version given to --check-version.""" 131 check_version_string = '15.100' 132 self.assertEquals((15, 100), getos.ParseVersion(check_version_string)) 133 134 def testCheckVersion(self): 135 """correctly rejects SDK versions earlier than the required one.""" 136 actual_version = (16, 100, 'f00baacabba6e-refs/heads/master@{#100}') 137 with open(os.path.join(self.tempdir, 'README'), 'w') as out: 138 out.write('Version: %d\n' % actual_version[0]) 139 out.write('Chrome Revision: %d\n' % actual_version[1]) 140 out.write('Chrome Commit Position: %s\n' % actual_version[2]) 141 142 required_version = (15, 150) 143 getos.CheckVersion(required_version) 144 145 required_version = (16, 99) 146 getos.CheckVersion(required_version) 147 148 required_version = (16, 100) 149 getos.CheckVersion(required_version) 150 151 required_version = (16, 101) 152 self.assertRaisesRegexp( 153 getos.Error, 154 r'SDK version too old \(current: 16.100, required: 16.101\)', 155 getos.CheckVersion, 156 required_version) 157 158 required_version = (17, 50) 159 self.assertRaisesRegexp( 160 getos.Error, 161 r'SDK version too old \(current: 16.100, required: 17.50\)', 162 getos.CheckVersion, 163 required_version) 164 165 166 if __name__ == '__main__': 167 unittest.main() 168