1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
# Copyright 2014 Rackspace
# 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 unittest.mock import MagicMock
from trove.common import configurations
from trove.common.exception import UnprocessableEntity
from trove.configuration.service import ConfigurationsController
from trove.extensions.mgmt.configuration import service
from trove.tests.unittests import trove_testtools
class TestConfigurationParser(trove_testtools.TestCase):
def setUp(self):
super(TestConfigurationParser, self).setUp()
def test_parse_my_cnf_correctly(self):
config = """
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
connect_timeout = 15
# we need to test no value params
skip-external-locking
;another comment
!includedir /etc/mysql/conf.d/
"""
cfg_parser = configurations.MySQLConfParser(config)
parsed = cfg_parser.parse()
d_parsed = dict(parsed)
self.assertIsNotNone(d_parsed)
self.assertEqual("/var/run/mysqld/mysqld.pid", d_parsed["pid-file"])
self.assertEqual(15, d_parsed["connect_timeout"])
self.assertEqual('1', d_parsed["skip-external-locking"])
class TestConfigurationController(trove_testtools.TestCase):
def setUp(self):
super(TestConfigurationController, self).setUp()
self.controller = ConfigurationsController()
def _test_validate_configuration_with_action(self, body, action,
is_valid=True):
schema = self.controller.get_schema(action, body)
self.assertIsNotNone(schema)
validator = jsonschema.Draft4Validator(schema)
if is_valid:
self.assertTrue(validator.is_valid(body))
else:
self.assertFalse(validator.is_valid(body))
errors = sorted(validator.iter_errors(body), key=lambda e: e.path)
error_messages = [error.message for error in errors]
return error_messages
def test_validate_create_configuration(self):
body = {
"configuration": {
"values": {},
"name": "test",
"datastore": {
"type": "test_type",
"version": "test_version"
}
}
}
self._test_validate_configuration_with_action(body, action='create')
def test_validate_create_configuration_no_datastore(self):
body = {
"configuration": {
"values": {},
"name": "test"
}
}
self._test_validate_configuration_with_action(body, action='create')
def test_validate_create_invalid_values_param(self):
body = {
"configuration": {
"values": '',
"name": "test",
"datastore": {
"type": "test_type",
"version": "test_version"
}
}
}
error_messages = (
self._test_validate_configuration_with_action(body,
action='create',
is_valid=False))
self.assertIn("'' is not of type 'object'", error_messages)
def test_validate_create_invalid_name_param(self):
body = {
"configuration": {
"values": {},
"name": "",
"datastore": {
"type": "test_type",
"version": "test_version"
}
}
}
error_messages = (
self._test_validate_configuration_with_action(body,
action='create',
is_valid=False))
try:
self.assertIn("'' should be non-empty", error_messages)
except:
self.assertIn("'' is too short", error_messages)
def test_validate_edit_configuration(self):
body = {
"configuration": {
"values": {}
}
}
self._test_validate_configuration_with_action(body, action="edit")
def _test_validate_configuration(self, input_values, config_rules=None):
if config_rules is None:
config_val1 = MagicMock()
config_val1.name = 'max_connections'
config_val1.restart_required = 'false'
config_val1.datastore_version_id = 5.5
config_val1.max = 1
config_val1.min = 0
config_val1.data_type = 'integer'
config_rules = [config_val1]
data_version = MagicMock()
data_version.id = 42
data_version.name = 5.5
data_version.datastore_name = 'test'
self.assertRaises(UnprocessableEntity,
ConfigurationsController._validate_configuration,
input_values,
data_version,
config_rules)
def test_validate_configuration_with_no_rules(self):
self._test_validate_configuration({'max_connections': 5}, [])
def test_validate_configuration_with_invalid_param(self):
self._test_validate_configuration({'test': 5})
def test_validate_configuration_with_invalid_type(self):
self._test_validate_configuration({'max_connections': '1'})
def test_validate_configuration_with_invalid_max(self):
self._test_validate_configuration({'max_connections': 5})
def test_validate_configuration_with_invalid_min(self):
self._test_validate_configuration({'max_connections': -1})
def test_validate_long_value(self):
config_val1 = MagicMock()
config_val1.name = 'myisam_sort_buffer_size'
config_val1.max_size = 18446744073709551615
config_val1.min_size = 4096
config_val1.data_type = 'integer'
config_rules = [config_val1]
ConfigurationsController._validate_configuration(
{'myisam_sort_buffer_size': 18446744073709551615},
None, config_rules)
class TestConfigurationsParameterController(trove_testtools.TestCase):
def setUp(self):
super(TestConfigurationsParameterController, self).setUp()
self.controller = service.ConfigurationsParameterController()
def _test_validate_configuration_with_action(self, body, action,
is_valid=True):
schema = self.controller.get_schema(action, body)
self.assertIsNotNone(schema)
validator = jsonschema.Draft4Validator(schema)
if is_valid:
self.assertTrue(validator.is_valid(body))
else:
self.assertFalse(validator.is_valid(body))
errors = sorted(validator.iter_errors(body), key=lambda e: e.path)
error_messages = [error.message for error in errors]
return error_messages
def test_validate_create_configuration_param(self):
body = {
'configuration-parameter': {
'name': 'test',
'restart_required': 1,
'data_type': 'string',
'min': '0',
'max': '255'
}
}
self._test_validate_configuration_with_action(body, action='create')
def test_validate_create_invalid_restart_required(self):
body = {
'configuration-parameter': {
'name': 'test',
'restart_required': 5,
'data_type': 'string',
'min': 0,
'max': 255
}
}
error_messages = (
self._test_validate_configuration_with_action(body,
action='create',
is_valid=False))
self.assertIn("5 is greater than the maximum of 1", error_messages)
self.assertIn("0 is not of type 'string'", error_messages)
self.assertIn("255 is not of type 'string'", error_messages)
def test_validate_create_invalid_restart_required_2(self):
body = {
'configuration-parameter': {
'name': 'test',
'restart_required': -1,
'data_type': 'string',
'min': '0',
'max': '255'
}
}
error_messages = (
self._test_validate_configuration_with_action(body,
action='create',
is_valid=False))
self.assertIn("-1 is less than the minimum of 0", error_messages)
def test_validate_create_invalid_restart_required_3(self):
body = {
'configuration-parameter': {
'name': 'test',
'restart_required': 'yes',
'data_type': 'string',
'min': '0',
'max': '255'
}
}
error_messages = (
self._test_validate_configuration_with_action(body,
action='create',
is_valid=False))
self.assertIn("'yes' is not of type 'integer'", error_messages)
|