def _get_req(qstring, version): req = webob.Request.blank('?' + qstring, ) mv_parsed = microversion_parse.Version(*version) mv_parsed.max_version = microversion_parse.parse_version_string( microversion.max_version_string()) mv_parsed.min_version = microversion_parse.parse_version_string( microversion.min_version_string()) req.environ[placement.microversion.MICROVERSION_ENVIRON] = mv_parsed return req
def do_parse(qstring, version=(1, 18)): """Converts a querystring to a MultiDict, mimicking request.GET, and runs parse_qs_request_groups on it. """ req = webob.Request.blank('?' + qstring) mv_parsed = microversion_parse.Version(*version) mv_parsed.max_version = microversion_parse.parse_version_string( microversion.max_version_string()) mv_parsed.min_version = microversion_parse.parse_version_string( microversion.min_version_string()) req.environ['placement.microversion'] = mv_parsed return util.parse_qs_request_groups(req)
def do_parse(qstring, version=(1, 18)): """Converts a querystring to a MultiDict, mimicking request.GET, and runs dict_from_request on it. """ req = webob.Request.blank('?' + qstring) mv_parsed = microversion_parse.Version(*version) mv_parsed.max_version = microversion_parse.parse_version_string( microversion.max_version_string()) mv_parsed.min_version = microversion_parse.parse_version_string( microversion.min_version_string()) req.environ['placement.microversion'] = mv_parsed d = pl.RequestGroup.dict_from_request(req) # Sort for easier testing return [d[suff] for suff in sorted(d)]
def do_parse(qstring, version=(1, 18)): """Converts a querystring to a MultiDict, mimicking request.GET, and runs parse_qs_request_groups on it. """ req = webob.Request.blank('?' + qstring) mv_parsed = microversion_parse.Version(*version) mv_parsed.max_version = microversion_parse.parse_version_string( microversion.max_version_string()) mv_parsed.min_version = microversion_parse.parse_version_string( microversion.min_version_string()) req.environ['placement.microversion'] = mv_parsed d = util.parse_qs_request_groups(req) # Sort for easier testing return [d[suff] for suff in sorted(d)]
def test_microversion_406_handling(self): body = '' status = '400 Bad Request' title = '' # Not a 406, no version info required. result = util.json_error_formatter(body, status, title, self.environ) self.assertNotIn('max_version', result['errors'][0]) self.assertNotIn('min_version', result['errors'][0]) # A 406 but not because of microversions (microversion # parsing was successful), no version info # required. status = '406 Not Acceptable' version_obj = microversion_parse.parse_version_string('2.3') self.environ[microversion.MICROVERSION_ENVIRON] = version_obj result = util.json_error_formatter(body, status, title, self.environ) self.assertNotIn('max_version', result['errors'][0]) self.assertNotIn('min_version', result['errors'][0]) # Microversion parsing failed, status is 406, send version info. del self.environ[microversion.MICROVERSION_ENVIRON] result = util.json_error_formatter(body, status, title, self.environ) self.assertEqual(microversion.max_version_string(), result['errors'][0]['max_version']) self.assertEqual(microversion.min_version_string(), result['errors'][0]['min_version'])
def test_microversion_406_handling(self): body = '' status = '400 Bad Request' title = '' # Not a 406, no version info required. result = util.json_error_formatter( body, status, title, self.environ) self.assertNotIn('max_version', result['errors'][0]) self.assertNotIn('min_version', result['errors'][0]) # A 406 but not because of microversions (microversion # parsing was successful), no version info # required. status = '406 Not Acceptable' version_obj = microversion_parse.parse_version_string('2.3') self.environ[microversion.MICROVERSION_ENVIRON] = version_obj result = util.json_error_formatter( body, status, title, self.environ) self.assertNotIn('max_version', result['errors'][0]) self.assertNotIn('min_version', result['errors'][0]) # Microversion parsing failed, status is 406, send version info. del self.environ[microversion.MICROVERSION_ENVIRON] result = util.json_error_formatter( body, status, title, self.environ) self.assertEqual(microversion.max_version_string(), result['errors'][0]['max_version']) self.assertEqual(microversion.min_version_string(), result['errors'][0]['min_version'])
def decorator(f): min_version = microversion_parse.parse_version_string(min_ver) if max_ver: max_version = microversion_parse.parse_version_string(max_ver) else: max_version = microversion_parse.parse_version_string( max_version_string()) qualified_name = _fully_qualified_name(f) VERSIONED_METHODS[qualified_name].append((min_version, max_version, f)) def decorated_func(req, *args, **kwargs): version = req.environ[MICROVERSION_ENVIRON] return _find_method(f, version, status_code)(req, *args, **kwargs) # Sort highest min version to beginning of list. VERSIONED_METHODS[qualified_name].sort(key=lambda x: x[0], reverse=True) return decorated_func
def decorator(f): min_version = microversion_parse.parse_version_string(min_ver) if max_ver: max_version = microversion_parse.parse_version_string(max_ver) else: max_version = microversion_parse.parse_version_string( max_version_string()) qualified_name = _fully_qualified_name(f) VERSIONED_METHODS[qualified_name].append( (min_version, max_version, f)) def decorated_func(req, *args, **kwargs): version = req.environ[MICROVERSION_ENVIRON] return _find_method(f, version, status_code)(req, *args, **kwargs) # Sort highest min version to beginning of list. VERSIONED_METHODS[qualified_name].sort(key=lambda x: x[0], reverse=True) return decorated_func
def test_microversion_sequential(self): for method_name, method_list in microversion.VERSIONED_METHODS.items(): previous_min_version = method_list[0][0] for method in method_list[1:]: previous_min_version = microversion_parse.parse_version_string( '%s.%s' % (previous_min_version.major, previous_min_version.minor - 1)) self.assertEqual(previous_min_version, method[1], "The microversions aren't sequential in the mehtod %s" % method_name) previous_min_version = method[0]
def test_microversion_sequential(self): for method_name, method_list in microversion.VERSIONED_METHODS.items(): previous_min_version = method_list[0][0] for method in method_list[1:]: previous_min_version = microversion_parse.parse_version_string( '%s.%s' % (previous_min_version.major, previous_min_version.minor - 1)) self.assertEqual( previous_min_version, method[1], "The microversions aren't sequential in the mehtod %s" % method_name) previous_min_version = method[0]
def setUp(self): super(TestEnsureConsumer, self).setUp() self.conf = cfg.ConfigOpts() self.useFixture(config_fixture.Config(self.conf)) conf.register_opts(self.conf) self.mock_project_get = self.useFixture( fixtures.MockPatch('placement.objects.project.' 'Project.get_by_uuid')).mock self.mock_user_get = self.useFixture( fixtures.MockPatch('placement.objects.user.' 'User.get_by_uuid')).mock self.mock_consumer_get = self.useFixture( fixtures.MockPatch('placement.objects.consumer.' 'Consumer.get_by_uuid')).mock self.mock_project_create = self.useFixture( fixtures.MockPatch('placement.objects.project.' 'Project.create')).mock self.mock_user_create = self.useFixture( fixtures.MockPatch('placement.objects.user.' 'User.create')).mock self.mock_consumer_create = self.useFixture( fixtures.MockPatch('placement.objects.consumer.' 'Consumer.create')).mock self.ctx = context.RequestContext(user_id='fake', project_id='fake') self.ctx.config = self.conf self.consumer_id = uuidsentinel.consumer self.project_id = uuidsentinel.project self.user_id = uuidsentinel.user mv_parsed = microversion_parse.Version(1, 27) mv_parsed.max_version = microversion_parse.parse_version_string( microversion.max_version_string()) mv_parsed.min_version = microversion_parse.parse_version_string( microversion.min_version_string()) self.before_version = mv_parsed mv_parsed = microversion_parse.Version(1, 28) mv_parsed.max_version = microversion_parse.parse_version_string( microversion.max_version_string()) mv_parsed.min_version = microversion_parse.parse_version_string( microversion.min_version_string()) self.after_version = mv_parsed
def setUp(self): super(TestEnsureConsumer, self).setUp() self.mock_project_get = self.useFixture(fixtures.MockPatch( 'placement.objects.project.' 'Project.get_by_external_id')).mock self.mock_user_get = self.useFixture(fixtures.MockPatch( 'placement.objects.user.' 'User.get_by_external_id')).mock self.mock_consumer_get = self.useFixture(fixtures.MockPatch( 'placement.objects.consumer.' 'Consumer.get_by_uuid')).mock self.mock_project_create = self.useFixture(fixtures.MockPatch( 'placement.objects.project.' 'Project.create')).mock self.mock_user_create = self.useFixture(fixtures.MockPatch( 'placement.objects.user.' 'User.create')).mock self.mock_consumer_create = self.useFixture(fixtures.MockPatch( 'placement.objects.consumer.' 'Consumer.create')).mock self.ctx = mock.sentinel.ctx self.consumer_id = uuidsentinel.consumer self.project_id = uuidsentinel.project self.user_id = uuidsentinel.user mv_parsed = microversion_parse.Version(1, 27) mv_parsed.max_version = microversion_parse.parse_version_string( microversion.max_version_string()) mv_parsed.min_version = microversion_parse.parse_version_string( microversion.min_version_string()) self.before_version = mv_parsed mv_parsed = microversion_parse.Version(1, 28) mv_parsed.max_version = microversion_parse.parse_version_string( microversion.max_version_string()) mv_parsed.min_version = microversion_parse.parse_version_string( microversion.min_version_string()) self.after_version = mv_parsed
def setUp(self): super(TestEnsureConsumer, self).setUp() self.mock_project_get = self.useFixture(fixtures.MockPatch( 'nova.api.openstack.placement.objects.project.' 'Project.get_by_external_id')).mock self.mock_user_get = self.useFixture(fixtures.MockPatch( 'nova.api.openstack.placement.objects.user.' 'User.get_by_external_id')).mock self.mock_consumer_get = self.useFixture(fixtures.MockPatch( 'nova.api.openstack.placement.objects.consumer.' 'Consumer.get_by_uuid')).mock self.mock_project_create = self.useFixture(fixtures.MockPatch( 'nova.api.openstack.placement.objects.project.' 'Project.create')).mock self.mock_user_create = self.useFixture(fixtures.MockPatch( 'nova.api.openstack.placement.objects.user.' 'User.create')).mock self.mock_consumer_create = self.useFixture(fixtures.MockPatch( 'nova.api.openstack.placement.objects.consumer.' 'Consumer.create')).mock self.ctx = mock.sentinel.ctx self.consumer_id = uuidsentinel.consumer self.project_id = uuidsentinel.project self.user_id = uuidsentinel.user mv_parsed = microversion_parse.Version(1, 27) mv_parsed.max_version = microversion_parse.parse_version_string( microversion.max_version_string()) mv_parsed.min_version = microversion_parse.parse_version_string( microversion.min_version_string()) self.before_version = mv_parsed mv_parsed = microversion_parse.Version(1, 28) mv_parsed.max_version = microversion_parse.parse_version_string( microversion.max_version_string()) mv_parsed.min_version = microversion_parse.parse_version_string( microversion.min_version_string()) self.after_version = mv_parsed
def _parse_provider_yaml(path): """Loads schema, parses a provider.yaml file and validates the content. :param path: File system path to the file to parse. :return: dict representing the contents of the file. :raise ProviderConfigException: If the specified file does not validate against the schema, the schema version is not supported, or if unable to read configuration or schema files. """ yaml_file = _load_yaml_file(path) try: schema_version = microversion_parse.parse_version_string( yaml_file['meta']['schema_version']) except (KeyError, TypeError): message = _("Unable to detect schema version: %s") % yaml_file raise nova_exc.ProviderConfigException(error=message) if schema_version.major not in SUPPORTED_SCHEMA_VERSIONS: message = _( "Unsupported schema major version: %d") % schema_version.major raise nova_exc.ProviderConfigException(error=message) if schema_version.minor not in \ SUPPORTED_SCHEMA_VERSIONS[schema_version.major]: # TODO(sean-k-mooney): We should try to provide a better # message that identifies which fields may be ignored # and the max minor version supported by this version of nova. message = ("Provider config file [%(path)s] is at schema version " "%(schema_version)s. Nova supports the major version, " "but not the minor. Some fields may be ignored." % { "path": path, "schema_version": schema_version }) LOG.warning(message) try: jsonschema.validate(yaml_file, SCHEMA_V1) except jsonschema.exceptions.ValidationError as e: message = _( "The provider config file %(path)s did not pass validation " "for schema version %(schema_version)s: %(reason)s") % { "path": path, "schema_version": schema_version, "reason": e } raise nova_exc.ProviderConfigException(error=message) return yaml_file
def test_adapt_whitespace(self): version = microversion_parse.parse_version_string(' 1.1 ') self.assertEqual((1, 1), version) self.assertEqual(microversion_parse.Version(1, 1), version)
def test_good_version(self): version = microversion_parse.parse_version_string('1.1') self.assertEqual((1, 1), version) self.assertEqual(microversion_parse.Version(1, 1), version)