def test_serializer(self): self.job.worker_options['sentry'] = Client() serialized = Job.serializer(self.job) self.assertEqual(serialized['id'], self.job.id) self.assertEqual(serialized['finished'], self.job.finished) self.assertEqual(serialized['owner'], self.job.owner) self.assertEqual(serialized['name'], self.job.name) self.assertEqual(serialized['results'], []) self.assertNotIn('worker_options', serialized) self.assertNotIn('docker', serialized) self.assertNotIn('api', serialized) self.job.tasks.append('tox') self.job.results['tox'] = Result('tox') serialized = Job.serializer(self.job) self.assertEqual(serialized['setup_results'], []) self.assertEqual(serialized['results'], [{'task': 'tox', 'pending': True}]) result = ProcessResult('tox') result.out = 'Success' result.return_code = 0 self.job.results['tox'].update_result(result) self.assertEqual(serialized['results'], [{'task': 'tox', 'pending': False, 'log': 'Success', 'return_code': 0, 'succeeded': True}]) self.assertEqual(serialized['setup_results'], [])
def test_serializer_with_setup_and_tasks(self): self.job.worker_options['sentry'] = Client() self.job.tasks.append('tox') self.job.setup_tasks.append('apt-get install nginx') self.job.results['tox'] = Result('tox') self.job.setup_results['apt-get install nginx'] = Result('apt-get install nginx') serialized = Job.serializer(self.job) self.assertEqual(serialized['setup_results'], [{'task': 'apt-get install nginx', 'pending': True}]) self.assertEqual(serialized['results'], [{'task': 'tox', 'pending': True}]) result = ProcessResult('tox') result.out = 'Success' result.return_code = 0 self.job.results['tox'].update_result(result) setup_result = ProcessResult('apt-get install nginx') setup_result.out = 'Success' setup_result.return_code = 0 self.job.setup_results['apt-get install nginx'].update_result(setup_result) self.assertEqual(serialized['results'], [{'task': 'tox', 'pending': False, 'log': 'Success', 'return_code': 0, 'succeeded': True}]) self.assertEqual(serialized['setup_results'], [{'task': 'apt-get install nginx', 'pending': False, 'log': 'Success', 'return_code': 0, 'succeeded': True}]) self.assertIn('worker_host', serialized)
def setUp(self): self.docker = Docker() self.job = Job(1, DATA, self.docker, WORKER_OPTIONS)
class BuildTests(unittest.TestCase): def setUp(self): self.docker = Docker() self.job = Job(1, DATA, self.docker, WORKER_OPTIONS) def test_init(self): self.assertEquals(self.job.id, 1) self.assertEquals(len(self.job.results), 0) self.assertEquals(self.job.branch, DATA['branch']) self.assertEquals(self.job.sha, DATA['sha']) self.assertEquals(self.job.sha, DATA['sha']) self.assertEquals(self.job.clone_url, DATA['clone_url']) self.assertEquals(self.job.owner, DATA['owner']) self.assertEquals(self.job.name, DATA['name']) self.assertEquals(self.job.worker_host, 'albus.frigg.io') def test_error(self): self.job.error('tox', 'Command not found') self.assertEquals(len(self.job.results), 1) self.assertTrue(self.job.errored) self.assertFalse(self.job.results['tox'].succeeded) self.assertEquals(self.job.results['tox'].log, 'Command not found') self.assertEquals(self.job.results['tox'].task, 'tox') @mock.patch('docker.manager.Docker.start') @mock.patch('docker.manager.Docker.stop') @mock.patch('docker.manager.Docker.run') def test_succeeded(self, mock_docker_run, mock_docker_stop, mock_docker_start): success = Result('tox') success.succeeded = True failure = Result('flake8') failure.succeeded = False self.job.results['tox'] = success self.assertTrue(self.job.succeeded) self.job.results['flake8'] = failure self.assertFalse(self.job.succeeded) @mock.patch('frigg_worker.api.APIWrapper.report_run') @mock.patch('frigg_worker.jobs.Job.serializer', lambda *x: {}) @mock.patch('frigg_worker.jobs.build_settings', lambda *x: {}) def test_report_run(self, mock_report_run): self.job.report_run() mock_report_run.assert_called_once_with('Job', 1, '{}') @mock.patch('frigg_worker.api.APIWrapper.report_run') @mock.patch('frigg_worker.jobs.Job.gh_token', 'a-gh-token') @mock.patch('frigg_worker.jobs.Job.serializer', lambda *x: {'results': 'a-gh-token'}) @mock.patch('frigg_worker.jobs.build_settings', lambda *x: {}) def test_report_run_should_remove_gh_token_from_payload(self, mock_report_run): self.job.report_run() mock_report_run.assert_called_once_with('Job', 1, '{"results": ""}') @mock.patch('docker.manager.Docker.directory_exist') @mock.patch('docker.manager.Docker.run') def test_delete_working_dir(self, mock_local_run, mock_directory_exist): self.job.delete_working_dir() self.assertTrue(mock_directory_exist.called) mock_local_run.assert_called_once_with('rm -rf ~/builds/1') @mock.patch('docker.manager.Docker.run') def test_run_task(self, mock_local_run): self.job.results['tox'] = Result('tox') self.job.run_task('tox') mock_local_run.assert_called_once_with('tox', '~/builds/1') self.assertEqual(len(self.job.results), 1) self.assertEqual(self.job.results['tox'].task, 'tox') self.assertEqual(self.job.results['tox'].pending, False) @mock.patch('docker.manager.Docker.run') def test_clone_repo_regular(self, mock_local_run): self.job.clone_repo(1) mock_local_run.assert_called_once_with( 'git clone --depth=1 --branch=master https://github.com/frigg/test-repo.git ~/builds/1' ' && cd ~/builds/1 && git reset --hard superbhash' ) @mock.patch('docker.manager.Docker.start') @mock.patch('docker.manager.Docker.stop') @mock.patch('docker.manager.Docker.run') def test_clone_repo_pull_request(self, mock_local_run, mock_docker_stop, mock_docker_start): self.job.pull_request_id = 2 self.job.clone_repo(1) mock_local_run.assert_called_once_with( 'git clone --depth=1 https://github.com/frigg/test-repo.git ~/builds/1 && cd ~/builds/1' ' && git fetch origin pull/2/head:pull-2 && git checkout pull-2 && ' 'cd ~/builds/1 && git reset --hard superbhash' ) @mock.patch('docker.manager.Docker.run') def test_clone_repo_no_depth(self, mock_local_run): self.job.clone_repo(0) mock_local_run.assert_called_once_with( 'git clone --branch=master https://github.com/frigg/test-repo.git ~/builds/1' ' && cd ~/builds/1 && git reset --hard superbhash' ) @mock.patch('frigg_worker.jobs.build_settings', lambda *x: BUILD_SETTINGS_WITH_NO_SERVICES) def test_serializer(self): self.job.worker_options['sentry'] = Client() serialized = Job.serializer(self.job) self.assertEqual(serialized['id'], self.job.id) self.assertEqual(serialized['finished'], self.job.finished) self.assertEqual(serialized['owner'], self.job.owner) self.assertEqual(serialized['name'], self.job.name) self.assertEqual(serialized['results'], []) self.assertNotIn('worker_options', serialized) self.assertNotIn('docker', serialized) self.assertNotIn('api', serialized) self.job.tasks.append('tox') self.job.results['tox'] = Result('tox') serialized = Job.serializer(self.job) self.assertEqual(serialized['setup_results'], []) self.assertEqual(serialized['results'], [{'task': 'tox', 'pending': True}]) result = ProcessResult('tox') result.out = 'Success' result.return_code = 0 self.job.results['tox'].update_result(result) self.assertEqual(serialized['results'], [{'task': 'tox', 'pending': False, 'log': 'Success', 'return_code': 0, 'succeeded': True}]) self.assertEqual(serialized['setup_results'], []) @mock.patch('frigg_worker.jobs.build_settings', lambda *x: BUILD_SETTINGS_SERVICES_AND_SETUP) def test_serializer_with_setup_and_tasks(self): self.job.worker_options['sentry'] = Client() self.job.tasks.append('tox') self.job.setup_tasks.append('apt-get install nginx') self.job.results['tox'] = Result('tox') self.job.setup_results['apt-get install nginx'] = Result('apt-get install nginx') serialized = Job.serializer(self.job) self.assertEqual(serialized['setup_results'], [{'task': 'apt-get install nginx', 'pending': True}]) self.assertEqual(serialized['results'], [{'task': 'tox', 'pending': True}]) result = ProcessResult('tox') result.out = 'Success' result.return_code = 0 self.job.results['tox'].update_result(result) setup_result = ProcessResult('apt-get install nginx') setup_result.out = 'Success' setup_result.return_code = 0 self.job.setup_results['apt-get install nginx'].update_result(setup_result) self.assertEqual(serialized['results'], [{'task': 'tox', 'pending': False, 'log': 'Success', 'return_code': 0, 'succeeded': True}]) self.assertEqual(serialized['setup_results'], [{'task': 'apt-get install nginx', 'pending': False, 'log': 'Success', 'return_code': 0, 'succeeded': True}]) self.assertIn('worker_host', serialized) @mock.patch('frigg_worker.jobs.build_settings', lambda *x: BUILD_SETTINGS_PREVIEW) def test_serializer_preview_settings(self): self.job.settings # load the settings object serialized = Job.serializer(self.job) self.assertEqual(serialized['settings']['preview']['image'], 'frigg/frigg-test-base') self.assertEqual(serialized['settings']['preview']['tasks'], ['gunicorn']) @mock.patch('docker.manager.Docker.start') @mock.patch('docker.manager.Docker.stop') @mock.patch('docker.manager.Docker.run') @mock.patch('frigg_worker.jobs.build_settings', lambda *x: BUILD_SETTINGS_WITH_NO_SERVICES) def test_start_no_services(self, mock_docker_run, mock_docker_stop, mock_docker_start): self.job.create_pending_tasks() self.job.start_services() self.assertFalse(mock_docker_run.called) @mock.patch('docker.manager.Docker.run') @mock.patch('frigg_worker.jobs.build_settings', lambda *x: BUILD_SETTINGS_ONE_SERVICE) def test_start_one_service(self, mock_docker_run): self.job.create_pending_tasks() self.job.start_services() mock_docker_run.assert_called_once_with('sudo service redis-server start') @mock.patch('docker.manager.Docker.run') @mock.patch('frigg_worker.jobs.build_settings', lambda *x: BUILD_SETTINGS_FOUR_SERVICES) def test_start_four_services_in_order(self, mock_docker_run): self.job.create_pending_tasks() self.job.start_services() mock_docker_run.assert_has_calls([ mock.call('sudo service redis-server start'), mock.call('sudo service postgresql start'), mock.call('sudo service nginx start'), mock.call('sudo service mongodb start'), ]) @mock.patch('frigg_worker.jobs.build_settings', lambda *x: BUILD_SETTINGS_SERVICES_AND_SETUP) def test_create_pending_tasks_splitted_into_setup_tasks_and_tasks(self): self.assertEqual([], self.job.tasks) self.assertEqual([], self.job.setup_tasks) self.job.create_pending_tasks() self.assertEqual(["apt-get install nginx"], self.job.setup_tasks) self.assertEqual(["tox"], self.job.tasks)
def test_serializer_preview_settings(self): self.job.settings # load the settings object serialized = Job.serializer(self.job) self.assertEqual(serialized['settings']['preview']['image'], 'frigg/frigg-test-base') self.assertEqual(serialized['settings']['preview']['tasks'], ['gunicorn'])