def __verify_zone(self, resource_location, resource_payload): location_payload_to_compare = JsonComparator.odict_to_dict( self.discovery_container.get(resource_location).body["Links"] ["Endpoints"]) payload_to_compare = resource_payload["Links"]["Endpoints"] return JsonComparator.compare_lists(location_payload_to_compare, payload_to_compare)
def test_fields_not_in_golden(self): json = dict( a=1, b="xyz", c=[1, 2 ,3]) golden = dict( a=1, b="xyz") self.assertFalse(JsonComparator.compare_jsons(json, golden)) self.assertTrue(JsonComparator.compare_json_to_golden(json, golden))
def test_fields_in_ignore_list(self): json = dict( a=1, b="xyz", c=[1, 2 ,3]) golden = dict( a=1, b="xyz", d=dict()) self.assertFalse(JsonComparator.compare_jsons(json, golden)) self.assertFalse(JsonComparator.compare_json_to_golden(json, golden)) self.assertTrue(JsonComparator.compare_json_to_golden(json, golden, ignore = ["/d"]))
def _test_case_get_created_logical_drive(self, logical_drive=None): print "TEST_CASE::Get the created logical drive" if not logical_drive: logical_drive = self.created_logical_drive status, status_code, response_body, _ = self.api_caller.get_resource(logical_drive) if not status: print "ERROR::Wrong status code of GET response: %s" % status_code print "ERROR::Response body:\n%s" % response_body self.set_status_failed() return ValidationStatus.FAILED else: api_resource = ApiResource(response_body, response_body["@odata.type"]) self.discovery_container.add_resource(logical_drive, api_resource) print "MESSAGE::Comparing newly created logical drive to given payload" ignored_fields = ["/Name", "/Image"] # Name setting not supported by PSME REST API, Image is optional print "MESSAGE::Comparing newly created logical drive to given payload" if not JsonComparator.compare_json_to_golden(response_body, self.expected_created_logical_drive_body, ignore = ignored_fields): print "ERROR::Newly created logical drive's body incorrect" self.set_status_failed() print "MESSAGE::Proceeding with deleting the resource" return ValidationStatus.FAILED print "MESSAGE::Newly created logical drive's body correct" self.set_status_passed() return ValidationStatus.PASSED
def _test_case_get_created_logical_drive(self, logical_drive): print "TEST_CASE::Get the created logical drive" link, status, status_code, response_body, _ = \ self.api_caller.get_resource(self.created_logical_drive, self.discovery_container, api_endpoint_override=self.created_logical_drive_netloc) if not status: cts_error("Wrong status code of GET response: {status_code}; response body: {response_body:response_body}", **locals()) self.set_status_failed() return ValidationStatus.FAILED else: print "MESSAGE::Comparing newly created logical drive to given payload" if not JsonComparator.compare_json_to_golden(response_body, self.expected_created_logical_drive_body, ignore = ["/Name"]): # Name setting not supported by PSME REST API cts_error("Newly created logical drive's body incorrect") self.set_status_failed() print "MESSAGE::Proceeding with deleting the resource" return ValidationStatus.FAILED print "MESSAGE::Newly created logical drive's correct" self.set_status_passed() return ValidationStatus.PASSED
def _test_case_update_created_logical_drive(self): print "TEST_CASE::Update the created logical drive" patch_logical_drive_payload = dict( Bootable = not self.post_logical_drive_payload["Bootable"] ) status, status_code, response_body, _ = self.api_caller.patch_resource(self.created_logical_drive, payload=patch_logical_drive_payload) if not status: print "ERROR::Wrong status code of PATCH reponse %s" % status_code print "ERROR::Response body:\n%s" % response_body self.set_status_failed() return ValidationStatus.FAILED status, status_code, response_body, _ = self.api_caller.get_resource(self.created_logical_drive) if not status: print "ERROR::Wrong status code of GET reponse %s" % status_code print "ERROR::Response body:\n%s" % response_body self.set_status_failed() return ValidationStatus.FAILED else: print "MESSAGE::Verifying whether the logical drive's update has taken place" if not JsonComparator.compare_json_to_golden(response_body, patch_logical_drive_payload): print "ERROR::Newly created logical drive could not be updated" self.set_status_failed() print "MESSAGE::Proceeding with deleting the resource" return ValidationStatus.FAILED print "MESSAGE::Newly created logical drive updated correctly" self.set_status_passed() return ValidationStatus.PASSED
def _test_case_get_created_remote_target(self): print "TEST_CASE::Get the created remote target" link, status, status_code, response_body, _ = \ self.api_caller.get_resource(self.created_remote_target, self.discovery_container, api_endpoint_override=self.created_remote_target_netloc) if not status: cts_error( "Wrong status code of GET response: {status_code}; response body: {response_body:response_body}", **locals()) self.set_status_failed() return ValidationStatus.FAILED else: print "MESSAGE::Comparing newly created remote target to given payload" if not JsonComparator.compare_json_to_golden( response_body, self.post_remote_target_payload): cts_error("Newly created remote target's body incorrect") self.set_status_failed() print "MESSAGE::Proceeding with deleting the resource" return ValidationStatus.FAILED print "MESSAGE::Newly created remote target's body correct" self.set_status_passed() return ValidationStatus.PASSED
def _test_case_get_created_static_mac(self): print "TEST_CASE::Get created Static MAC" link, status, status_code, response_body, _ = \ self.api_caller.get_resource(self.created_static_mac, self.discovery_container, api_endpoint_override=self.created_static_mac_netloc) if not status: cts_error( "Wrong status code of GET response: {status_code}; response body: {response_body:response_body}", **locals()) self.set_status_failed() return False else: print "MESSAGE::Comparing newly created Static MAC to given payload" if not JsonComparator.compare_json_to_golden( response_body, self.post_static_mac_payload): cts_error("Newly created Static MAC's body incorrect") self.set_status_failed() return False print "MESSAGE::Newly created Static MAC correct" self.set_status_passed() return True
def _test_case_get_created_vlan(self): print "TEST_CASE::Get created VLAN" link, status, status_code, response_body, _ = \ self.api_caller.get_resource(self.created_vlan_network_interface, self.discovery_container, api_endpoint_override=self.created_vlan_network_interface_netlock) if not status: cts_error("Wrong status code of GET response: {status_code}; response body: {response_body:response_body}", **locals()) self.set_status_failed() return False else: print "MESSAGE::Comparing newly created VLAN Network Interface to given payload" # PSME is unable to set "Name" and "VLANEnable" fields properly but only "VLANEnable" is mandatory if not JsonComparator.compare_json_to_golden(response_body, self.post_vlan_payload, ignore=["/VLANEnable"]): cts_error("Newly created VLAN Network Interface's body incorrect") self.set_status_failed() return False print "MESSAGE::Newly created VLAN Network Interface correct" self.set_status_passed() return True
def _test_case_get_created_vlan(self): print "TEST_CASE::Get created VLAN" status, status_code, response_body, _ = self.api_caller.get_resource( self.created_vlan_network_interface) if not status: print "ERROR::Wrong status code of GET response: %s" % status_code print "ERROR::Response body:\n%s" % response_body self.set_status_failed() return False else: print "MESSAGE::Comparing newly created vlan network interface to given payload" # PSME is unable to set "Name" and "VLANEnable" fields properly if not JsonComparator.compare_json_to_golden( response_body, self.post_vlan_payload, ignore=["/Name", "/VLANEnable"]): print "ERROR::newly created vlan network interface's body incorrect" self.set_status_failed() print "MESSAGE::proceeding with deleting the resource" return False print "MESSAGE::newly created vlan network interface correct" self.set_status_passed() return True
def _test_case_get_created_vlan(self): print "TEST_CASE::Get created VLAN" link, status, status_code, response_body, _ = \ self.api_caller.get_resource(self.created_vlan_network_interface, self.discovery_container, api_endpoint_override=self.created_vlan_network_interface_netlock) if not status: cts_error( "Wrong status code of GET response: {status_code}; response body: {response_body:response_body}", **locals()) self.set_status_failed() return False else: print "MESSAGE::Comparing newly created VLAN Network Interface to given payload" # PSME is unable to set "Name" and "VLANEnable" fields properly but only "VLANEnable" is mandatory if not JsonComparator.compare_json_to_golden( response_body, self.post_vlan_payload, ignore=["/VLANEnable"]): cts_error( "Newly created VLAN Network Interface's body incorrect") self.set_status_failed() return False print "MESSAGE::Newly created VLAN Network Interface correct" self.set_status_passed() return True
def verify(self, api_resource, expected_value): try: value_post = self.get_property_value() except KeyError: return ValidationStatus.FAILED if JsonComparator.compare_json_to_golden(value_post, expected_value): return ValidationStatus.PASSED else: return ValidationStatus.FAILED
def __verify_created_initiator_endpoint(self, resource_location, resource_payload): print "MESSAGE::Comparing newly created initiator endpoint to given payload" location_payload_to_compare = JsonComparator.odict_to_dict( self.discovery_container.get(resource_location).body) payload_to_compare = resource_payload verification_dict = {'ConnectedEntities': ['EntityRole'], 'Identifiers': ['DurableNameFormat', 'DurableName']} return self.__verify_body(location_payload_to_compare, payload_to_compare, verification_dict)
def _test_case_update_created_remote_target(self): print "TEST_CASE::Update the created remote target" patch_remote_target_payload = dict( Initiator=[dict( iSCSI=dict( InitiatorIQN="cts.initiator:patched_cts_initiator" ) )] ) status, status_code, response_body, _ = self.api_caller.patch_resource(self.created_remote_target, self.discovery_container, payload=patch_remote_target_payload) if not status: cts_error("Error while executing PATCH; status code: {status_code}; response body: " "{response_body:response_body}", **locals()) self.set_status_failed() return ValidationStatus.FAILED link, status, status_code, response_body, _ = \ self.api_caller.get_resource(self.created_remote_target, self.discovery_container, api_endpoint_override=self.created_remote_target_netloc) if not status: cts_error("Wrong status code of GET response: {status_code}; response body: {response_body:response_body}", **locals()) self.set_status_failed() return ValidationStatus.FAILED else: print "MESSAGE::Verifying whether the remote target's update has taken place" if not JsonComparator.compare_json_to_golden(response_body["Initiator"], patch_remote_target_payload["Initiator"]): cts_error("Newly created remote target could not be updated") self.set_status_failed() print "MESSAGE::Proceeding with deleting the resource" return ValidationStatus.FAILED print "MESSAGE::Newly created remote target updated correctly" self.set_status_passed() return ValidationStatus.PASSED
def _test_case_update_created_logical_drive(self): print "TEST_CASE::Update the created logical drive" patch_logical_drive_payload = dict( Bootable=not self.post_logical_drive_payload["Bootable"]) status, status_code, response_body, _ = self.api_caller.patch_resource( self.created_logical_drive, self.discovery_container, payload=patch_logical_drive_payload) if not status: cts_error( "Error while executing PATCH; status code: {status_code}; response body: " "{response_body:response_body}", **locals()) self.set_status_failed() return ValidationStatus.FAILED link, status, status_code, response_body, _ = \ self.api_caller.get_resource(self.created_logical_drive, self.discovery_container, api_endpoint_override=self.created_logical_drive_netloc) if not status: cts_error( "Wrong status code of GET response: {status_code}; response body: {response_body:response_body}", **locals()) self.set_status_failed() return ValidationStatus.FAILED else: print "MESSAGE::Verifying whether the logical drive's update has taken place" if not JsonComparator.compare_json_to_golden( response_body["Bootable"], patch_logical_drive_payload["Bootable"]): cts_error("Newly created logical drive could not be updated") self.set_status_failed() print "MESSAGE::Proceeding with deleting the resource" return ValidationStatus.FAILED print "MESSAGE::Newly created logical drive updated correctly" self.set_status_passed() return ValidationStatus.PASSED
def _test_case_get_created_remote_target(self): print "TEST_CASE::Get the created remote target" status, status_code, response_body, _ = self.api_caller.get_resource(self.created_remote_target) if not status: print "ERROR::Wrong status code of GET response: %s" % status_code print "ERROR::Response body:\n%s" % response_body self.set_status_failed() return ValidationStatus.FAILED else: print "MESSAGE::Comparing newly created remote target to given payload" if not JsonComparator.compare_json_to_golden(response_body, self.post_remote_target_payload): print "ERROR::Newly created remote target's body incorrect" self.set_status_failed() print "MESSAGE::Proceeding with deleting the resource" return ValidationStatus.FAILED print "MESSAGE::Newly created remote target's correct" self.set_status_passed() return ValidationStatus.PASSED
def _test_case_update_created_remote_target(self): print "TEST_CASE::Update the created remote target" patch_remote_target_payload = dict( Initiator=[dict( iSCSI=dict( InitiatorIQN="cts.initiator:patched_cts_initiator" ) )] ) status, status_code, response_body, _ = self.api_caller.patch_resource(self.created_remote_target, payload=patch_remote_target_payload) if not status: print "ERROR::Wrong status code of PATCH reponse %s" % status_code print "ERROR::Response body:\n%s" % response_body self.set_status_failed() return ValidationStatus.FAILED status, status_code, response_body, _ = self.api_caller.get_resource(self.created_remote_target) if not status: print "ERROR::Wrong status code of GET reponse %s" % status_code print "ERROR::Response body:\n%s" % response_body self.set_status_failed() return ValidationStatus.FAILED else: print "MESSAGE::Verifying whether the remote target's update has taken place" if not JsonComparator.compare_json_to_golden(response_body, patch_remote_target_payload): print "ERROR::Newly created remote target could not be updated" self.set_status_failed() print "MESSAGE::Proceeding with deleting the resource" return ValidationStatus.FAILED print "MESSAGE::Newly created remote target updated correctly" self.set_status_passed() return ValidationStatus.PASSED
def test_list_length_mismatch(self): self.assertFalse(JsonComparator.compare_jsons([1, 2, 3], [1, 2]))
def _test_case_add_rules_to_acl(self): print "TEST_CASE::Add rules to the ACL" self.created_rules = dict() optional_rules_triggered_an_error = False rule_collection = self.created_acl + "/Rules" for rule_type, rule_payload in self.rules_payloads.iteritems(): print "MESSAGE::Adding a {type} rule".format(type=rule_type) status, status_code, response_body, headers = \ self.api_caller.post_resource( rule_collection, self.discovery_container, payload=rule_payload, api_endpoint_override=self.created_acl_netloc) if not status: if rule_type not in self.mandatory_rules: optional_rules_triggered_an_error = True cts_warning( "Wrong status code of POST response: {status_code}; response body: {response_body:response_body}", **locals()) cts_warning( "Rule {rule_type} was defined as optional, continuing", **locals()) continue cts_error( "Wrong status code of POST response: {status_code}; response body: {response_body:response_body}", **locals()) self.set_status_failed() return False try: self.created_rules[rule_type] = headers["Location"] print "MESSAGE::Newly created ACL Rule URI: %s" % self.created_acl except KeyError: cts_error( "Incorrect response - header shall contain the location of the created resource" ) self.set_status_failed() return False print "MESSAGE::Getting the newly created ACL Rule URI: %s" % self.created_acl link, status, status_code, response_body, _ = \ self.api_caller.get_resource(self.created_rules[rule_type], self.discovery_container, api_endpoint_override=self.created_acl_netloc) if not status: cts_error( "Wrong status code of GET response: {status_code}; response body: {response_body:response_body}", **locals()) self.set_status_failed() return False else: print "MESSAGE::Comparing newly created ACL Rule to given payload" if not JsonComparator.compare_json_to_golden( response_body, rule_payload): cts_error("Newly created Rule's body incorrect") self.set_status_failed() return False else: print "MESSAGE::Newly created Rule's body correct" print "MESSAGE::Rule added" print "MESSAGE::ACL Rules created succesfully" if optional_rules_triggered_an_error: self.set_status_passed_with_warnings() else: self.set_status_passed() return True
def __verify_zone(self, resource_location, resource_payload): location_payload_to_compare = JsonComparator.odict_to_dict( self.discovery_container.get(resource_location).body["Links"]["Endpoints"]) payload_to_compare = resource_payload["Links"]["Endpoints"] return JsonComparator.compare_lists(location_payload_to_compare, payload_to_compare)
def test_empty_lists(self): self.assertTrue(JsonComparator.compare_jsons(list(), list()))
def test_none(self): self.assertTrue(JsonComparator.compare_jsons(None, None))
def test_empty_dicts(self): self.assertTrue(JsonComparator.compare_jsons(dict(), dict()))
def test_type_mismatch(self): self.assertFalse(JsonComparator.compare_jsons(dict(), list()))
def test_key_mismatch(self): self.assertFalse(JsonComparator.compare_jsons(dict(a=1), dict(b=1)))
def test_values_mismatch(self): self.assertFalse(JsonComparator.compare_jsons(dict(a=1), dict(a=2)))
def test_elements_in_list_mismatch(self): self.assertFalse(JsonComparator.compare_jsons([1, 2, 3], [1, 2, 4]))