# Path: shpkpr/deployment/bluegreen/validate.py
# class MarathonStateValidator(object):
#     """Ensure that the application is in a clean state on Marathon before
#     deploying.
# 
#     This validator checks that exactly one application is running in the target
#     deployment group. If more than one application stack (typically both blue
#     and green) are detected then it may indicate that another deploy is in
#     progress or that a previous failed but wasn't cleaned up. Either way a
#     deployment should not continue under those circumstances.
#     """
# 
#     ERROR_MESSAGE = (
#         "More than one active application stack detected on Marathon, please "
#         "resolve before continuing with your deploy. This may indicate that "
#         "another deploy is already in progress or that a previous one has "
#         "failed."
#     )
# 
#     def __init__(self, marathon_client, *args, **kwargs):
#         self.marathon_client = marathon_client
# 
#     def validate(self, app_definition):
#         def get_deployment_group(app_definition):
#             labels = app_definition.get('labels', {})
#             return labels.get('HAPROXY_DEPLOYMENT_GROUP')
# 
#         app_group = get_deployment_group(app_definition)
# 
#         remote_apps = self.marathon_client.list_applications()
#         remote_groups = [get_deployment_group(app) for app in remote_apps]
# 
#         if Counter(remote_groups)[app_group] > 1:
#             raise ValidationError(self.ERROR_MESSAGE)
# 
# Path: shpkpr/deployment/bluegreen/validate.py
# class ValidationError(exceptions.ShpkprException):
#     exit_code = 2
# 
#     def format_message(self):
#         return 'Unable to validate deployment: %s' % self.message


import mock
import pytest
from shpkpr.deployment.bluegreen.validate import MarathonStateValidator
from shpkpr.deployment.bluegreen.validate import ValidationError
        pytest.fail(e)


def test_valid_state_one_existing_stack(valid_app_definition, json_fixture):
    """Test that an app validates successfully when one existing stack is
    present on Marathon.
    """
    marathon_client = mock.Mock()
    marathon_client.list_applications.side_effect = lambda: [
        json_fixture("marathon/bluegreen_app_existing"),
    ]

    validator = MarathonStateValidator(marathon_client)
    try:
        validator.validate(valid_app_definition)
    except ValidationError as e:
        pytest.fail(e)


def test_invalid_state_two_existing_stacks(valid_app_definition, json_fixture):
    """Test that an app fails validation when more than one existing stack is
    present on Marathon.
    """
    marathon_client = mock.Mock()
    marathon_client.list_applications.side_effect = lambda: [
        json_fixture("marathon/bluegreen_app_existing"),
        json_fixture("marathon/bluegreen_app_existing"),
    ]

    validator = MarathonStateValidator(marathon_client)

# Path: shpkpr/deployment/bluegreen/validate.py
# class MarathonStateValidator(object):
#     """Ensure that the application is in a clean state on Marathon before
#     deploying.
# 
#     This validator checks that exactly one application is running in the target
#     deployment group. If more than one application stack (typically both blue
#     and green) are detected then it may indicate that another deploy is in
#     progress or that a previous failed but wasn't cleaned up. Either way a
#     deployment should not continue under those circumstances.
#     """
# 
#     ERROR_MESSAGE = (
#         "More than one active application stack detected on Marathon, please "
#         "resolve before continuing with your deploy. This may indicate that "
#         "another deploy is already in progress or that a previous one has "
#         "failed."
#     )
# 
#     def __init__(self, marathon_client, *args, **kwargs):
#         self.marathon_client = marathon_client
# 
#     def validate(self, app_definition):
#         def get_deployment_group(app_definition):
#             labels = app_definition.get('labels', {})
#             return labels.get('HAPROXY_DEPLOYMENT_GROUP')
# 
#         app_group = get_deployment_group(app_definition)
# 
#         remote_apps = self.marathon_client.list_applications()
#         remote_groups = [get_deployment_group(app) for app in remote_apps]
# 
#         if Counter(remote_groups)[app_group] > 1:
#             raise ValidationError(self.ERROR_MESSAGE)
# 
# Path: shpkpr/deployment/bluegreen/validate.py
# class ValidationError(exceptions.ShpkprException):
#     exit_code = 2
# 
#     def format_message(self):
#         return 'Unable to validate deployment: %s' % self.message


# Path: tests/deployment/bluegreen/validate/test_marathon_state.py
import mock
import pytest
from shpkpr.deployment.bluegreen.validate import MarathonStateValidator
from shpkpr.deployment.bluegreen.validate import ValidationError

        pytest.fail(e)


def test_valid_state_one_existing_stack(valid_app_definition, json_fixture):
    """Test that an app validates successfully when one existing stack is
    present on Marathon.
    """
    marathon_client = mock.Mock()
    marathon_client.list_applications.side_effect = lambda: [
        json_fixture("marathon/bluegreen_app_existing"),
    ]

    validator = MarathonStateValidator(marathon_client)
    try:
        validator.validate(valid_app_definition)
    except ValidationError as e:
        pytest.fail(e)


def test_invalid_state_two_existing_stacks(valid_app_definition, json_fixture):
    """Test that an app fails validation when more than one existing stack is
    present on Marathon.
    """
    marathon_client = mock.Mock()
    marathon_client.list_applications.side_effect = lambda: [
        json_fixture("marathon/bluegreen_app_existing"),
        json_fixture("marathon/bluegreen_app_existing"),
    ]

    validator = MarathonStateValidator(marathon_client)

    with pytest.raises(ValidationError):