Files
trove/trove/tests/unittests/module/test_module_controller.py
T
Peter Stachowski bf3fb085cc Server side of module maintenance commands
This changeset handles the details of creating,
updating, listing and deleting Trove 'modules.'

Two new tables have been added to the Trove database:
    modules
    instance_modules
although the instance_modules table is at present unused.

Scenario tests have been written as well, to exercise the
new functionality.  These tests can be run by:
    ./redstack int-tests --group=module_create
In the follow-up changeset, all module tests can be run
by:
    ./redstack int-tests --group=module
Since module support is available for all datastores
(controlled by a CONF option) the module test has been
added to the common modules group.

Note: Trying to do admin tasks with none admin
credentials results in an Unauthorized exception
being thrown, instead of Forbidden.  This
is due to the fact that Forbidden is in the
HTTPUnauthorized section of wsgi.py instead of
the HTTPForbidden section.  Moving the exception
caused too many failures, so I created a 'Module'
Forbidden exception and put it in the right section.

Change-Id: I755b0431b33b870ae02d903527f071fd8e23130d
Depends-On: I54d37025275dee4731ad49ebbd21612c4464e4c4
Depends-On: I779c24472d3d96a7b2fe4ed0284fd5869cdef93b
Partially-Implements: blueprint module-maintenance
2016-02-25 11:10:51 -05:00

81 lines
3.0 KiB
Python

# Copyright 2016 Tesora, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
import jsonschema
from testtools.matchers import Is, Equals
from trove.module.service import ModuleController
from trove.tests.unittests import trove_testtools
class TestModuleController(trove_testtools.TestCase):
def setUp(self):
super(TestModuleController, self).setUp()
self.controller = ModuleController()
self.module = {
"module": {
"name": 'test_module',
"module_type": 'test',
"contents": 'my_contents\n',
}
}
def verify_errors(self, errors, msg=None, properties=None, path=None):
msg = msg or []
properties = properties or []
self.assertThat(len(errors), Is(len(msg)))
i = 0
while i < len(msg):
self.assertIn(errors[i].message, msg)
if path:
self.assertThat(path, Equals(properties[i]))
else:
self.assertThat(errors[i].path.pop(), Equals(properties[i]))
i += 1
def test_get_schema_create(self):
schema = self.controller.get_schema('create', {'module': {}})
self.assertIsNotNone(schema)
self.assertTrue('module' in schema['properties'])
def test_validate_create_complete(self):
body = self.module
schema = self.controller.get_schema('create', body)
validator = jsonschema.Draft4Validator(schema)
self.assertTrue(validator.is_valid(body))
def test_validate_create_blankname(self):
body = self.module
body['module']['name'] = " "
schema = self.controller.get_schema('create', body)
validator = jsonschema.Draft4Validator(schema)
self.assertFalse(validator.is_valid(body))
errors = sorted(validator.iter_errors(body), key=lambda e: e.path)
self.assertThat(len(errors), Is(1))
self.assertThat(errors[0].message,
Equals("' ' does not match '^.*[0-9a-zA-Z]+.*$'"))
def test_validate_create_invalid_name(self):
body = self.module
body['module']['name'] = "$#$%^^"
schema = self.controller.get_schema('create', body)
validator = jsonschema.Draft4Validator(schema)
self.assertFalse(validator.is_valid(body))
errors = sorted(validator.iter_errors(body), key=lambda e: e.path)
self.assertEqual(1, len(errors))
self.assertIn("'$#$%^^' does not match '^.*[0-9a-zA-Z]+.*$'",
errors[0].message)