示例#1
0
    def test_must_log_on_download_exception(self, tempfilemock, botomock):

        s3_mock = Mock()
        botomock.client.return_value = s3_mock

        fp_mock = Mock()
        tempfilemock.TemporaryFile.return_value.__enter__.return_value = fp_mock  # mocking context manager
        s3_mock.download_fileobj.side_effect = botocore.exceptions.ClientError(
            {"Error": {}}, "download_file")

        with self.assertRaises(botocore.exceptions.ClientError):
            SwaggerReader._download_from_s3(self.bucket, self.key)

            fp_mock.read.assert_not_called()
示例#2
0
    def test_must_fail_on_download_from_s3(self, tempfilemock, botomock):
        s3_mock = Mock()
        botomock.client.return_value = s3_mock

        fp_mock = Mock()
        tempfilemock.TemporaryFile.return_value.__enter__.return_value = fp_mock  # mocking context manager
        s3_mock.download_fileobj.side_effect = botocore.exceptions.ClientError(
            {"Error": {}}, "download_file")

        with self.assertRaises(Exception) as cm:
            SwaggerReader._download_from_s3(self.bucket, self.key)
        self.assertIn(cm.exception.__class__,
                      (botocore.exceptions.NoCredentialsError,
                       botocore.exceptions.ClientError))
示例#3
0
    def test_must_skip_non_s3_dictionaries(self, yaml_parse_mock):

        location = {"some": "value"}

        reader = SwaggerReader(definition_uri=location)
        reader._download_from_s3 = Mock()

        actual = reader._download_swagger(location)

        self.assertIsNone(actual)
        reader._download_from_s3.assert_not_called()
        yaml_parse_mock.assert_not_called()
示例#4
0
    def test_must_work_without_object_version_id(self, tempfilemock, botomock):

        s3_mock = Mock()
        botomock.client.return_value = s3_mock

        fp_mock = Mock()
        tempfilemock.TemporaryFile.return_value.__enter__.return_value = fp_mock  # mocking context manager

        expected = "data from file"
        fp_mock.read.return_value = expected

        actual = SwaggerReader._download_from_s3(self.bucket, self.key)
        self.assertEquals(actual, expected)

        s3_mock.download_fileobj.assert_called_with(self.bucket,
                                                    self.key,
                                                    fp_mock,
                                                    ExtraArgs={})
示例#5
0
    def test_must_download_from_s3_for_s3_locations(self, yaml_parse_mock):
        location = {
            "Bucket": "mybucket",
            "Key": "swagger.yaml",
            "Version": "versionId"
        }
        swagger_str = "some swagger str"
        expected = "some data"

        reader = SwaggerReader(definition_uri=location)
        reader._download_from_s3 = Mock()
        reader._download_from_s3.return_value = swagger_str
        yaml_parse_mock.return_value = expected

        actual = reader._download_swagger(location)

        self.assertEquals(actual, expected)
        reader._download_from_s3.assert_called_with(location["Bucket"],
                                                    location["Key"],
                                                    location["Version"])
        yaml_parse_mock.assert_called_with(swagger_str)
示例#6
0
    def test_must_download_file_from_s3(self, tempfilemock, botomock):

        s3_mock = Mock()
        botomock.client.return_value = s3_mock

        fp_mock = Mock()
        tempfilemock.TemporaryFile.return_value.__enter__.return_value = fp_mock  # mocking context manager

        expected = "data from file"
        fp_mock.read.return_value = expected

        actual = SwaggerReader._download_from_s3(self.bucket, self.key,
                                                 self.version)
        self.assertEquals(actual, expected)

        s3_mock.download_fileobj.assert_called_with(
            self.bucket,
            self.key,
            fp_mock,
            ExtraArgs={"VersionId": self.version})

        fp_mock.seek.assert_called_with(
            0)  # make sure we seek the file before reading
        fp_mock.read.assert_called_with()