def test_update_bot(self): # In a real case 'update_bot' never exits and doesn't call 'post_error'. # Under the test however forever-blocking calls finish, and post_error is # called. self.mock(self.bot, 'post_error', lambda *_: None) self.mock(net, 'url_retrieve', lambda *_: True) calls = [] def exec_python(args): calls.append(args) return 23 self.mock(bot_main.common, 'exec_python', exec_python) self.mock( bot_main, 'THIS_FILE', os.path.join(test_env_bot_code.BOT_DIR, 'swarming_bot.1.zip')) with self.assertRaises(SystemExit) as e: bot_main.update_bot(self.bot, '123') self.assertEqual(23, e.exception.code) cmd = [ os.path.join(test_env_bot_code.BOT_DIR, 'swarming_bot.2.zip'), 'start_slave', '--survive', ] self.assertEqual([cmd], calls)
def test_update_bot(self): # In a real case 'update_bot' never exits and doesn't call 'post_error'. # Under the test however forever-blocking calls finish, and post_error is # called. self.mock(self.bot, 'post_error', lambda *_: None) # Mock the file to download in the temporary directory. self.mock( bot_main, 'THIS_FILE', os.path.join(self.root_dir, 'swarming_bot.1.zip')) new_zip = os.path.join(self.root_dir, 'swarming_bot.2.zip') # This is necessary otherwise zipfile will crash. self.mock(time, 'time', lambda: 1400000000) def url_retrieve(f, url): self.assertEqual( 'https://localhost:1/swarming/api/v1/bot/bot_code/123', url) self.assertEqual(new_zip, f) # Create a valid zip that runs properly. with zipfile.ZipFile(f, 'w') as z: z.writestr('__main__.py', 'print("hi")') return True self.mock(net, 'url_retrieve', url_retrieve) calls = [] def exec_python(args): calls.append(args) return 23 self.mock(bot_main.common, 'exec_python', exec_python) with self.assertRaises(SystemExit) as e: bot_main.update_bot(self.bot, '123') self.assertEqual(23, e.exception.code) self.assertEqual([[new_zip, 'start_slave', '--survive']], calls)
def test_update_bot_win(self): self.mock(sys, 'platform', 'win32') # In a real case 'update_bot' never exists and doesn't call 'post_error'. # Under the test forever blocking calls self.mock(self.bot, 'post_error', lambda *_: None) self.mock(bot_main, 'THIS_FILE', 'swarming_bot.1.zip') self.mock(net, 'url_retrieve', lambda *_: True) calls = [] cmd = [sys.executable, 'swarming_bot.2.zip', 'start_slave', '--survive'] self.mock(subprocess, 'Popen', lambda *args: calls.append(args)) self.mock(os, 'execv', self.fail) with self.assertRaises(SystemExit): bot_main.update_bot(self.bot, '123') self.assertEqual([(cmd,)], calls)
def test_update_bot_linux(self): self.mock(sys, 'platform', 'linux2') # In a real case 'update_bot' never exits and doesn't call 'post_error'. # Under the test however forever-blocking calls finish, and post_error is # called. self.mock(self.bot, 'post_error', lambda *_: None) self.mock(bot_main, 'THIS_FILE', 'swarming_bot.1.zip') self.mock(net, 'url_retrieve', lambda *_: True) calls = [] cmd = [sys.executable, 'swarming_bot.2.zip', 'start_slave', '--survive'] self.mock(subprocess, 'Popen', self.fail) self.mock(os, 'execv', lambda *args: calls.append(args)) bot_main.update_bot(self.bot, '123') self.assertEqual([(sys.executable, cmd)], calls)
def test_update_bot_win(self): self.mock(sys, 'platform', 'win32') # In a real case 'update_bot' never exists and doesn't call 'post_error'. # Under the test forever blocking calls self.mock(self.bot, 'post_error', lambda *_: None) self.mock(bot_main, 'THIS_FILE', 'swarming_bot.1.zip') self.mock(net, 'url_retrieve', lambda *_: True) calls = [] cmd = [ sys.executable, 'swarming_bot.2.zip', 'start_slave', '--survive' ] self.mock(subprocess, 'Popen', lambda *args: calls.append(args)) self.mock(os, 'execv', self.fail) with self.assertRaises(SystemExit): bot_main.update_bot(self.bot, '123') self.assertEqual([(cmd, )], calls)
def test_update_bot_linux(self): self.mock(sys, 'platform', 'linux2') # In a real case 'update_bot' never exits and doesn't call 'post_error'. # Under the test however forever-blocking calls finish, and post_error is # called. self.mock(self.bot, 'post_error', lambda *_: None) self.mock(bot_main, 'THIS_FILE', 'swarming_bot.1.zip') self.mock(net, 'url_retrieve', lambda *_: True) calls = [] cmd = [ sys.executable, 'swarming_bot.2.zip', 'start_slave', '--survive' ] self.mock(subprocess, 'Popen', self.fail) self.mock(os, 'execv', lambda *args: calls.append(args)) bot_main.update_bot(self.bot, '123') self.assertEqual([(sys.executable, cmd)], calls)
def test_update_bot(self): # In a real case 'update_bot' never exits and doesn't call 'post_error'. # Under the test however forever-blocking calls finish, and post_error is # called. self.mock(self.bot, 'post_error', lambda *_: None) # Mock the file to download in the temporary directory. self.mock( bot_main, 'THIS_FILE', os.path.join(self.root_dir, 'swarming_bot.1.zip')) new_zip = os.path.join(self.root_dir, 'swarming_bot.2.zip') # This is necessary otherwise zipfile will crash. self.mock(time, 'time', lambda: 1400000000) def url_retrieve(f, url, headers=None, timeout=None): self.assertEqual( 'https://localhost:1/swarming/api/v1/bot/bot_code' '/123?bot_id=localhost', url) self.assertEqual(new_zip, f) self.assertEqual({}, headers) self.assertEqual(remote_client.NET_CONNECTION_TIMEOUT_SEC, timeout) # Create a valid zip that runs properly. with zipfile.ZipFile(f, 'w') as z: z.writestr('__main__.py', 'print("hi")') return True self.mock(net, 'url_retrieve', url_retrieve) calls = [] def exec_python(args): calls.append(args) return 23 self.mock(bot_main.common, 'exec_python', exec_python) with self.assertRaises(SystemExit) as e: bot_main.update_bot(self.bot, '123') self.assertEqual(23, e.exception.code) self.assertEqual([[new_zip, 'start_slave', '--survive']], calls)