def test_create_and_update_multiple_hosts_with_different_accounts(self): with test.support.EnvironmentVarGuard() as env: env.set("INVENTORY_SHARED_SECRET", SHARED_SECRET) facts = None host1 = HostWrapper(test_data(display_name="host1", facts=facts)) host1.account = "111111" host1.ip_addresses = ["10.0.0.1"] host1.rhel_machine_id = str(uuid.uuid4()) host2 = HostWrapper(test_data(display_name="host2", facts=facts)) host2.account = "222222" host2.ip_addresses = ["10.0.0.2"] host2.rhel_machine_id = str(uuid.uuid4()) host_list = [host1.data(), host2.data()] # Create the host response = self.post(HOST_URL, host_list, 207) self.assertEqual(len(host_list), len(response["data"])) self.assertEqual(response["total"], len(response["data"])) self.assertEqual(response["errors"], 0) for host in response["data"]: self.assertEqual(host["status"], 201) host_list[0]["id"] = response["data"][0]["host"]["id"] host_list[0]["bios_uuid"] = str(uuid.uuid4()) host_list[0]["display_name"] = "fred" host_list[1]["id"] = response["data"][1]["host"]["id"] host_list[1]["bios_uuid"] = str(uuid.uuid4()) host_list[1]["display_name"] = "barney" # Update the host updated_host = self.post(HOST_URL, host_list, 207) self.assertEqual(updated_host["errors"], 0) i = 0 for host in updated_host["data"]: self.assertEqual(host["status"], 200) expected_host = HostWrapper(host_list[i]) self._validate_host(host["host"], expected_host, expected_id=expected_host.id) i += 1
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_and_update_multiple_hosts_with_account_mismatch(self): """ Attempt to create multiple hosts, one host has the wrong account number. Verify this causes an error response to be returned. """ facts = None host1 = HostWrapper(test_data(display_name="host1", facts=facts)) host1.ip_addresses = ["10.0.0.1"] host1.rhel_machine_id = generate_uuid() host2 = HostWrapper(test_data(display_name="host2", facts=facts)) # Set the account number to the wrong account for this request host2.account = "222222" host2.ip_addresses = ["10.0.0.2"] host2.rhel_machine_id = generate_uuid() host_list = [host1.data(), host2.data()] # Create the host created_host = self.post(HOST_URL, host_list, 207) self.assertEqual(len(host_list), len(created_host["data"])) self.assertEqual(created_host["errors"], 1) self.assertEqual(created_host["data"][0]["status"], 201) self.assertEqual(created_host["data"][1]["status"], 400)
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 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_mismatched_account_numbers(self): host_data = HostWrapper(test_data(facts=None)) host_data.account = ACCOUNT[::-1] response_data = self.post(HOST_URL, host_data.data(), 400) assert "Invalid request" in response_data["title"] assert "status" in response_data assert "detail" in response_data assert "type" in response_data
def test_create_host_with_mismatched_account_numbers(self): host_data = HostWrapper(test_data(facts=None)) host_data.account = ACCOUNT[::-1] response_data = self.post(HOST_URL, [host_data.data()], 207) self._verify_host_status(response_data, 0, 400) response_data = response_data["data"][0] self.verify_error_response( response_data, expected_title="Invalid request", expected_detail="The account number associated with the user does not match the account number associated " "with the host", )
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_mismatched_account_numbers(self): host_data = HostWrapper(test_data(facts=None)) host_data.account = ACCOUNT[::-1] # FIXME: Verify response? response_data = self.post(HOST_URL, host_data.data(), 400)
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