def test_setup_service_srv_already_exists(self):
     fake_name = 'if_trees_could_talk'
     fake_instance = 'would_they_scream'
     fake_client = mock.MagicMock(get_app=mock.Mock(return_value=True))
     full_id = marathon_tools.format_job_id(fake_name, fake_instance)
     fake_complete = {
         'seven': 'full',
         'eight': 'frightened',
         'nine': 'eaten',
         'id': full_id,
     }
     with contextlib.nested(
         mock.patch(
             'paasta_tools.marathon_tools.create_complete_config',
             return_value=fake_complete,
             autospec=True,
         ),
         mock.patch(
             'paasta_tools.marathon_tools.load_marathon_config',
             return_value=self.fake_marathon_config,
             autospec=True,
         ),
         mock.patch(
             'setup_marathon_job.deploy_service',
             autospec=True,
         ),
     ) as (
         create_config_patch,
         get_config_patch,
         deploy_service_patch,
     ):
         setup_marathon_job.setup_service(
             service=fake_name,
             instance=fake_instance,
             client=fake_client,
             marathon_config=self.fake_marathon_config,
             service_marathon_config=self.fake_marathon_service_config,
             soa_dir=None,
         )
         create_config_patch.assert_called_once_with(
             fake_name,
             fake_instance,
             self.fake_marathon_config,
         )
         assert deploy_service_patch.call_count == 1
 def test_setup_service_srv_complete_config_raises(self):
     fake_name = 'test_service'
     fake_instance = 'test_instance'
     with mock.patch(
         'setup_marathon_job.marathon_tools.create_complete_config',
         side_effect=NoDockerImageError,
     ):
         status, output = setup_marathon_job.setup_service(
             service=fake_name,
             instance=fake_instance,
             client=None,
             marathon_config=None,
             service_marathon_config=None,
             soa_dir=None,
         )
         assert status == 1
         expected = 'Docker image for test_service.test_instance not in'
         assert expected in output
    def test_setup_service_srv_does_not_exist(self):
        fake_name = 'if_talk_was_cheap'
        fake_instance = 'psychatrists_would_be_broke'
        fake_response = mock.Mock(
            json=mock.Mock(return_value={'message': 'test'}))
        fake_client = mock.MagicMock(get_app=mock.Mock(
            side_effect=marathon.exceptions.NotFoundError(fake_response)))
        full_id = marathon_tools.format_job_id(fake_name, fake_instance, 'oogabooga', 'bananafanafofooga')
        fake_complete = {
            'do': 'you', 'even': 'dota', 'id': full_id,
            'docker_image': 'fake_docker_registry/fake_docker_image',
        }
        fake_bounce = 'trampoline'
        fake_drain_method = 'noop'
        fake_drain_method_params = {}
        with contextlib.nested(
            mock.patch(
                'paasta_tools.marathon_tools.create_complete_config',
                return_value=fake_complete,
                autospec=True,
            ),
            mock.patch(
                'setup_marathon_job.deploy_service',
                return_value=(111, 'Never'),
                autospec=True,
            ),
            mock.patch.object(
                self.fake_marathon_service_config,
                'get_bounce_method',
                return_value=fake_bounce,
                autospec=True,
            ),
            mock.patch.object(
                self.fake_marathon_service_config,
                'get_drain_method',
                return_value=fake_drain_method,
                autospec=True,
            ),
            mock.patch.object(
                self.fake_marathon_service_config,
                'get_drain_method_params',
                return_value=fake_drain_method_params,
                autospec=True,
            ),
            mock.patch(
                'paasta_tools.marathon_tools.load_marathon_service_config',
                return_value=self.fake_marathon_service_config,
                autospec=True,
            ),
            mock.patch(
                'paasta_tools.marathon_tools.load_service_namespace_config',
                return_value=self.fake_service_namespace_config,
                autospec=True,
            ),
        ) as (
            create_config_patch,
            deploy_service_patch,
            get_bounce_patch,
            get_drain_method_patch,
            get_drain_method_params_patch,
            read_service_conf_patch,
            read_namespace_conf_patch,
        ):
            status, output = setup_marathon_job.setup_service(
                service=fake_name,
                instance=fake_instance,
                client=fake_client,
                marathon_config=self.fake_marathon_config,
                service_marathon_config=self.fake_marathon_service_config,
                soa_dir=None,
            )
            assert status == 111
            assert output == 'Never'

            create_config_patch.assert_called_once_with(
                fake_name,
                fake_instance,
                self.fake_marathon_config
            )
            get_bounce_patch.assert_called_once_with()
            get_drain_method_patch.assert_called_once_with(read_namespace_conf_patch.return_value)
            deploy_service_patch.assert_called_once_with(
                service=fake_name,
                instance=fake_instance,
                marathon_jobid=full_id,
                config=fake_complete,
                client=fake_client,
                bounce_method=fake_bounce,
                drain_method_name=fake_drain_method,
                drain_method_params=fake_drain_method_params,
                nerve_ns=self.fake_marathon_service_config.get_nerve_namespace(),
                bounce_health_params=self.fake_marathon_service_config.get_bounce_health_params(
                    read_namespace_conf_patch.return_value),
                soa_dir=None,
            )