Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / tools / swarming_client / tests / isolated_format_test.py
1 #!/usr/bin/env python
2 # Copyright 2014 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 logging
8 import os
9 import shutil
10 import sys
11 import tempfile
12 import unittest
13
14 # net_utils adjusts sys.path.
15 import net_utils
16
17 import isolated_format
18
19
20 ALGO = hashlib.sha1
21
22
23 class TestCase(net_utils.TestCase):
24   def test_get_hash_algo(self):
25     # Tests here assume ALGO is used for default namespaces, check this
26     # assumption.
27     self.assertIs(isolated_format.get_hash_algo('default'), ALGO)
28     self.assertIs(isolated_format.get_hash_algo('default-gzip'), ALGO)
29
30
31 class SymlinkTest(unittest.TestCase):
32   def setUp(self):
33     super(SymlinkTest, self).setUp()
34     self.old_cwd = os.getcwd()
35     self.cwd = tempfile.mkdtemp(prefix='isolate_')
36     # Everything should work even from another directory.
37     os.chdir(self.cwd)
38
39   def tearDown(self):
40     try:
41       os.chdir(self.old_cwd)
42       shutil.rmtree(self.cwd)
43     finally:
44       super(SymlinkTest, self).tearDown()
45
46   if sys.platform == 'darwin':
47     def test_expand_symlinks_path_case(self):
48       # Ensures that the resulting path case is fixed on case insensitive file
49       # system.
50       os.symlink('dest', os.path.join(self.cwd, 'link'))
51       os.mkdir(os.path.join(self.cwd, 'Dest'))
52       open(os.path.join(self.cwd, 'Dest', 'file.txt'), 'w').close()
53
54       result = isolated_format.expand_symlinks(unicode(self.cwd), 'link')
55       self.assertEqual((u'Dest', [u'link']), result)
56       result = isolated_format.expand_symlinks(
57           unicode(self.cwd), 'link/File.txt')
58       self.assertEqual((u'Dest/file.txt', [u'link']), result)
59
60     def test_expand_directories_and_symlinks_path_case(self):
61       # Ensures that the resulting path case is fixed on case insensitive file
62       # system. A superset of test_expand_symlinks_path_case.
63       # Create *all* the paths with the wrong path case.
64       basedir = os.path.join(self.cwd, 'baseDir')
65       os.mkdir(basedir.lower())
66       subdir = os.path.join(basedir, 'subDir')
67       os.mkdir(subdir.lower())
68       open(os.path.join(subdir, 'Foo.txt'), 'w').close()
69       os.symlink('subDir', os.path.join(basedir, 'linkdir'))
70       actual = isolated_format.expand_directories_and_symlinks(
71           unicode(self.cwd), [u'baseDir/'], lambda _: None, True, False)
72       expected = [
73         u'basedir/linkdir',
74         u'basedir/subdir/Foo.txt',
75         u'basedir/subdir/Foo.txt',
76       ]
77       self.assertEqual(expected, actual)
78
79     def test_file_to_metadata_path_case_simple(self):
80       # Ensure the symlink dest is saved in the right path case.
81       subdir = os.path.join(self.cwd, 'subdir')
82       os.mkdir(subdir)
83       linkdir = os.path.join(self.cwd, 'linkdir')
84       os.symlink('subDir', linkdir)
85       actual = isolated_format.file_to_metadata(
86           unicode(linkdir.upper()), {}, True, ALGO)
87       expected = {'l': u'subdir', 'm': 360, 't': int(os.stat(linkdir).st_mtime)}
88       self.assertEqual(expected, actual)
89
90     def test_file_to_metadata_path_case_complex(self):
91       # Ensure the symlink dest is saved in the right path case. This includes 2
92       # layers of symlinks.
93       basedir = os.path.join(self.cwd, 'basebir')
94       os.mkdir(basedir)
95
96       linkeddir2 = os.path.join(self.cwd, 'linkeddir2')
97       os.mkdir(linkeddir2)
98
99       linkeddir1 = os.path.join(basedir, 'linkeddir1')
100       os.symlink('../linkedDir2', linkeddir1)
101
102       subsymlinkdir = os.path.join(basedir, 'symlinkdir')
103       os.symlink('linkedDir1', subsymlinkdir)
104
105       actual = isolated_format.file_to_metadata(
106           unicode(subsymlinkdir.upper()), {}, True, ALGO)
107       expected = {
108         'l': u'linkeddir1', 'm': 360, 't': int(os.stat(subsymlinkdir).st_mtime),
109       }
110       self.assertEqual(expected, actual)
111
112       actual = isolated_format.file_to_metadata(
113           unicode(linkeddir1.upper()), {}, True, ALGO)
114       expected = {
115         'l': u'../linkeddir2', 'm': 360, 't': int(os.stat(linkeddir1).st_mtime),
116       }
117       self.assertEqual(expected, actual)
118
119   if sys.platform != 'win32':
120     def test_symlink_input_absolute_path(self):
121       # A symlink is outside of the checkout, it should be treated as a normal
122       # directory.
123       # .../src
124       # .../src/out -> .../tmp/foo
125       # .../tmp
126       # .../tmp/foo
127       src = os.path.join(self.cwd, u'src')
128       src_out = os.path.join(src, 'out')
129       tmp = os.path.join(self.cwd, 'tmp')
130       tmp_foo = os.path.join(tmp, 'foo')
131       os.mkdir(src)
132       os.mkdir(tmp)
133       os.mkdir(tmp_foo)
134       # The problem was that it's an absolute path, so it must be considered a
135       # normal directory.
136       os.symlink(tmp, src_out)
137       open(os.path.join(tmp_foo, 'bar.txt'), 'w').close()
138       actual = isolated_format.expand_symlinks(src, u'out/foo/bar.txt')
139       self.assertEqual((u'out/foo/bar.txt', []), actual)
140
141
142 if __name__ == '__main__':
143   if '-v' in sys.argv:
144     unittest.TestCase.maxDiff = None
145   logging.basicConfig(
146       level=(logging.DEBUG if '-v' in sys.argv else logging.ERROR))
147   unittest.main()