Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split AWS tests into unit and integration tests #237

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/changes_1.1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ n/a
* #220: Changed default ports in the external database configuration.
* #221: Changed wording in the main configuration notebook, as suggested by PM.
* #66: Used a non-root user to run Jupyter in the Docker Image ai-lab
* #149: Split AWS tests
19 changes: 13 additions & 6 deletions exasol/ds/sandbox/lib/ansible/ansible_runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging

from pathlib import Path
Expand All @@ -16,9 +17,16 @@
LOG = get_status_logger(LogType.ANSIBLE)


class DurationHandler(logging.StreamHandler):
def __init__(self):
super().__init__()
self.setFormatter(logging.Formatter('%(message)s'))


class AnsibleRunner:
"""
Encapsulates invocation ansible access. It creates the inventory file, writing the host info, during run.
Encapsulates invocation ansible access. It creates the inventory file,
writing the host info, during run.
"""
def __init__(self, ansible_access: AnsibleAccess, work_dir: Path):
self._ansible_access = ansible_access
Expand All @@ -27,14 +35,13 @@ def __init__(self, ansible_access: AnsibleAccess, work_dir: Path):

@classmethod
def duration_logger(cls) -> logging.Logger:
def handler():
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(message)s'))
return handler
logger = logging.getLogger(f"{__name__}:{cls.__name__}")
for h in logger.handlers:
if isinstance(h, DurationHandler):
return logger
logger.setLevel(logging.DEBUG)
logger.propagate = False
logger.addHandler(handler())
logger.addHandler(DurationHandler())
return logger

def event_handler(self, event: AnsibleEvent) -> bool:
Expand Down
35 changes: 35 additions & 0 deletions test/aws/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest

from exasol.ds.sandbox.lib.vm_bucket.vm_dss_bucket import create_vm_bucket_cf_template
from exasol.ds.sandbox.lib.vm_bucket.vm_dss_bucket_waf import get_cloudformation_template
from test.aws.templates import (
ec2_template,
vm_bucket_template,
waf_template,
DEFAULT_ASSET_ID,
TEST_DUMMY_AMI_ID,
)

@pytest.fixture
def default_asset_id():
return DEFAULT_ASSET_ID


@pytest.fixture()
def test_dummy_ami_id():
return TEST_DUMMY_AMI_ID


@pytest.fixture
def vm_bucket_cloudformation_yml():
return vm_bucket_template()


@pytest.fixture
def ec2_cloudformation_yml():
return ec2_template()


@pytest.fixture
def waf_cloudformation_yml():
return waf_template()
File renamed without changes.
4 changes: 2 additions & 2 deletions test/aws_mock_data.py → test/aws/mock_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
from exasol.ds.sandbox.lib.aws_access.snapshot import Snapshot
from exasol.ds.sandbox.lib.aws_access.stack_resource import StackResource
from exasol.ds.sandbox.lib.tags import create_default_asset_tag
from test.conftest import DEFAULT_ASSET_ID
from test.aws.templates import DEFAULT_ASSET_ID
from test.aws.templates import TEST_ACL_ARN

TEST_ROLE_ID = 'VM-DSS-Bucket-VMImportRole-TEST'
TEST_BUCKET_ID = 'vm-dss-bucket-vmdssbucket-TEST'
TEST_AMI_ID = "AMI-IMAGE-12345"
TEST_CLOUDFRONT_ID = "test-cloudfrontet-TEST"
TEST_CLOUDFRONT_DOMAIN_NAME = "test-s3.cloudfront.net"
TEST_ACL_ARN = "TEST-DOWNLOAD-ACL"
INSTANCE_ID = "test-instance"


Expand Down
48 changes: 48 additions & 0 deletions test/aws/templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from exasol.ds.sandbox.lib.asset_id import AssetId
from exasol.ds.sandbox.lib.render_template import render_template
from exasol.ds.sandbox.lib.tags import DEFAULT_TAG_KEY
from exasol.ds.sandbox.lib.vm_bucket import (
vm_dss_bucket,
vm_dss_bucket_waf,
)

DEFAULT_ASSET_ID = AssetId("test", stack_prefix="test-stack", ami_prefix="test-ami")
TEST_IP = "1.1.1.1"
TEST_ACL_ARN = "TEST-DOWNLOAD-ACL"
TEST_DUMMY_AMI_ID = "ami-123"


def ci_codebuild_template():
return render_template(
"ci_code_build.jinja.yaml",
vm_bucket="test-bucket-123",
)


def release_codebuild_template():
return render_template(
"release_code_build.jinja.yaml",
vm_bucket="test-bucket-123",
path_in_bucket=AssetId.BUCKET_PREFIX,
dockerhub_secret_arn="secret_arn",
)


def ec2_template():
return render_template(
"ec2_cloudformation.jinja.yaml",
key_name="test_key",
user_name="test_user",
trace_tag=DEFAULT_TAG_KEY,
trace_tag_value=DEFAULT_ASSET_ID.tag_value,
ami_id=TEST_DUMMY_AMI_ID,
)


def vm_bucket_template():
return vm_dss_bucket.create_vm_bucket_cf_template(TEST_ACL_ARN)


def waf_template():
return vm_dss_bucket_waf.get_cloudformation_template(TEST_IP)

61 changes: 0 additions & 61 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,80 +10,19 @@
from importlib.metadata import version
from exasol.ds.sandbox.lib.tags import DEFAULT_TAG_KEY
from exasol.ds.sandbox.lib.asset_id import AssetId
from test.aws_local_stack_access import AwsLocalStackAccess

