def execute(self, description): """ Execute a http call on an api that are expected to result in client errors. First it uses invalid resources that are part of the url, and then invalid data for queries and http request bodies. :param description: A json file or dictionary with the following entries: name (required) name for the api http-method (required) one of HEAD,GET,PUT,POST,PATCH,DELETE url (required) the url to be appended to the catalog url with '%s' for each resource mentioned resources: (optional) A list of resource names such as "server", "flavor", etc. with an element for each '%s' in the url. This method will call self.get_resource for each element when constructing the positive test case template so negative subclasses are expected to return valid resource ids when appropriate. json-schema (optional) A valid json schema that will be used to create invalid data for the api calls. For "GET" and "HEAD", the data is used to generate query strings appended to the url, otherwise for the body of the http call. """ LOG.info("Executing %s" % description["name"]) LOG.debug(description) method = description["http-method"] url = description["url"] resources = [ self.get_resource(r) for r in description.get("resources", []) ] if hasattr(self, "resource"): # Note(mkoderer): The resources list already contains an invalid # entry (see get_resource). # We just send a valid json-schema with it valid_schema = None schema = description.get("json-schema", None) if schema: valid_schema = \ valid.ValidTestGenerator().generate_valid(schema) new_url, body = self._http_arguments(valid_schema, url, method) elif hasattr(self, "schema"): new_url, body = self._http_arguments(self.schema, url, method) else: raise Exception("testscenarios are not active. Please make sure " "that your test runner supports the load_tests " "mechanism") if "admin_client" in description and description["admin_client"]: client = self.admin_client else: client = self.client resp, resp_body = client.send_request(method, new_url, resources, body=body) self._check_negative_response(resp.status, resp_body)
def gen_obj_remove_attr(self, schema): invalids = [] valid_schema = valid.ValidTestGenerator().generate_valid(schema) required = schema.get("required", []) for r in required: new_valid = copy.deepcopy(valid_schema) del new_valid[r] invalids.append(("gen_obj_remove_attr", new_valid, None)) return invalids
def test_generate_obj(self): schema = self.fake_input_obj scenarios = self.generator.generate_scenarios(schema) for scenario in scenarios: test = self.fake_test_class(scenario) valid_schema = \ valid_generator.ValidTestGenerator().generate_valid(schema) schema_under_test = copy.copy(valid_schema) expected_result = \ self.generator.generate_payload(test, schema_under_test) self.assertEqual(expected_result, None) self._validate_result(valid_schema, schema_under_test)
def gen_inv_prop_obj(self, schema): LOG.debug("generate_invalid_object: %s" % schema) valid_schema = valid.ValidTestGenerator().generate_valid(schema) invalids = [] properties = schema["properties"] for k, v in properties.iteritems(): for invalid in self.generate(v): LOG.debug(v) new_valid = copy.deepcopy(valid_schema) new_valid[k] = invalid[1] name = "prop_%s_%s" % (k, invalid[0]) invalids.append((name, new_valid, invalid[2])) LOG.debug("generate_invalid_object return: %s" % invalids) return invalids
def execute(self, description): """Execute a http call Execute a http call on an api that are expected to result in client errors. First it uses invalid resources that are part of the url, and then invalid data for queries and http request bodies. :param description: A json file or dictionary with the following entries: name (required) name for the api http-method (required) one of HEAD,GET,PUT,POST,PATCH,DELETE url (required) the url to be appended to the catalog url with '%s' for each resource mentioned resources: (optional) A list of resource names such as "server", "flavor", etc. with an element for each '%s' in the url. This method will call self.get_resource for each element when constructing the positive test case template so negative subclasses are expected to return valid resource ids when appropriate. json-schema (optional) A valid json schema that will be used to create invalid data for the api calls. For "GET" and "HEAD", the data is used to generate query strings appended to the url, otherwise for the body of the http call. """ LOG.info("Executing %s" % description["name"]) LOG.debug(description) generator = importutils.import_class( CONF.negative.test_generator)() schema = description.get("json-schema", None) method = description["http-method"] url = description["url"] expected_result = None if "default_result_code" in description: expected_result = description["default_result_code"] resources = [self.get_resource(r) for r in description.get("resources", [])] if hasattr(self, "resource"): # Note(mkoderer): The resources list already contains an invalid # entry (see get_resource). # We just send a valid json-schema with it valid_schema = None if schema: valid_schema = \ valid.ValidTestGenerator().generate_valid(schema) new_url, body = self._http_arguments(valid_schema, url, method) elif hasattr(self, "_negtest_name"): schema_under_test = \ valid.ValidTestGenerator().generate_valid(schema) local_expected_result = \ generator.generate_payload(self, schema_under_test) if local_expected_result is not None: expected_result = local_expected_result new_url, body = \ self._http_arguments(schema_under_test, url, method) else: raise Exception("testscenarios are not active. Please make sure " "that your test runner supports the load_tests " "mechanism") if "admin_client" in description and description["admin_client"]: if not credentials.is_admin_available( identity_version=self.get_identity_version()): msg = ("Missing Identity Admin API credentials in" "configuration.") raise self.skipException(msg) creds = self.credentials_provider.get_admin_creds() os_adm = clients.Manager(credentials=creds) client = os_adm.negative_client else: client = self.client resp, resp_body = client.send_request(method, new_url, resources, body=body) self._check_negative_response(expected_result, resp.status, resp_body)
def setUp(self): super(TestNegativeValidGenerator, self).setUp() self.generator = valid_generator.ValidTestGenerator()
def gen_obj_add_attr(self, schema): valid_schema = valid.ValidTestGenerator().generate_valid(schema) new_valid = copy.deepcopy(valid_schema) new_valid["$$$$$$$$$$"] = "xxx" return new_valid
def gen_obj_add_attr(self, schema): valid_schema = valid.ValidTestGenerator().generate_valid(schema) if not schema.get("additionalProperties", True): new_valid = copy.deepcopy(valid_schema) new_valid["$$$$$$$$$$"] = "xxx" return new_valid