def test_main_no_deployments(self): with contextlib.nested( mock.patch('setup_chronos_job.parse_args', return_value=self.fake_args, autospec=True), mock.patch('paasta_tools.chronos_tools.load_chronos_config', autospec=True), mock.patch('paasta_tools.chronos_tools.get_chronos_client', return_value=self.fake_client, autospec=True), mock.patch('paasta_tools.chronos_tools.load_chronos_job_config', return_value=self.fake_chronos_job_config, autospec=True, side_effect=NoDeploymentsAvailable), mock.patch('setup_chronos_job.setup_job', return_value=(0, 'it_is_finished'), autospec=True), mock.patch('setup_chronos_job.load_system_paasta_config', autospec=True), mock.patch('setup_chronos_job.send_event', autospec=True), ) as ( parse_args_patch, load_chronos_config_patch, get_client_patch, load_chronos_job_config_patch, setup_job_patch, load_system_paasta_config_patch, send_event_patch ): load_system_paasta_config_patch.return_value.get_cluster = mock.MagicMock(return_value=self.fake_cluster) with raises(SystemExit) as excinfo: setup_chronos_job.main() assert excinfo.value.code == 0
def test_main_no_deployments(self): with contextlib.nested( mock.patch('setup_chronos_job.parse_args', return_value=self.fake_args, autospec=True), mock.patch('paasta_tools.chronos_tools.load_chronos_config', autospec=True), mock.patch('paasta_tools.chronos_tools.get_chronos_client', return_value=self.fake_client, autospec=True), mock.patch( 'paasta_tools.chronos_tools.load_chronos_job_config', return_value=self.fake_chronos_job_config, autospec=True, side_effect=NoDeploymentsAvailable), mock.patch('setup_chronos_job.setup_job', return_value=(0, 'it_is_finished'), autospec=True), mock.patch('setup_chronos_job.load_system_paasta_config', autospec=True), mock.patch('setup_chronos_job.send_event', autospec=True), ) as (parse_args_patch, load_chronos_config_patch, get_client_patch, load_chronos_job_config_patch, setup_job_patch, load_system_paasta_config_patch, send_event_patch): load_system_paasta_config_patch.return_value.get_cluster = mock.MagicMock( return_value=self.fake_cluster) with raises(SystemExit) as excinfo: setup_chronos_job.main() assert excinfo.value.code == 0
def test_main_success(self): expected_status = 0 expected_output = "it_is_finished" fake_complete_job_config = {"foo": "bar"} with contextlib.nested( mock.patch("setup_chronos_job.parse_args", return_value=self.fake_args, autospec=True), mock.patch("paasta_tools.chronos_tools.load_chronos_config", autospec=True), mock.patch("paasta_tools.chronos_tools.get_chronos_client", return_value=self.fake_client, autospec=True), mock.patch( "paasta_tools.chronos_tools.create_complete_config", return_value=fake_complete_job_config, autospec=True, ), mock.patch("setup_chronos_job.setup_job", return_value=(expected_status, expected_output), autospec=True), mock.patch("setup_chronos_job.send_event", autospec=True), mock.patch("setup_chronos_job.load_system_paasta_config", autospec=True), mock.patch("sys.exit", autospec=True), ) as ( parse_args_patch, load_chronos_config_patch, get_client_patch, create_complete_config_patch, setup_job_patch, send_event_patch, load_system_paasta_config_patch, sys_exit_patch, ): load_system_paasta_config_patch.return_value.get_cluster = mock.MagicMock(return_value=self.fake_cluster) setup_chronos_job.main() parse_args_patch.assert_called_once_with() get_client_patch.assert_called_once_with(load_chronos_config_patch.return_value) setup_job_patch.assert_called_once_with( service=self.fake_service, instance=self.fake_instance, complete_job_config=fake_complete_job_config, client=self.fake_client, cluster=self.fake_cluster, ) send_event_patch.assert_called_once_with( service=self.fake_service, instance=self.fake_instance, soa_dir=self.fake_args.soa_dir, status=expected_status, output=expected_output, ) sys_exit_patch.assert_called_once_with(0)
def test_main_bad_chronos_job_config_notifies_user(self): with contextlib.nested( mock.patch('setup_chronos_job.parse_args', return_value=self.fake_args, autospec=True), mock.patch('paasta_tools.chronos_tools.load_chronos_config', autospec=True), mock.patch('paasta_tools.chronos_tools.get_chronos_client', return_value=self.fake_client, autospec=True), mock.patch( 'paasta_tools.chronos_tools.load_chronos_job_config', return_value=self.fake_chronos_job_config, autospec=True, side_effect=chronos_tools.InvalidChronosConfigError( 'test bad configuration')), mock.patch('setup_chronos_job.setup_job', return_value=(0, 'it_is_finished'), autospec=True), mock.patch('setup_chronos_job.load_system_paasta_config', autospec=True), mock.patch('setup_chronos_job.send_event', autospec=True), ) as ( parse_args_patch, load_chronos_config_patch, get_client_patch, load_chronos_job_config_patch, setup_job_patch, load_system_paasta_config_patch, send_event_patch, ): load_system_paasta_config_patch.return_value.get_cluster = mock.MagicMock( return_value=self.fake_cluster) with raises(SystemExit) as excinfo: setup_chronos_job.main() assert excinfo.value.code == 0 expected_error_msg = ( "Could not read chronos configuration file for %s in cluster %s\nError was: test bad configuration" % (compose_job_id(self.fake_service, self.fake_instance), self.fake_cluster)) send_event_patch.assert_called_once_with( service=self.fake_service, instance=self.fake_instance, soa_dir=self.fake_args.soa_dir, status=Status.CRITICAL, output=expected_error_msg)
def test_main_bad_chronos_job_config_notifies_user(self): with contextlib.nested( mock.patch('setup_chronos_job.parse_args', return_value=self.fake_args, autospec=True), mock.patch('paasta_tools.chronos_tools.load_chronos_config', autospec=True), mock.patch('paasta_tools.chronos_tools.get_chronos_client', return_value=self.fake_client, autospec=True), mock.patch('paasta_tools.chronos_tools.load_chronos_job_config', return_value=self.fake_chronos_job_config, autospec=True, side_effect=chronos_tools.InvalidChronosConfigError('test bad configuration')), mock.patch('setup_chronos_job.setup_job', return_value=(0, 'it_is_finished'), autospec=True), mock.patch('setup_chronos_job.load_system_paasta_config', autospec=True), mock.patch('setup_chronos_job.send_event', autospec=True), ) as ( parse_args_patch, load_chronos_config_patch, get_client_patch, load_chronos_job_config_patch, setup_job_patch, load_system_paasta_config_patch, send_event_patch, ): load_system_paasta_config_patch.return_value.get_cluster = mock.MagicMock(return_value=self.fake_cluster) with raises(SystemExit) as excinfo: setup_chronos_job.main() assert excinfo.value.code == 0 expected_error_msg = ( "Could not read chronos configuration file for %s in cluster %s\nError was: test bad configuration" % (compose_job_id(self.fake_service, self.fake_instance), self.fake_cluster) ) send_event_patch.assert_called_once_with( service=self.fake_service, instance=self.fake_instance, soa_dir=self.fake_args.soa_dir, status=Status.CRITICAL, output=expected_error_msg )
def test_main_success(self): expected_status = 0 expected_output = 'it_is_finished' fake_complete_job_config = {'foo': 'bar'} with contextlib.nested( mock.patch('setup_chronos_job.parse_args', return_value=self.fake_args, autospec=True), mock.patch('paasta_tools.chronos_tools.load_chronos_config', autospec=True), mock.patch('paasta_tools.chronos_tools.get_chronos_client', return_value=self.fake_client, autospec=True), mock.patch( 'paasta_tools.chronos_tools.load_chronos_job_config', return_value=self.fake_chronos_job_config, autospec=True), mock.patch('paasta_tools.chronos_tools.create_complete_config', return_value=fake_complete_job_config, autospec=True), mock.patch('setup_chronos_job.setup_job', return_value=(expected_status, expected_output), autospec=True), mock.patch('setup_chronos_job.send_event', autospec=True), mock.patch('setup_chronos_job.load_system_paasta_config', autospec=True), mock.patch('sys.exit', autospec=True), ) as ( parse_args_patch, load_chronos_config_patch, get_client_patch, load_chronos_job_config_patch, create_complete_config_patch, setup_job_patch, send_event_patch, load_system_paasta_config_patch, sys_exit_patch, ): load_system_paasta_config_patch.return_value.get_cluster = mock.MagicMock( return_value=self.fake_cluster) setup_chronos_job.main() parse_args_patch.assert_called_once_with() get_client_patch.assert_called_once_with( load_chronos_config_patch.return_value) load_chronos_job_config_patch.assert_called_once_with( service=self.fake_service, instance=self.fake_instance, cluster=self.fake_cluster, soa_dir=self.fake_args.soa_dir, ) setup_job_patch.assert_called_once_with( service=self.fake_service, instance=self.fake_instance, chronos_job_config=self.fake_chronos_job_config, complete_job_config=fake_complete_job_config, client=self.fake_client, cluster=self.fake_cluster, ) send_event_patch.assert_called_once_with( service=self.fake_service, instance=self.fake_instance, soa_dir=self.fake_args.soa_dir, status=expected_status, output=expected_output, ) sys_exit_patch.assert_called_once_with(0)