def default_collectors(): collector_config = yaml_read(os.environ['COLLECTOR_CONFIG']).get('default_collectors') return [collector for collector in collector_config] if collector_config else None
def test_collector_metrics(self): metrics_yaml = yaml_read('tests/metrics.yaml') # every collector got to be tested in here random_prometheus_port = random.randrange(9000, 9700, 1) print("chosen testport: " + str(random_prometheus_port)) Resources.get_token = MagicMock( return_value="2ed214d523-235f-h283-4566-6sf356124fd62::f234234-234" ) Resources.get_adapter = MagicMock( return_value=[{ 'name': "vcenter1", 'uuid': '5628-9ba1-55e84701' }]) # test tool get_resources to create resource objects Resources.get_datacenter = MagicMock( return_value=[{ 'name': 'datacenter1', 'uuid': '5628-9ba1-55e847050814' }, { 'name': 'datacenter2', 'uuid': '5628-9ba1-55e847050815' }, { 'name': 'datacenter3', 'uuid': '7422-91h7-52s842060815' }]) Resources.get_cluster = MagicMock( return_value=[{ 'name': 'cluster1', 'uuid': '3628-93a1-56e84634050814' }, { 'name': 'cluster2', 'uuid': '5628-9ba1-55e847050815' }, { 'name': 'cluster3', 'uuid': '7422-91h7-52s842060815' }]) Resources.get_hosts = MagicMock( return_value=[{ 'name': 'hostsystem1', 'uuid': '3628-93a1-56e84634050814' }, { 'name': 'hostsystem2', 'uuid': '5628-9ba1-55e847050815' }, { 'name': 'hostsystem3', 'uuid': '7422-91h7-52s842060815' }]) Resources.get_vmfolders = MagicMock( return_value=[{ 'name': 'vmfolder1', 'uuid': '3628-93a1-56e84634050814' }, { 'name': 'vmfolder2', 'uuid': '5628-9ba1-55e847050815' }, { 'name': 'vmfolder3', 'uuid': '7422-91h7-52s842060815' }]) Resources.get_virtualmachines = MagicMock( return_value=[{ 'name': 'vm1', 'uuid': '3628-93a1-56e84634050814' }, { 'name': 'vm2', 'uuid': '5628-9ba1-55e847050815' }, { 'name': 'vm3', 'uuid': '7422-91h7-52s842060815' }]) Resources.get_datastores = MagicMock( return_value=[{ 'name': 'datastore1', 'uuid': '3628-93a1-56e84634050814' }, { 'name': 'datastore2', 'uuid': '5628-9ba1-55e847050815' }, { 'name': 'datastore3', 'uuid': '7422-91h7-52s842060815' }]) Resources.get_resources = MagicMock( return_value=[{ 'name': 'resource1', 'uuid': '5628-9ba1-55e847050814' }, { 'name': 'resource2', 'uuid': '5628-9ba1-55e847050815' }]) Resources.get_latest_stat = MagicMock(return_value=1) Resources.get_property = MagicMock(return_value="test_property") Resources.get_project_ids = MagicMock( return_value=[{ "3628-93a1-56e84634050814": "0815" }, { "7422-91h7-52s842060815": "0815" }, { "5628-9ba1-55e847050815": "internal" }]) thread = Thread(target=InventoryBuilder, args=('./tests/test.json', 8000, 180)) thread.daemon = True thread.start() for collector in metrics_yaml.keys(): print("\nTesting " + collector) if 'Stats' in collector: # mocking all values from yaml statkey_yaml = yaml_read(os.environ['CONFIG'])['statkeys'] multiple_metrics_generated = list() for statkey_pair in statkey_yaml[collector]: multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "stat-list": { "stat": [{ "timestamps": [1582797716394], "statKey": { "key": statkey_pair['statkey'] }, "data": [88.0] }] } }) multiple_metrics_generated.append({ "resourceId": "5628-9ba1-55e847050815", "stat-list": { "stat": [{ "timestamps": [1582797716394], "statKey": { "key": statkey_pair['statkey'] }, "data": [44.0] }] } }) multiple_metrics_generated.append({ "resourceId": "7422-91h7-52s842060815", "stat-list": { "stat": [{ "timestamps": [1582797716394], "statKey": { "key": statkey_pair['statkey'] }, "data": [55.0] }] } }) Resources.get_latest_stat_multiple = MagicMock( return_value=multiple_metrics_generated) if "Properties" in collector: propkey_yaml = yaml_read(os.environ['CONFIG'])['properties'] multiple_enum_properties_generated = list() if 'enum_metrics' in propkey_yaml[collector]: for propkey_pair in propkey_yaml[collector][ 'enum_metrics']: multiple_enum_properties_generated.append({ 'resourceId': '3628-93a1-56e84634050814', 'propkey': propkey_pair['property'], 'value': "test_enum_property" }) multiple_enum_properties_generated.append({ 'resourceId': "5628-9ba1-55e847050815", 'propkey': propkey_pair['property'], 'value': "test_enum_property" }) multiple_enum_properties_generated.append({ 'resourceId': "7422-91h7-52s842060815", 'propkey': propkey_pair['property'], 'value': "test_enum_property" }) Resources.get_latest_enum_properties_multiple = MagicMock( return_value=multiple_enum_properties_generated) multiple_number_properties_generated = list() if 'number_metrics' in propkey_yaml[collector]: for propkey_pair in propkey_yaml[collector][ 'number_metrics']: multiple_number_properties_generated.append({ 'resourceId': '3628-93a1-56e84634050814', 'propkey': propkey_pair['property'], 'data': 19.54 }) multiple_number_properties_generated.append({ 'resourceId': "5628-9ba1-55e847050815", 'propkey': propkey_pair['property'], 'data': '6.5' }) multiple_number_properties_generated.append({ 'resourceId': "7422-91h7-52s842060815", 'propkey': propkey_pair['property'], 'data': 33 }) Resources.get_latest_number_properties_multiple = MagicMock( return_value=multiple_number_properties_generated) multiple_info_properties_generated = list() if 'info_metrics' in propkey_yaml[collector]: for propkey_pair in propkey_yaml[collector][ 'info_metrics']: multiple_info_properties_generated.append({ 'resourceId': '3628-93a1-56e84634050814', 'propkey': propkey_pair['property'], 'data': 'test_info_property' }) multiple_info_properties_generated.append({ 'resourceId': "5628-9ba1-55e847050815", 'propkey': propkey_pair['property'], 'data': 'test_info_property' }) multiple_info_properties_generated.append({ 'resourceId': "7422-91h7-52s842060815", 'propkey': propkey_pair['property'], 'data': 'test_info_property' }) Resources.get_latest_info_properties_multiple = MagicMock( return_value=multiple_info_properties_generated) thread_list = list() # start prometheus server to provide metrics later on collector_instance = globals()[collector]() thread1 = Thread(target=run_prometheus_server, args=(random_prometheus_port, [collector_instance])) thread1.daemon = True thread1.start() thread_list.append(thread1) # give grandpa thread some time to get prometheus started and run a couple intervals of InventoryBuilder time.sleep(10) print("prometheus query port " + str(random_prometheus_port)) c = http.client.HTTPConnection("localhost:" + str(random_prometheus_port)) c.request("GET", "/") r = c.getresponse() self.assertEqual(r.status, 200, "HTTP server return code should be 200") self.assertEqual(r.reason, "OK", "HTTP status should be OK") data = r.read().decode() data_array = data.split('\n') metrics = set() for entry in data_array: if entry.startswith('#'): continue if entry.startswith('python_gc'): continue if entry.startswith('process_'): continue if entry.startswith('python_info'): continue split_entry = entry.split("}") if len(split_entry) != 2: continue metrics.add(split_entry[0] + "}") metrics_yaml_list = metrics_yaml[collector]['metrics'] self.assertTrue(metrics_yaml_list, msg=collector + " has no metrics defined, FIX IT!") self.assertTrue( metrics, msg=collector + " is not producing any metrics at all, how should I continue?") # check if there are more metrics being produced and they are not listed in metrics.yaml?! issubsetdifference = metrics.difference(metrics_yaml_list) self.assertTrue( metrics.issubset(metrics_yaml_list), msg=collector + ": metric not covered by testcase, probably missing in yaml\n" + "\n".join(issubsetdifference)) # check if all metrics from yaml are here supersetdifference = set(metrics_yaml_list).difference(metrics) self.assertTrue(set(metrics).issuperset(metrics_yaml_list), msg=collector + ": missing metrics from yaml:\n" + "\n".join(supersetdifference)) for t in thread_list: t.join(timeout=5) # we don't want to have any port locks if prometheus server thread is not shutting down random_prometheus_port += 1 REGISTRY.unregister(collector_instance)
def test_collector_metrics(self): self.metrics_yaml = yaml_read('tests/metrics.yaml') self.collector_config = yaml_read(os.environ['COLLECTOR_CONFIG']) self.target = os.getenv('TARGET') self.token = '2ed214d523-235f-h283-4566-6sf356124fd62::f234234-234' # every collector got to be tested in here self.random_prometheus_port = random.randrange(9000, 9700, 1) print("chosen testport: " + str(self.random_prometheus_port)) BaseCollector.get_target_tokens = MagicMock( return_value={self.target: self.token}) Vrops.get_token = MagicMock(return_value=( "2ed214d523-235f-h283-4566-6sf356124fd62::f234234-234", 200)) def create_adapter_objects(adapterkind) -> list: uuids = ["3628-93a1-56e84634050814", "3628-93a1-56e84634050814"] object_list = list() for i, _ in enumerate(uuids): resource_object = type( adapterkind.capitalize(), (object, ), { "name": f'{adapterkind.lower()}_{i+1}', "uuid": uuids[0], "target": self.target, "token": self.token }) object_list.append(resource_object) return object_list Vrops.get_adapter = MagicMock(return_value=None) Vrops.get_vcenter_adapter = MagicMock( return_value=(create_adapter_objects("Vcenter"), 200)) Vrops.get_nsxt_adapter = MagicMock( return_value=(create_adapter_objects("NSXTAdapterInstance"), 200)) Vrops.get_vcenter_operations_adapter_intance = MagicMock( return_value=(create_adapter_objects("VcopsAdapterInstance"), 200)) Vrops.get_sddc_health_adapter_intance = MagicMock( return_value=(create_adapter_objects("SDDCAdapterInstance"), 200)) # test tool get_resources to create resource objects def create_resource_objects(resourcekind) -> list: uuids = [ "3628-93a1-56e84634050814", "7422-91h7-52s842060815", "5628-9ba1-55e847050815" ] object_list = list() for i, _ in enumerate(uuids): resource_object = type( resourcekind.capitalize(), (object, ), { "name": f'{resourcekind.capitalize()}_{i+1}', "uuid": uuids[i], "resourcekind": resourcekind, "parent": uuids[0], "internal_name": f'{resourcekind.lower()}_1234', "instance_uuid": f'{resourcekind.lower()}_12345678' }) object_list.append(resource_object) return object_list Vrops.get_nsxt_mgmt_cluster = MagicMock( return_value=(create_resource_objects("ManagementCluster"), 200)) Vrops.get_nsxt_mgmt_nodes = MagicMock( return_value=(create_resource_objects("ManagementNode"), 200)) Vrops.get_nsxt_mgmt_service = MagicMock( return_value=(create_resource_objects("ManagementService"), 200)) Vrops.get_nsxt_transport_zone = MagicMock( return_value=(create_resource_objects("TransportZone"), 200)) Vrops.get_nsxt_transport_node = MagicMock( return_value=(create_resource_objects("TransportNode"), 200)) Vrops.get_nsxt_logical_switch = MagicMock( return_value=(create_resource_objects("LogicalSwitch"), 200)) Vrops.get_datacenter = MagicMock( return_value=(create_resource_objects("Datacenter"), 200)) Vrops.get_cluster = MagicMock( return_value=(create_resource_objects("ClusterComputeResource"), 200)) datastores = create_resource_objects("Datastore") for ds in datastores: ds.type = 'other' Vrops.get_datastores = MagicMock(return_value=(datastores, 200)) Vrops.get_hosts = MagicMock( return_value=(create_resource_objects("HostSystem"), 200)) Vrops.get_vms = MagicMock( return_value=(create_resource_objects("VirtualMachine"), 200)) Vrops.get_dis_virtual_switch = MagicMock( return_value=(create_resource_objects("VmwareDistributedSwitch"), 200)) Vrops.get_vcops_instances = MagicMock( return_value=(create_resource_objects("vcops_object"), 200)) Vrops.get_sddc_instances = MagicMock( return_value=(create_resource_objects("sddc_object"), 200)) Vrops.get_project_ids = MagicMock( return_value=[{ "3628-93a1-56e84634050814": "0815" }, { "7422-91h7-52s842060815": "0815" }, { "5628-9ba1-55e847050815": "internal" }]) Vrops.get_alertdefinitions = MagicMock( return_value={ 'id': 'test-id', 'name': 'test-alert', 'symptoms': [{ 'name': 'test_symptom', 'state': 'test-state' }], 'recommendation': [{ 'id': 'test-re', 'description': 'test-description' }] }) thread = Thread(target=InventoryBuilder, args=('./tests/test.json', 8000, 180, 300)) thread.daemon = True thread.start() for collector in self.metrics_yaml.keys(): print("\nTesting " + collector) class_module = importlib.import_module(f'collectors.{collector}') collector_instance = class_module.__getattribute__(collector)() if "Stats" in collector: multiple_metrics_generated = list() for metric in self.collector_config[collector]: multiple_metrics_generated.append({ "resourceId": "7422-91h7-52s842060815", "stat-list": { "stat": [{ "timestamps": [1582797716394], "statKey": { "key": metric['key'] }, "data": [88.0] }] } }) multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "stat-list": { "stat": [{ "timestamps": [1582797716394], "statKey": { "key": metric['key'] }, "data": [44.0] }] } }) multiple_metrics_generated.append({ "resourceId": "5628-9ba1-55e847050815", "stat-list": { "stat": [{ "timestamps": [1582797716394], "statKey": { "key": metric['key'] }, "data": [55.0] }] } }) Vrops.get_latest_stats_multiple = MagicMock( return_value=(multiple_metrics_generated, 200, 0.5)) if "Properties" in collector: multiple_metrics_generated = list() for metric in self.collector_config[collector]: multiple_metrics_generated.append({ "resourceId": "7422-91h7-52s842060815", "property-contents": { "property-content": [{ "timestamps": [1582797716394], "statKey": metric['key'], "data": [88.0] }] } }) multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "property-contents": { "property-content": [{ "timestamps": [1582797716394], "statKey": metric['key'], "values": ["test"] }] } }) multiple_metrics_generated.append({ "resourceId": "5628-9ba1-55e847050815", "property-contents": { "property-content": [{ "timestamps": [1582797716394], "statKey": metric['key'], "values": ["test"] }] } }) Vrops.get_latest_properties_multiple = MagicMock( return_value=(multiple_metrics_generated, 200, 0.5)) thread_list = list() # start prometheus server to provide metrics later on thread1 = Thread(target=run_prometheus_server, args=(self.random_prometheus_port, [collector_instance])) thread1.daemon = True thread1.start() thread_list.append(thread1) # give grandpa thread some time to get prometheus started and run a couple intervals of InventoryBuilder time.sleep(3) print("prometheus query port " + str(self.random_prometheus_port)) c = http.client.HTTPConnection("localhost:" + str(self.random_prometheus_port)) c.request("GET", "/") r = c.getresponse() self.assertEqual(r.status, 200, "HTTP server return code should be 200") self.assertEqual(r.reason, "OK", "HTTP status should be OK") data = r.read().decode() data_array = data.split('\n') metrics = set() for entry in data_array: if entry.startswith('#'): continue if entry.startswith('python_gc'): continue if entry.startswith('process_'): continue if entry.startswith('python_info'): continue split_entry = entry.split("}") if len(split_entry) != 2: continue metrics.add(split_entry[0] + "}") metrics_yaml_list = self.metrics_yaml[collector] self.assertTrue(metrics_yaml_list, msg=collector + " has no metrics defined, FIX IT!") self.assertTrue( metrics, msg=collector + " is not producing any metrics at all, how should I continue?") # check if there are more metrics being produced and they are not listed in metrics.yaml?! issubsetdifference = metrics.difference(metrics_yaml_list) self.assertTrue( metrics.issubset(metrics_yaml_list), msg=collector + ": metric not covered by testcase, probably missing in yaml\n" + "\n".join(issubsetdifference)) # check if all metrics from yaml are here supersetdifference = set(metrics_yaml_list).difference(metrics) self.assertTrue(set(metrics).issuperset(metrics_yaml_list), msg=collector + ": missing metrics from yaml:\n" + "\n".join(supersetdifference)) for t in thread_list: t.join(timeout=5) # we don't want to have any port locks if prometheus server thread is not shutting down self.random_prometheus_port += 1 REGISTRY.unregister(collector_instance)
def read_collector_config(self): config_file = yaml_read(os.environ['CONFIG']) return config_file
def test_collector_metrics(self): self.metrics_yaml = yaml_read('tests/metrics.yaml') self.collector_config = yaml_read(os.environ['CONFIG']) self.target = os.getenv('TARGET') self.token = '2ed214d523-235f-h283-4566-6sf356124fd62::f234234-234' # every collector got to be tested in here self.random_prometheus_port = random.randrange(9000, 9700, 1) print("chosen testport: " + str(self.random_prometheus_port)) BaseCollector.get_target_tokens = MagicMock( return_value={self.target: self.token}) Vrops.get_token = MagicMock(return_value=( "2ed214d523-235f-h283-4566-6sf356124fd62::f234234-234", 200)) vc = Vcenter(target=self.target, token=self.token) vc.name = "vcenter1" vc.uuid = "3628-93a1-56e84634050814" nsxt_adapter1 = NSXTAdapterInstance(target=self.target, token=self.token) nsxt_adapter2 = NSXTAdapterInstance(target=self.target, token=self.token) nsxt_adapter1.name = "nsxt_adapter1" nsxt_adapter2.name = "nsxt_adapter2" nsxt_adapter1.uuid = nsxt_adapter2.uuid = "3628-93a1-56e84634050814" Vrops.get_adapter = MagicMock(return_value=None) Vrops.get_vcenter_adapter = MagicMock(return_value=([vc][0])) Vrops.get_nsxt_adapter = MagicMock( return_value=([nsxt_adapter1, nsxt_adapter2])) # test tool get_resources to create resource objects nsxt1 = NSXTManagementCluster() nsxt2 = NSXTManagementCluster() nsxt3 = NSXTManagementCluster() nsxt1.name = "nsxt_mgmt_cluster1" nsxt2.name = "nsxt_mgmt_cluster2" nsxt3.name = "nsxt_mgmt_cluster3" nsxt1.uuid = "5628-9ba1-55e847050815" nsxt2.uuid = "3628-93a1-56e84634050814" nsxt3.uuid = "7422-91h7-52s842060815" nsxt1.resourcekind = nsxt2.resourcekind = nsxt3.resourcekind = "ManagementCluster" nsxt1.parent = nsxt2.parent = nsxt3.parent = "3628-93a1-56e84634050814" dc1 = Datacenter() dc2 = Datacenter() dc3 = Datacenter() dc1.name = "datacenter1" dc2.name = "datacenter2" dc3.name = "datacenter3" dc1.uuid = "3628-93a1-56e84634050814" dc2.uuid = "5628-9ba1-55e847050815" dc3.uuid = "7422-91h7-52s842060815" dc1.resourcekind = dc2.resourcekind = dc3.resourcekind = "Datacenter" dc1.parent = dc2.parent = dc3.parent = "3628-93a1-56e84634050814" cl1 = Cluster() cl2 = Cluster() cl3 = Cluster() cl1.name = "cluster1" cl2.name = "cluster2" cl3.name = "cluster3" cl1.uuid = "3628-93a1-56e84634050814" cl2.uuid = "5628-9ba1-55e847050815" cl3.uuid = "7422-91h7-52s842060815" cl1.resourcekind = cl2.resourcekind = cl3.resourcekind = "ClusterComputeResource" cl1.parent = cl2.parent = cl3.parent = "3628-93a1-56e84634050814" ds1 = Datastore() ds2 = Datastore() ds3 = Datastore() ds1.name = "vmfs_vc-w-0_p_ssd_bb091_001" ds2.name = "eph-bb112-1" ds3.name = "B121_Management_DS03" ds1.uuid = "3628-93a1-56e84634050814" ds2.uuid = "5628-9ba1-55e847050815" ds3.uuid = "7422-91h7-52s842060815" ds1.type = "vmfs_p_ssd" ds2.type = "ephemeral" ds3.type = "Management" ds1.resourcekind = ds2.resourcekind = ds3.resourcekind = "Datastore" ds1.parent = ds2.parent = ds3.parent = "7422-91h7-52s842060815" hs1 = Host() hs2 = Host() hs3 = Host() hs1.name = "hostsystem1" hs2.name = "hostsystem2" hs3.name = "hostsystem3" hs1.uuid = "3628-93a1-56e84634050814" hs2.uuid = "5628-9ba1-55e847050815" hs3.uuid = "7422-91h7-52s842060815" hs1.resourcekind = hs2.resourcekind = hs3.resourcekind = "HostSystem" hs1.parent = hs2.parent = hs3.parent = "7422-91h7-52s842060815" vm1 = VirtualMachine() vm2 = VirtualMachine() vm3 = VirtualMachine() vm1.name = "vm1" vm2.name = "vm2" vm3.name = "vm3" vm1.uuid = "3628-93a1-56e84634050814" vm2.uuid = "5628-9ba1-55e847050815" vm3.uuid = "7422-91h7-52s842060815" vm1.resourcekind = vm2.resourcekind = vm3.resourcekind = "VirtualMachine" vm1.parent = vm2.parent = vm3.parent = "7422-91h7-52s842060815" Vrops.get_nsxt_mgmt_cluster = MagicMock( return_value=[nsxt1, nsxt2, nsxt3]) Vrops.get_datacenter = MagicMock(return_value=[dc1, dc2, dc3]) Vrops.get_cluster = MagicMock(return_value=[cl1, cl2, cl3]) Vrops.get_datastores = MagicMock(return_value=[ds1, ds2, ds3]) Vrops.get_hosts = MagicMock(return_value=[hs1, hs2, hs3]) Vrops.get_vms = MagicMock(return_value=[vm1, vm2, vm3]) Vrops.get_latest_stat = MagicMock(return_value=1) Vrops.get_property = MagicMock(return_value="test_property") Vrops.get_project_ids = MagicMock( return_value=[{ "3628-93a1-56e84634050814": "0815" }, { "7422-91h7-52s842060815": "0815" }, { "5628-9ba1-55e847050815": "internal" }]) thread = Thread(target=InventoryBuilder, args=('./tests/test.json', 8000, 180, 300)) thread.daemon = True thread.start() for collector in self.metrics_yaml.keys(): print("\nTesting " + collector) class_module = importlib.import_module(f'collectors.{collector}') collector_instance = class_module.__getattribute__(collector)() if "Stats" in collector: multiple_metrics_generated = list() for metric in self.collector_config[collector]: multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "stat-list": { "stat": [{ "timestamps": [1582797716394], "statKey": { "key": metric['key'] }, "data": [88.0] }] } }) multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "stat-list": { "stat": [{ "timestamps": [1582797716394], "statKey": { "key": metric['key'] }, "data": [44.0] }] } }) multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "stat-list": { "stat": [{ "timestamps": [1582797716394], "statKey": { "key": metric['key'] }, "data": [55.0] }] } }) Vrops.get_latest_stats_multiple = MagicMock( return_value=(multiple_metrics_generated, 200, 0.5)) if "Properties" in collector: multiple_metrics_generated = list() for metric in self.collector_config[collector]: multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "property-contents": { "property-content": [{ "timestamps": [1582797716394], "statKey": metric['key'], "data": [88.0] }] } }) multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "property-contents": { "property-content": [{ "timestamps": [1582797716394], "statKey": metric['key'], "values": ["test"] }] } }) multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "property-contents": { "property-content": [{ "timestamps": [1582797716394], "statKey": metric['key'], "values": ["test"] }] } }) Vrops.get_latest_properties_multiple = MagicMock( return_value=(multiple_metrics_generated, 200, 0.5)) thread_list = list() # start prometheus server to provide metrics later on thread1 = Thread(target=run_prometheus_server, args=(self.random_prometheus_port, [collector_instance])) thread1.daemon = True thread1.start() thread_list.append(thread1) # give grandpa thread some time to get prometheus started and run a couple intervals of InventoryBuilder time.sleep(3) print("prometheus query port " + str(self.random_prometheus_port)) c = http.client.HTTPConnection("localhost:" + str(self.random_prometheus_port)) c.request("GET", "/") r = c.getresponse() self.assertEqual(r.status, 200, "HTTP server return code should be 200") self.assertEqual(r.reason, "OK", "HTTP status should be OK") data = r.read().decode() data_array = data.split('\n') metrics = set() for entry in data_array: if entry.startswith('#'): continue if entry.startswith('python_gc'): continue if entry.startswith('process_'): continue if entry.startswith('python_info'): continue split_entry = entry.split("}") if len(split_entry) != 2: continue metrics.add(split_entry[0] + "}") metrics_yaml_list = self.metrics_yaml[collector] self.assertTrue(metrics_yaml_list, msg=collector + " has no metrics defined, FIX IT!") self.assertTrue( metrics, msg=collector + " is not producing any metrics at all, how should I continue?") # check if there are more metrics being produced and they are not listed in metrics.yaml?! issubsetdifference = metrics.difference(metrics_yaml_list) self.assertTrue( metrics.issubset(metrics_yaml_list), msg=collector + ": metric not covered by testcase, probably missing in yaml\n" + "\n".join(issubsetdifference)) # check if all metrics from yaml are here supersetdifference = set(metrics_yaml_list).difference(metrics) self.assertTrue(set(metrics).issuperset(metrics_yaml_list), msg=collector + ": missing metrics from yaml:\n" + "\n".join(supersetdifference)) for t in thread_list: t.join(timeout=5) # we don't want to have any port locks if prometheus server thread is not shutting down self.random_prometheus_port += 1 REGISTRY.unregister(collector_instance)
def default_collectors(): return [ collector for collector in yaml_read(os.environ['CONFIG'])['default_collectors'] ]
def test_collector_metrics(self): self.metrics_yaml = yaml_read('tests/metrics.yaml') # every collector got to be tested in here self.random_prometheus_port = random.randrange(9000, 9700, 1) print("chosen testport: " + str(self.random_prometheus_port)) Vrops.get_token = MagicMock( return_value="2ed214d523-235f-h283-4566-6sf356124fd62::f234234-234" ) Vrops.get_adapter = MagicMock( return_value=[{ 'name': "vcenter1", 'uuid': '5628-9ba1-55e84701' }]) # test tool get_resources to create resource objects Vrops.get_datacenter = MagicMock( return_value=[{ 'name': 'datacenter1', 'uuid': '5628-9ba1-55e847050814' }, { 'name': 'datacenter2', 'uuid': '5628-9ba1-55e847050815' }, { 'name': 'datacenter3', 'uuid': '7422-91h7-52s842060815' }]) Vrops.get_cluster = MagicMock( return_value=[{ 'name': 'cluster1', 'uuid': '3628-93a1-56e84634050814' }, { 'name': 'cluster2', 'uuid': '5628-9ba1-55e847050815' }, { 'name': 'cluster3', 'uuid': '7422-91h7-52s842060815' }]) Vrops.get_hosts = MagicMock( return_value=[{ 'name': 'hostsystem1', 'uuid': '3628-93a1-56e84634050814' }, { 'name': 'hostsystem2', 'uuid': '5628-9ba1-55e847050815' }, { 'name': 'hostsystem3', 'uuid': '7422-91h7-52s842060815' }]) Vrops.get_vmfolders = MagicMock( return_value=[{ 'name': 'vmfolder1', 'uuid': '3628-93a1-56e84634050814' }, { 'name': 'vmfolder2', 'uuid': '5628-9ba1-55e847050815' }, { 'name': 'vmfolder3', 'uuid': '7422-91h7-52s842060815' }]) Vrops.get_virtualmachines = MagicMock( return_value=[{ 'name': 'vm1', 'uuid': '3628-93a1-56e84634050814' }, { 'name': 'vm2', 'uuid': '5628-9ba1-55e847050815' }, { 'name': 'vm3', 'uuid': '7422-91h7-52s842060815' }]) Vrops.get_datastores = MagicMock( return_value=[{ 'name': 'vmfs_vc-w-0_p_ssd_bb091_001', 'uuid': '3628-93a1-56e84634050814' }, { 'name': 'eph-bb112-1', 'uuid': '5628-9ba1-55e847050815' }, { 'name': 'B121_Management_DS03', 'uuid': '7422-91h7-52s842060815' }]) Vrops.get_resources = MagicMock( return_value=[{ 'name': 'resource1', 'uuid': '5628-9ba1-55e847050814' }, { 'name': 'resource2', 'uuid': '5628-9ba1-55e847050815' }]) Vrops.get_latest_stat = MagicMock(return_value=1) Vrops.get_property = MagicMock(return_value="test_property") Vrops.get_project_ids = MagicMock( return_value=[{ "3628-93a1-56e84634050814": "0815" }, { "7422-91h7-52s842060815": "0815" }, { "5628-9ba1-55e847050815": "internal" }]) thread = Thread(target=InventoryBuilder, args=('./tests/test.json', 8000, 180)) thread.daemon = True thread.start() for collector in self.metrics_yaml.keys(): print("\nTesting " + collector) if self.metrics_yaml[collector]['rubrics']: for rubric in self.metrics_yaml[collector]['metrics']: os.environ['RUBRIC'] = rubric print("Testing rubric:", rubric) self.collector_testrun(collector, rubric) else: self.collector_testrun(collector)
def collector_testrun(self, collector, rubric=None): if 'Stats' in collector: # mocking all values from yaml statkey_yaml = yaml_read(os.environ['CONFIG'])['statkeys'] multiple_metrics_generated = list() def append_metrics(statkey_pair): multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "stat-list": { "stat": [{ "timestamps": [1582797716394], "statKey": { "key": statkey_pair['statkey'] }, "data": [88.0] }] } }) multiple_metrics_generated.append({ "resourceId": "5628-9ba1-55e847050815", "stat-list": { "stat": [{ "timestamps": [1582797716394], "statKey": { "key": statkey_pair['statkey'] }, "data": [44.0] }] } }) multiple_metrics_generated.append({ "resourceId": "7422-91h7-52s842060815", "stat-list": { "stat": [{ "timestamps": [1582797716394], "statKey": { "key": statkey_pair['statkey'] }, "data": [55.0] }] } }) if rubric: for statkey_pair in statkey_yaml[collector][rubric]: append_metrics(statkey_pair) else: for statkey_pair in statkey_yaml[collector]: append_metrics(statkey_pair) Vrops.get_latest_stat_multiple = MagicMock( return_value=multiple_metrics_generated) if "Properties" in collector: propkey_yaml = yaml_read(os.environ['CONFIG'])['properties'] multiple_enum_properties_generated = list() if 'enum_metrics' in propkey_yaml[collector]: for propkey_pair in propkey_yaml[collector]['enum_metrics']: multiple_enum_properties_generated.append({ 'resourceId': '3628-93a1-56e84634050814', 'propkey': propkey_pair['property'], 'value': "test_enum_property" }) multiple_enum_properties_generated.append({ 'resourceId': "5628-9ba1-55e847050815", 'propkey': propkey_pair['property'], 'value': "test_enum_property" }) multiple_enum_properties_generated.append({ 'resourceId': "7422-91h7-52s842060815", 'propkey': propkey_pair['property'], 'value': "test_enum_property" }) Vrops.get_latest_enum_properties_multiple = MagicMock( return_value=multiple_enum_properties_generated) multiple_number_properties_generated = list() if 'number_metrics' in propkey_yaml[collector]: for propkey_pair in propkey_yaml[collector]['number_metrics']: multiple_number_properties_generated.append({ 'resourceId': '3628-93a1-56e84634050814', 'propkey': propkey_pair['property'], 'data': 19.54 }) multiple_number_properties_generated.append({ 'resourceId': "5628-9ba1-55e847050815", 'propkey': propkey_pair['property'], 'data': '6.5' }) multiple_number_properties_generated.append({ 'resourceId': "7422-91h7-52s842060815", 'propkey': propkey_pair['property'], 'data': 33 }) Vrops.get_latest_number_properties_multiple = MagicMock( return_value=multiple_number_properties_generated) multiple_info_properties_generated = list() if 'info_metrics' in propkey_yaml[collector]: for propkey_pair in propkey_yaml[collector]['info_metrics']: multiple_info_properties_generated.append({ 'resourceId': '3628-93a1-56e84634050814', 'propkey': propkey_pair['property'], 'data': 'test_info_property' }) multiple_info_properties_generated.append({ 'resourceId': "5628-9ba1-55e847050815", 'propkey': propkey_pair['property'], 'data': 'test_info_property' }) multiple_info_properties_generated.append({ 'resourceId': "7422-91h7-52s842060815", 'propkey': propkey_pair['property'], 'data': 'test_info_property' }) Vrops.get_latest_info_properties_multiple = MagicMock( return_value=multiple_info_properties_generated) thread_list = list() # start prometheus server to provide metrics later on collector_instance = globals()[collector]() thread1 = Thread(target=run_prometheus_server, args=(self.random_prometheus_port, [collector_instance])) thread1.daemon = True thread1.start() thread_list.append(thread1) # give grandpa thread some time to get prometheus started and run a couple intervals of InventoryBuilder time.sleep(10) print("prometheus query port " + str(self.random_prometheus_port)) c = http.client.HTTPConnection("localhost:" + str(self.random_prometheus_port)) c.request("GET", "/") r = c.getresponse() self.assertEqual(r.status, 200, "HTTP server return code should be 200") self.assertEqual(r.reason, "OK", "HTTP status should be OK") data = r.read().decode() data_array = data.split('\n') metrics = set() for entry in data_array: if entry.startswith('#'): continue if entry.startswith('python_gc'): continue if entry.startswith('process_'): continue if entry.startswith('python_info'): continue split_entry = entry.split("}") if len(split_entry) != 2: continue metrics.add(split_entry[0] + "}") if rubric: metrics_yaml_list = self.metrics_yaml[collector]['metrics'][rubric] else: metrics_yaml_list = self.metrics_yaml[collector]['metrics'] self.assertTrue(metrics_yaml_list, msg=collector + " has no metrics defined, FIX IT!") self.assertTrue( metrics, msg=collector + " is not producing any metrics at all, how should I continue?") # check if there are more metrics being produced and they are not listed in metrics.yaml?! issubsetdifference = metrics.difference(metrics_yaml_list) self.assertTrue( metrics.issubset(metrics_yaml_list), msg=collector + ": metric not covered by testcase, probably missing in yaml\n" + "\n".join(issubsetdifference)) # check if all metrics from yaml are here supersetdifference = set(metrics_yaml_list).difference(metrics) self.assertTrue(set(metrics).issuperset(metrics_yaml_list), msg=collector + ": missing metrics from yaml:\n" + "\n".join(supersetdifference)) for t in thread_list: t.join(timeout=5) # we don't want to have any port locks if prometheus server thread is not shutting down self.random_prometheus_port += 1 REGISTRY.unregister(collector_instance)
def test_collector_metrics(self): self.metrics_yaml = yaml_read('tests/metrics.yaml') self.collector_config = yaml_read(os.environ['CONFIG']) # every collector got to be tested in here self.random_prometheus_port = random.randrange(9000, 9700, 1) print("chosen testport: " + str(self.random_prometheus_port)) BaseCollector.get_target_tokens = MagicMock( return_value={ 'testhost.test': '2ed214d523-235f-h283-4566-6sf356124fd62::f234234-234' }) Vrops.get_token = MagicMock(return_value=( "2ed214d523-235f-h283-4566-6sf356124fd62::f234234-234", 200)) Vrops.get_adapter = MagicMock( return_value=("vcenter1", "3628-93a1-56e84634050814")) # test tool get_resources to create resource objects Vrops.get_datacenter = MagicMock( return_value=[{ 'name': 'datacenter1', 'uuid': '3628-93a1-56e84634050814', 'resourcekind': 'Datacenter', 'parent': '3628-93a1-56e84634050814' }, { 'name': 'datacenter2', 'uuid': '5628-9ba1-55e847050815', 'resourcekind': 'Datacenter', 'parent': '3628-93a1-56e84634050814' }, { 'name': 'datacenter3', 'uuid': '7422-91h7-52s842060815', 'resourcekind': 'Datacenter', 'parent': '3628-93a1-56e84634050814' }]) Vrops.get_cluster_and_datastores = MagicMock( return_value=[{ 'name': 'cluster1', 'uuid': '3628-93a1-56e84634050814', 'resourcekind': 'ClusterComputeResource', 'parent': '7422-91h7-52s842060815' }, { 'name': 'cluster2', 'uuid': '5628-9ba1-55e847050815', 'resourcekind': 'ClusterComputeResource', 'parent': '7422-91h7-52s842060815' }, { 'name': 'cluster3', 'uuid': '7422-91h7-52s842060815', 'resourcekind': 'ClusterComputeResource', 'parent': '7422-91h7-52s842060815' }, { 'name': 'vmfs_vc-w-0_p_ssd_bb091_001', 'uuid': '3628-93a1-56e84634050814', 'resourcekind': 'Datastore', 'parent': '7422-91h7-52s842060815' }, { 'name': 'eph-bb112-1', 'uuid': '5628-9ba1-55e847050815', 'resourcekind': 'Datastore', 'parent': '7422-91h7-52s842060815' }, { 'name': 'B121_Management_DS03', 'uuid': '7422-91h7-52s842060815', 'resourcekind': 'Datastore', 'parent': '7422-91h7-52s842060815' }]) Vrops.get_hosts = MagicMock( return_value=[{ 'name': 'hostsystem1', 'uuid': '3628-93a1-56e84634050814', 'resourcekind': 'HostSystem', 'parent': '7422-91h7-52s842060815' }, { 'name': 'hostsystem2', 'uuid': '5628-9ba1-55e847050815', 'resourcekind': 'HostSystem', 'parent': '7422-91h7-52s842060815' }, { 'name': 'hostsystem3', 'uuid': '7422-91h7-52s842060815', 'resourcekind': 'HostSystem', 'parent': '7422-91h7-52s842060815' }]) Vrops.get_vms = MagicMock( return_value=[{ 'name': 'vm1', 'uuid': '3628-93a1-56e84634050814', 'resourcekind': 'VirtualMachine', 'parent': '7422-91h7-52s842060815' }, { 'name': 'vm2', 'uuid': '5628-9ba1-55e847050815', 'resourcekind': 'VirtualMachine', 'parent': '7422-91h7-52s842060815' }, { 'name': 'vm3', 'uuid': '7422-91h7-52s842060815', 'resourcekind': 'VirtualMachine', 'parent': '7422-91h7-52s842060815' }]) Vrops.get_latest_stat = MagicMock(return_value=1) Vrops.get_property = MagicMock(return_value="test_property") Vrops.get_project_ids = MagicMock( return_value=[{ "3628-93a1-56e84634050814": "0815" }, { "7422-91h7-52s842060815": "0815" }, { "5628-9ba1-55e847050815": "internal" }]) thread = Thread(target=InventoryBuilder, args=('./tests/test.json', 8000, 180, 300)) thread.daemon = True thread.start() for collector in self.metrics_yaml.keys(): print("\nTesting " + collector) if "Stats" in collector: multiple_metrics_generated = list() for metric in self.collector_config[collector]: multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "stat-list": { "stat": [{ "timestamps": [1582797716394], "statKey": { "key": metric['key'] }, "data": [88.0] }] } }) multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "stat-list": { "stat": [{ "timestamps": [1582797716394], "statKey": { "key": metric['key'] }, "data": [44.0] }] } }) multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "stat-list": { "stat": [{ "timestamps": [1582797716394], "statKey": { "key": metric['key'] }, "data": [55.0] }] } }) Vrops.get_latest_stats_multiple = MagicMock( return_value=(multiple_metrics_generated, 200, 0.5)) if "Properties" in collector: multiple_metrics_generated = list() for metric in self.collector_config[collector]: multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "property-contents": { "property-content": [{ "timestamps": [1582797716394], "statKey": metric['key'], "data": [88.0] }] } }) multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "property-contents": { "property-content": [{ "timestamps": [1582797716394], "statKey": metric['key'], "values": ["test"] }] } }) multiple_metrics_generated.append({ "resourceId": "3628-93a1-56e84634050814", "property-contents": { "property-content": [{ "timestamps": [1582797716394], "statKey": metric['key'], "values": ["test"] }] } }) Vrops.get_latest_properties_multiple = MagicMock( return_value=(multiple_metrics_generated, 200, 0.5)) thread_list = list() # start prometheus server to provide metrics later on collector_instance = globals()[collector]() thread1 = Thread(target=run_prometheus_server, args=(self.random_prometheus_port, [collector_instance])) thread1.daemon = True thread1.start() thread_list.append(thread1) # give grandpa thread some time to get prometheus started and run a couple intervals of InventoryBuilder time.sleep(3) print("prometheus query port " + str(self.random_prometheus_port)) c = http.client.HTTPConnection("localhost:" + str(self.random_prometheus_port)) c.request("GET", "/") r = c.getresponse() self.assertEqual(r.status, 200, "HTTP server return code should be 200") self.assertEqual(r.reason, "OK", "HTTP status should be OK") data = r.read().decode() data_array = data.split('\n') metrics = set() for entry in data_array: if entry.startswith('#'): continue if entry.startswith('python_gc'): continue if entry.startswith('process_'): continue if entry.startswith('python_info'): continue split_entry = entry.split("}") if len(split_entry) != 2: continue metrics.add(split_entry[0] + "}") metrics_yaml_list = self.metrics_yaml[collector] self.assertTrue(metrics_yaml_list, msg=collector + " has no metrics defined, FIX IT!") self.assertTrue( metrics, msg=collector + " is not producing any metrics at all, how should I continue?") # check if there are more metrics being produced and they are not listed in metrics.yaml?! issubsetdifference = metrics.difference(metrics_yaml_list) self.assertTrue( metrics.issubset(metrics_yaml_list), msg=collector + ": metric not covered by testcase, probably missing in yaml\n" + "\n".join(issubsetdifference)) # check if all metrics from yaml are here supersetdifference = set(metrics_yaml_list).difference(metrics) self.assertTrue(set(metrics).issuperset(metrics_yaml_list), msg=collector + ": missing metrics from yaml:\n" + "\n".join(supersetdifference)) for t in thread_list: t.join(timeout=5) # we don't want to have any port locks if prometheus server thread is not shutting down self.random_prometheus_port += 1 REGISTRY.unregister(collector_instance)
def read_inventory_config(self): return yaml_read(os.environ['INVENTORY_CONFIG'])