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.
16 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17 sys.path.insert(0, ROOT_DIR)
19 import isolated_format
21 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
23 # Ensure that the testing machine has access to this server.
24 ISOLATE_SERVER = 'https://isolateserver.appspot.com/'
26 # The directory containing the test data files.
27 TEST_DATA_DIR = os.path.join(ROOT_DIR, 'tests', 'isolateserver')
30 class IsolateServerArchiveSmokeTest(unittest.TestCase):
32 super(IsolateServerArchiveSmokeTest, self).setUp()
33 # The namespace must end in '-gzip' since all files are now compressed
34 # before being uploaded.
35 # TODO(maruel): This should not be leaked to the client. It's a
36 # transport/storage detail.
37 self.namespace = ('temporary' + str(long(time.time())).split('.', 1)[0]
39 self.rootdir = tempfile.mkdtemp(prefix='isolateserver')
43 shutil.rmtree(self.rootdir)
45 super(IsolateServerArchiveSmokeTest, self).tearDown()
48 """Runs isolateserver.py."""
51 os.path.join(ROOT_DIR, 'isolateserver.py'),
56 '--isolate-server', ISOLATE_SERVER,
57 '--namespace', self.namespace
60 cmd.append('--verbose')
61 subprocess.check_call(cmd)
63 subprocess.check_output(cmd)
65 def _archive_given_files(self, files):
66 """Given a list of files, call isolateserver.py with them. Then
67 verify they are all on the server."""
68 files = [os.path.join(TEST_DATA_DIR, filename) for filename in files]
69 self._run(['archive'] + files)
70 self._download_given_files(files)
72 def _download_given_files(self, files):
73 """Tries to download the files from the server."""
74 args = ['download', '--target', self.rootdir]
75 file_hashes = [isolated_format.hash_file(f, hashlib.sha1) for f in files]
77 args.extend(['--file', f, f])
79 # Assert the files are present.
81 isolated_format.hash_file(os.path.join(self.rootdir, f), hashlib.sha1)
82 for f in os.listdir(self.rootdir)
84 self.assertEqual(sorted(file_hashes), sorted(actual))
86 def test_archive_empty_file(self):
87 self._archive_given_files(['empty_file.txt'])
89 def test_archive_small_file(self):
90 self._archive_given_files(['small_file.txt'])
92 def test_archive_huge_file(self):
93 # Create a file over 2gbs.
97 handle, filepath = tempfile.mkstemp(prefix='isolateserver')
99 chunk = chr(0) + chr(57) + chr(128) + chr(255)
100 chunk1mb = chunk * (1024 * 1024 / len(chunk))
101 for _ in xrange(1280):
102 os.write(handle, chunk1mb)
106 self._archive_given_files([filepath])
112 if __name__ == '__main__':
113 if len(sys.argv) > 1 and sys.argv[1].startswith('http'):
114 ISOLATE_SERVER = sys.argv.pop(1).rstrip('/') + '/'
116 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR)