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("paasta_tools.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 create_complete_app(context): with contextlib.nested( mock.patch('paasta_tools.marathon_tools.create_complete_config'), mock.patch('paasta_tools.marathon_tools.load_marathon_config', return_value=context.marathon_config), mock.patch('paasta_tools.marathon_tools.load_system_paasta_config', return_value=context.system_paasta_config), mock.patch('paasta_tools.bounce_lib.load_system_paasta_config', return_value=context.system_paasta_config), mock.patch( 'paasta_tools.setup_marathon_job.load_system_paasta_config', return_value=context.system_paasta_config), ) as ( mock_create_complete_config, _, _, _, mock_load_system_paasta_config, ): mock_create_complete_config.return_value = fake_service_config mock_load_system_paasta_config.return_value.get_cluster = mock.Mock( return_value=context.cluster) print marathon_tools.load_marathon_config() return_tuple = setup_marathon_job.setup_service( service=fake_service_name, instance=fake_instance_name, client=context.marathon_client, marathon_config=context.marathon_config, service_marathon_config=fake_service_marathon_config, soa_dir=None, ) assert return_tuple[0] == 0 assert 'deployed' in return_tuple[1]
def create_complete_app(context): with contextlib.nested( mock.patch('paasta_tools.marathon_tools.create_complete_config'), mock.patch('paasta_tools.marathon_tools.load_marathon_config', return_value=context.marathon_config), mock.patch('paasta_tools.marathon_tools.load_system_paasta_config', return_value=context.system_paasta_config), mock.patch('paasta_tools.bounce_lib.load_system_paasta_config', return_value=context.system_paasta_config), mock.patch('paasta_tools.setup_marathon_job.load_system_paasta_config', return_value=context.system_paasta_config), ) as ( mock_create_complete_config, _, _, _, mock_load_system_paasta_config, ): mock_create_complete_config.return_value = fake_service_config mock_load_system_paasta_config.return_value.get_cluster = mock.Mock(return_value=context.cluster) print marathon_tools.load_marathon_config() return_tuple = setup_marathon_job.setup_service( service=fake_service_name, instance=fake_instance_name, client=context.marathon_client, marathon_config=context.marathon_config, service_marathon_config=fake_service_marathon_config, soa_dir=None, ) assert return_tuple[0] == 0 assert 'deployed' in return_tuple[1]
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( 'paasta_tools.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 when_setup_service_initiated(context): config = { 'master': '%s' % get_service_connection_string('mesosmaster'), 'scheme': 'http', 'response_timeout': 5, } with contextlib.nested( mock.patch( 'paasta_tools.bounce_lib.get_happy_tasks', autospec=True, # Wrap function call so we can select a subset of tasks or test # intermediate steps, like when an app is not completely up side_effect=lambda app, _, __, ___, **kwargs: get_happy_tasks( app, context.service, "fake_nerve_ns", context.system_paasta_config)[:context.max_tasks], ), mock.patch('paasta_tools.bounce_lib.bounce_lock_zookeeper', autospec=True), mock.patch('paasta_tools.bounce_lib.create_app_lock', autospec=True), mock.patch('paasta_tools.bounce_lib.time.sleep', autospec=True), mock.patch('paasta_tools.setup_marathon_job.load_system_paasta_config', autospec=True), mock.patch('paasta_tools.setup_marathon_job._log', autospec=True), mock.patch('paasta_tools.marathon_tools.get_config_hash', autospec=True, return_value='confighash'), mock.patch('paasta_tools.marathon_tools.get_code_sha_from_dockerurl', autospec=True, return_value='newapp'), mock.patch('paasta_tools.marathon_tools.get_docker_url', autospec=True, return_value='busybox'), mock.patch('paasta_tools.paasta_maintenance.load_credentials', autospec=True), mock.patch.object(mesos.cli.master, 'CFG', config), ) as ( _, _, _, _, mock_load_system_paasta_config, _, _, _, _, mock_load_credentials, _, ): mock_load_credentials.side_effect = paasta_maintenance.load_credentials(mesos_secrets='/etc/mesos-slave-secret') mock_load_system_paasta_config.return_value.get_cluster = mock.Mock(return_value=context.cluster) # 120 * 0.5 = 60 seconds for _ in xrange(120): try: (code, message) = setup_marathon_job.setup_service( service=context.service, instance=context.instance, client=context.marathon_client, service_marathon_config=context.new_marathon_service_config, soa_dir='/nail/etc/services', ) assert code == 0, message return except MarathonHttpError: time.sleep(0.5) raise Exception("Unable to acquire app lock for setup_marathon_job.setup_service")
def when_setup_service_initiated(context): with mock.patch( 'paasta_tools.bounce_lib.get_happy_tasks', autospec=True, # Wrap function call so we can select a subset of tasks or test # intermediate steps, like when an app is not completely up side_effect=lambda app, _, __, ___, **kwargs: get_happy_tasks( app, context.service, "fake_nerve_ns", context.system_paasta_config, )[:context.max_tasks], ), mock.patch( 'paasta_tools.bounce_lib.bounce_lock_zookeeper', autospec=True, ), mock.patch( 'paasta_tools.bounce_lib.time.sleep', autospec=True, ), mock.patch( 'paasta_tools.setup_marathon_job.load_system_paasta_config', autospec=True, ) as mock_load_system_paasta_config, mock.patch( 'paasta_tools.setup_marathon_job._log', autospec=True, ), mock.patch( 'paasta_tools.marathon_tools.get_config_hash', autospec=True, return_value='confighash', ), mock.patch( 'paasta_tools.marathon_tools.get_code_sha_from_dockerurl', autospec=True, return_value='newapp', ), mock.patch( 'paasta_tools.utils.InstanceConfig.get_docker_url', autospec=True, return_value='busybox', ), mock.patch( 'paasta_tools.mesos_maintenance.get_principal', autospec=True, ) as mock_get_principal, mock.patch( 'paasta_tools.mesos_maintenance.get_secret', autospec=True, ) as mock_get_secret: credentials = mesos_maintenance.load_credentials(mesos_secrets='/etc/mesos-slave-secret') mock_get_principal.return_value = credentials.principal mock_get_secret.return_value = credentials.secret mock_load_system_paasta_config.return_value.get_cluster = mock.Mock(return_value=context.cluster) # 120 * 0.5 = 60 seconds for _ in range(120): try: marathon_apps_with_clients = marathon_tools.get_marathon_apps_with_clients( clients=context.marathon_clients.get_all_clients(), embed_tasks=True, ) (code, message, bounce_again) = setup_marathon_job.setup_service( service=context.service, instance=context.instance, clients=context.marathon_clients, marathon_apps_with_clients=marathon_apps_with_clients, job_config=context.new_marathon_service_config, soa_dir='/nail/etc/services', ) assert code == 0, message return except MarathonHttpError: time.sleep(0.5) raise Exception("Unable to acquire app lock for setup_marathon_job.setup_service")
def test_setup_service_srv_complete_config_raises(self): fake_name = "test_service" fake_instance = "test_instance" with mock.patch( "paasta_tools.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_complete_config_raises(self): fake_name = 'test_service' fake_instance = 'test_instance' with mock.patch( 'paasta_tools.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( 'paasta_tools.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, )
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("paasta_tools.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, )