def test_always_update_stale_timestamp_from_next_reporter(self): old_stale_timestamp = now() + timedelta(days=2) old_reporter = "old reporter" timestamps = (old_stale_timestamp + timedelta(days=1), old_stale_timestamp - timedelta(days=1)) reporters = (old_reporter, "new reporter") for new_stale_timestamp, new_reporter in product(timestamps, reporters): with self.subTest(stale_timestamp=new_stale_timestamp, reporter=new_reporter): print("DISTEST", new_stale_timestamp, new_reporter) insights_id = generate_uuid() created_host = self._add_host( 201, insights_id=insights_id, stale_timestamp=old_stale_timestamp.isoformat(), reporter=old_reporter, ) old_retrieved_host = self._retrieve_host(created_host) self.assertEqual(old_stale_timestamp, old_retrieved_host.stale_timestamp) self.assertEqual(old_reporter, old_retrieved_host.reporter) self._add_host( 200, insights_id=insights_id, stale_timestamp=new_stale_timestamp.isoformat(), reporter=new_reporter, ) new_retrieved_host = self._retrieve_host(created_host) self.assertEqual(new_stale_timestamp, new_retrieved_host.stale_timestamp) self.assertEqual(new_reporter, new_retrieved_host.reporter)
def test_create_and_update(self): facts = None host_data = HostWrapper(test_data(facts=facts)) # Create the host response = self.post(HOST_URL, [host_data.data()], 207) self._verify_host_status(response, 0, 201) created_host = self._pluck_host_from_response(response, 0) original_id = created_host["id"] self._validate_host(created_host, host_data, expected_id=original_id) created_time = dateutil.parser.parse(created_host["created"]) current_timestamp = now() self.assertGreater(current_timestamp, created_time) self.assertLess(current_timestamp - timedelta(minutes=15), created_time) host_data.facts = copy.deepcopy(FACTS) # Replace facts under the first namespace host_data.facts[0]["facts"] = {"newkey1": "newvalue1"} # Add a new set of facts under a new namespace host_data.facts.append({"namespace": "ns2", "facts": {"key2": "value2"}}) # Add a new canonical fact host_data.rhel_machine_id = generate_uuid() host_data.ip_addresses = ["10.10.0.1", "10.0.0.2", "fe80::d46b:2807:f258:c319"] host_data.mac_addresses = ["c2:00:d0:c8:61:01"] host_data.external_id = "i-05d2313e6b9a42b16" host_data.insights_id = generate_uuid() # Update the host with the new data response = self.post(HOST_URL, [host_data.data()], 207) self._verify_host_status(response, 0, 200) updated_host = self._pluck_host_from_response(response, 0) # Make sure the id from the update post matches the id from the create self.assertEqual(updated_host["id"], original_id) # Verify the timestamp has been modified self.assertIsNotNone(updated_host["updated"]) modified_time = dateutil.parser.parse(updated_host["updated"]) self.assertGreater(modified_time, created_time) host_lookup_results = self.get(f"{HOST_URL}/{original_id}", 200) # sanity check # host_lookup_results["results"][0]["facts"][0]["facts"]["key2"] = "blah" # host_lookup_results["results"][0]["insights_id"] = "1.2.3.4" self._validate_host(host_lookup_results["results"][0], host_data, expected_id=original_id)
def test_create_host_with_stale_timestamp_and_reporter(self): stale_timestamp = now() reporter = "some reporter" created_host_id = self._add_host(201, stale_timestamp=stale_timestamp.isoformat(), reporter=reporter) retrieved_host = self._retrieve_host(created_host_id) self.assertEqual(stale_timestamp, retrieved_host.stale_timestamp) self.assertEqual(reporter, retrieved_host.reporter)
def setUp(self): super().setUp() # add a host with no insights id host_wrapper = HostWrapper() host_wrapper.account = ACCOUNT host_wrapper.id = generate_uuid() host_wrapper.satellite_id = generate_uuid() host_wrapper.stale_timestamp = now().isoformat() host_wrapper.reporter = "test" self.post(HOST_URL, [host_wrapper.data()], 207)
def _update_hosts(self, id_updates): # New modified_on value must be set explicitly so it’s saved the same to all # records. Otherwise SQLAlchemy would consider it unchanged and update it # automatically to its own "now" only for records whose ID changed. new_modified_on = now() with self.app.app_context(): for added_host_index, new_id in id_updates: self._update_host(added_host_index, new_id, new_modified_on) db.session.commit()
def setUp(self): super().setUp() host_wrapper = HostWrapper() host_wrapper.account = ACCOUNT host_wrapper.display_name = "host1" # Same as self.added_hosts[0] host_wrapper.insights_id = generate_uuid() host_wrapper.stale_timestamp = now().isoformat() host_wrapper.reporter = "test" response_data = self.post(HOST_URL, [host_wrapper.data()], 207) self.added_hosts.append(HostWrapper(response_data["data"][0]["host"]))
def setUp(self): super().setUp() current_timestamp = now() self.fresh_host = self._create_host(current_timestamp + timedelta(hours=1)) self.stale_host = self._create_host(current_timestamp - timedelta(hours=1)) self.stale_warning_host = self._create_host( current_timestamp - timedelta(weeks=1, hours=1)) self.culled_host = self._create_host(current_timestamp - timedelta(weeks=2, hours=1)) self.unknown_host = self._create_host(current_timestamp) self._nullify_culling_fields(self.unknown_host["id"])
def test_ignore_culled_host_on_update_by_elevated_id(self): # Culled host host_to_create_data = test_data( insights_id=generate_uuid(), facts=None, stale_timestamp=(now() - timedelta(weeks=3)).isoformat() ) # Create the host response = self.post(HOST_URL, [host_to_create_data], 207) self._verify_host_status(response, 0, 201) created_host = self._pluck_host_from_response(response, 0) # Update the host host_to_update_data = {**host_to_create_data, "ip_addresses": ["10.10.0.2"]} new_response = self.post(HOST_URL, [host_to_update_data], 207) self._verify_host_status(new_response, 0, 201) updated_host = self._pluck_host_from_response(new_response, 0) self.assertNotEqual(created_host["id"], updated_host["id"])
def test_with_stale_timestamp(self): def _assert_values(response_host): self.assertIn("stale_timestamp", response_host) self.assertIn("stale_warning_timestamp", response_host) self.assertIn("culled_timestamp", response_host) self.assertIn("reporter", response_host) self.assertEqual(stale_timestamp_str, response_host["stale_timestamp"]) self.assertEqual(stale_warning_timestamp_str, response_host["stale_warning_timestamp"]) self.assertEqual(culled_timestamp_str, response_host["culled_timestamp"]) self.assertEqual(reporter, response_host["reporter"]) stale_timestamp = now() stale_timestamp_str = stale_timestamp.isoformat() stale_warning_timestamp = stale_timestamp + timedelta(weeks=1) stale_warning_timestamp_str = stale_warning_timestamp.isoformat() culled_timestamp = stale_timestamp + timedelta(weeks=2) culled_timestamp_str = culled_timestamp.isoformat() reporter = "some reporter" host_to_create = HostWrapper({ "account": ACCOUNT, "fqdn": "matching fqdn", "stale_timestamp": stale_timestamp_str, "reporter": reporter }) create_response = self.post(HOST_URL, [host_to_create.data()], 207) self._verify_host_status(create_response, 0, 201) created_host = self._pluck_host_from_response(create_response, 0) _assert_values(created_host) update_response = self.post(HOST_URL, [host_to_create.data()], 207) self._verify_host_status(update_response, 0, 200) updated_host = self._pluck_host_from_response(update_response, 0) _assert_values(updated_host) get_list_response = self.get(HOST_URL, 200) _assert_values(get_list_response["results"][0]) created_host_id = created_host["id"] get_by_id_response = self.get(f"{HOST_URL}/{created_host_id}", 200) _assert_values(get_by_id_response["results"][0])
def test_get_host_with_escaped_special_characters(self): host_wrapper = HostWrapper({ "account": ACCOUNT, "insights_id": generate_uuid(), "stale_timestamp": now().isoformat(), "reporter": "test", "tags": [ { "namespace": ";,/?:@&=+$", "key": "-_.!~*'()", "value": "#" }, { "namespace": " \t\n\r\f\v", "key": " \t\n\r\f\v", "value": " \t\n\r\f\v" }, ], }) message = {"operation": "add_host", "data": host_wrapper.data()} with self.app.app_context(): mock_event_producer = MockEventProducer() handle_message(json.dumps(message), mock_event_producer) response_data = json.loads(mock_event_producer.event) created_host = response_data["host"] for namespace, key, value in ((";,/?:@&=+$", "-_.!~*'()", "#"), (" \t\n\r\f\v", " \t\n\r\f\v", " \t\n\r\f\v")): with self.subTest(namespace=namespace, key=key, value=value): tags_query = url_quote( f"{quote_everything(namespace)}/{quote_everything(key)}={quote_everything(value)}" ) get_response = self.get(f"{HOST_URL}?tags={tags_query}", 200) self.assertEqual(get_response["count"], 1) self.assertEqual(get_response["results"][0]["id"], created_host["id"])
def test_ignore_culled_host_on_update_by_canonical_facts(self): # Culled host host_data = test_data( fqdn="my awesome fqdn", facts=None, stale_timestamp=(now() - timedelta(weeks=3)).isoformat() ) # Create the host response = self.post(HOST_URL, [host_data], 207) self._verify_host_status(response, 0, 201) created_host = self._pluck_host_from_response(response, 0) # Update the host new_response = self.post(HOST_URL, [host_data], 207) self._verify_host_status(new_response, 0, 201) updated_host = self._pluck_host_from_response(new_response, 0) self.assertNotEqual(created_host["id"], updated_host["id"])