def test_main_naked_with_account_switch(self): self.capture_luci_ctx = True self.mock_popen_with_oserr() cmd = [ '--no-log', '--cache', self.tempdir, '--named-cache-root', os.path.join(self.tempdir, 'c'), '--switch-to-account', 'task', '--', '/bin/echo', 'hello', 'world', ] root_ctx = { 'accounts': [{'id': 'bot'}, {'id': 'task'}], 'default_account_id' : 'bot', 'secret': 'sekret', 'rpc_port': 12345, } with luci_context.write(local_auth=root_ctx): run_isolated.main(cmd) # Switched default account to task. task_ctx = root_ctx.copy() task_ctx['default_account_id'] = 'task' self.assertEqual(task_ctx, self.popen_calls[0][1]['luci_ctx']['local_auth'])
def test_main_naked_with_account_pop(self): self.capture_luci_ctx = True self.mock_popen_with_oserr() cmd = [ '--no-log', '--cache', os.path.join(self.tempdir, 'isolated_cache'), '--named-cache-root', os.path.join(self.tempdir, 'named_cache'), '--switch-to-account', 'task', '--raw-cmd', '--', '/bin/echo', 'hello', 'world', ] root_ctx = { 'accounts': [{ 'id': 'bot' }], # only 'bot', there's no 'task' 'default_account_id': 'bot', 'secret': 'sekret', 'rpc_port': 12345, } with luci_context.write(local_auth=root_ctx): run_isolated.main(cmd) # Unset default account, since 'task' account is not defined. task_ctx = root_ctx.copy() task_ctx.pop('default_account_id') self.assertEqual(task_ctx, self.popen_calls[0][1]['luci_ctx']['local_auth'])
def test_main_relative_cwd_no_cmd(self): cmd = [ '--relative-cwd', 'a', ] with self.assertRaises(SystemExit): run_isolated.main(cmd)
def test_main_bad_relative_cwd(self): cmd = [ '--raw-cmd', '--relative-cwd', 'a/../../b', '--', 'bin/echo${EXECUTABLE_SUFFIX}', 'hello', 'world', ] with self.assertRaises(SystemExit): run_isolated.main(cmd)
def test_main_args(self): self.mock(run_isolated.tools, "disable_buffering", lambda: None) calls = [] # Unused argument '' # pylint: disable=W0613 def call(command, cwd, env): calls.append(command) return 0 self.mock(run_isolated.subprocess, "call", call) isolated = json_dumps({"command": ["foo.exe", "cmd with space"]}) isolated_hash = ALGO(isolated).hexdigest() def get_storage(_isolate_server, _namespace): return StorageFake({isolated_hash: isolated}) self.mock(run_isolated.isolateserver, "get_storage", get_storage) cmd = [ "--no-log", "--hash", isolated_hash, "--cache", self.tempdir, "--isolate-server", "https://localhost", "--", "--extraargs", "bar", ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) self.assertEqual([[self.temp_join(u"foo.exe"), u"cmd with space", "--extraargs", "bar"]], calls)
def test_main_naked_with_caches(self): # An empty named cache is not kept! # Interestingly, because we would need to put something in the named cache # for it to be kept, we need the tool to write to it. This is tested in the # smoke test. trimmed = [] def trim_caches(caches, root, min_free_space, max_age_secs): trimmed.append(True) self.assertEqual(2, len(caches)) self.assertTrue(root) # The name cache root is increased by the sum of the two hints. self.assertEqual(2*1024*1024*1024 + 1100, min_free_space) self.assertEqual(1814400, max_age_secs) self.mock(local_caching, 'trim_caches', trim_caches) nc = os.path.join(self.tempdir, 'named_cache') cmd = [ '--no-log', '--leak-temp-dir', '--cache', os.path.join(self.tempdir, 'isolated_cache'), '100', '--named-cache-root', nc, '--named-cache', 'cache_foo', 'foo', '100', '--named-cache', 'cache_bar', 'bar', '1000', '--raw-cmd', '--', 'bin/echo${EXECUTABLE_SUFFIX}', 'hello', 'world', ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) for cache_name in ('cache_foo', 'cache_bar'): named_path = os.path.join(nc, 'named', cache_name) self.assertFalse(os.path.exists(named_path)) self.assertTrue(trimmed)
def test_main_naked_with_caches(self): cmd = [ '--no-log', '--leak-temp-dir', '--cache', os.path.join(self.tempdir, 'cache'), '--named-cache-root', os.path.join(self.tempdir, 'c'), '--named-cache', 'cache_foo', 'foo', '--named-cache', 'cache_bar', 'bar', 'bin/echo${EXECUTABLE_SUFFIX}', 'hello', 'world', ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) for path, cache_name in [('foo', 'cache_foo'), ('bar', 'cache_bar')]: self.assertEqual( os.path.abspath( os.readlink(os.path.join(self.tempdir, 'ir', path))), os.path.abspath( os.readlink( os.path.join(self.tempdir, 'c', 'named', cache_name))), )
def test_main(self): self.mock(run_isolated.tools, 'disable_buffering', lambda: None) calls = [] # Unused argument '' # pylint: disable=W0613 def call(command, cwd, env): calls.append(command) return 0 self.mock(run_isolated.subprocess, 'call', call) isolated = json_dumps({ 'command': ['foo.exe', 'cmd with space'], }) isolated_hash = ALGO(isolated).hexdigest() def get_storage(_isolate_server, _namespace): return StorageFake({isolated_hash: isolated}) self.mock(run_isolated.isolateserver, 'get_storage', get_storage) cmd = [ '--no-log', '--hash', isolated_hash, '--cache', self.tempdir, '--isolate-server', 'https://localhost', ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) self.assertEqual([[self.temp_join(u'foo.exe'), u'cmd with space']], calls)
def test_main_naked_with_caches(self): cmd = [ '--no-log', '--leak-temp-dir', '--cache', os.path.join(self.tempdir, 'cache'), '--named-cache-root', os.path.join(self.tempdir, 'c'), '--named-cache', 'cache_foo', 'foo', '--named-cache', 'cache_bar', 'bar', 'bin/echo${EXECUTABLE_SUFFIX}', 'hello', 'world', ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) for cache_name in ('cache_foo', 'cache_bar'): named_path = os.path.join(self.tempdir, 'c', 'named', cache_name) self.assertTrue(os.path.exists(named_path)) self.assertEqual( os.path.join(self.tempdir, 'c'), os.path.dirname(os.readlink(named_path)), )
def test_main_args(self): self.mock(tools, 'disable_buffering', lambda: None) isolated = json_dumps({'command': ['foo.exe', 'cmd w/ space']}) isolated_hash = isolateserver_mock.hash_content(isolated) def get_storage(_isolate_server, _namespace): return StorageFake({isolated_hash: isolated}) self.mock(isolateserver, 'get_storage', get_storage) cmd = [ '--no-log', '--isolated', isolated_hash, '--cache', self.tempdir, '--isolate-server', 'https://localhost', '--', '--extraargs', 'bar', ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) self.assertEqual([ ([ self.temp_join(u'foo.exe'), u'cmd w/ space', '--extraargs', 'bar' ], { 'detached': True }), ], self.popen_calls)
def test_main_args(self): self.mock(tools, 'disable_buffering', lambda: None) isolated = json_dumps({'command': ['foo.exe', 'cmd w/ space']}) isolated_hash = isolateserver_mock.hash_content(isolated) def get_storage(_isolate_server, _namespace): return StorageFake({isolated_hash:isolated}) self.mock(isolateserver, 'get_storage', get_storage) cmd = [ '--no-log', '--isolated', isolated_hash, '--cache', self.tempdir, '--isolate-server', 'https://localhost', '--', '--extraargs', 'bar', ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) self.assertEqual( [ ([self.temp_join(u'foo.exe'), u'cmd w/ space', '--extraargs', 'bar'], {'detached': True}), ], self.popen_calls)
def test_main_naked(self): self.mock_popen_with_oserr() self.mock(on_error, 'report', lambda _: None) # The most naked .isolated file that can exist. self.mock(tools, 'disable_buffering', lambda: None) isolated = json_dumps({'command': ['invalid', 'command']}) isolated_hash = isolateserver_mock.hash_content(isolated) def get_storage(_isolate_server, _namespace): return StorageFake({isolated_hash: isolated}) self.mock(isolateserver, 'get_storage', get_storage) cmd = [ '--no-log', '--isolated', isolated_hash, '--cache', os.path.join(self.tempdir, 'isolated_cache'), '--isolate-server', 'https://localhost', '--named-cache-root', os.path.join(self.tempdir, 'named_cache'), '--root-dir', self.tempdir, ] ret = run_isolated.main(cmd) self.assertEqual(1, ret) self.assertEqual(1, len(self.popen_calls)) self.assertEqual([ ([self.ir_dir(u'invalid'), u'command'], { 'cwd': self.ir_dir(), 'detached': True }), ], self.popen_calls)
def test_main_naked(self): # The most naked .isolated file that can exist. self.mock(run_isolated.tools, 'disable_buffering', lambda: None) isolated = json_dumps({'command': ['invalid', 'command']}) isolated_hash = ALGO(isolated).hexdigest() def get_storage(_isolate_server, _namespace): return StorageFake({isolated_hash:isolated}) self.mock(run_isolated.isolateserver, 'get_storage', get_storage) # Keeps tuple of (args, kwargs). subprocess_call = [] self.mock( run_isolated.subprocess, 'call', lambda *x, **y: subprocess_call.append((x, y)) or 8) cmd = [ '--no-log', '--hash', isolated_hash, '--cache', self.tempdir, '--isolate-server', 'https://localhost', ] ret = run_isolated.main(cmd) self.assertEqual(8, ret) self.assertEqual(1, len(subprocess_call)) self.assertTrue(subprocess_call[0][1].pop('cwd')) self.assertTrue(subprocess_call[0][1].pop('env')) self.assertEqual( [(([self.temp_join(u'invalid'), u'command'],), {})], subprocess_call)
def test_main_naked(self): # The most naked .isolated file that can exist. self.mock(run_isolated.tools, 'disable_buffering', lambda: None) isolated = json_dumps({'command': ['invalid', 'command']}) isolated_hash = ALGO(isolated).hexdigest() def get_storage(_isolate_server, _namespace): return StorageFake({isolated_hash: isolated}) self.mock(run_isolated.isolateserver, 'get_storage', get_storage) # Keeps tuple of (args, kwargs). subprocess_call = [] self.mock(run_isolated.subprocess, 'call', lambda *x, **y: subprocess_call.append((x, y)) or 8) cmd = [ '--no-log', '--hash', isolated_hash, '--cache', self.tempdir, '--isolate-server', 'https://localhost', ] ret = run_isolated.main(cmd) self.assertEqual(8, ret) self.assertEqual(1, len(subprocess_call)) self.assertTrue(subprocess_call[0][1].pop('cwd')) self.assertTrue(subprocess_call[0][1].pop('env')) self.assertEqual([(([self.temp_join(u'invalid'), u'command'], ), {})], subprocess_call)
def test_main_args(self): self.mock(run_isolated.tools, 'disable_buffering', lambda: None) calls = [] # Unused argument '' # pylint: disable=W0613 def call(command, cwd, env): calls.append(command) return 0 self.mock(run_isolated.subprocess, 'call', call) isolated = json_dumps( { 'command': ['foo.exe', 'cmd with space'], }) isolated_hash = ALGO(isolated).hexdigest() def get_storage(_isolate_server, _namespace): return StorageFake({isolated_hash:isolated}) self.mock(run_isolated.isolateserver, 'get_storage', get_storage) cmd = [ '--no-log', '--hash', isolated_hash, '--cache', self.tempdir, '--isolate-server', 'https://localhost', '--', '--extraargs', 'bar', ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) self.assertEqual( [[self.temp_join(u'foo.exe'), u'cmd with space', '--extraargs', 'bar']], calls)
def test_main_naked(self): self.mock(on_error, 'report', lambda _: None) # The most naked .isolated file that can exist. self.mock(tools, 'disable_buffering', lambda: None) isolated = json_dumps({'command': ['invalid', 'command']}) isolated_hash = isolateserver_mock.hash_content(isolated) def get_storage(_isolate_server, _namespace): return StorageFake({isolated_hash:isolated}) self.mock(isolateserver, 'get_storage', get_storage) def r(self, args, **kwargs): old_init(self, args, **kwargs) raise OSError('Unknown') old_init = self.mock(subprocess42.Popen, '__init__', r) cmd = [ '--no-log', '--isolated', isolated_hash, '--cache', self.tempdir, '--isolate-server', 'https://localhost', ] ret = run_isolated.main(cmd) self.assertEqual(1, ret) self.assertEqual(1, len(self.popen_calls)) self.assertEqual( [([self.temp_join(u'invalid'), u'command'], {'detached': True})], self.popen_calls)
def test_main_naked(self): self.mock(on_error, 'report', lambda _: None) # The most naked .isolated file that can exist. self.mock(tools, 'disable_buffering', lambda: None) isolated = json_dumps({'command': ['invalid', 'command']}) isolated_hash = isolateserver_mock.hash_content(isolated) def get_storage(_isolate_server, _namespace): return StorageFake({isolated_hash: isolated}) self.mock(isolateserver, 'get_storage', get_storage) def r(self, args, **kwargs): old_init(self, args, **kwargs) raise OSError('Unknown') old_init = self.mock(subprocess42.Popen, '__init__', r) cmd = [ '--no-log', '--isolated', isolated_hash, '--cache', self.tempdir, '--isolate-server', 'https://localhost', ] ret = run_isolated.main(cmd) self.assertEqual(1, ret) self.assertEqual(1, len(self.popen_calls)) self.assertEqual([([self.temp_join(u'invalid'), u'command'], { 'detached': True })], self.popen_calls)
def test_main_naked_with_caches(self): # An empty named cache is not kept! # Interestingly, because we would need to put something in the named cache # for it to be kept, we need the tool to write to it. This is tested in the # smoke test. nc = os.path.join(self.tempdir, 'named_cache') cmd = [ '--no-log', '--leak-temp-dir', '--cache', os.path.join(self.tempdir, 'isolated_cache'), '--named-cache-root', nc, '--named-cache', 'cache_foo', 'foo', '--named-cache', 'cache_bar', 'bar', '--raw-cmd', '--', 'bin/echo${EXECUTABLE_SUFFIX}', 'hello', 'world', ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) for cache_name in ('cache_foo', 'cache_bar'): named_path = os.path.join(nc, 'named', cache_name) self.assertFalse(os.path.exists(named_path))
def test_main(self): self.mock(tools, 'disable_buffering', lambda: None) isolated = json_dumps({ 'command': ['foo.exe', 'cmd with space'], }) isolated_hash = isolateserver_mock.hash_content(isolated) def get_storage(_isolate_server, _namespace): return StorageFake({isolated_hash: isolated}) self.mock(isolateserver, 'get_storage', get_storage) cmd = [ '--no-log', '--isolated', isolated_hash, '--cache', os.path.join(self.tempdir, 'isolated_cache'), '--named-cache-root', os.path.join(self.tempdir, 'named_cache'), '--isolate-server', 'https://localhost', '--root-dir', self.tempdir, ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) self.assertEqual([ ([self.ir_dir(u'foo.exe'), u'cmd with space'], { 'cwd': self.ir_dir(), 'detached': True }), ], self.popen_calls)
def test_main_naked_with_cipd_client_no_packages(self): cipd_cache = os.path.join(self.tempdir, 'cipd_cache') cmd = [ '--no-log', '--cache', os.path.join(self.tempdir, 'isolated_cache'), '--cipd-enabled', '--cipd-client-version', 'git:wowza', '--cipd-server', self.cipd_server.url, '--cipd-cache', cipd_cache, '--named-cache-root', os.path.join(self.tempdir, 'named_cache'), '--raw-cmd', '--relative-cwd', 'a', '--', 'bin/echo${EXECUTABLE_SUFFIX}', 'hello', 'world', ] self.capture_popen_env = True ret = run_isolated.main(cmd) self.assertEqual(0, ret) # The CIPD client was bootstrapped and hardlinked (or copied on Win). client_binary_file = unicode( os.path.join(cipd_cache, 'clients', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')) self.assertTrue(fs.isfile(client_binary_file)) client_binary_link = unicode( os.path.join(cipd_cache, 'bin', 'cipd' + cipd.EXECUTABLE_SUFFIX)) self.assertTrue(fs.isfile(client_binary_link)) # 'cipd ensure' was NOT called (only 'echo hello world' was). env = self.popen_calls[0][1].pop('env') self.assertEqual([ ([self.ir_dir(u'a', 'bin', 'echo'), u'hello', u'world'], { 'cwd': self.ir_dir('a'), 'detached': True }), ], self.popen_calls) # Directory with cipd client is in front of PATH. path = env['PATH'].split(os.pathsep) self.assertEqual(os.path.join(cipd_cache, 'bin'), path[0]) # CIPD_CACHE_DIR is set. self.assertEqual(os.path.join(cipd_cache, 'cache'), env['CIPD_CACHE_DIR'])
def test_main_json(self): # Instruct the Popen mock to write a file in ISOLATED_OUTDIR so it will be # archived back on termination. self.mock(tools, 'disable_buffering', lambda: None) sub_cmd = [ self.ir_dir(u'foo.exe'), u'cmd with space', '${ISOLATED_OUTDIR}/out.txt', ] isolated_in_json = json_dumps({'command': sub_cmd}) isolated_in_hash = isolateserver_mock.hash_content(isolated_in_json) def get_storage(_isolate_server, _namespace): return StorageFake({isolated_in_hash: isolated_in_json}) self.mock(isolateserver, 'get_storage', get_storage) out = os.path.join(self.tempdir, 'res.json') cmd = [ '--no-log', '--isolated', isolated_in_hash, '--cache', os.path.join(self.tempdir, 'isolated_cache'), '--isolate-server', 'https://localhost:1', '--named-cache-root', os.path.join(self.tempdir, 'named_cache'), '--json', out, '--root-dir', self.tempdir, ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) # Replace ${ISOLATED_OUTDIR} with the temporary directory. sub_cmd[2] = self.popen_calls[0][0][2] self.assertNotIn('ISOLATED_OUTDIR', sub_cmd[2]) self.assertEqual([(sub_cmd, { 'cwd': self.ir_dir(), 'detached': True })], self.popen_calls) isolated_out = { 'algo': 'sha-1', 'files': { 'out.txt': { 'h': isolateserver_mock.hash_content('generated data\n'), 's': 15, 'm': 0640, }, }, 'version': isolated_format.ISOLATED_FILE_VERSION, }
def test_main_naked_without_isolated(self): self.mock_popen_with_oserr() cmd = [ '--no-log', '--cache', self.tempdir, '--named-cache-root', os.path.join(self.tempdir, 'c'), '/bin/echo', 'hello', 'world', ] ret = run_isolated.main(cmd) self.assertEqual(1, ret) self.assertEqual(1, len(self.popen_calls)) self.assertEqual( [([u'/bin/echo', u'hello', u'world'], {'detached': True})], self.popen_calls)
def test_main_naked_leaking(self): workdir = tempfile.mkdtemp() try: cmd = [ '--no-log', '--cache', self.tempdir, '--root-dir', workdir, '--leak-temp-dir', '--named-cache-root', os.path.join(self.tempdir, 'c'), '/bin/echo', 'hello', 'world', ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) finally: fs.rmtree(unicode(workdir))
def test_main_naked(self): # The most naked .isolated file that can exist. self.mock(run_isolated.tools, "disable_buffering", lambda: None) isolated = json_dumps({"command": ["invalid", "command"]}) isolated_hash = ALGO(isolated).hexdigest() def get_storage(_isolate_server, _namespace): return StorageFake({isolated_hash: isolated}) self.mock(run_isolated.isolateserver, "get_storage", get_storage) # Keeps tuple of (args, kwargs). subprocess_call = [] self.mock(run_isolated.subprocess, "call", lambda *x, **y: subprocess_call.append((x, y)) or 8) cmd = ["--no-log", "--hash", isolated_hash, "--cache", self.tempdir, "--isolate-server", "https://localhost"] ret = run_isolated.main(cmd) self.assertEqual(8, ret) self.assertEqual(1, len(subprocess_call)) self.assertTrue(subprocess_call[0][1].pop("cwd")) self.assertTrue(subprocess_call[0][1].pop("env")) self.assertEqual([(([self.temp_join(u"invalid"), u"command"],), {})], subprocess_call)
def test_main_json(self): # Instruct the Popen mock to write a file in ISOLATED_OUTDIR so it will be # archived back on termination. self.mock(tools, 'disable_buffering', lambda: None) sub_cmd = [ self.temp_join(u'foo.exe'), u'cmd with space', '${ISOLATED_OUTDIR}/out.txt', ] isolated = json_dumps({'command': sub_cmd}) isolated_hash = isolateserver_mock.hash_content(isolated) def get_storage(_isolate_server, _namespace): return StorageFake({isolated_hash:isolated}) self.mock(isolateserver, 'get_storage', get_storage) out = os.path.join(self.tempdir, 'res.json') cmd = [ '--no-log', '--isolated', isolated_hash, '--cache', self.tempdir, '--isolate-server', 'https://localhost:1', '--json', out, ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) # Replace ${ISOLATED_OUTDIR} with the temporary directory. sub_cmd[2] = self.popen_calls[0][0][2] self.assertNotIn('ISOLATED_OUTDIR', sub_cmd[2]) self.assertEqual([(sub_cmd, {'detached': True})], self.popen_calls) expected = { u'exit_code': 0, u'internal_failure': None, u'outputs_ref': { u'isolated': u'e0a0fffa0910dd09e7ef4c89496116f60317e6c4', u'isolatedserver': u'http://localhost:1', u'namespace': u'default-gzip', }, u'version': 1, } self.assertEqual(expected, tools.read_json(out))
def test_main_json(self): # Instruct the Popen mock to write a file in ISOLATED_OUTDIR so it will be # archived back on termination. self.mock(tools, 'disable_buffering', lambda: None) sub_cmd = [ self.temp_join(u'foo.exe'), u'cmd with space', '${ISOLATED_OUTDIR}/out.txt', ] isolated_in_json = json_dumps({'command': sub_cmd}) isolated_in_hash = isolateserver_mock.hash_content(isolated_in_json) def get_storage(_isolate_server, _namespace): return StorageFake({isolated_in_hash:isolated_in_json}) self.mock(isolateserver, 'get_storage', get_storage) out = os.path.join(self.tempdir, 'res.json') cmd = [ '--no-log', '--isolated', isolated_in_hash, '--cache', self.tempdir, '--isolate-server', 'https://localhost:1', '--json', out, ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) # Replace ${ISOLATED_OUTDIR} with the temporary directory. sub_cmd[2] = self.popen_calls[0][0][2] self.assertNotIn('ISOLATED_OUTDIR', sub_cmd[2]) self.assertEqual([(sub_cmd, {'detached': True})], self.popen_calls) isolated_out = { 'algo': 'sha-1', 'files': { 'out.txt': { 'h': isolateserver_mock.hash_content('generated data\n'), 's': 15, 'm': 0640, }, }, 'version': isolated_format.ISOLATED_FILE_VERSION, }
def CMDrun_isolated(args): """Internal command to run an isolated command.""" sys.path.insert(0, os.path.join(THIS_FILE, 'client')) # run_isolated setups logging by itself. import run_isolated return run_isolated.main(args)
def test_main_naked_with_packages(self): pin_idx_ref = [0] pins = [ [ ('infra/data/x', 'badc0fee'*5), ('infra/data/y', 'cafebabe'*5), ], [ ('infra/tools/echo/linux-amd64', 'deadbeef'*5), ], ] def fake_ensure(args, **_kwargs): if (args[0].endswith('/cipd') and args[1] == 'ensure' and '-json-output' in args): idx = args.index('-json-output') with open(args[idx+1], 'w') as json_out: json.dump({ 'result': [ {'package': pkg, 'instance_id': ver} for pkg, ver in pins[pin_idx_ref[0]] ], }, json_out) pin_idx_ref[0] += 1 return 0 self.popen_mocks.append(fake_ensure) cipd_cache = os.path.join(self.tempdir, 'cipd_cache') cmd = [ '--no-log', '--cache', os.path.join(self.tempdir, 'cache'), '--cipd-client-version', 'git:wowza', '--cipd-package', 'bin:infra/tools/echo/${platform}:latest', '--cipd-package', '.:infra/data/x:latest', '--cipd-package', '.:infra/data/y:canary', '--cipd-server', self.cipd_server.url, '--cipd-cache', cipd_cache, '--named-cache-root', os.path.join(self.tempdir, 'c'), 'bin/echo${EXECUTABLE_SUFFIX}', 'hello', 'world', ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) self.assertEqual(3, len(self.popen_calls)) # Test cipd-ensure command for installing packages. for cipd_ensure_cmd, _ in self.popen_calls[0:2]: self.assertEqual(cipd_ensure_cmd[:2], [ os.path.join(cipd_cache, 'cipd' + cipd.EXECUTABLE_SUFFIX), 'ensure', ]) cache_dir_index = cipd_ensure_cmd.index('-cache-dir') self.assertEqual( cipd_ensure_cmd[cache_dir_index+1], os.path.join(cipd_cache, 'cipd_internal')) # Test cipd client cache. `git:wowza` was a tag and so is cacheable. self.assertEqual(len(os.listdir(os.path.join(cipd_cache, 'versions'))), 2) version_file = unicode(os.path.join( cipd_cache, 'versions', '633d2aa4119cc66803f1600f9c4d85ce0e0581b5')) self.assertTrue(fs.isfile(version_file)) with open(version_file) as f: self.assertEqual(f.read(), 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') client_binary_file = unicode(os.path.join( cipd_cache, 'clients', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')) self.assertTrue(fs.isfile(client_binary_file)) # Test echo call. echo_cmd, _ = self.popen_calls[2] self.assertTrue(echo_cmd[0].endswith( os.path.sep + 'bin' + os.path.sep + 'echo' + cipd.EXECUTABLE_SUFFIX), echo_cmd[0]) self.assertEqual(echo_cmd[1:], ['hello', 'world'])
def test_main_naked_with_packages(self): self.mock(cipd, 'get_platform', lambda: 'linux-amd64') pins = { '': [ ('infra/data/x', 'badc0fee' * 5), ('infra/data/y', 'cafebabe' * 5), ], 'bin': [ ('infra/tools/echo/linux-amd64', 'deadbeef' * 5), ], } def fake_ensure(args, **_kwargs): if (args[0].endswith('/cipd') and args[1] == 'ensure' and '-json-output' in args): idx = args.index('-json-output') with open(args[idx + 1], 'w') as json_out: json.dump( { 'result': { subdir: [{ 'package': pkg, 'instance_id': ver } for pkg, ver in packages] for subdir, packages in pins.iteritems() } }, json_out) return 0 self.popen_mocks.append(fake_ensure) cipd_cache = os.path.join(self.tempdir, 'cipd_cache') cmd = [ '--no-log', '--cache', os.path.join(self.tempdir, 'isolated_cache'), '--cipd-client-version', 'git:wowza', '--cipd-package', 'bin:infra/tools/echo/${platform}:latest', '--cipd-package', '.:infra/data/x:latest', '--cipd-package', '.:infra/data/y:canary', '--cipd-server', self.cipd_server.url, '--cipd-cache', cipd_cache, '--named-cache-root', os.path.join(self.tempdir, 'named_cache'), '--raw-cmd', '--', 'bin/echo${EXECUTABLE_SUFFIX}', 'hello', 'world', ] ret = run_isolated.main(cmd) self.assertEqual(0, ret) self.assertEqual(2, len(self.popen_calls)) # Test cipd-ensure command for installing packages. cipd_ensure_cmd, _ = self.popen_calls[0] self.assertEqual(cipd_ensure_cmd[:2], [ os.path.join(cipd_cache, 'bin', 'cipd' + cipd.EXECUTABLE_SUFFIX), 'ensure', ]) cache_dir_index = cipd_ensure_cmd.index('-cache-dir') self.assertEqual(cipd_ensure_cmd[cache_dir_index + 1], os.path.join(cipd_cache, 'cache')) # Test cipd client cache. `git:wowza` was a tag and so is cacheable. self.assertEqual(len(os.listdir(os.path.join(cipd_cache, 'versions'))), 2) version_file = unicode( os.path.join(cipd_cache, 'versions', '765a0de4c618f91faf923cb68a47bb564aed412d')) self.assertTrue(fs.isfile(version_file)) with open(version_file) as f: self.assertEqual(f.read(), 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') client_binary_file = unicode( os.path.join(cipd_cache, 'clients', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')) self.assertTrue(fs.isfile(client_binary_file)) # Test echo call. echo_cmd, _ = self.popen_calls[1] self.assertTrue( echo_cmd[0].endswith(os.path.sep + 'bin' + os.path.sep + 'echo' + cipd.EXECUTABLE_SUFFIX), echo_cmd[0]) self.assertEqual(echo_cmd[1:], [u'hello', u'world'])
def CMDrun_isolated(args): """Internal command to run an isolated command.""" logging_utils.prepare_logging('run_isolated_bot.log') sys.path.insert(0, os.path.join(THIS_FILE, 'client')) import run_isolated return run_isolated.main(args)