Example #1
0
class ValidateController(object):
    """
    Validate Controller Object
    Implements Application logic
    """
    def __init__(self):
        self.ve = ValidateEngine()

    def validate(self, request, body):
        """Validate the given cookbook
        :param request: request context
        :param body: a json with deployment parameters
        :return : a json file with process results
        """
        body = body or {}
        if len(body) < 1:
            raise exc.HTTPBadRequest(_("No action specified"))
        try:
            cookbook = body['cookbook']
            image = body['image']
        except KeyError:
            raise exc.HTTPBadRequest(_("Insufficient payload"))

        if 'recipe' in body.keys():
            recipe = body['recipe']
        else:
            recipe = "default"
        LOG.info(
            _LI('Processing Request for cookbook {cookbook}, recipe {recipe}, '
                'image {image}').format(cookbook=cookbook, recipe=recipe,
                                        image=image))
        res = self.ve.validate_cookbook(
            cookbook, recipe, image, request
        )
        return res
class ValidateEngineTestCase(ValidatorTestCase):
    """Tests for class ValidateEngine """

    def setUp(self):
        """Create a ValidateEngine instance """
        super(ValidateEngineTestCase, self).setUp()
        self.validate = ValidateEngine()

    def test_validate_cookbook(self):
        """Tests for method validate_cookbook """
        self.validate.d.cookbook_deployment_test = mock.MagicMock(
            return_value="OK")
        test_input = "MyInput"
        cookbook = recipe = image = request = test_input
        expected = "OK"
        observed = self.validate.validate_cookbook(
                              cookbook,
                              recipe,
                              image,
                              request)
        self.assertEqual(observed, expected)


    def tearDown(self):
        """Cleanup the ValidateEngine instance """
        super(ValidateEngineTestCase, self).tearDown()
        self.m.UnsetStubs()
        self.m.ResetAll()