def test_empty(self): valuemap = [] values = [] self.setup_for_property(valuemap, values) vm = ValueMapping.for_property(self.server, NAMESPACE, CLASSNAME, PROPNAME) self.assertOutsideValueMap(vm, 0)
def test_one_cimtype(self): valuemap = ['1'] values = ['one'] self.setup_for_property(valuemap, values) vm = ValueMapping.for_property(self.server, NAMESPACE, CLASSNAME, PROPNAME) self.assertEqual(vm.tovalues(Uint8(1)), 'one')
def test_zero(self): valuemap = ['0'] values = ['zero'] self.setup_for_property(valuemap, values) vm = ValueMapping.for_property(self.server, NAMESPACE, CLASSNAME, PROPNAME) self.assertEqual(vm.tovalues(0), 'zero') self.assertOutsideValueMap(vm, 1)
def test_one(self): valuemap = ['1'] values = ['one'] self.setup_for_property(valuemap, values) vm = ValueMapping.for_property(self.server, NAMESPACE, CLASSNAME, PROPNAME) self.assertOutsideValueMap(vm, 0) self.assertEqual(vm.tovalues(1), 'one') self.assertOutsideValueMap(vm, 2)
def test_unclaimed(self): valuemap = ['..'] values = ['unclaimed'] self.setup_for_property(valuemap, values) vm = ValueMapping.for_property(self.server, NAMESPACE, CLASSNAME, PROPNAME) self.assertEqual(vm.tovalues(0), 'unclaimed') self.assertEqual(vm.tovalues(1), 'unclaimed') self.assertEqual(vm.tovalues(2), 'unclaimed')
def explore_server(server_url, username, password): print("WBEM server URL:\n %s" % server_url) conn = WBEMConnection(server_url, (username, password)) server = WBEMServer(conn) print("Brand:\n %s" % server.brand) print("Version:\n %s" % server.version) print("Interop namespace:\n %s" % server.interop_ns) print("All namespaces:") for ns in server.namespaces: print(" %s" % ns) print("Advertised management profiles:") org_vm = ValueMapping.for_property(server, server.interop_ns, 'CIM_RegisteredProfile', 'RegisteredOrganization') indications_profile = None server_profile = None for inst in server.profiles: org = org_vm.tovalues(inst['RegisteredOrganization']) name = inst['RegisteredName'] vers = inst['RegisteredVersion'] print(" %s %s Profile %s" % (org, name, vers)) if org == "DMTF" and name == "Indications": indications_profile = inst if org == "SNIA" and name == "Server": server_profile = inst if indications_profile is not None: print("Central instances for DMTF Indications profile (component):") try: ci_paths = server.get_central_instances( indications_profile.path, "CIM_IndicationService", "CIM_System", ["CIM_HostedService"]) except Exception as exc: print("Error: %s" % str(exc)) ci_paths = [] for ip in ci_paths: print(" %s" % str(ip)) if server_profile is not None: print("Central instances for SNIA Server profile (autonomous):") try: ci_paths = server.get_central_instances(server_profile.path) except Exception as exc: print("Error: %s" % str(exc)) ci_paths = [] for ip in ci_paths: print(" %s" % str(ip))
def test_singles(self): valuemap = ['0', '1', '9'] values = ['zero', 'one', 'nine'] self.setup_for_property(valuemap, values) vm = ValueMapping.for_property(self.server, NAMESPACE, CLASSNAME, PROPNAME) self.assertEqual(vm.tovalues(0), 'zero') self.assertEqual(vm.tovalues(1), 'one') self.assertOutsideValueMap(vm, 2) self.assertOutsideValueMap(vm, 8) self.assertEqual(vm.tovalues(9), 'nine') self.assertOutsideValueMap(vm, 10)
def setup_for_property(self, server, type_, valuemap, values): """ Return a new ValueMapping object that is set up for a CIM property with the specified data type and valuemap and values qualifiers. """ test_prop = CIMProperty(PROPNAME, value=None, type=type_) if valuemap is not None: test_prop.qualifiers['ValueMap'] = \ CIMQualifier('ValueMap', valuemap, 'string') if values is not None: test_prop.qualifiers['Values'] = \ CIMQualifier('Values', values, 'string') test_class = CIMClass(CLASSNAME) test_class.properties[PROPNAME] = test_prop self.conn.GetClass = Mock(return_value=test_class) vm = ValueMapping.for_property(server, NAMESPACE, CLASSNAME, PROPNAME) return vm
def build_reg_profile_insts(self, conn, profiles): """ Build and install in repository the registered profiles define by the profiles parameter. A dictionary of tuples where each tuple contains RegisteredOrganization, RegisteredName, RegisteredVersion Parameters: conn: profiles (dict of lists where each list contains org, name, version for a profiles) """ # Map ValueMap to Value org_vm = ValueMapping.for_property(conn, self.interop_ns, 'CIM_RegisteredProfile', 'RegisteredOrganization') # This is a workaround hack to get ValueMap from Value org_vm_dict = {} # reverse mapping dictionary (valueMap from Value) for value in range(0, 22): org_vm_dict[org_vm.tovalues(value)] = value for p in profiles: instance_id = '%s+%s+%s' % (p[0], p[1], p[2]) reg_prof_dict = { 'RegisteredOrganization': org_vm_dict[p[0]], 'RegisteredName': p[1], 'RegisteredVersion': p[2], 'InstanceID': instance_id } rpinst = self.inst_from_classname(conn, "CIM_RegisteredProfile", namespace=self.interop_ns, property_values=reg_prof_dict, include_missing_properties=False, include_path=True) conn.add_cimobjects(rpinst, namespace=self.interop_ns) rtn_rpinsts = conn.EnumerateInstances("CIM_RegisteredProfile", namespace=self.interop_ns) assert len(rtn_rpinsts) == len(profiles), \ "Expected registered profiles: %r, got %s" % (len(profiles), len(rtn_rpinsts))
def cmd_server_centralinsts(context, options): """ Display general information on the central instances of one or more profiles. """ server = context.wbem_server try: found_server_profiles = server.get_selected_profiles( registered_org=options['organization'], registered_name=options['profile']) org_vm = ValueMapping.for_property(server, server.interop_ns, 'CIM_RegisteredProfile', 'RegisteredOrganization') rows = [] for inst in found_server_profiles: pi = get_profile_info(org_vm, inst) row = [":".join(pi)] try: ci = server.get_central_instances( inst.path, central_class=options['central_class'], scoping_class=options['scoping_class'], scoping_path=options['scoping_path'], reference_direction=options['reference_direction']) row.append("\n".join([str(p) for p in ci])) # mark current inst as failed and continue except Exception as ex: # pylint: disable=broad-except click.echo('Exception: {} {}'.format(row, ex)) row.append("Failed") rows.append(row) # sort by org rows.sort(key=lambda x: (x[0])) headers = ['Profile', 'Central Instance paths'] click.echo( format_table(rows, headers, title='Advertised Central Instances:', table_format=context.output_format)) except Error as er: raise_pywbem_error_exception(er)
def test_singles_ranges(self): valuemap = ['0', '1', '2..4', '..6', '7..', '9'] values = ['zero', 'one', 'two-four', 'five-six', 'seven-eight', 'nine'] self.setup_for_property(valuemap, values) vm = ValueMapping.for_property(self.server, NAMESPACE, CLASSNAME, PROPNAME) self.assertEqual(vm.tovalues(0), 'zero') self.assertEqual(vm.tovalues(1), 'one') self.assertEqual(vm.tovalues(2), 'two-four') self.assertEqual(vm.tovalues(3), 'two-four') self.assertEqual(vm.tovalues(4), 'two-four') self.assertEqual(vm.tovalues(5), 'five-six') self.assertEqual(vm.tovalues(6), 'five-six') self.assertEqual(vm.tovalues(7), 'seven-eight') self.assertEqual(vm.tovalues(8), 'seven-eight') self.assertEqual(vm.tovalues(9), 'nine') self.assertOutsideValueMap(vm, 10)
def test_singles_ranges(self): valuemap = ['0', '1', '2..4', '..6', '7..', '9'] values = ['zero', 'one', 'two-four', 'five-six', 'seven-eight', 'nine'] self.setup_for_property(valuemap, values) vm = ValueMapping.for_property(self.server, NAMESPACE, CLASSNAME, PROPNAME) self.assertEqual(vm.tovalues(0), 'zero') self.assertEqual(vm.tovalues(1), 'one') self.assertEqual(vm.tovalues(2), 'two-four') self.assertEqual(vm.tovalues(3), 'two-four') self.assertEqual(vm.tovalues(4), 'two-four') self.assertEqual(vm.tovalues(5), 'five-six') self.assertEqual(vm.tovalues(6), 'five-six') self.assertEqual(vm.tovalues(7), 'seven-eight') self.assertEqual(vm.tovalues(8), 'seven-eight') self.assertEqual(vm.tovalues(9), 'nine') self.assertOutsideValueMap(vm, 10)
def test_singles_ranges_unclaimed2(self): valuemap = ['0', '2..4', '..6', '7..', '9', '..'] values = ['zero', 'two-four', 'five-six', 'seven-eight', 'nine', 'unclaimed'] self.setup_for_property(valuemap, values) vm = ValueMapping.for_property(self.server, NAMESPACE, CLASSNAME, PROPNAME) self.assertEqual(vm.tovalues(0), 'zero') self.assertEqual(vm.tovalues(1), 'unclaimed') # '..' fills this gap self.assertEqual(vm.tovalues(2), 'two-four') self.assertEqual(vm.tovalues(3), 'two-four') self.assertEqual(vm.tovalues(4), 'two-four') self.assertEqual(vm.tovalues(5), 'five-six') self.assertEqual(vm.tovalues(6), 'five-six') self.assertEqual(vm.tovalues(7), 'seven-eight') self.assertEqual(vm.tovalues(8), 'seven-eight') self.assertEqual(vm.tovalues(9), 'nine') self.assertEqual(vm.tovalues(10), 'unclaimed') self.assertEqual(vm.tovalues(11), 'unclaimed')
def test_singles_ranges_unclaimed2(self): valuemap = ['0', '2..4', '..6', '7..', '9', '..'] values = [ 'zero', 'two-four', 'five-six', 'seven-eight', 'nine', 'unclaimed' ] self.setup_for_property(valuemap, values) vm = ValueMapping.for_property(self.server, NAMESPACE, CLASSNAME, PROPNAME) self.assertEqual(vm.tovalues(0), 'zero') self.assertEqual(vm.tovalues(1), 'unclaimed') # '..' fills this gap self.assertEqual(vm.tovalues(2), 'two-four') self.assertEqual(vm.tovalues(3), 'two-four') self.assertEqual(vm.tovalues(4), 'two-four') self.assertEqual(vm.tovalues(5), 'five-six') self.assertEqual(vm.tovalues(6), 'five-six') self.assertEqual(vm.tovalues(7), 'seven-eight') self.assertEqual(vm.tovalues(8), 'seven-eight') self.assertEqual(vm.tovalues(9), 'nine') self.assertEqual(vm.tovalues(10), 'unclaimed') self.assertEqual(vm.tovalues(11), 'unclaimed')
def build_reg_profile_insts(self, conn, namespace, profiles): """ Build and install in repository the registered profiles define by the profiles dictionary. The profiles to be build are defined by the profiles parameter, A dictionary of tuples where each tuple contains RegisteredOrganization, RegisteredName, RegisteredVersion """ # Map ValueMap to Value org_vm = ValueMapping.for_property(conn, namespace, 'CIM_RegisteredProfile', 'RegisteredOrganization') # This is a workaround hack to get ValueMap from Value org_vm_dict = {} # reverse mapping dictionary (valueMap from Value) for value in range(0, 22): org_vm_dict[org_vm.tovalues(value)] = value for p in profiles: instance_id = '%s+%s+%s' % (p[0], p[1], p[2]) reg_prof_dict = { 'RegisteredOrganization': org_vm_dict[p[0]], 'RegisteredName': p[1], 'RegisteredVersion': p[2], 'InstanceID': instance_id } rpinst = self.inst_from_classname(conn, "CIM_RegisteredProfile", namespace=namespace, property_values=reg_prof_dict, strict=True, include_null_properties=False, include_path=True) conn.add_cimobjects(rpinst, namespace=namespace) assert (conn.EnumerateInstances("CIM_RegisteredProfile", namespace=namespace))
def explore_server(server_url, username, password): """ Demo of exploring a cim server for characteristics defined by the server class """ print("WBEM server URL:\n %s" % server_url) conn = WBEMConnection(server_url, (username, password), no_verification=True) server = WBEMServer(conn) print("Brand:\n %s" % server.brand) print("Version:\n %s" % server.version) print("Interop namespace:\n %s" % server.interop_ns) print("All namespaces:") for ns in server.namespaces: print(" %s" % ns) print("Advertised management profiles:") org_vm = ValueMapping.for_property(server, server.interop_ns, 'CIM_RegisteredProfile', 'RegisteredOrganization') for inst in server.profiles: print_profile_info(org_vm, inst) indication_profiles = server.get_selected_profiles('DMTF', 'Indications') print('Profiles for DMTF:Indications') for inst in indication_profiles: print_profile_info(org_vm, inst) server_profiles = server.get_selected_profiles('SNIA', 'Server') print('Profiles for SNIA:Server') for inst in server_profiles: print_profile_info(org_vm, inst) # get Central Instances for inst in indication_profiles: org = org_vm.tovalues(inst['RegisteredOrganization']) name = inst['RegisteredName'] vers = inst['RegisteredVersion'] print("Central instances for profile %s:%s:%s (component):" % \ (org, name, vers)) try: ci_paths = server.get_central_instances(inst.path, "CIM_IndicationService", "CIM_System", ["CIM_HostedService"]) except Exception as exc: print("Error: %s" % str(exc)) ci_paths = [] for ip in ci_paths: print(" %s" % str(ip)) for inst in server_profiles: org = org_vm.tovalues(inst['RegisteredOrganization']) name = inst['RegisteredName'] vers = inst['RegisteredVersion'] print("Central instances for profile %s:%s:%s(autonomous):" % (org, name, vers)) try: ci_paths = server.get_central_instances(inst.path) except Exception as exc: print("Error: %s" % str(exc)) ci_paths = [] for ip in ci_paths: print(" %s" % str(ip))
def test_wbemserver_basic(self, tst_namespace): """ Test the basic functions that access server information. This test creates the mock repository and adds classes and instances for the WBEMServer tests that involve namespaces, brand, profiles and a subset of the central_instance tests. It includes no tests for errors. The primary goal of this test was to develop the mechanisms for easily getting classes and instances into the repo and to provide a basic test of functionality. """ system_name = 'Mock_Test_subscription_mgr' object_manager_name = 'MyFakeObjectManager' conn = self.build_class_repo(tst_namespace) server = WBEMServer(conn) # Build CIM_ObjectManager instance om_inst = self.build_obj_mgr_inst(conn, tst_namespace, system_name, object_manager_name) # build CIM_Namespace instances test_namespaces = [tst_namespace, 'root/cimv2'] self.build_cimnamespace_insts(conn, tst_namespace, system_name, object_manager_name, test_namespaces) # Build RegisteredProfile instances profiles = [('DMTF', 'Indications', '1.1.0'), ('DMTF', 'Profile Registration', '1.0.0'), ('SNIA', 'Server', '1.2.0'), ('SNIA', 'Server', '1.1.0'), ('SNIA', 'SMI-S', '1.2.0')] self.build_reg_profile_insts(conn, tst_namespace, profiles) # Build instances for get_central instance # Using central methodology, i.e. ElementConformsToProfile # Element conforms for SNIA server to object manager prof_inst = server.get_selected_profiles(registered_org='SNIA', registered_name='Server', registered_version='1.1.0') self.build_elementconformstoprofile_inst(conn, tst_namespace, prof_inst[0].path, om_inst.path) # Test basic brand, version, namespace methods assert server.namespace_classname == 'CIM_Namespace' assert server.url == 'http://FakedUrl' assert server.brand == "OpenPegasus" assert server.version == "2.15.0" assert server.interop_ns == tst_namespace assert set(server.namespaces) == set([tst_namespace, 'root/cimv2']) # Test basic profiles methods org_vm = ValueMapping.for_property(server, server.interop_ns, 'CIM_RegisteredProfile', 'RegisteredOrganization') for inst in server.profiles: org = org_vm.tovalues(inst['RegisteredOrganization']) name = inst['RegisteredName'] vers = inst['RegisteredVersion'] tst_tup = (org, name, vers) pass_tst = False for tup in profiles: if tst_tup == tup: pass_tst = True break assert pass_tst sel_prof = server.get_selected_profiles(registered_org='DMTF', registered_name='Indications') assert len(sel_prof) == 1 for inst in sel_prof: assert org_vm.tovalues(inst['RegisteredOrganization']) == 'DMTF' assert inst['RegisteredName'] == 'Indications' sel_prof = server.get_selected_profiles(registered_org='DMTF') assert len(sel_prof) == 2 for inst in sel_prof: assert org_vm.tovalues(inst['RegisteredOrganization']) == 'DMTF' # Simple get_cental_instance. # profile_path, central_class=None, # scoping_class=None, scoping_path=None profile_insts = server.get_selected_profiles( registered_org='SNIA', registered_name='Server', registered_version='1.1.0') profile_path = profile_insts[0].path insts = server.get_central_instances(profile_path, 'CIM_ObjectManager') print('central inst %s' % insts[0]) assert len(insts) == 1 kb = NocaseDict([('SystemCreationClassName', 'CIM_ComputerSystem'), ('SystemName', system_name), ('CreationClassName', 'CIM_ObjectManager')]) assert insts[0] == CIMInstanceName('CIM_ObjectManager', keybindings=kb, namespace=tst_namespace, host=conn.host)
def test_wbemserver_basic(self, tst_namespace): # pylint: disable=no-self-use """ Test the basic functions that access server information. This test creates the CIM repository and adds classes and instances for the WBEMServer tests that involve namespaces, brand, profiles and a subset of the central_instance tests. It includes no tests for errors. The primary goal of this test was to develop the mechanisms for easily getting classes and instances into the repo and to provide a basic test of functionality. """ # Build the wbem server mock using the WbemServerMock default test # data except that we define the interop namespace mock_wbemserver = WbemServerMock(interop_ns=tst_namespace) server = mock_wbemserver.wbem_server # Build instances for get_central instance # using central methodology, i.e. ElementConformsToProfile # Test basic brand, version, namespace methods assert server.namespace_classname == 'CIM_Namespace' assert server.url == 'http://FakedUrl:5988' assert server.brand == "Mock_Test" # assert server.version == "2.15.0" assert server.interop_ns == tst_namespace assert set(server.namespaces) == set([tst_namespace]) # Test basic profiles methods org_vm = ValueMapping.for_property(server, server.interop_ns, 'CIM_RegisteredProfile', 'RegisteredOrganization') for inst in server.profiles: org = org_vm.tovalues(inst['RegisteredOrganization']) name = inst['RegisteredName'] vers = inst['RegisteredVersion'] tst_tup = (org, name, vers) pass_tst = False for tup in mock_wbemserver.registered_profiles: if tst_tup == tup: pass_tst = True break assert pass_tst sel_prof = server.get_selected_profiles(registered_org='DMTF', registered_name='Indications') assert len(sel_prof) == 1 for inst in sel_prof: assert org_vm.tovalues(inst['RegisteredOrganization']) == 'DMTF' assert inst['RegisteredName'] == 'Indications' # Test case insensitive matching sel_prof = server.get_selected_profiles(registered_org='DmtF', registered_name='inDiCations') assert len(sel_prof) == 1 for inst in sel_prof: assert org_vm.tovalues(inst['RegisteredOrganization']) == 'DMTF' assert inst['RegisteredName'] == 'Indications' sel_prof = server.get_selected_profiles(registered_org='DMTF') assert len(sel_prof) == 3 for inst in sel_prof: assert org_vm.tovalues(inst['RegisteredOrganization']) == 'DMTF' # Simple get_cental_instance. # profile_path, central_class=None, # scoping_class=None, scoping_path=None profile_insts = server.get_selected_profiles( registered_org='SNIA', registered_name='Server', registered_version='1.1.0') profile_path = profile_insts[0].path insts = server.get_central_instances(profile_path, 'CIM_ObjectManager') assert len(insts) == 1 kb = NocaseDict([ ('SystemCreationClassName', 'CIM_ComputerSystem'), ('SystemName', mock_wbemserver.system_name), ('CreationClassName', 'CIM_ObjectManager'), ('Name', 'FakeObjectManager'), ]) assert insts[0] == CIMInstanceName('CIM_ObjectManager', keybindings=kb, namespace=tst_namespace, host=server.conn.host)
Parameters: org_vm: The value mapping for CIMRegisterdProfile and RegisteredOrganization so that the value and not value mapping is displayed. profile_instance: instance of a profile to be printed """ org = org_vm.tovalues(profile_instance['RegisteredOrganization']) name = profile_instance['RegisteredName'] vers = profile_instance['RegisteredVersion'] print(" %s %s Profile %s" % (org, name, vers)) # Create the server and save in global SERVER SERVER = WBEMServer(CONN) # create the CIMRegisterd Profile ValueMapping for the # defined server. This can be used to org_vm = ValueMapping.for_property(SERVER, SERVER.interop_ns, 'CIM_RegisteredProfile', 'RegisteredOrganization') print("Brand:\n %s" % SERVER.brand) print("Version:\n %s" % SERVER.version) print("Interop namespace:\n %s" % SERVER.interop_ns) for inst in SERVER.profiles: print_profile_info(org_vm, inst)
def explore_server(server_url, username, password): """ Demo of exploring a cim server for characteristics defined by the server class """ print("WBEM server URL:\n %s" % server_url) conn = WBEMConnection(server_url, (username, password), no_verification=True) server = WBEMServer(conn) print("Brand:\n %s" % server.brand) print("Version:\n %s" % server.version) print("Interop namespace:\n %s" % server.interop_ns) print("All namespaces:") for ns in server.namespaces: print(" %s" % ns) print("Advertised management profiles:") org_vm = ValueMapping.for_property(server, server.interop_ns, 'CIM_RegisteredProfile', 'RegisteredOrganization') for inst in server.profiles: print_profile_info(org_vm, inst) indication_profiles = server.get_selected_profiles('DMTF', 'Indications') print('Profiles for DMTF:Indications') for inst in indication_profiles: print_profile_info(org_vm, inst) server_profiles = server.get_selected_profiles('SNIA', 'Server') print('Profiles for SNIA:Server') for inst in server_profiles: print_profile_info(org_vm, inst) # get Central Instances for inst in indication_profiles: org = org_vm.tovalues(inst['RegisteredOrganization']) name = inst['RegisteredName'] vers = inst['RegisteredVersion'] print("Central instances for profile %s:%s:%s (component):" % \ (org, name, vers)) try: ci_paths = server.get_central_instances( inst.path, "CIM_IndicationService", "CIM_System", ["CIM_HostedService"]) except Exception as exc: print("Error: %s" % str(exc)) ci_paths = [] for ip in ci_paths: print(" %s" % str(ip)) for inst in server_profiles: org = org_vm.tovalues(inst['RegisteredOrganization']) name = inst['RegisteredName'] vers = inst['RegisteredVersion'] print("Central instances for profile %s:%s:%s(autonomous):" % (org, name, vers)) try: ci_paths = server.get_central_instances(inst.path) except Exception as exc: print("Error: %s" % str(exc)) ci_paths = [] for ip in ci_paths: print(" %s" % str(ip))
""" Print information on a profile defined by profile_instance. Parameters: org_vm: The value mapping for CIMRegisterdProfile and RegisteredOrganization so that the value and not value mapping is displayed. profile_instance: instance of a profile to be printed """ org = org_vm.tovalues(profile_instance["RegisteredOrganization"]) name = profile_instance["RegisteredName"] vers = profile_instance["RegisteredVersion"] print(" %s %s Profile %s" % (org, name, vers)) # Create the server and save in global SERVER SERVER = WBEMServer(CONN) # create the CIMRegisterd Profile ValueMapping for the # defined server. This can be used to org_vm = ValueMapping.for_property(SERVER, SERVER.interop_ns, "CIM_RegisteredProfile", "RegisteredOrganization") print("Brand:\n %s" % SERVER.brand) print("Version:\n %s" % SERVER.version) print("Interop namespace:\n %s" % SERVER.interop_ns) for inst in SERVER.profiles: print_profile_info(org_vm, inst)