def test_uploading_simple_product_usage(self): consumer = "consumer_uuid" splice_server = "splice_server_uuid" inst_id = "mac_addr" date = datetime.now(tzutc()) allowed_product_info = ["1", "2", "3"] unallowed_product_info = ["0"] facts = {"tbd": "values"} pu_a = self.create_product_usage(consumer, splice_server, inst_id, date, allowed_product_info=allowed_product_info, unallowed_product_info=unallowed_product_info, facts=facts) self.assertEquals(len(ProductUsage.objects()), 0) post_data = [pu_a._data] LOG.info("Calling api for productusage import with post data: '%s'" % (post_data)) resp = self.api_client.post('/api/v1/productusage/', format='json', data=post_data, SSL_CLIENT_CERT=self.expected_valid_splice_server_identity_pem) LOG.info("Response for productusage import: Status Code: %s, Response: %s" % (resp.status_code, resp)) self.assertEquals(resp.status_code, 202) # Now check that the server api saved the object as expected found = ProductUsage.objects() self.assertEquals(len(found), 1) self.assertEquals(found[0].consumer, consumer) self.assertEquals(found[0].splice_server, splice_server) self.assertEquals(found[0].instance_identifier, inst_id) self.assertEquals(found[0].date.year, date.year) self.assertEquals(found[0].date.month, date.month) self.assertEquals(found[0].date.day, date.day) # TODO: Fix timezone issue, we are doing something wrong and not handling timezones correctly # self.assertEquals(found[0].date.hour, date.hour) self.assertEquals(found[0].date.minute, date.minute) self.assertEquals(found[0].date.second, date.second) self.assertEquals(found[0].allowed_product_info, allowed_product_info) self.assertEquals(found[0].unallowed_product_info, unallowed_product_info) self.assertEquals(found[0].facts, facts)
def test_create_simple_product_usage(self): self.assertEquals(len(ProductUsage.objects()), 0) consumer = "consumer_uuid" splice_server = "splice_server_uuid" inst_id = "mac_addr" date = datetime.now(tzutc()) pu = self.create_product_usage(consumer, splice_server, inst_id, date) pu.save() self.assertEquals(len(ProductUsage.objects()), 1)
def test_duplicate_product_usage_not_allowed(self): self.assertEquals(len(ProductUsage.objects()), 0) consumer = "consumer_uuid" splice_server = "splice_server_uuid" inst_id = "mac_addr" date = datetime.now(tzutc()) pu_a = self.create_product_usage(consumer, splice_server, inst_id, date) pu_a.save() self.assertEquals(len(ProductUsage.objects()), 1) caught = False pu_b = self.create_product_usage(consumer, splice_server, inst_id, date) try: pu_b.save() except OperationError, e: caught = True
def test_tracker_for_product_usage_prevents_duplicate_server_entries(self): self.assertEqual(len(ProductUsage.objects()), 0) pu = ProductUsage() pu.consumer = "consumer_uuid" pu.splice_server = "splice server uuid" pu.date = datetime.now(tzutc()) pu.instance_identifier = "mac addr" pu.allowed_product_info = ["1"] pu.unallowed_product_info = [] pu.facts = {"key":"value"} self.assertEqual(pu.tracker, []) pu.tracker.append("a.example.com") pu.tracker.append("a.example.com") pu.tracker.append("b.example.com") self.assertEqual(len(pu.tracker), 3) pu.save() self.assertEqual(len(pu.tracker), 2) found = ProductUsage.objects() self.assertEqual(len(found), 1) self.assertEqual(len(found[0].tracker), 2) self.assertIn("a.example.com", found[0].tracker) self.assertIn("b.example.com", found[0].tracker)
def test_uploading_multiple_product_usages(self): consumer = "consumer_uuid" splice_server = "splice_server_uuid" date = datetime.now(tzutc()) allowed_product_info = ["1", "2", "3"] unallowed_product_info = ["0"] facts = {"tbd": "values"} pu_a = self.create_product_usage(consumer, splice_server, "instance_a", date, allowed_product_info=allowed_product_info, unallowed_product_info=unallowed_product_info, facts=facts) pu_b = self.create_product_usage(consumer, splice_server, "instance_b", date, allowed_product_info=allowed_product_info, unallowed_product_info=unallowed_product_info, facts=facts) self.assertEquals(len(ProductUsage.objects()), 0) post_data = [pu_a._data, pu_b._data] LOG.info("Calling api for productusage import with post data: '%s'" % (post_data)) resp = self.api_client.post('/api/v1/productusage/', format='json', data=post_data, SSL_CLIENT_CERT=self.expected_valid_splice_server_identity_pem) LOG.info("Response for productusage import: Status Code: %s, Response: %s" % (resp.status_code, resp)) self.assertEquals(resp.status_code, 202) # Now check that the server api saved the object as expected found = ProductUsage.objects() self.assertEquals(len(found), 2)
def _get_product_usage_data(addr, limit): """ Returns product usage data which has not yet been uploaded to 'addr' @param addr: remote server to upload data to @param limit: max amount of objects to process per request @return: list of product usage objects ordered by date """ #TODO: # - Modify query to not fetch the "tracker" field this way it is always blank prod_usage_data = ProductUsage.objects(tracker__nin=[addr]) prod_usage_data = prod_usage_data.order_by("date") if limit: prod_usage_data = prod_usage_data.limit(limit) # Keep 'tracker' information private to this server for pu in prod_usage_data: pu.tracker = [] # _LOG.info("Retrieved %s items to send to %s" % (len(prod_usage_data), addr)) return prod_usage_data
def _unmark_sent(object_ids, addr): for oid in object_ids: ProductUsage.objects(id=oid).update(pull__tracker=addr)
def _mark_sent(object_ids, addr): for oid in object_ids: ProductUsage.objects(id=oid).update(add_to_set__tracker=addr)