def test_get_tags_count_from_host_with_null_tags(tags, mq_create_four_specific_hosts, api_get): # FIXME: Remove this test after migration to NOT NULL. created_hosts = mq_create_four_specific_hosts host_id = created_hosts[0].id update_host_in_db(host_id, tags=tags) url = build_tags_count_url(host_list_or_id=host_id) response_status, response_data = api_get(url) assert response_status == 200 assert {host_id: 0} == response_data["results"]
def _test_order_by_id_desc(inventory_config, api_get, subtests, created_hosts, specifications, order_by, order_how): for updates, expected_added_hosts in specifications: # Update hosts to they have a same modified_on timestamp, but different IDs. # 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() for added_host_index, new_id in updates: host = update_host_in_db(created_hosts[added_host_index].id, id=new_id, modified_on=new_modified_on) created_hosts[added_host_index] = serialize_db_host(host, inventory_config) # Check the order in the response against the expected order. Only indexes # are passed, because self.added_hosts values were replaced during the # update. expected_hosts = tuple(created_hosts[added_host_index] for added_host_index in expected_added_hosts) urls = (HOST_URL, build_hosts_url(created_hosts), build_system_profile_url(created_hosts)) for url in urls: with subtests.test(url=url, updates=updates): order_query_parameters = build_order_query_parameters(order_by=order_by, order_how=order_how) response_status, response_data = api_get(url, query_parameters=order_query_parameters) assert_response_status(response_status, expected_status=200) assert_host_ids_in_response(response_data, expected_hosts)
def test_delete_duplicates_last_modified(event_producer, db_create_multiple_hosts, db_get_host, inventory_config): """Test that the deletion script always keeps host with the latest 'modified_on' date""" canonical_facts = { "provider_id": generate_uuid(), "insights_id": generate_uuid(), "subscription_manager_id": generate_uuid(), "bios_uuid": generate_uuid(), "satellite_id": generate_uuid(), "fqdn": generate_random_string(), "provider_type": "aws", } host_count = 100 hosts = [minimal_db_host(canonical_facts=canonical_facts) for _ in range(host_count)] created_host_ids = [host.id for host in db_create_multiple_hosts(hosts=hosts)] updated_host = update_host_in_db(choice(created_host_ids), display_name="new-display-name") for host_id in created_host_ids: assert db_get_host(host_id) Session = _init_db(inventory_config) sessions = [Session() for _ in range(3)] with multi_session_guard(sessions): deleted_hosts_count = host_delete_duplicates_run( inventory_config, mock.Mock(), *sessions, event_producer, shutdown_handler=mock.Mock(**{"shut_down.return_value": False}), ) assert deleted_hosts_count == host_count - 1 for host_id in created_host_ids: if host_id != updated_host.id: assert not db_get_host(host_id) else: assert db_get_host(host_id)