def test_ignore_culled_host_on_update_by_elevated_id( api_create_or_update_host): # Culled host host = minimal_host(insights_id=generate_uuid(), stale_timestamp=(now() - timedelta(weeks=3)).isoformat()) # Create the host multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response) # Update the host host.ip_addresses = ["10.10.0.2"] multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) update_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(update_host_response) assert create_host_response["host"]["id"] != update_host_response["host"][ "id"]
def test_ignore_culled_host_on_update_by_canonical_facts( api_create_or_update_host): # Culled host host = minimal_host(fqdn="my awesome fqdn", stale_timestamp=(now() - timedelta(weeks=3)).isoformat()) # Create the host multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response) # Update the host multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) update_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(update_host_response) assert create_host_response["host"]["id"] != update_host_response["host"][ "id"]
def test_create_host_update_with_same_insights_id_and_different_canonical_facts( api_create_or_update_host, api_get): original_insights_id = generate_uuid() host = minimal_host( insights_id=original_insights_id, rhel_machine_id=generate_uuid(), subscription_manager_id=generate_uuid(), satellite_id=generate_uuid(), bios_uuid=generate_uuid(), fqdn="original_fqdn", mac_addresses=["aa:bb:cc:dd:ee:ff"], external_id="abcdef", ) # Create the host multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response) created_host_id = create_host_response["host"]["id"] # Change the canonical facts except for the insights_id host.rhel_machine_id = generate_uuid() host.ip_addresses = ["192.168.1.44", "10.0.0.2"] host.subscription_manager_id = generate_uuid() host.satellite_id = generate_uuid() host.bios_uuid = generate_uuid() host.fqdn = "expected_fqdn" host.mac_addresses = ["ff:ee:dd:cc:bb:aa"] host.external_id = "fedcba" host.facts = [{"namespace": "ns1", "facts": {"newkey": "newvalue"}}] # Update the host multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) update_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_updated(create_host_response, update_host_response) # Retrieve the host using the id that we first received response_status, response_data = api_get(f"{HOST_URL}/{created_host_id}") assert_response_status(response_status, 200) host_response = get_host_from_response(response_data) assert_host_data(actual_host=host_response, expected_host=host, expected_id=created_host_id)
def test_create_and_update_multiple_hosts_with_different_accounts( api_create_or_update_host, monkeypatch): monkeypatch.setenv("INVENTORY_SHARED_SECRET", SHARED_SECRET) host1 = minimal_host(display_name="host1", account="111111", ip_addresses=["10.0.0.1"], rhel_machine_id=generate_uuid()) host2 = minimal_host(display_name="host2", account="222222", ip_addresses=["10.0.0.2"], rhel_machine_id=generate_uuid()) host_list = [host1, host2] # Create the host multi_response_status, multi_response_data = api_create_or_update_host( host_list, auth_type="token") assert_response_status(multi_response_status, 207) assert len(host_list) == len(multi_response_data["data"]) assert multi_response_data["total"] == len(multi_response_data["data"]) assert multi_response_status, multi_response_data["errors"] == 0 for i, host in enumerate(host_list): create_host_response = get_host_from_multi_response( multi_response_data, host_index=i) assert_host_was_created(create_host_response) host_list[i].id = create_host_response["host"]["id"] host_list[0].bios_uuid = generate_uuid() host_list[0].display_name = "fred" host_list[1].bios_uuid = generate_uuid() host_list[1].display_name = "barney" # Update the host multi_response_status, multi_response_data = api_create_or_update_host( host_list, auth_type="token") assert_response_status(multi_response_status, 207) for i, host in enumerate(host_list): update_host_response = get_host_from_multi_response( multi_response_data, host_index=i) assert_host_response_status(update_host_response, expected_status=200) assert_host_data(actual_host=update_host_response["host"], expected_host=host_list[i], expected_id=host_list[i].id)
def test_create_and_update(api_create_or_update_host, api_get): host = minimal_host(facts=FACTS) multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response) assert_host_data(actual_host=create_host_response["host"], expected_host=host) created_host_id = create_host_response["host"]["id"] host.facts = copy.deepcopy(FACTS) # Replace facts under the first namespace host.facts[0]["facts"] = {"newkey1": "newvalue1"} # Add a new set of facts under a new namespace host.facts.append({"namespace": "ns2", "facts": {"key2": "value2"}}) # Add a new canonical fact host.rhel_machine_id = generate_uuid() host.ip_addresses = ["10.10.0.1", "10.0.0.2", "fe80::d46b:2807:f258:c319"] host.mac_addresses = ["c2:00:d0:c8:61:01"] host.external_id = "i-05d2313e6b9a42b16" host.insights_id = generate_uuid() multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) update_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_updated(create_host_response, update_host_response) assert_host_data(actual_host=update_host_response["host"], expected_host=host) response_status, response_data = api_get(f"{HOST_URL}/{created_host_id}") assert_response_status(response_status, 200) host_response = get_host_from_response(response_data) assert_host_data(actual_host=host_response, expected_host=host, expected_id=created_host_id)
def test_match_host_by_elevated_id_performance(mocker, api_create_or_update_host): canonical_fact_host_query_mock = mocker.patch( "lib.host_repository.canonical_fact_host_query", wraps=canonical_fact_host_query) canonical_facts_host_query_mock = mocker.patch( "lib.host_repository.canonical_facts_host_query", wraps=canonical_facts_host_query) subscription_manager_id = generate_uuid() host = minimal_host(subscription_manager_id=subscription_manager_id) multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response) # Create a host with Subscription Manager ID insights_id = generate_uuid() host = minimal_host(insights_id=insights_id, subscription_manager_id=subscription_manager_id) mocker.resetall() # Update a host with Insights ID and Subscription Manager ID multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) update_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_updated(create_host_response, update_host_response) expected_calls = ( mocker.call(ACCOUNT, "insights_id", insights_id), mocker.call(ACCOUNT, "subscription_manager_id", subscription_manager_id), ) canonical_fact_host_query_mock.assert_has_calls(expected_calls) assert canonical_fact_host_query_mock.call_count == len(expected_calls) canonical_facts_host_query_mock.assert_not_called()
def test_create_host_with_system_profile_sap_fact(api_create_or_update_host, api_get): system_profile = valid_system_profile() system_profile["sap_system"] = True host = minimal_host(system_profile=system_profile) # Create the host multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response) created_host = create_host_response["host"] response_status, response_data = api_get( f"{HOST_URL}/{created_host['id']}/system_profile") assert_response_status(response_status, 200) host_response = get_host_from_response(response_data) assert host_response["system_profile"] == host.system_profile assert host_response["system_profile"]["sap_system"] == system_profile[ "sap_system"]
def test_get_system_profile_of_host_that_does_not_have_system_profile( api_create_or_update_host, api_get): host = minimal_host() # Create the host multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response) created_host = create_host_response["host"] # verify system_profile is not included assert "system_profile" not in created_host response_status, response_data = api_get( f"{HOST_URL}/{created_host['id']}/system_profile") assert_response_status(response_status, 200) host_response = get_host_from_response(response_data) assert host_response["id"] == created_host["id"] assert host_response["system_profile"] == {}
def test_create_host_with_system_profile_with_different_cloud_providers( api_create_or_update_host, api_get, cloud_provider): host = minimal_host(system_profile={"cloud_provider": cloud_provider}) # Create the host multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response) created_host = create_host_response["host"] # verify system_profile is not included assert "system_profile" not in created_host response_status, response_data = api_get( f"{HOST_URL}/{created_host['id']}/system_profile") assert_response_status(response_status, 200) host_response = get_host_from_response(response_data) assert host_response["id"] == created_host["id"] assert host_response["system_profile"] == host.system_profile
def test_create_host_with_system_profile_and_query_with_branch_id( api_create_or_update_host, api_get): host = minimal_host(system_profile=valid_system_profile()) # Create the host multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response) created_host = create_host_response["host"] # verify system_profile is not included assert "system_profile" not in created_host response_status, response_data = api_get( f"{HOST_URL}/{created_host['id']}/system_profile", query_parameters={"branch_id": 1234}) assert_response_status(response_status, 200) host_response = get_host_from_response(response_data) assert host_response["id"] == created_host["id"] assert host_response["system_profile"] == host.system_profile
def test_create_host_without_display_name_and_without_fqdn( api_create_or_update_host, api_get): """ This test should verify that the display_name is set to the id when neither the display name or fqdn is set. """ host = minimal_host() del host.display_name del host.fqdn # Create the host multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response) created_host_id = create_host_response["host"]["id"] response_status, response_data = api_get(f"{HOST_URL}/{created_host_id}") assert_response_status(response_status, 200) host_response = get_host_from_response(response_data) assert_host_data(actual_host=host_response, expected_host=host, expected_id=created_host_id)
def test_create_host_without_display_name_and_with_fqdn( api_create_or_update_host, api_get): """ 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 = minimal_host(fqdn=expected_display_name) del host.display_name multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response) created_host_id = create_host_response["host"]["id"] response_status, response_data = api_get(f"{HOST_URL}/{created_host_id}") assert_response_status(response_status, 200) host_response = get_host_from_response(response_data) host.display_name = expected_display_name assert_host_data(actual_host=host_response, expected_host=host, expected_id=created_host_id)
def test_create_host_with_20_byte_mac_address(api_create_or_update_host, api_get): system_profile = { "network_interfaces": [{ "mac_address": "00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22:33" }] } host = minimal_host(system_profile=system_profile) multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response) created_host_id = create_host_response["host"]["id"] response_status, response_data = api_get(f"{HOST_URL}/{created_host_id}") assert_response_status(response_status, 200) host_response = get_host_from_response(response_data) assert_host_data(actual_host=host_response, expected_host=host, expected_id=created_host_id)
def test_create_host_with_empty_facts_display_name_then_update( api_create_or_update_host, api_get): # Create a host with empty facts, and display_name # then update those fields host = minimal_host() del host.display_name del host.facts # Create the host multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response) created_host_id = create_host_response["host"]["id"] # Update the facts and display name host.facts = copy.deepcopy(FACTS) host.display_name = "expected_display_name" # Update the hosts multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) update_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_updated(create_host_response, update_host_response) response_status, response_data = api_get(f"{HOST_URL}/{created_host_id}") assert_response_status(response_status, 200) host_response = get_host_from_response(response_data) assert_host_data(actual_host=host_response, expected_host=host, expected_id=created_host_id)
def test_create_with_branch_id(api_create_or_update_host): host = minimal_host() multi_response_status, multi_response_data = api_create_or_update_host( [host], query_parameters={"branch_id": "1234"}) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response)
def test_always_update_stale_timestamp_from_next_reporter( api_create_or_update_host, db_get_host, new_stale_timestamp, new_reporter): old_stale_timestamp = now() + timedelta(days=2) old_reporter = "old reporter" host = minimal_host(stale_timestamp=old_stale_timestamp.isoformat(), reporter=old_reporter) multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response) created_host_id = create_host_response["host"]["id"] retrieved_host = db_get_host(created_host_id) assert old_stale_timestamp == retrieved_host.stale_timestamp assert old_reporter == retrieved_host.reporter host.stale_timestamp = new_stale_timestamp.isoformat() host.reporter = new_reporter multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) update_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_updated(create_host_response, update_host_response) retrieved_host = db_get_host(created_host_id) assert new_stale_timestamp == retrieved_host.stale_timestamp assert new_reporter == retrieved_host.reporter
def test_create_host_without_ansible_host_then_update( api_create_or_update_host, api_get, ansible_host): # Create a host without ansible_host field # then update those fields host = minimal_host() del host.ansible_host # Create the host multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response) created_host_id = create_host_response["host"]["id"] host.ansible_host = ansible_host # Update the hosts multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) update_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_updated(create_host_response, update_host_response) response_status, response_data = api_get(f"{HOST_URL}/{created_host_id}") assert_response_status(response_status, 200) host_response = get_host_from_response(response_data) assert_host_data(actual_host=host_response, expected_host=host, expected_id=created_host_id)
def test_create_host_with_valid_mac_address(api_create_or_update_host, valid_mac_array): host = minimal_host(insights_id=generate_uuid(), mac_addresses=valid_mac_array) multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response(multi_response_data) assert_host_was_created(create_host_response)
def test_create_host_with_invalid_stale_timestamp(api_create_or_update_host): host = minimal_host(stale_timestamp="not a timestamp") multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) host_response = get_host_from_multi_response(multi_response_data) assert_error_response(host_response, expected_title="Bad Request", expected_status=400)
def test_create_host_with_empty_json_key_in_facts(api_create_or_update_host, facts): host = minimal_host(facts=facts) multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) host_response = get_host_from_multi_response(multi_response_data) assert_error_response(host_response, expected_title="Bad Request", expected_status=400)
def test_create_host_with_empty_json_key_in_system_profile( api_create_or_update_host, sample): host = minimal_host(system_profile=sample) multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) host_response = get_host_from_multi_response(multi_response_data) assert_error_response(host_response, expected_title="Bad Request", expected_status=400)
def test_create_host_with_empty_culling_fields(api_create_or_update_host, culling_fields): host = minimal_host(**{field: "" for field in culling_fields}) multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) host_response = get_host_from_multi_response(multi_response_data) assert_error_response(host_response, expected_title="Bad Request", expected_status=400)
def test_create_host_with_invalid_external_id(api_create_or_update_host, invalid_external_id): host = minimal_host(external_id=invalid_external_id) multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) host_response = get_host_from_multi_response(multi_response_data) assert_error_response(host_response, expected_title="Bad Request", expected_status=400)
def test_create_host_with_system_profile_with_invalid_data( api_create_or_update_host, api_get, system_profile): host = minimal_host(system_profile=system_profile) # Create the host multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) host_response = get_host_from_multi_response(multi_response_data) assert_error_response(host_response, expected_title="Bad Request", expected_status=400)
def test_create_host_with_invalid_mac_address(api_create_or_update_host, invalid_mac_array): host = minimal_host(insights_id=generate_uuid(), mac_addresses=invalid_mac_array) multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) host_response = get_host_from_multi_response(multi_response_data) assert_error_response(host_response, expected_title="Bad Request", expected_status=400)
def test_create_host_with_invalid_uuid_field_values(api_create_or_update_host, uuid_field): host = minimal_host() setattr(host, uuid_field, "notauuid") multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) host_response = get_host_from_multi_response(multi_response_data) assert_error_response(host_response, expected_title="Bad Request", expected_status=400)
def test_create_host_with_invalid_account(api_create_or_update_host, account): host = minimal_host(account=account) multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) host_response = get_host_from_multi_response(multi_response_data) assert_error_response( host_response, expected_title="Bad Request", expected_detail="{'account': ['Length must be between 1 and 10.']}", expected_status=400, )
def test_create_host_with_mismatched_account_numbers( api_create_or_update_host): host = minimal_host(account=ACCOUNT[::-1]) multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) host_response = get_host_from_multi_response(multi_response_data) assert_error_response( host_response, expected_title="Invalid request", expected_detail= "The account number associated with the user does not match the account number associated " "with the host", expected_status=400, )
def test_create_host_with_too_long_mac_address(api_create_or_update_host): system_profile = { "network_interfaces": [{ "mac_address": "00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22:33:44" }] } host = minimal_host(system_profile=system_profile) multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) host_response = get_host_from_multi_response(multi_response_data) assert_error_response(host_response, expected_title="Bad Request", expected_status=400)
def test_get_system_profile_of_multiple_hosts(api_create_or_update_host, api_get): host_id_list = [] expected_system_profiles = [] for i in range(2): system_profile = valid_system_profile() system_profile["number_of_cpus"] = i host = minimal_host(ip_addresses=[f"10.0.0.{i}"], system_profile=system_profile) multi_response_status, multi_response_data = api_create_or_update_host( [host]) assert_response_status(multi_response_status, 207) create_host_response = get_host_from_multi_response( multi_response_data) assert_host_was_created(create_host_response) created_host_id = create_host_response["host"]["id"] host_id_list.append(created_host_id) expected_system_profiles.append({ "id": created_host_id, "system_profile": host.system_profile }) url_host_id_list = ",".join(host_id_list) test_url = f"{HOST_URL}/{url_host_id_list}/system_profile" response_status, response_data = api_get(test_url) assert_response_status(response_status, 200) assert len(expected_system_profiles) == len(response_data["results"]) for expected_system_profile in expected_system_profiles: assert expected_system_profile in response_data["results"]