Exemplo n.º 1
0
"""Test all services accessible for pytest_localstack.patch_fixture."""
import boto3
import botocore

import pytest_localstack

localstack = pytest_localstack.patch_fixture(scope="module", autouse=True)


def _assert_key_isinstance(result, key, type):
    assert key in result
    assert isinstance(result[key], type)


class TestBotocore(object):
    """Test service accessibility via botocore."""
    @property
    def session(self):
        if not hasattr(self, "_session"):
            self._session = botocore.session.get_session()
        return self._session

    def client(self, *args, **kwargs):
        return self.session.create_client(*args, **kwargs)

    def test_apigateway_available(self):
        client = self.client("apigateway")
        result = client.get_rest_apis()
        assert result["items"] == []

    def test_cloudformation_available(self):
Exemplo n.º 2
0
import json
import pytest_localstack

localstack = pytest_localstack.patch_fixture(
    services=["s3"],  # Limit to the AWS services you need.
    scope=
    'module',  # Use the same Localstack container for all tests in this module.
    autouse=True,  # Automatically use this fixture in tests.
)


def test_check_resource(app, client):
    res = check_resource('goatfish100', 'jltest', expiration=3600)
    assert res == True
    # expected = {'hello': 'world'}
    # assert expected == json.loads(res.get_data(as_text=True))
Exemplo n.º 3
0
import logging

import moto
import pytest
import pytest_localstack
from tests import fixtures

import lpipe

logger = logging.getLogger()
localstack = pytest_localstack.patch_fixture(
    services=["dynamodb", "kinesis", "sqs", "s3", "lambda"],
    scope="class",
    autouse=False,
    region_name=fixtures.ENV["AWS_DEFAULT_REGION"],
)


@pytest.fixture(scope="session", autouse=True)
def requests_log_level():
    for name in ("requests", "urllib3"):
        logger = logging.getLogger(name)
        logger.setLevel(logging.ERROR)
        logger.propagate = True


@pytest.fixture(scope="session")
def kinesis_streams():
    return ["test-kinesis-stream"]

Exemplo n.º 4
0
import hashlib
import logging
import os
import shutil
from pathlib import Path

import boto3
import pytest
import pytest_localstack
import requests

# noinspection PyUnresolvedReferences
localstack = pytest_localstack.patch_fixture(
    scope="module",  # Use the same Localstack container for all tests in this module.
    services=["s3"],  # Limit to the AWS services you need.
    autouse=True,  # Automatically use this fixture in tests.
    region_name="ap-southeast-1",
    container_log_level=logging.NOTSET,
    pull_image=False,
)

from sagemaker_helpers import Storage

web_source = "https://www.dropbox.com/s/iw7fohd4bykm6ti/traffic_jam_1050x700.jpg?dl=1"
fname = "traffic_jam_1050x700.jpg"
fpath = os.path.join("assertions", fname)
with open(fpath, "rb") as f:
    fcontent = f.read()
expected_hash = "36fab743f782449ed474e487edf87118"
bucket_name = "sagemaker-iaro-test"
notebook_id = "sagemaker-helpers-test"
Exemplo n.º 5
0
"""Test sync_buckets.py"""
import boto3
from sync_buckets import sync_buckets

import pytest_localstack

patch = pytest_localstack.patch_fixture(services=["s3"])
localstack_1 = pytest_localstack.session_fixture()
localstack_2 = pytest_localstack.session_fixture()


def test_sync_buckets_patch(patch):
    """Test using patch_fixture."""
    s3 = boto3.resource("s3")
    src_bucket = s3.Bucket("src-bucket")
    src_bucket.create()
    dest_bucket = s3.Bucket("dest-bucket")
    dest_bucket.create()

    src_bucket.put_object(Key="test", Body=b"foobar")

    result = sync_buckets(src_bucket, dest_bucket)
    assert result == 1

    assert len(list(dest_bucket.objects.all())) == 1

    response = dest_bucket.Object("test").get()
    data = response["Body"].read()
    assert data == b"foobar"

Exemplo n.º 6
0
import boto3
import docker
import pytest
import pytest_localstack

from constants import DEFAULT_ENTRYPOINT, DEFAULT_REQUIREMENTS
from core.services import user as user_service
from core.services import workspace as workspace_service
from core.storage import Type, _get_s3_bucket_name

SECOND = 1000000000


localstack = pytest_localstack.patch_fixture(services=["s3", "events"],
                                             scope='session',
                                             autouse=True,
                                             localstack_version='0.11.4')


@pytest.fixture(scope='session', autouse=True)
def lambda_proxy_env(localstack):
    """
    We use LAMBDA_PROXY_NAME and LAMBDA_PROXY_ARN while creating an event
    """
    os_back = copy.deepcopy(os.environ)

    os.environ['LAMBDA_PROXY_NAME'] = 'proxy_lambda_name'
    os.environ['LAMBDA_PROXY_ARN'] = 'proxy_lambda_arn'

    yield
"""Test all services accessible for pytest_localstack.patch_fixture."""
import boto3
import botocore

import pytest_localstack

localstack = pytest_localstack.patch_fixture(scope="module", autouse=True)


def _assert_key_isinstance(result, key, type):
    assert key in result
    assert isinstance(result[key], type)


class TestBotocore(object):
    """Test service accessibility via botocore."""

    @property
    def session(self):
        if not hasattr(self, "_session"):
            self._session = botocore.session.get_session()
        return self._session

    def client(self, *args, **kwargs):
        return self.session.create_client(*args, **kwargs)

    def test_apigateway_available(self):
        client = self.client("apigateway")
        result = client.get_rest_apis()
        assert result["items"] == []
Exemplo n.º 8
0
"""Test examples from the README."""
import boto3

import pytest

import pytest_localstack

patch_s3 = pytest_localstack.patch_fixture(services=["s3"])  # type: ignore


@pytest.mark.usefixtures("patch_s3")
def test_s3_bucket_creation():
    """Test S3 bucket creation with patch fixture."""
    s3 = boto3.resource("s3")  # Will use Localstack
    assert len(list(s3.buckets.all())) == 0
    bucket = s3.Bucket("foobar")
    bucket.create()
Exemplo n.º 9
0
import pytest
import pytest_localstack
from tests import fixtures

import boto3_fixtures as b3f

logger = logging.getLogger()

stack_config = {
    "services": ["dynamodb", "kinesis", "sqs", "s3", "lambda", "sns"],
    "scope": "class",
    "autouse": False,
    "region_name": fixtures.ENV["AWS_DEFAULT_REGION"],
}

localstack = pytest_localstack.patch_fixture(**stack_config)
moto = b3f.contrib.pytest.moto_fixture(**stack_config)

sqs = b3f.contrib.pytest.service_fixture("sqs", scope="class", queues=fixtures.SQS)
kinesis = b3f.contrib.pytest.service_fixture(
    "kinesis", scope="class", streams=fixtures.KINESIS,
)
dynamodb = b3f.contrib.pytest.service_fixture(
    "dynamodb", scope="class", tables=fixtures.DYNAMODB,
)
s3 = b3f.contrib.pytest.service_fixture("s3", scope="class", buckets=fixtures.S3)
lam = b3f.contrib.pytest.service_fixture(
    "lambda", scope="class", lambdas=fixtures.LAMBDA,
)
sns = b3f.contrib.pytest.service_fixture("sns", scope="class", topics=fixtures.SNS)