Exemplo n.º 1
0
    def test_process_messages(self):
        """Test the process_message function."""
        def _expected_success_path(msg, test, confirmation_mock):
            confirmation_mock.assert_called()

        def _expected_fail_path(msg, test, confirmation_mock):
            confirmation_mock.assert_not_called()

        report_meta_1 = {
            "schema_name": "test_schema",
            "manifest_id": "1",
            "provider_uuid": uuid.uuid4(),
            "provider_type": "OCP",
            "compression": "UNCOMPRESSED",
            "file": "/path/to/file.csv",
            "date": datetime.today(),
        }
        summarize_manifest_uuid = uuid.uuid4()
        test_matrix = [
            {
                "test_value": {
                    "request_id": "1",
                    "account": "10001",
                    "category": "tar",
                    "metadata": {
                        "reporter": "",
                        "stale_timestamp": "0001-01-01T00:00:00Z"
                    },
                },
                "handle_message_returns":
                (msg_handler.SUCCESS_CONFIRM_STATUS, [report_meta_1]),
                "summarize_manifest_returns":
                summarize_manifest_uuid,
                "expected_fn":
                _expected_success_path,
            },
            {
                "test_value": {
                    "request_id": "1",
                    "account": "10001",
                    "category": "tar",
                    "metadata": {
                        "reporter": "",
                        "stale_timestamp": "0001-01-01T00:00:00Z"
                    },
                },
                "handle_message_returns":
                (msg_handler.FAILURE_CONFIRM_STATUS, None),
                "summarize_manifest_returns":
                summarize_manifest_uuid,
                "expected_fn":
                _expected_success_path,
            },
            {
                "test_value": {
                    "request_id": "1",
                    "account": "10001",
                    "category": "tar",
                    "metadata": {
                        "reporter": "",
                        "stale_timestamp": "0001-01-01T00:00:00Z"
                    },
                },
                "handle_message_returns": (None, None),
                "summarize_manifest_returns": summarize_manifest_uuid,
                "expected_fn": _expected_fail_path,
            },
            {
                "test_value": {
                    "request_id": "1",
                    "account": "10001",
                    "category": "tar",
                    "metadata": {
                        "reporter": "",
                        "stale_timestamp": "0001-01-01T00:00:00Z"
                    },
                },
                "handle_message_returns": (None, [report_meta_1]),
                "summarize_manifest_returns": summarize_manifest_uuid,
                "expected_fn": _expected_fail_path,
            },
        ]
        for test in test_matrix:
            with self.subTest(test=test):
                msg = MockMessage(
                    topic="platform.upload.hccm",
                    offset=5,
                    url="https://insights-quarantine.s3.amazonaws.com/myfile",
                    value_dict=test.get("test_value"),
                )
                with patch("masu.external.kafka_msg_handler.handle_message",
                           return_value=test.get("handle_message_returns")):
                    with patch(
                            "masu.external.kafka_msg_handler.summarize_manifest",
                            return_value=test.get(
                                "summarize_manifest_returns"),
                    ):
                        with patch(
                                "masu.external.kafka_msg_handler.process_report"
                        ):
                            with patch(
                                    "masu.external.kafka_msg_handler.send_confirmation"
                            ) as confirmation_mock:
                                msg_handler.process_messages(msg)
                                test.get("expected_fn")(msg, test,
                                                        confirmation_mock)
Exemplo n.º 2
0
    def test_process_messages(self):
        """Test the process_message function."""
        def _expected_success_path(msg, test, confirmation_mock):
            confirmation_mock.mock.assert_called()

        def _expected_fail_path(msg, test, confirmation_mock):
            confirmation_mock.mock.assert_not_called()

        report_meta_1 = {
            "schema_name": "test_schema",
            "manifest_id": "1",
            "provider_uuid": uuid.uuid4(),
            "provider_type": "OCP",
            "compression": "UNCOMPRESSED",
            "file": "/path/to/file.csv",
        }
        summarize_manifest_uuid = uuid.uuid4()
        test_matrix = [
            {
                "value":
                json.dumps({
                    "request_id": "1",
                    "account": "10001",
                    "category": "tar",
                    "metadata": {
                        "reporter": "",
                        "stale_timestamp": "0001-01-01T00:00:00Z"
                    },
                }),
                "handle_message_returns":
                (msg_handler.SUCCESS_CONFIRM_STATUS, [report_meta_1]),
                "summarize_manifest_returns":
                summarize_manifest_uuid,
                "expected_fn":
                _expected_success_path,
            },
            {
                "value":
                json.dumps({
                    "request_id": "1",
                    "account": "10001",
                    "category": "tar",
                    "metadata": {
                        "reporter": "",
                        "stale_timestamp": "0001-01-01T00:00:00Z"
                    },
                }),
                "handle_message_returns":
                (msg_handler.FAILURE_CONFIRM_STATUS, None),
                "summarize_manifest_returns":
                summarize_manifest_uuid,
                "expected_fn":
                _expected_success_path,
            },
            {
                "value":
                json.dumps({
                    "request_id": "1",
                    "account": "10001",
                    "category": "tar",
                    "metadata": {
                        "reporter": "",
                        "stale_timestamp": "0001-01-01T00:00:00Z"
                    },
                }),
                "handle_message_returns": (None, None),
                "summarize_manifest_returns":
                summarize_manifest_uuid,
                "expected_fn":
                _expected_fail_path,
            },
            {
                "value":
                json.dumps({
                    "request_id": "1",
                    "account": "10001",
                    "category": "tar",
                    "metadata": {
                        "reporter": "",
                        "stale_timestamp": "0001-01-01T00:00:00Z"
                    },
                }),
                "handle_message_returns": (None, [report_meta_1]),
                "summarize_manifest_returns":
                summarize_manifest_uuid,
                "expected_fn":
                _expected_fail_path,
            },
        ]
        for test in test_matrix:
            msg = ConsumerRecord(
                topic="platform.upload.hccm",
                offset=5,
                url="https://insights-quarantine.s3.amazonaws.com/myfile",
                value=bytes(test.get("value"), encoding="utf-8"),
            )
            run_loop = asyncio.new_event_loop()
            with patch("masu.external.kafka_msg_handler.handle_message",
                       return_value=test.get("handle_message_returns")):
                with patch(
                        "masu.external.kafka_msg_handler.summarize_manifest",
                        return_value=test.get("summarize_manifest_returns"),
                ):
                    with patch(
                            "masu.external.kafka_msg_handler.process_report"):
                        with patch(
                                "masu.external.kafka_msg_handler.send_confirmation",
                                new=AsyncMock()) as confirmation_mock:
                            run_loop.run_until_complete(
                                msg_handler.process_messages(msg, run_loop))
                            test.get("expected_fn")(msg, test,
                                                    confirmation_mock)