def test_report_data(self): """ Basic sanity test """ rhel_product = TestData.create_products() rhel_entry = TestData.create_entry(RHEL, mem_high=True) rhel_entry.save() lookup = ReportData.objects.all() self.assertEqual(len(lookup), 1)
def test_import_interval_2_with_real_dupe01(self): # interval set at 2... for each checkin two total checkins will be created. # 2 real checkins should be created.. and 2 dupes.. # A second real checkin occurs but one hour of each checkin is a dupe. # Total should be six fact1 = {"memory_dot_memtotal": "604836", "lscpu_dot_cpu_socket(s)": "1", "lscpu_dot_cpu(s)": "1"} time = datetime.strptime("10102012:0530", constants.mn_fmt) time2 = datetime.strptime("10102012:0639", constants.mn_fmt) uuid = products_dict[RHEL][1] prod = products_dict[RHEL][0] TestData.create_product_usage(self.ss, fact1, time, consumer=uuid, instance='mac01', products=prod) TestData.create_product_usage(self.ss, fact1, time2, consumer=uuid, instance='mac01', products=prod) TestData.create_product_usage(self.ss, fact1, time, consumer=uuid, instance='mac02', products=prod) TestData.create_product_usage(self.ss, fact1, time2, consumer=uuid, instance='mac02', products=prod) # run import results = import_data(checkin_interval=2, force_import=True) # verify 1 items in db lookup = ReportData.objects.all() self.assertEqual(len(lookup), 6)
def test_advanced_mcu_mdu(self): """ three report data entries, each w/ unique instance_ident 2 in the same $hour 1 in $hour + 1 mcu = 2 , mdu = 3 """ delta_day=timedelta(days=1) delta_hour=timedelta(hours=1) start = datetime.now() - delta_day end = datetime.now() + delta_day hour_plus_1 = datetime.now() + delta_hour ReportData.drop_collection() rhel_entry = TestData.create_entry(RHEL, instance_identifier="00:10") rhel_entry.save() rhel_entry = TestData.create_entry(RHEL, instance_identifier="00:11") rhel_entry.save() rhel_entry = TestData.create_entry(RHEL, date=hour_plus_1, instance_identifier="00:12") rhel_entry.save() lookup = ReportData.objects.all() self.assertEqual(len(lookup), 3) filter_args = { "memtotal__gt": rhel_entry.memtotal - 1, "product": rhel_entry.product, "contract_id": rhel_entry.contract_id, "support": rhel_entry.support, "memtotal__lt": rhel_entry.memtotal + 1, "consumer_uuid": rhel_entry.consumer_uuid, "sla": rhel_entry.sla } args = { "start": start.strftime(constants.epoch), "end": end.strftime(constants.epoch), "filter_args_dict": filter_args, "description": {"Product": RHEL} } test_dict = MaxUsage.get_MDU_MCU(**args) mdu = test_dict['mdu'][1] mcu = test_dict['mcu'][1] self.assertEqual(mdu[1], 3, "correct mdu found") self.assertEqual(mcu[1], 2, "correct mcu found")
def test_post_204(self): self.assertEqual(0, MarketingReportData.objects.all().count()) pool = TestData.create_candlepin_pool() product = TestData.create_candlepin_product() mpu_entry_json = TestData.create_marketing_product_usage_json() resp = self.api_client.post( '/api/v1/marketingproductusage/', data=mpu_entry_json, SSL_CLIENT_CERT=self.valid_identity_cert_pem) self.assertEqual(204, resp.status_code, 'http status code is expected') self.assertEqual(2, MarketingReportData.objects.all().count(), 'marketing product_usuage successfully imported') self.assertEqual(0, QuarantinedReportData.objects.all().count(), 'asserted no marketing product usuage quarantined')
def import_bulk_load_base(self, items_to_load): # turn off bulk load option time = datetime.strptime("10102012:0530", constants.mn_fmt) fact1 = {"memory_dot_memtotal": "604836", "lscpu_dot_cpu_socket(s)": "1", "lscpu_dot_cpu(s)": "1"} timedelt = timedelta(hours=1) uuid = products_dict[RHEL][1] instance = 'mac01' products = products_dict[RHEL][0] bulk_load = {} for i in range(items_to_load): time += timedelt this_hash = hash( str(uuid) + str(instance) + str(time) + str(products)) td = TestData.create_product_usage(self.ss, fact1, time, consumer=uuid, instance=instance, products=products, save=False) bulk_load[this_hash] = td # print(len(bulk_load)) my_list = [] for key, value in bulk_load.items(): my_list.append(value) timer_start = datetime.now() results = import_data(product_usage=my_list, force_import=True) lookup = ReportData.objects.all() self.assertEqual(len(lookup), items_to_load) timer_stop = datetime.now()
def test_basic_mdu(self): """ Basic test of mdu """ ReportData.drop_collection() rhel_entry = TestData.create_entry(RHEL, mem_high=True) rhel_entry.save() lookup = ReportData.objects.all() self.assertEqual(len(lookup), 1) delta=timedelta(days=1) start = datetime.now() - delta end = datetime.now() + delta filter_args = { "memtotal__gt": rhel_entry.memtotal - 1, "product": rhel_entry.product, "contract_id": rhel_entry.contract_id, "support": rhel_entry.support, "memtotal__lt": rhel_entry.memtotal + 1, "consumer_uuid": rhel_entry.consumer_uuid, "sla": rhel_entry.sla } args = { "start": start.strftime(constants.epoch), "end": end.strftime(constants.epoch), "filter_args_dict": filter_args, "description": {"Product": RHEL} } test_dict = MaxUsage.get_MDU_MCU(**args) result = test_dict['mdu'][1] self.assertEqual(result[1], 1, "correct mdu found")
def test_getlist(self): #create json here n = (datetime.now() + timedelta(days=1)).strftime('%s') entry = TestData.create_product_usage_json() #entry["date"] = "abcd" """
def test_post_204(self): self.assertEqual(0, Rules.objects.all().count()) rules_entry_json = TestData.create_candlepin_rules_json() resp = self.api_client.post( '/api/v1/rules/', data=rules_entry_json, SSL_CLIENT_CERT=self.valid_identity_cert_pem) self.assertEqual(204, resp.status_code, 'http status code is expected') self.assertEqual(1, Rules.objects.all().count())
def test_post_204(self): self.assertEqual(0, SpliceServer.objects.all().count()) spliceserver_entry_json = TestData.create_splice_server_json() resp = self.api_client.post( '/api/v1/spliceserver/', data=spliceserver_entry_json, SSL_CLIENT_CERT=self.valid_identity_cert_pem) self.assertEqual(204, resp.status_code, 'http status code is expected') self.assertEqual(1, SpliceServer.objects.all().count())
def test_post_unique_ident(self): entry1 = TestData.create_product_usage_json(instance_identifier='00:11') entry2 = TestData.create_product_usage_json(instance_identifier='00:22') resp0 = self.api_client.post('/api/v1/productusage/', format='json', data=entry1) resp1 = self.api_client.post('/api/v1/productusage/', format='json', data=entry2) self.assertEqual(202, resp0.status_code, 'http status code is expected') self.assertEqual(202, resp1.status_code, 'http status code is expected') self.assertEqual(2, ReportData.objects.all().count(), 'product_usuage successfully imported') self.assertEqual(0, QuarantinedReportData.objects.all().count(), 'asserted 1 product usuage quarantined')
def test_import_dup(self): fact1 = {"memory_dot_memtotal": "604836", "lscpu_dot_cpu_socket(s)": "1", "lscpu_dot_cpu(s)": "1"} time = datetime.strptime("10102012:0530", constants.mn_fmt) time2 = datetime.strptime("10102012:0531", constants.mn_fmt) uuid = products_dict[RHEL][1] prod = products_dict[RHEL][0] pu = TestData.create_product_usage(self.ss, fact1, time, consumer=uuid, instance='mac01', products=prod) pu = TestData.create_product_usage(self.ss, fact1, time2, consumer=uuid, instance='mac01', products=prod) # run import results = import_data(force_import=True) # verify 1 items in db lookup = ReportData.objects.all() self.assertEqual(len(lookup), 1)
def test_post_202(self): self.assertEqual(0, ReportData.objects.all().count(), 'product_usuage successfully imported') entry = TestData.create_product_usage_json() resp = self.api_client.post( '/api/v1/productusage/', format='json', data=entry) self.assertEqual(202, resp.status_code, 'http status code is expected') self.assertEqual(1, ReportData.objects.all().count(), 'product_usuage successfully imported') self.assertEqual(0, QuarantinedReportData.objects.all().count(), 'asserted no product usuage quarantined')
def test_getlist(self): #create json here, memory greater than biz rules allow e = TestData.create_product_usage_json(memory='604836000000') entry = json.dumps(e) resp = self.post('/api/v1/productusage/', data=entry ) self.assertEqual(202, resp.status_code, 'http status code is expected') self.assertEqual(ReportData.objects.count(), 1) resp = self.get('/api/v1/compliancedata/') self.assertEqual(200, resp.status_code, 'http status code is expected') self.assertContains(resp, '"rule": "0 > 8388608; 8388608 > 83886080"', count=1, status_code=200)
def test_post_duplicate(self): entry = TestData.create_product_usage_json() resp0 = self.api_client.post( '/api/v1/productusage/', format='json', data=entry) resp1 = self.api_client.post( '/api/v1/productusage/', format='json', data=entry) self.assertEqual( 202, resp0.status_code, 'http status code is expected') #duplicates are now only logged and not rejected w/ a 409 self.assertEqual( 202, resp1.status_code, 'http status code is expected') self.assertEqual(1, ReportData.objects.all().count(), 'product_usuage successfully imported') #get a 202, but still there is only one object in db self.assertEqual(1, ReportData.objects.all().count(), 'product_usuage successfully imported')
def main(): SpliceServer.drop_collection() ProductUsage.drop_collection() ReportData.drop_collection() ss1 = TestData.create_splice_server("test01", "east") uuid_rhel_jboss = products_dict[RHEL][1] prod_rhel = products_dict[RHEL][0] prod_jboss = products_dict[JBoss][0] uuid_ha = products_dict[HA][1] prod_ha = products_dict[HA][0] uuid_edu = products_dict[EDU][1] prod_edu = products_dict[EDU][0] now = datetime.now() delta_day = timedelta(days=4) delta_hour = timedelta(days=3) for i in range(1, 10): this_time = now - (delta_hour * i) create_set_of_usage( prod_rhel, uuid_rhel_jboss, get_times(this_time), ss1, 7) for i in range(1, 10): this_time = now - (delta_hour * i) create_set_of_usage( prod_jboss, uuid_rhel_jboss, get_times(this_time), ss1, 4) for i in range(1, 4): this_time = now - (delta_hour * i) create_set_of_usage(prod_ha, uuid_ha, get_times(this_time), ss1, 7) for i in range(5, 10): this_time = now - (delta_hour * i) create_set_of_usage( prod_edu, uuid_edu, get_times(this_time), ss1, 5) # run import results = import_data(force_import=True) # verify 1 items in db lookup = ReportData.objects.all()
def test_getlist(self): #create json here, a valid entry self.assertEqual(0, ReportData.objects.all().count()) #print('count: ' + str(ReportData.objects.all().count())) e = TestData.create_product_usage_json(instance_identifier="00:11") entry = json.dumps(e) #print('ENTRY: ' + entry) resp = self.post('/api/v1/productusage/', data=entry) #print('resp:' + str(resp.status_code)) self.assertEqual(202, resp.status_code, 'http status code is expected') self.assertEqual(1, ReportData.objects.all().count()) q = {"user": "******", "byMonth": "11,2012", "contract_number": "All", "rhic": "null", "env": "All"} myquery = json.dumps(q) resp = self.post('/api/v1/reportmeter/', data=myquery, code=200) #print(resp) self.assertEqual(200, resp.status_code, 'http status code is expected') self.assertContains(resp, '"count": 1', count=1, status_code=200)
def create_set_of_usage(prod, uuid, list_of_times, splice_server, iterate_num): fact1 = {"memory_dot_memtotal": "604836", "lscpu_dot_cpu_socket(s)": "1", "lscpu_dot_cpu(s)": "1"} for i in range(iterate_num): lst = [random.choice( string.ascii_letters + string.digits) for n in xrange(10)] rand = "".join(lst) # print rand mac01 = '00:24:7E:69:5C:57-03:54:' + rand mac02 = '00:DF:FD:45:6V:54-05:34:' + rand mac03 = '34:EF:GR:34:4C:4T-34:45:' + rand mac04 = 'RT:ER:G4:24:C6:47-38:49:' + rand mac05 = 'Z4:EF:ZR:34:XC:4Z-34:43:' + rand # MCU =2 MDU =3 TestData.create_product_usage(splice_server, fact1, list_of_times[0], consumer=uuid, instance=mac01, products=prod) TestData.create_product_usage(splice_server, fact1, list_of_times[0], consumer=uuid, instance=mac02, products=prod) TestData.create_product_usage(splice_server, fact1, list_of_times[1], consumer=uuid, instance=mac03, products=prod) # MCU=3 MDU =5 TestData.create_product_usage(splice_server, fact1, list_of_times[8], consumer=uuid, instance=mac01, products=prod) TestData.create_product_usage(splice_server, fact1, list_of_times[9], consumer=uuid, instance=mac02, products=prod) TestData.create_product_usage(splice_server, fact1, list_of_times[9], consumer=uuid, instance=mac03, products=prod) TestData.create_product_usage(splice_server, fact1, list_of_times[9], consumer=uuid, instance=mac04, products=prod) TestData.create_product_usage(splice_server, fact1, list_of_times[11], consumer=uuid, instance=mac01, products=prod)