def test_get_healthcheck_for_instance_tcp(self): fake_service = 'fake_service' fake_namespace = 'fake_namespace' fake_hostname = 'fake_hostname' fake_random_port = 666 fake_service_config = long_running_service_tools.LongRunningServiceConfig( service=fake_service, cluster='fake_cluster', instance=fake_namespace, config_dict={}, branch_dict={}, ) fake_service_namespace_config = long_running_service_tools.ServiceNamespaceConfig( { 'mode': 'tcp', }) with mock.patch( 'paasta_tools.long_running_service_tools.load_service_namespace_config', autospec=True, return_value=fake_service_namespace_config, ), mock.patch( 'socket.getfqdn', autospec=True, return_value=fake_hostname, ): expected = ('tcp', 'tcp://%s:%d' % (fake_hostname, fake_random_port)) actual = long_running_service_tools.get_healthcheck_for_instance( fake_service, fake_namespace, fake_service_config, fake_random_port, ) assert expected == actual
def test_get_healthcheck_for_instance_http(self): fake_service = 'fake_service' fake_namespace = 'fake_namespace' fake_hostname = 'fake_hostname' fake_random_port = 666 fake_path = '/fake_path' fake_service_config = long_running_service_tools.LongRunningServiceConfig( service=fake_service, cluster='fake_cluster', instance=fake_namespace, config_dict={}, branch_dict={}, ) fake_service_namespace_config = long_running_service_tools.ServiceNamespaceConfig({ 'mode': 'http', 'healthcheck_uri': fake_path, }) with contextlib.nested( mock.patch('paasta_tools.long_running_service_tools.load_service_namespace_config', autospec=True, return_value=fake_service_namespace_config), mock.patch('socket.getfqdn', autospec=True, return_value=fake_hostname), ) as ( load_service_namespace_config_patch, hostname_patch ): expected = ('http', 'http://%s:%d%s' % (fake_hostname, fake_random_port, fake_path)) actual = long_running_service_tools.get_healthcheck_for_instance( fake_service, fake_namespace, fake_service_config, fake_random_port) assert expected == actual
def test_get_healthcheck_for_instance_custom_soadir(self): fake_service = 'fake_service' fake_namespace = 'fake_namespace' fake_hostname = 'fake_hostname' fake_random_port = 666 fake_soadir = '/fake/soadir' fake_service_config = long_running_service_tools.LongRunningServiceConfig( service=fake_service, cluster='fake_cluster', instance=fake_namespace, config_dict={ 'healthcheck_mode': None, }, branch_dict={}, ) fake_service_namespace_config = long_running_service_tools.ServiceNamespaceConfig({}) with contextlib.nested( mock.patch('paasta_tools.long_running_service_tools.load_service_namespace_config', autospec=True, return_value=fake_service_namespace_config), mock.patch('socket.getfqdn', autospec=True, return_value=fake_hostname), ) as ( load_service_namespace_config_patch, hostname_patch ): expected = (None, None) actual = long_running_service_tools.get_healthcheck_for_instance( fake_service, fake_namespace, fake_service_config, fake_random_port, soa_dir=fake_soadir) assert expected == actual load_service_namespace_config_patch.assert_called_once_with(fake_service, fake_namespace, fake_soadir)
def test_get_healthcheck_for_instance_not_matching_mode(self): fake_service = "fake_service" fake_namespace = "fake_namespace" fake_hostname = "fake_hostname" fake_random_port = 666 fake_service_config = long_running_service_tools.LongRunningServiceConfig( service=fake_service, cluster="fake_cluster", instance=fake_namespace, config_dict={}, branch_dict=None, ) fake_service_namespace_config = long_running_service_tools.ServiceNamespaceConfig( {"mode": "http"}) with mock.patch( "paasta_tools.long_running_service_tools.load_service_namespace_config", autospec=True, return_value=fake_service_namespace_config, ), mock.patch("socket.getfqdn", autospec=True, return_value=fake_hostname): expected = ("http", "http://fake_hostname:666/status") actual = long_running_service_tools.get_healthcheck_for_instance( fake_service, fake_namespace, fake_service_config, fake_random_port) assert expected == actual
def test_get_healthcheck_for_instance_cmd(self): fake_service = 'fake_service' fake_namespace = 'fake_namespace' fake_hostname = 'fake_hostname' fake_random_port = 666 fake_cmd = '/bin/fake_command' fake_service_config = long_running_service_tools.LongRunningServiceConfig( service=fake_service, cluster='fake_cluster', instance=fake_namespace, config_dict={ 'healthcheck_mode': 'cmd', 'healthcheck_cmd': fake_cmd }, branch_dict={}, ) fake_service_namespace_config = long_running_service_tools.ServiceNamespaceConfig({}) with contextlib.nested( mock.patch('paasta_tools.long_running_service_tools.load_service_namespace_config', autospec=True, return_value=fake_service_namespace_config), mock.patch('socket.getfqdn', autospec=True, return_value=fake_hostname), ) as ( load_service_namespace_config_patch, hostname_patch ): expected = ('cmd', fake_cmd) actual = long_running_service_tools.get_healthcheck_for_instance( fake_service, fake_namespace, fake_service_config, fake_random_port) assert expected == actual
def test_get_healthcheck_for_instance_custom_soadir(self): fake_service = "fake_service" fake_namespace = "fake_namespace" fake_hostname = "fake_hostname" fake_random_port = 666 fake_soadir = "/fake/soadir" fake_service_config = long_running_service_tools.LongRunningServiceConfig( service=fake_service, cluster="fake_cluster", instance=fake_namespace, config_dict={"healthcheck_mode": None}, branch_dict=None, ) fake_service_namespace_config = long_running_service_tools.ServiceNamespaceConfig( {}) with mock.patch( "paasta_tools.long_running_service_tools.load_service_namespace_config", autospec=True, return_value=fake_service_namespace_config, ) as load_service_namespace_config_patch, mock.patch( "socket.getfqdn", autospec=True, return_value=fake_hostname): expected = (None, None) actual = long_running_service_tools.get_healthcheck_for_instance( fake_service, fake_namespace, fake_service_config, fake_random_port, soa_dir=fake_soadir, ) assert expected == actual load_service_namespace_config_patch.assert_called_once_with( fake_service, fake_namespace, fake_soadir)
def test_get_healthcheck_for_instance_cmd(self): fake_service = "fake_service" fake_namespace = "fake_namespace" fake_hostname = "fake_hostname" fake_random_port = 666 fake_cmd = "/bin/fake_command" fake_service_config = long_running_service_tools.LongRunningServiceConfig( service=fake_service, cluster="fake_cluster", instance=fake_namespace, config_dict={ "instances": 1, "healthcheck_mode": "cmd", "healthcheck_cmd": fake_cmd, }, branch_dict=None, ) fake_service_namespace_config = long_running_service_tools.ServiceNamespaceConfig( {}) with mock.patch( "paasta_tools.long_running_service_tools.load_service_namespace_config", autospec=True, return_value=fake_service_namespace_config, ), mock.patch("socket.getfqdn", autospec=True, return_value=fake_hostname): expected = ("cmd", fake_cmd) actual = long_running_service_tools.get_healthcheck_for_instance( fake_service, fake_namespace, fake_service_config, fake_random_port) assert expected == actual
def test_get_instances_respects_false(self): fake_conf = long_running_service_tools.LongRunningServiceConfig( service='fake_name', cluster='fake_cluster', instance='fake_instance', config_dict={'instances': False}, branch_dict={'desired_state': 'start'}, ) assert fake_conf.get_instances() == 0
def test_get_instances_default(self): fake_conf = long_running_service_tools.LongRunningServiceConfig( service='fake_name', cluster='fake_cluster', instance='fake_instance', config_dict={}, branch_dict={}, ) assert fake_conf.get_instances() == 1
def test_get_instances_respects_false(self): fake_conf = long_running_service_tools.LongRunningServiceConfig( service="fake_name", cluster="fake_cluster", instance="fake_instance", config_dict={"instances": False}, branch_dict={"desired_state": "start"}, ) assert fake_conf.get_instances() == 0
def test_get_healthcheck_cmd_happy(self): fake_conf = long_running_service_tools.LongRunningServiceConfig( service='fake_name', cluster='fake_cluster', instance='fake_instance', config_dict={'healthcheck_cmd': 'test_cmd'}, branch_dict={}, ) actual = fake_conf.get_healthcheck_cmd() assert actual == 'test_cmd'
def test_get_healthcheck_cmd_happy(self): fake_conf = long_running_service_tools.LongRunningServiceConfig( service="fake_name", cluster="fake_cluster", config_dict={"healthcheck_cmd": "/bin/true"}, instance="fake_instance", branch_dict=None, ) actual = fake_conf.get_healthcheck_cmd() assert actual == "/bin/true"
def test_get_healthcheck_cmd_raises_when_unset(self): fake_conf = long_running_service_tools.LongRunningServiceConfig( service='fake_name', cluster='fake_cluster', instance='fake_instance', config_dict={}, branch_dict={}, ) with raises(InvalidInstanceConfig) as exc: fake_conf.get_healthcheck_cmd() assert "healthcheck mode 'cmd' requires a healthcheck_cmd to run" in str(exc.value)
def test_validate_with_bad_registration(self): fake_conf = long_running_service_tools.LongRunningServiceConfig( service="fake_name", cluster="fake_cluster", instance="fake_instance", config_dict={ "registrations": ["fake_name.fake_instance", "bad_registration"], "deploy_group": None, }, branch_dict=None, ) error_messages = fake_conf.validate() assert "bad_registration" in error_messages[0]
def test_get_instances_respects_false(self): fake_conf = long_running_service_tools.LongRunningServiceConfig( service="fake_name", cluster="fake_cluster", instance="fake_instance", config_dict={"instances": False}, branch_dict={ "desired_state": "start", "git_sha": "c0debabe", "docker_image": "docker_image", "force_bounce": None, }, ) assert fake_conf.get_instances() == 0
def test_get_instances_in_config(self): fake_conf = long_running_service_tools.LongRunningServiceConfig( service="fake_name", cluster="fake_cluster", instance="fake_instance", config_dict={"instances": -10}, branch_dict={ "desired_state": "start", "git_sha": "c0ded00d", "docker_image": "docker_image", "image_version": None, "force_bounce": None, }, ) assert fake_conf.get_instances() == -10