def test_add_namespace(default_ns, additional_ns, test_ns, exp_ns, exp_exc): # pylint: disable=no-self-use """ Test add_namespace() and the namespaces property """ # setup the inmemory repository repo = InMemoryRepository(default_ns) for ns in additional_ns: repo.add_namespace(ns) if not exp_exc: # The code to be tested repo.add_namespace(test_ns) assert isinstance(repo.namespaces, list) assert exp_ns in repo.namespaces for ns in additional_ns: assert ns in repo.namespaces if default_ns: assert default_ns in repo.namespaces else: with pytest.raises(exp_exc.__class__) as exec_info: # The code to be tested repo.add_namespace(test_ns) print(exec_info)
def create_repo(init_namespace, init_objects): """ Create a InMemoryRepository object from the init parameters. """ repo = InMemoryRepository(initial_namespace=init_namespace) if init_objects: class_store = repo.get_class_store(init_namespace) inst_store = repo.get_instance_store(init_namespace) qual_store = repo.get_qualifier_store(init_namespace) for item in init_objects: if isinstance(item, CIMClass): class_store.create(item.classname, item) elif isinstance(item, CIMInstance): inst_store.create(item.path, item) else: assert isinstance(item, CIMQualifierDeclaration) qual_store.create(item.name, item) return repo
def test_remove_namespace(default_ns, additional_ns, test_ns, exp_ns, exp_exc): # pylint: disable=no-self-use """ Test _remove_namespace() """ repo = InMemoryRepository(default_ns) for ns in additional_ns: repo.add_namespace(ns) if not exp_exc: # The code to be tested repo.remove_namespace(test_ns) assert exp_ns not in repo.namespaces else: with pytest.raises(exp_exc.__class__) as exec_info: # The code to be tested repo.remove_namespace(test_ns) print(exec_info)
def test_repository_method_errs(desc, args, condition, capsys): # pylint: disable=unused-argument """ This is a test of the various methods of the repository that return valid responses for all of the types. This test create, update, get, delete and inter* methods of the object store as accessed through the InMemoryRepository for good responses. It does not test exception responses. """ if not condition: pytest.skip("Condition for test case not met") init_args = args['init_args'] init_objs = args['init_objs'] err_names = args['err_names'] valid_names = args['valid_names'] namespace = init_args # setup the inmemory repository and the methods that provide access to # the repository for each object type repo = InMemoryRepository(namespace) # Relate repository names to corresponsing get_xxx_repo function repos = {'classes': repo.get_class_store(namespace), 'instances': repo.get_instance_store(namespace), 'qualifiers': repo.get_qualifier_store(namespace)} for item in init_objs: if isinstance(item, CIMClass): class_store = repos['classes'] class_store.create(item.classname, item) elif isinstance(item, CIMInstance): inst_repo = repos['instances'] inst_repo.create(item.path, item) elif isinstance(item, CIMQualifierDeclaration): qual_repo = repos['qualifiers'] qual_repo.create(item.name, item) else: assert False for repo_name, xxx_repo in repos.items(): err_name = err_names[repo_name] valid_name = valid_names[repo_name] try: xxx_repo.get(err_name) assert False, "Get should return exception" except KeyError: pass try: xxx_repo.delete(err_name) assert False, "Delete Exception failed {} {}" except KeyError: pass # Try to get object from repo and create it. try: obj = xxx_repo.get(valid_name) obj = xxx_repo.create(valid_name, obj) except ValueError: pass # Try to get object from repo and update it with err_name. # NOTE: this works because repo does not validate names against # objects on create or update. try: obj = xxx_repo.get(valid_name) obj = xxx_repo.update(err_name, obj) except KeyError: pass
def test_repository_valid_methods(desc, args, condition, capsys): """ This is a test of the various methods of the repository that return valid responses for all of the types. This test create, update, get, delete and inter* methods of the object store as accessed through the InMemoryRepository for good responses. It does not test exception responses. """ if not condition: pytest.skip("Condition for test case not met") init_args = args['init_args'] init_objs = args['init_objs'] namespace = init_args # setup the inmemory repository and the methods that provide access to # the repository for each object type repo = InMemoryRepository(initial_namespace=namespace) class_store = repo.get_class_store(namespace) inst_repo = repo.get_instance_store(namespace) qual_repo = repo.get_qualifier_store(namespace) # Holding dict for items created in repository by repository name input_items = {'classes': {}, 'instances': {}, 'qualifiers': {}} # Relate repository names to corresponsing get_xxx_repo function repos = {'classes': repo.get_class_store(namespace), 'instances': repo.get_instance_store(namespace), 'qualifiers': repo.get_qualifier_store(namespace)} # Insert the items in init_objs into the repository # Counter of number of objects by type created. inst_count = 0 class_count = 0 qual_count = 0 for item in init_objs: if isinstance(item, CIMClass): class_store = repos['classes'] class_store.create(item.classname, item) class_count += 1 input_items['classes'][item.classname] = item elif isinstance(item, CIMInstance): inst_repo = repos['instances'] inst_repo.create(item.path, item) inst_count += 1 input_items['instances'][item.path] = item elif isinstance(item, CIMQualifierDeclaration): qual_repo = repos['qualifiers'] qual_repo.create(item.name, item) qual_count += 1 input_items['qualifiers'][item.name] = item else: assert False # # Execute repository object access methods and verifyresults # # Test counts of objects in the repository assert class_count == len(input_items['classes']) assert inst_count == len(input_items['instances']) assert qual_count == len(input_items['qualifiers']) assert class_store.len() == class_count assert inst_repo.len() == inst_count assert qual_repo.len() == qual_count # verify the print_repository returns correct data print(repo.print_repository()) captured = capsys.readouterr() result = captured.out assert "QUALIFIERS: Namespace: root/blah Repo: qualifier len:2" in result assert "CLASSES: Namespace: root/blah Repo: class len:2" in result assert "INSTANCES: Namespace: root/blah Repo: instance len:2" in result # Test exists(), get(), iter_names, iter_values, and delete with with # valid names # Verify the correct response for each objstore for type_dict, obj_dict in input_items.items(): xxx_repo = repos[type_dict] for name, obj in obj_dict.items(): assert xxx_repo.object_exists(name), '{} exists() fail {}' \ .format(desc, name) rtnd_obj = xxx_repo.get(name) assert rtnd_obj == input_items[type_dict][name] # update each object with the same object as originally installed. xxx_repo.update(name, obj) # test obj store repr print(repr(xxx_repo)) captured = capsys.readouterr() result = captured.out assert "InMemoryObjectStore(" in result assert "size=2" in result # Test iter_names and iter_values names = list(xxx_repo.iter_names()) assert set(names) == set(input_items[type_dict].keys()) objs = list(xxx_repo.iter_values()) assert set(objs) == set(input_items[type_dict].values()) for name, obj in obj_dict.items(): xxx_repo.delete(name) for type_dict, obj_dict in input_items.items(): xxx_repo = repos[type_dict] assert xxx_repo.len() == 0