# Path: shpkpr/deployment/bluegreen/validate.py
# class AppDefinitionValidator(object):
#     """Ensure that an app definition has the minimum set of labels required for
#     a bluegreen deploy.
# 
#     These labels ensure shpkpr is able to manage and track "groups" of otherwise
#     independent/standalone applications that have previously been deployed to
#     Marathon.
#     """
# 
#     REQUIRED_LABELS = [
#         "HAPROXY_DEPLOYMENT_GROUP"
#     ]
# 
#     ERROR_MESSAGE = "Missing label(s) from application definition: {0}"
# 
#     def __init__(self, *args, **kwargs):
#         pass
# 
#     def validate(self, app_definition):
#         missing_labels = []
#         labels = app_definition.get('labels', {})
#         for required_label in self.REQUIRED_LABELS:
#             if required_label in labels:
#                 continue
#             missing_labels.append(required_label)
#         if missing_labels:
#             msg = self.ERROR_MESSAGE.format(", ".join(missing_labels))
#             raise ValidationError(msg)
# 
# 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 pytest
from shpkpr.deployment.bluegreen.validate import AppDefinitionValidator
from shpkpr.deployment.bluegreen.validate import ValidationError
@pytest.fixture
def valid_app_definition(json_fixture):
    return json_fixture("marathon/bluegreen_app_new")


def test_valid_app(valid_app_definition):
    """Test that an app validates successfully when all the required labels are
    present.
    """
    validator = AppDefinitionValidator()
    try:
        validator.validate(valid_app_definition)
    except ValidationError as e:
        pytest.fail(e)


@pytest.fixture(params=[
    ["HAPROXY_DEPLOYMENT_GROUP"]
])
def invalid_app_definition(json_fixture, request):
    app_definition = json_fixture("marathon/bluegreen_app_new")
    for label in request.param:
        del(app_definition['labels'][label])
    return app_definition


def test_invalid_app(invalid_app_definition):
    """Test that an app fails validation when one or more required labels are
    missing.
    """

# Path: shpkpr/deployment/bluegreen/validate.py
# class AppDefinitionValidator(object):
#     """Ensure that an app definition has the minimum set of labels required for
#     a bluegreen deploy.
# 
#     These labels ensure shpkpr is able to manage and track "groups" of otherwise
#     independent/standalone applications that have previously been deployed to
#     Marathon.
#     """
# 
#     REQUIRED_LABELS = [
#         "HAPROXY_DEPLOYMENT_GROUP"
#     ]
# 
#     ERROR_MESSAGE = "Missing label(s) from application definition: {0}"
# 
#     def __init__(self, *args, **kwargs):
#         pass
# 
#     def validate(self, app_definition):
#         missing_labels = []
#         labels = app_definition.get('labels', {})
#         for required_label in self.REQUIRED_LABELS:
#             if required_label in labels:
#                 continue
#             missing_labels.append(required_label)
#         if missing_labels:
#             msg = self.ERROR_MESSAGE.format(", ".join(missing_labels))
#             raise ValidationError(msg)
# 
# 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_app_definition.py
import pytest
from shpkpr.deployment.bluegreen.validate import AppDefinitionValidator
from shpkpr.deployment.bluegreen.validate import ValidationError

@pytest.fixture
def valid_app_definition(json_fixture):
    return json_fixture("marathon/bluegreen_app_new")


def test_valid_app(valid_app_definition):
    """Test that an app validates successfully when all the required labels are
    present.
    """
    validator = AppDefinitionValidator()
    try:
        validator.validate(valid_app_definition)
    except ValidationError as e:
        pytest.fail(e)


@pytest.fixture(params=[
    ["HAPROXY_DEPLOYMENT_GROUP"]
])
def invalid_app_definition(json_fixture, request):
    app_definition = json_fixture("marathon/bluegreen_app_new")
    for label in request.param:
        del(app_definition['labels'][label])
    return app_definition


def test_invalid_app(invalid_app_definition):
    """Test that an app fails validation when one or more required labels are
    missing.
    """

    validator = AppDefinitionValidator()