def test_create_host_with_display_name_as_None_with_fqdn(self): """ This test should verify that the display_name is set to the fqdn when a display_name is not passed in but the fqdn is passed in. """ expected_display_name = "fred.flintstone.bedrock.com" host_data = HostWrapper(test_data(facts=None)) # Explicitly set the display name to None host_data.display_name = None host_data.fqdn = expected_display_name # Create the host created_host = self.post(HOST_URL, host_data.data(), 201) original_id = created_host["id"] host_lookup_results = self.get("%s/%s" % (HOST_URL, original_id), 200) # Explicitly set the display_name ...this is expected here host_data.display_name = expected_display_name self._validate_host(host_lookup_results["results"][0], host_data, expected_id=original_id, verify_tags=False)
def create_hosts(self): hosts_to_create = [ ("host1", "12345", "host1.domain.test"), ("host2", "54321", "host1.domain.test"), # the same fqdn is intentional ("host3", "56789", "host2.domain.test"), ] host_list = [] for host in hosts_to_create: host_wrapper = HostWrapper() host_wrapper.account = ACCOUNT host_wrapper.tags = copy.deepcopy(TAGS) host_wrapper.display_name = host[0] host_wrapper.insights_id = host[1] host_wrapper.fqdn = host[2] host_wrapper.facts = [{ "namespace": "ns1", "facts": { "key1": "value1" } }] response_data = self.post(HOST_URL, host_wrapper.data(), 201) host_list.append(HostWrapper(response_data)) return host_list
def test_create_host_without_display_name_and_with_fqdn(self): """ This test should verify that the display_name is set to the fqdn when a display_name is not passed in but the fqdn is passed in. """ expected_display_name = "fred.flintstone.bedrock.com" host_data = HostWrapper(test_data(facts=None)) del host_data.display_name host_data.fqdn = expected_display_name # 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"] host_lookup_results = self.get(f"{HOST_URL}/{original_id}", 200) # Explicitly set the display_name ...this is expected here host_data.display_name = expected_display_name self._validate_host(host_lookup_results["results"][0], host_data, expected_id=original_id)
def test_create_host_with_empty_tags_facts_display_name_then_update(self): # Create a host with empty tags, facts, and display_name # then update those fields host_data = HostWrapper(test_data(facts=None)) del host_data.tags del host_data.display_name del host_data.facts expected_tags = [] # initial create results = self.post(HOST_URL, host_data.data(), 201) self.assertIsNotNone(results["id"]) original_id = results["id"] host_data.tags = ["aws/new_tag_1:new_value_1", "aws/k:v"] host_data.facts = FACTS host_data.display_name = "expected_display_name" self.post(HOST_URL, host_data.data(), 200) data = self.get("%s/%s" % (HOST_URL, original_id), 200) results = HostWrapper(data["results"][0]) self.assertListEqual(results.tags, expected_tags) self.assertListEqual(results.facts, host_data.facts) self.assertEqual(results.display_name, host_data.display_name)
def test_create_host_with_empty_facts_display_name_then_update(self): # Create a host with empty facts, and display_name # then update those fields host_data = HostWrapper(test_data(facts=None)) del host_data.display_name del host_data.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) self.assertIsNotNone(created_host["id"]) original_id = created_host["id"] # Update the facts and display name host_data.facts = copy.deepcopy(FACTS) host_data.display_name = "expected_display_name" # Update the hosts self.post(HOST_URL, [host_data.data()], 207) host_lookup_results = self.get(f"{HOST_URL}/{original_id}", 200) self._validate_host(host_lookup_results["results"][0], host_data, expected_id=original_id)
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 test_create_host_with_display_name_as_None(self): host_data = HostWrapper(test_data(facts=None)) # Explicitly set the display name to None host_data.display_name = None # initial create results = self.post(HOST_URL, host_data.data(), 201) self.assertIsNotNone(results["id"]) self.assertIsNone(results["display_name"])
def _build_test_host_list(self): host_with_display_name_as_none = HostWrapper(test_data(facts=None)) host_with_display_name_as_none.insights_id = str(uuid.uuid4()) # Explicitly set the display name to None host_with_display_name_as_none.display_name = None host_without_display_name = HostWrapper(test_data(facts=None)) host_without_display_name.insights_id = str(uuid.uuid4()) # Remove the display name del host_without_display_name.display_name host_display_name_empty_str = HostWrapper(test_data(facts=None)) host_display_name_empty_str.insights_id = str(uuid.uuid4()) # Explicitly set the display name to an empty string host_display_name_empty_str.display_name = "" test_host_list = [ ("no display_name", host_without_display_name), ("display_name is None", host_with_display_name_as_none), ("display_name is empty string", host_display_name_empty_str), ] return test_host_list
def test_create_host_with_display_name_as_None(self): host_data = HostWrapper(test_data(facts=None)) # Explicitly set the display name to None host_data.display_name = None # Create the host created_host = self.post(HOST_URL, host_data.data(), 201) self.assertIsNotNone(created_host["id"]) self._validate_host(created_host, host_data, expected_id=created_host["id"], verify_tags=False)
def test_create_host_with_display_name_as_None_no_fqdn(self): """ This test should verify that the display_name is set to the id when neither the display name or fqdn is set. """ host_data = HostWrapper(test_data(facts=None)) # Explicitly set the display name to None host_data.display_name = None # Create the host created_host = self.post(HOST_URL, host_data.data(), 201) original_id = created_host["id"] host_lookup_results = self.get("%s/%s" % (HOST_URL, original_id), 200) # Explicitly set the display_name to the be id...this is expected here host_data.display_name = created_host["id"] self._validate_host(host_lookup_results["results"][0], host_data, expected_id=original_id, verify_tags=False)
def test_create_host_with_invalid_display_name(self): host_data = HostWrapper(test_data(facts=None)) invalid_display_names = ["", "a" * 201] for display_name in invalid_display_names: with self.subTest(display_name=display_name): host_data.display_name = display_name response = self.post(HOST_URL, [host_data.data()], 207) error_host = response["data"][0] self.assertEqual(error_host["status"], 400) self.verify_error_response(error_host, expected_title="Bad Request")
def create_hosts(self): hosts_to_create = [("host1", "12345"), ("host2", "54321")] host_list = [] for host in hosts_to_create: host_wrapper = HostWrapper() host_wrapper.account = ACCOUNT host_wrapper.tags = TAGS host_wrapper.display_name = host[0] host_wrapper.insights_id = host[1] host_wrapper.facts = [{ "namespace": "ns1", "facts": { "key1": "value1" } }] response_data = self.post(HOST_URL, host_wrapper.data(), 201) host_list.append(HostWrapper(response_data)) return host_list
def test_create_host_with_empty_tags_facts_display_name_then_update(self): # Create a host with empty tags, facts, and display_name # then update those fields host_data = HostWrapper(test_data(facts=None)) del host_data.tags del host_data.display_name del host_data.facts # Tags are currently ignored on create and update expected_tags = [] # Create the host results = self.post(HOST_URL, host_data.data(), 201) self.assertIsNotNone(results["id"]) original_id = results["id"] # Update the tags, facts and display name host_data.tags = ["aws/new_tag_1:new_value_1", "aws/k:v"] host_data.facts = copy.deepcopy(FACTS) host_data.display_name = "expected_display_name" # Update the hosts self.post(HOST_URL, host_data.data(), 200) host_lookup_results = self.get("%s/%s" % (HOST_URL, original_id), 200) self._validate_host(host_lookup_results["results"][0], host_data, expected_id=original_id, verify_tags=False) # Tagging is not supported at the moment. Verity the tag was ignored results = HostWrapper(host_lookup_results["results"][0]) self.assertListEqual(results.tags, expected_tags)
def test_create_host_without_display_name_and_without_fqdn(self): """ This test should verify that the display_name is set to the id when neither the display name or fqdn is set. """ host_data = HostWrapper(test_data(facts=None)) del host_data.display_name del host_data.fqdn # 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"] host_lookup_results = self.get(f"{HOST_URL}/{original_id}", 200) # Explicitly set the display_name to the be id...this is expected here host_data.display_name = created_host["id"] self._validate_host(host_lookup_results["results"][0], host_data, expected_id=original_id)
def test_create_host_without_display_name_and_no_fqdn(self): """ This test should verify that the display_name is set to the id when neither the display name or fqdn is set. """ test_host_list = self._build_test_host_list() host_fqdn_is_empty_str = HostWrapper(test_data(facts=None)) host_fqdn_is_empty_str.insights_id = str(uuid.uuid4()) host_fqdn_is_empty_str.display_name = "" host_fqdn_is_empty_str.fqdn = "" test_host_list.append(("fqdn and display_name are empty strings", host_fqdn_is_empty_str)) for (test_name, host_data) in test_host_list: with self.subTest(test_name=test_name): # 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"] host_lookup_results = self.get( "%s/%s" % (HOST_URL, original_id), 200) # Explicitly set the display_name to the be id...this is expected here host_data.display_name = created_host["id"] self._validate_host(host_lookup_results["results"][0], host_data, expected_id=original_id)
def create_hosts(self): hosts_to_create = [ ( "host1", generate_uuid(), "host1.domain.test", [ { "namespace": "NS1", "key": "key1", "value": "val1" }, { "namespace": "NS1", "key": "key2", "value": "val1" }, { "namespace": "SPECIAL", "key": "tag", "value": "ToFind" }, { "namespace": "no", "key": "key", "value": None }, ], ), ( "host2", generate_uuid(), "host1.domain.test", [ { "namespace": "NS1", "key": "key1", "value": "val1" }, { "namespace": "NS2", "key": "key2", "value": "val2" }, { "namespace": "NS3", "key": "key3", "value": "val3" }, ], ), # the same fqdn is intentional ( "host3", generate_uuid(), "host2.domain.test", [ { "namespace": "NS2", "key": "key2", "value": "val2" }, { "namespace": "NS3", "key": "key3", "value": "val3" }, { "namespace": "NS1", "key": "key3", "value": "val3" }, { "namespace": None, "key": "key4", "value": "val4" }, { "namespace": None, "key": "key5", "value": None }, ], ), ] if hasattr(self, "hosts_to_create"): self.hosts_to_create = hosts_to_create + self.hosts_to_create else: self.hosts_to_create = hosts_to_create host_list = [] mock_event_producer = MockEventProducer() for host in self.hosts_to_create: host_wrapper = HostWrapper() host_wrapper.account = ACCOUNT host_wrapper.display_name = host[0] host_wrapper.insights_id = generate_uuid() host_wrapper.rhel_machine_id = generate_uuid() host_wrapper.subscription_manager_id = generate_uuid() host_wrapper.satellite_id = generate_uuid() host_wrapper.bios_uuid = generate_uuid() host_wrapper.ip_addresses = ["10.0.0.2"] host_wrapper.fqdn = host[2] host_wrapper.mac_addresses = ["aa:bb:cc:dd:ee:ff"] host_wrapper.external_id = generate_uuid() host_wrapper.facts = [{ "namespace": "ns1", "facts": { "key1": "value1" } }] host_wrapper.tags = host[3] host_wrapper.stale_timestamp = now().isoformat() host_wrapper.reporter = "test" message = {"operation": "add_host", "data": host_wrapper.data()} with self.app.app_context(): handle_message(json.dumps(message), mock_event_producer) response_data = json.loads(mock_event_producer.event) # add facts object since it's not returned by message host_data = { **response_data["host"], "facts": message["data"]["facts"] } host_list.append(HostWrapper(host_data)) return host_list