Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / tools / swarming_client / googletest / tests / fix_test_cases_smoke_test.py
1 #!/usr/bin/env python
2 # Copyright 2013 The Swarming Authors. All rights reserved.
3 # Use of this source code is governed under the Apache License, Version 2.0 that
4 # can be found in the LICENSE file.
5
6 import hashlib
7 import json
8 import logging
9 import os
10 import shutil
11 import subprocess
12 import sys
13 import tempfile
14 import unittest
15
16 BASE_DIR = os.path.dirname(os.path.abspath(__file__))
17 GOOGLETEST_DIR = os.path.dirname(BASE_DIR)
18 ROOT_DIR = os.path.dirname(GOOGLETEST_DIR)
19
20 sys.path.insert(0, ROOT_DIR)
21 sys.path.insert(0, os.path.join(ROOT_DIR, 'tests'))
22
23 import isolateserver
24 import run_isolated
25 import trace_test_util
26
27
28 class FixTestCases(unittest.TestCase):
29   def setUp(self):
30     self.tempdir = tempfile.mkdtemp(prefix='fix_test_case')
31     self.srcdir = os.path.join(self.tempdir, 'srcdir')
32     os.mkdir(self.srcdir)
33
34   def tearDown(self):
35     if self.tempdir:
36       if VERBOSE:
37         # If -v is used, this means the user wants to do further analysis on
38         # the data.
39         print('Leaking %s' % self.tempdir)
40       else:
41         shutil.rmtree(self.tempdir)
42
43   def _run(self, cmd):
44     if VERBOSE:
45       cmd = cmd + ['--verbose'] * 3
46     logging.info(cmd)
47     proc = subprocess.Popen(
48         [sys.executable] + cmd,
49         cwd=self.srcdir,
50         stdout=subprocess.PIPE,
51         stderr=subprocess.STDOUT)
52     out = proc.communicate()[0]
53     if VERBOSE:
54       print '\n-----'
55       print out.strip()
56       print '-----\n'
57     self.assertEqual(0, proc.returncode)
58     return out
59
60   @trace_test_util.check_can_trace
61   def test_simple(self):
62     # Create a directory with nothing in it and progressively add more stuff.
63     isolate = os.path.join(self.srcdir, 'gtest_fake_pass.isolate')
64     condition = 'OS=="linux" and chromeos==1'
65     with open(isolate, 'w') as f:
66       # Write a minimal .isolate file.
67       f.write(str({
68         'conditions': [
69           [condition, {
70             'variables': {
71               'command': [
72                 'run_test_cases.py', 'gtest_fake_pass.py',
73               ],
74             },
75           }],
76         ],
77       }))
78     def _copy(filename):
79       shutil.copy(
80           os.path.join(BASE_DIR, 'gtest_fake', filename),
81           os.path.join(self.srcdir, filename))
82     _copy('gtest_fake_base.py')
83     _copy('gtest_fake_pass.py')
84     shutil.copy(
85         os.path.join(GOOGLETEST_DIR, 'run_test_cases.py'),
86         os.path.join(self.srcdir, 'run_test_cases.py'))
87     # Deploy run_isolated with dependencies as zip into srcdir.
88     run_isolated.get_as_zip_package(executable=False).zip_into_file(
89         os.path.join(self.srcdir, 'run_isolated.zip'))
90
91     logging.debug('1. Create a .isolated file out of the .isolate file.')
92     isolated = os.path.join(self.srcdir, 'gtest_fake_pass.isolated')
93     out = self._run(
94         [
95           os.path.join(ROOT_DIR, 'isolate.py'),
96           'check', '-i', isolate, '-s', isolated,
97           '--config-variable', 'OS', 'linux',
98           '--config-variable', 'chromeos', '1',
99         ])
100     if not VERBOSE:
101       self.assertEqual('', out)
102
103     logging.debug('2. Run fix_test_cases.py on it.')
104     # Give up on looking at stdout.
105     cmd = [
106       os.path.join(GOOGLETEST_DIR, 'fix_test_cases.py'),
107       '-s', isolated,
108       '--trace-blacklist', '.*\\.run_test_cases',
109     ]
110     _ = self._run(cmd)
111
112     logging.debug('3. Asserting the content of the .isolated file.')
113     with open(isolated) as f:
114       actual_isolated = json.load(f)
115     gtest_fake_base_py = os.path.join(self.srcdir, 'gtest_fake_base.py')
116     gtest_fake_pass_py = os.path.join(self.srcdir, 'gtest_fake_pass.py')
117     run_isolated_zip = os.path.join(self.srcdir, 'run_isolated.zip')
118     run_test_cases_py = os.path.join(self.srcdir, 'run_test_cases.py')
119     algo = hashlib.sha1
120     expected_isolated = {
121       u'algo': u'sha-1',
122       u'command': [u'run_test_cases.py', u'gtest_fake_pass.py'],
123       u'files': {
124         u'gtest_fake_base.py': {
125           u'm': 416,
126           u'h': unicode(isolateserver.hash_file(gtest_fake_base_py, algo)),
127           u's': os.stat(gtest_fake_base_py).st_size,
128         },
129         u'gtest_fake_pass.py': {
130           u'm': 488,
131           u'h': unicode(isolateserver.hash_file(gtest_fake_pass_py, algo)),
132           u's': os.stat(gtest_fake_pass_py).st_size,
133         },
134         u'run_isolated.zip': {
135           u'm': 416,
136           u'h': unicode(isolateserver.hash_file(run_isolated_zip, algo)),
137           u's': os.stat(run_isolated_zip).st_size,
138         },
139         u'run_test_cases.py': {
140           u'm': 488,
141           u'h': unicode(isolateserver.hash_file(run_test_cases_py, algo)),
142           u's': os.stat(run_test_cases_py).st_size,
143         },
144       },
145       u'relative_cwd': u'.',
146       u'version': unicode(isolateserver.ISOLATED_FILE_VERSION),
147     }
148     if sys.platform == 'win32':
149       for value in expected_isolated['files'].itervalues():
150         self.assertTrue(value.pop('m'))
151     self.assertEqual(expected_isolated, actual_isolated)
152
153     # Now verify the .isolate file was updated! (That's the magical part where
154     # you say wow!)
155     with open(isolate) as f:
156       actual = eval(f.read(), {'__builtins__': None}, None)
157     expected = {
158       'conditions': [
159         [condition, {
160           'variables': {
161             'command': [
162               'run_test_cases.py', 'gtest_fake_pass.py'
163             ],
164             'isolate_dependency_tracked': [
165               'gtest_fake_base.py',
166               'gtest_fake_pass.py',
167               'run_isolated.zip',
168               'run_test_cases.py',
169             ],
170           },
171         }],
172       ],
173     }
174     self.assertEqual(expected, actual)
175
176
177 if __name__ == '__main__':
178   VERBOSE = '-v' in sys.argv
179   if VERBOSE:
180     unittest.TestCase.maxDiff = None
181   logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR)
182   unittest.main()