DEFAULT_ASSET_ID = AssetId("test", stack_prefix="test-stack", ami_prefix="test-ami")

TEST_DUMMY_AMI_ID = "ami-123"

pytest_plugins = (
"test.docker.dss_docker_image",
)


@pytest.fixture
def default_asset_id():
return DEFAULT_ASSET_ID


@pytest.fixture
def jupyter_port():
return 49494


@pytest.fixture
def ec2_cloudformation_yml():
return render_template("ec2_cloudformation.jinja.yaml", key_name="test_key", user_name="test_user",
trace_tag=DEFAULT_TAG_KEY, trace_tag_value=DEFAULT_ASSET_ID.tag_value,
ami_id=TEST_DUMMY_AMI_ID)


@pytest.fixture(scope="session")
def local_stack():
"""
This fixture starts/stops localstack as a context manager.
"""
command = "localstack start -d"

image_version = version('localstack')
# See https://github.com/localstack/localstack/issues/8254
# and https://github.com/localstack/localstack/issues/9939
#
# Until an official release of localstack Docker image with a concrete
# version is available incl. a fix for issue 9939 we only can use version
# "latest".
#
# See ai-lab issue for replacing this with a concrete version in order to
# make CI tests more robust.
image_version = "latest"
image_name = {"IMAGE_NAME": f"localstack/localstack:{image_version}"}
env_variables = {**os.environ, **image_name}

process = subprocess.run(shlex.split(command), env=env_variables)
assert process.returncode == 0

command = "localstack wait -t 30"

process = subprocess.run(shlex.split(command), env=env_variables)
assert process.returncode == 0
yield None

command = "localstack stop"
subprocess.run(shlex.split(command), env=env_variables)


@pytest.fixture(scope="session")
def local_stack_aws_access(local_stack):
return AwsLocalStackAccess().with_user("default_user")


@pytest.fixture(scope="session")
def test_config():
test_config = copy(default_config_object)
test_config.time_to_wait_for_polling = 0.1
return test_config


@pytest.fixture()
def test_dummy_ami_id():
return TEST_DUMMY_AMI_ID
5 changes: 0 additions & 5 deletions test/integration/aws/__init__.py

This file was deleted.

19 changes: 0 additions & 19 deletions test/integration/aws/cloudformation_validation.py

This file was deleted.

42 changes: 42 additions & 0 deletions test/integration/aws/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
import pytest
import subprocess
import shlex

from importlib.metadata import version
from test.aws.local_stack_access import AwsLocalStackAccess
from test.aws.conftest import (
default_asset_id,
test_dummy_ami_id,
ec2_cloudformation_yml,
)


@pytest.fixture(scope="session")
def local_stack():
"""
This fixture starts/stops localstack as a context manager.
"""
command = "localstack start -d"

image_version = version('localstack')
image_version = "3.2.0"
image_name = {"IMAGE_NAME": f"localstack/localstack:{image_version}"}
env_variables = {**os.environ, **image_name}

process = subprocess.run(shlex.split(command), env=env_variables)
assert process.returncode == 0

command = "localstack wait -t 30"

process = subprocess.run(shlex.split(command), env=env_variables)
assert process.returncode == 0
yield None

command = "localstack stop"
subprocess.run(shlex.split(command), env=env_variables)


@pytest.fixture(scope="session")
def local_stack_aws_access(local_stack):
return AwsLocalStackAccess().with_user("default_user")
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import botocore
import pytest

from exasol.ds.sandbox.lib.setup_ec2.cf_stack import CloudformationStack, \
CloudformationStackContextManager
from exasol.ds.sandbox.lib.setup_ec2.cf_stack import (
CloudformationStack,
CloudformationStackContextManager,
)
from exasol.ds.sandbox.lib.setup_ec2.run_setup_ec2 import run_lifecycle_for_ec2
from exasol.ds.sandbox.lib.tags import create_default_asset_tag


def test_ec2_lifecycle_with_local_stack(local_stack_aws_access, default_asset_id, test_dummy_ami_id):
def test_ec2_lifecycle_with_local_stack(
local_stack_aws_access,
default_asset_id,
test_dummy_ami_id,
):
"""
This test uses localstack to simulate lifecycle of an EC-2 instance
"""
Expand Down Expand Up @@ -36,7 +42,11 @@ def test_ec2_manage_keypair_with_local_stack(local_stack_aws_access, default_ass
aws.delete_ec2_key_pair("test")


def test_cloudformation_with_localstack(default_asset_id, local_stack_aws_access, ec2_cloudformation_yml):
def test_cloudformation_with_localstack(
default_asset_id,
local_stack_aws_access,
ec2_cloudformation_yml,
):
aws = local_stack_aws_access
aws.upload_cloudformation_stack(
ec2_cloudformation_yml, stack_name="test_stack",
Expand Down Expand Up @@ -99,7 +109,11 @@ def test_validate_cloudformation_template_fails_with_local_stack(local_stack_aws
local_stack_aws_access.validate_cloudformation_template(wrong_cloudformation_template)


def test_cloudformation_access_with_local_stack(local_stack_aws_access, default_asset_id, test_dummy_ami_id):
def test_cloudformation_access_with_local_stack(
local_stack_aws_access,
default_asset_id,
test_dummy_ami_id,
):
aws = local_stack_aws_access
with CloudformationStackContextManager(
CloudformationStack(
Expand Down
29 changes: 0 additions & 29 deletions test/integration/aws/test_deploy_codebuild.py

This file was deleted.

Loading
Loading