Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

increased test coverage for marathon_tools #309

Merged
merged 1 commit into from
Mar 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions paasta_tools/marathon_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,13 @@ def __repr__(self):
)

def copy(self):
return self.__class__(self.service, self.instance, dict(self.config_dict), dict(self.branch_dict))
return self.__class__(
service=self.service,
instance=self.instance,
cluster=self.cluster,
config_dict=dict(self.config_dict),
branch_dict=dict(self.branch_dict),
)

def get_min_instances(self):
return self.config_dict.get('min_instances', 1)
Expand Down Expand Up @@ -703,8 +709,6 @@ def get_proxy_port_for_instance(name, instance, cluster=None, soa_dir=DEFAULT_SO
:param cluster: The cluster to read the configuration for
:param soa_dir: The SOA config directory to read from
:returns: The proxy_port for the service instance, or None if not defined"""
if not cluster:
cluster = load_system_paasta_config().get_cluster()
namespace = read_namespace_for_service_instance(name, instance, cluster, soa_dir)
nerve_dict = load_service_namespace_config(name, namespace, soa_dir)
return nerve_dict.get('proxy_port')
Expand Down
85 changes: 85 additions & 0 deletions tests/test_marathon_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2159,3 +2159,88 @@ def test_create_complete_config():
):
marathon_tools.create_complete_config('service', 'instance', soa_dir=mock.Mock())
mock_format_marathon_app_dict.assert_called_once_with()


def test_marathon_config_key_errors():
fake_marathon_config = marathon_tools.MarathonConfig({}, '')
with raises(marathon_tools.MarathonNotConfigured):
fake_marathon_config.get_url()
with raises(marathon_tools.MarathonNotConfigured):
fake_marathon_config.get_username()
with raises(marathon_tools.MarathonNotConfigured):
fake_marathon_config.get_password()


def test_marathon_service_config_copy():
fake_marathon_service_config = marathon_tools.MarathonServiceConfig(
service='fake-service',
instance='fake-instance',
cluster='fake-cluster',
config_dict={'elem': 'test'},
branch_dict={'elem2': 'test2'},
)
fake_marathon_service_config_2 = fake_marathon_service_config.copy()
assert fake_marathon_service_config is not fake_marathon_service_config_2
assert fake_marathon_service_config == fake_marathon_service_config_2


def test_marathon_service_config_get_healthchecks_invalid_type():
fake_marathon_service_config = marathon_tools.MarathonServiceConfig(
service='fake-service',
instance='fake-instance',
cluster='fake-cluster',
config_dict={},
branch_dict={},
)
with mock.patch.object(marathon_tools.MarathonServiceConfig, 'get_healthcheck_mode', autospec=True,
return_value='fake-mode'):
with raises(marathon_tools.InvalidMarathonHealthcheckMode):
fake_marathon_service_config.get_healthchecks(mock.Mock())


def test_marathon_service_config_get_desired_state_human_invalid_desired_state():
fake_marathon_service_config = marathon_tools.MarathonServiceConfig(
service='fake-service',
instance='fake-instance',
cluster='fake-cluster',
config_dict={},
branch_dict={},
)
with mock.patch.object(marathon_tools.MarathonServiceConfig, 'get_desired_state', autospec=True,
return_value='fake-state'):
assert 'Unknown (desired_state: fake-state)' in fake_marathon_service_config.get_desired_state_human()


def test_read_namespace_for_service_instance_no_cluster():
mock_get_cluster = mock.Mock()
with contextlib.nested(
mock.patch('paasta_tools.marathon_tools.service_configuration_lib.read_extra_service_information',
autospec=True),
mock.patch('paasta_tools.marathon_tools.load_system_paasta_config', autospec=True,
return_value=mock.Mock(get_cluster=mock_get_cluster)),
) as (
_,
_,
):
marathon_tools.read_namespace_for_service_instance(mock.Mock(), mock.Mock())
mock_get_cluster.assert_called_once_with()


def wait_for_app_to_launch_tasks():
return_values = [False, None, True]

def get_mock_return_values(*args):
val = return_values.pop()
if val is None:
raise marathon_tools.NotFoundError
return val
with contextlib.nested(
mock.patch('paasta_tools.marathon_tools.app_has_tasks', autospec=True, side_effect=get_mock_return_values),
mock.patch('paasta_tools.marathon_tools.sleep', autospec=True),
) as (
mock_app_has_tasks,
mock_sleep,
):
marathon_tools.wait_for_app_to_launch_tasks(mock.Mock(), 'app_id', 0)
assert mock_app_has_tasks.call_count == 3
assert mock_sleep.call_count == 2