Пример #1
0
    def test_hash_unchanged(self, cfngin_context: MockCFNginContext) -> None:
        """Test upload is skipped if the has was unchanged."""
        s3_stub = cfngin_context.add_stubber("s3")
        ssm_stub = cfngin_context.add_stubber("ssm")

        extra: ExtraFileTypeDef = {
            "name": "test",
            "content": "test",
        }
        extra_hash = calculate_hash_of_extra_files([extra])

        ssm_stub.add_response(
            "get_parameter",
            {"Parameter": {
                "Value": extra_hash
            }},
            {"Name": "hash_nameextra"},
        )

        with s3_stub as s3_stub, ssm_stub as ssm_stub:
            assert (sync_extra_files(
                cfngin_context,
                "bucket",
                extra_files=[extra],
                hash_tracking_parameter="hash_name",
            ) == [])
            s3_stub.assert_no_pending_responses()
            ssm_stub.assert_no_pending_responses()
Пример #2
0
    def test_file_reference_with_content_type(
            self, cfngin_context: MockCFNginContext) -> None:
        """Test file is uploaded with the content type."""
        s3_stub = cfngin_context.add_stubber("s3")

        s3_stub.add_response(
            "put_object",
            {},
            {
                "Bucket": "bucket",
                "Key": "test.json",
                "Body": ANY,
                "ContentType": "application/json",
            },
        )

        files: List[ExtraFileTypeDef] = [{
            "name": "test.json",
            "file": "Pipfile"
        }]

        with s3_stub as stub:
            assert sync_extra_files(cfngin_context,
                                    "bucket",
                                    extra_files=files) == ["test.json"]
            stub.assert_no_pending_responses()
Пример #3
0
    def test_file_reference(self, cfngin_context: MockCFNginContext) -> None:
        """Test file is uploaded."""
        s3_stub = cfngin_context.add_stubber("s3")

        # This isn't ideal, but needed to get the correct stubbing.
        # Stubber doesn't support 'upload_file' so we need to assume it delegates to 'put_object'.
        # https://stackoverflow.com/questions/59303423/s3-boto3-stubber-doesnt-have-mapping-for-download-file
        s3_stub.add_response(
            "put_object",
            {},
            {
                "Bucket": "bucket",
                "Key": "test",
                # Don't want to make any more assumptions about how upload_file works
                "Body": ANY,
            },
        )

        files: List[ExtraFileTypeDef] = [{"name": "test", "file": "Pipfile"}]

        with s3_stub as stub:
            assert sync_extra_files(cfngin_context,
                                    "bucket",
                                    extra_files=files) == ["test"]
            stub.assert_no_pending_responses()
Пример #4
0
    def test_yaml_content(self, cfngin_context: MockCFNginContext) -> None:
        """Test yaml content is put in s3."""
        s3_stub = cfngin_context.add_stubber("s3")

        content = {"a": 0}

        s3_stub.add_response(
            "put_object",
            {},
            {
                "Bucket": "bucket",
                "Key": "test.yaml",
                "Body": yaml.safe_dump(content).encode(),
                "ContentType": "text/yaml",
            },
        )

        files: List[ExtraFileTypeDef] = [{
            "name": "test.yaml",
            "content": content
        }]

        with s3_stub as stub:
            assert sync_extra_files(cfngin_context,
                                    "bucket",
                                    extra_files=files) == ["test.yaml"]
            stub.assert_no_pending_responses()
Пример #5
0
    def test_hash_updated(self, cfngin_context: MockCFNginContext) -> None:
        """Test extra files hash is updated."""
        s3_stub = cfngin_context.add_stubber("s3")
        ssm_stub = cfngin_context.add_stubber("ssm")

        extra: ExtraFileTypeDef = {
            "name": "test",
            "content": "test",
            "content_type": "text/plain",
        }
        extra_hash = calculate_hash_of_extra_files([extra])

        ssm_stub.add_response(
            "get_parameter",
            {"Parameter": {
                "Value": "old value"
            }},
            {"Name": "hash_nameextra"},
        )

        ssm_stub.add_response(
            "put_parameter",
            {},
            {
                "Name": "hash_nameextra",
                "Description": ANY,
                "Value": extra_hash,
                "Type": "String",
                "Overwrite": True,
            },
        )

        s3_stub.add_response(
            "put_object",
            {},
            {
                "Bucket": "bucket",
                "Key": "test",
                "Body": "test".encode(),
                "ContentType": "text/plain",
            },
        )

        with s3_stub as s3_stub, ssm_stub as ssm_stub:
            assert (sync_extra_files(
                cfngin_context,
                "bucket",
                extra_files=[extra],
                hash_tracking_parameter="hash_name",
            ) == ["test"])
            s3_stub.assert_no_pending_responses()
            ssm_stub.assert_no_pending_responses()
Пример #6
0
    def test_empty_content(self, cfngin_context: MockCFNginContext) -> None:
        """Test empty content is not uploaded."""
        s3_stub = cfngin_context.add_stubber("s3")

        with s3_stub as stub:
            assert (sync_extra_files(
                cfngin_context,
                "bucket",
                extra_files=[{
                    "name": "test.yaml",
                    "content": {}
                }],
            ) == [])
            stub.assert_no_pending_responses()