def test_simulate_produces_event(kafka_cluster_info): consumer = KafkaConsumer(TEST_TOPIC) try: highwater = consumer.get_high_watermark() # run simulate to create a message to kafka # running following command: argv = MOCK_ARGV + ["v2_ingest_kafka_small.yaml"] print(f"running {argv}") with pytest.raises(SystemExit) as context: cli.start(argv) if context.value.code == 2: pytest.fail(cli.argparse_error(argv)) assert context.value.code == 0 messages = consumer.get_messages(expecting=2) assert len(messages) == 2 print( f"highwater: {highwater}, message offset: {messages[1].offset()}") assert ( b'{"uri": "iphone://settings/updates", "session": "111", "timestamp": 2}' ) == messages[0].value() assert ( b'{"uri": "iphone://settings/disconnect", "session": "000", "timestamp": 1}' ) == messages[1].value() # at last, prevent that we always consume the same message expected_offset = highwater + 1 assert expected_offset == messages[1].offset() finally: consumer.close()
def test_validate_fail_status_code(): with pytest.raises(SystemExit) as context: cli.start(ARGV_HTTP_VALIDATE_STAUTS_CODE_FAIL) if context.value.code == 2: pytest.fail(cli.argparse_error(ARGV_HTTP_VALIDATE_STAUTS_CODE_FAIL)) assert context.value.code == 1
def test_invalid_kafka_scenario(): argv = MOCK_ARGV + ["v2_ingest_kafka_invalid.yaml"] with pytest.raises(SystemExit) as context: cli.start(argv) if context.value.code == 2: pytest.fail(cli.argparse_error(argv)) assert context.value.code == 4
def test_validate_assertions_pass(): with pytest.raises(SystemExit) as context: cli.start(ARGV_HTTP_VALIDATE_1_OK) if context.value.code == 2: pytest.fail(cli.argparse_error(ARGV_HTTP_VALIDATE_1_OK)) assert context.value.code == 0
def test_received_no_events(monkeypatch, kafka_cluster_info): # run validate to consume a message from kafka # running following command: argv = MOCK_ARGV + ["v2_ingest_kafka_small.yaml"] print(f"running {argv}") with pytest.raises(SystemExit) as context: cli.start(argv) if context.value.code == 2: pytest.fail(cli.argparse_error(argv)) # exit code should be 1 (error) assert context.value.code == 1
def test_simulate_json_response_400(self): with vcr.use_cassette("test_simulate_json_response_400") as cassette: with self.assertRaises(SystemExit) as context: cli.start(ARGV_RESPONSE_400) assert len(cassette) == 1 r0 = cassette.requests[0] assert r0.path == "/cant_find_this" assert cassette.responses_of(r0)[0]["status"]["code"] == 404 assert cassette.all_played if context.exception.code == 2: self.fail(cli.argparse_error(ARGV_RESPONSE_400)) assert context.exception.code == 1
def test_simulate_json_response_200(self): with vcr.use_cassette("test_simulate_json_response_200") as cassette: with self.assertRaises(SystemExit) as context: cli.start(ARGV_RESPONSE_200) assert len(cassette) == 1 r1 = cassette.requests[0] assert r1.path == "/v1/actions/produce-event" assert cassette.responses_of(r1)[0]["status"]["code"] == 204 assert cassette.all_played if context.exception.code == 2: self.fail(cli.argparse_error(ARGV_RESPONSE_200)) # not all request had the expected status code (see assertions) assert context.exception.code == 0
def test_error_on_connection_timeout(monkeypatch): monkeypatch.setenv("KAFKA_BOOTSTRAP_SERVERS", "localhost:3330") argv = MOCK_ARGV + ["v2_ingest_kafka_small.yaml"] with pytest.raises(KafkaSetupError) as e: cli.start(argv)
def test_execute_a_simulation_fails(self, print_message): with self.assertRaises(SystemExit) as context: cli.start([]) self.assertEqual(context.exception.code, 2) print_message.assert_called_once()
def test_execute_a_sanitytest_fails(print_message): with pytest.raises(SystemExit) as context: cli.start([]) assert context.value.code == 2 print_message.assert_called()