def test_validate_sp_for_missing_branch_or_repo(api_post, mocker): # Mock schema fetch get_schema_from_url_mock = mocker.patch( "lib.system_profile_validate._get_schema_from_url") get_schema_from_url_mock.side_effect = ValueError( "Schema not found at URL!") # Mock Kafka consumer fake_consumer = mocker.Mock() config = Config(RuntimeEnvironment.SERVICE) tp = TopicPartition(config.host_ingress_topic, 0) fake_consumer.poll.return_value = { tp: [ SimpleNamespace( value=json.dumps(wrap_message(minimal_host().data()))) for _ in range(5) ] } fake_consumer.offsets_for_times.return_value = { tp: SimpleNamespace(offset=0) } with pytest.raises(expected_exception=ValueError) as excinfo: validate_sp_for_branch(config, fake_consumer, repo_fork="foo", repo_branch="bar", days=3) assert "Schema not found at URL" in str(excinfo.value)
def test_validate_sp_for_branch(mocker): # Mock schema fetch get_schema_from_url_mock = mocker.patch( "lib.system_profile_validate._get_schema_from_url") mock_schema = system_profile_specification() get_schema_from_url_mock.return_value = mock_schema # Mock Kafka consumer fake_consumer = mocker.Mock() config = Config(RuntimeEnvironment.SERVICE) tp = TopicPartition(config.host_ingress_topic, 0) fake_consumer.poll.return_value = { tp: [ SimpleNamespace( value=json.dumps(wrap_message(minimal_host().data()))) for _ in range(5) ] } fake_consumer.offsets_for_times.return_value = { tp: SimpleNamespace(offset=0) } validation_results = validate_sp_for_branch(config, fake_consumer, repo_fork="test_repo", repo_branch="test_branch", days=3) assert "test_repo/test_branch" in validation_results for reporter in validation_results["test_repo/test_branch"]: assert validation_results["test_repo/test_branch"][ reporter].pass_count > 0
def test_handle_message_happy_path(mocker, event_datetime_mock, flask_app): expected_insights_id = generate_uuid() host_id = generate_uuid() timestamp_iso = event_datetime_mock.isoformat() mocker.patch( "app.queue.queue.add_host", return_value=( {"id": host_id, "insights_id": expected_insights_id}, host_id, expected_insights_id, AddHostResult.created, ), ) mock_event_producer = mocker.Mock() host = minimal_host(insights_id=expected_insights_id) message = wrap_message(host.data()) handle_message(json.dumps(message), mock_event_producer) mock_event_producer.write_event.assert_called_once() assert json.loads(mock_event_producer.write_event.call_args[0][0]) == { "timestamp": timestamp_iso, "type": "created", "host": {"id": host_id, "insights_id": expected_insights_id}, "platform_metadata": {}, "metadata": {"request_id": "-1"}, }
def test_events_sent_to_correct_topic(mocker, flask_app, secondary_topic_enabled): host_id = generate_uuid() insights_id = generate_uuid() host = minimal_host(id=host_id, insights_id=insights_id) add_host = mocker.patch( "app.queue.queue.add_host", return_value=({"id": host_id}, host_id, insights_id, AddHostResult.created) ) mock_event_producer = mocker.Mock() message = wrap_message(host.data()) handle_message(json.dumps(message), mock_event_producer) # checking events sent to both egress and events topic assert mock_event_producer.write_event.call_count == 2 assert mock_event_producer.write_event.call_args_list[0][0][3] == Topic.egress assert mock_event_producer.write_event.call_args_list[1][0][3] == Topic.events mock_event_producer.reset_mock() # for host update events add_host.return_value = ({"id": host_id}, host_id, insights_id, AddHostResult.updated) message["data"].update(stale_timestamp=(now() + timedelta(hours=26)).isoformat()) handle_message(json.dumps(message), mock_event_producer) # checking events sent to both egress and events topic assert mock_event_producer.write_event.call_count == 2 assert mock_event_producer.write_event.call_args_list[0][0][3] == Topic.egress assert mock_event_producer.write_event.call_args_list[1][0][3] == Topic.events
def _mq_create_or_update_host( host_data, platform_metadata=None, return_all_data=False, event_producer=event_producer_mock ): message = wrap_message(host_data.data(), platform_metadata=platform_metadata) handle_message(json.dumps(message), event_producer) event = json.loads(event_producer.event) if return_all_data: return event_producer_mock.key, event, event_producer.headers # add facts object since it's not returned by event message return HostWrapper({**event["host"], **{"facts": host_data.facts}})
def test_handle_message_side_effect(mocker, flask_app): fake_add_host = mocker.patch("lib.host_repository.add_host", side_effect=OperationalError( "DB Problem", "fake_param", "fake_orig")) expected_insights_id = generate_uuid() host = minimal_host(insights_id=expected_insights_id) fake_add_host.reset_mock() message = json.dumps(wrap_message(host.data())) with pytest.raises(expected_exception=OperationalError): handle_message(message, mocker.MagicMock())
def _mq_create_or_update_host( host_data, platform_metadata=None, return_all_data=False, event_producer=event_producer_mock, message_operation=add_host, ): if not platform_metadata: platform_metadata = get_platform_metadata() host_data.data()["account"] = SYSTEM_IDENTITY.get("account_number") message = wrap_message(host_data.data(), platform_metadata=platform_metadata) handle_message(json.dumps(message), event_producer, message_operation) event = json.loads(event_producer.event) if return_all_data: return event_producer_mock.key, event, event_producer.headers # add facts object since it's not returned by event message return HostWrapper({**event["host"], **{"facts": host_data.facts}})