Skip to content

Commit 532c98a

Browse files
authored
Merge pull request #36 from nginxinc/e2e-tests
Prepare a POC of testing framework for e2e tests.
2 parents 1935ead + f05a9c6 commit 532c98a

File tree

14 files changed

+292
-0
lines changed

14 files changed

+292
-0
lines changed

tests/.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore pytest cache
2+
.pytest_cache

tests/.flake8

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[flake8]
2+
format = pylint
3+
max-complexity = 10
4+
max-line-length = 170
5+
exclude = .git,__pycache__,data,.idea,.pytest_cache

tests/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# IntelliJ project files
2+
.idea
3+
out
4+
gen
5+
6+
# Junit-style report for Jenkins integration
7+
results.xml
8+
9+
# Python specific files
10+
# Byte-compiled / optimized / DLL files
11+
__pycache__/
12+
*.py[cod]
13+
.pytest_cache

tests/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
NGINX_API =
2+
AWS_REGION = us-east-2
3+
PREFIX = test-runner
4+
TAG = edge
5+
PYTEST_ARGS =
6+
AWS_CREDENTIALS = ~/.aws/credentials
7+
8+
build:
9+
docker build -t $(PREFIX):$(TAG) -f docker/Dockerfile ..
10+
11+
run-tests: build
12+
docker run --rm -v $(AWS_CREDENTIALS):/root/.aws/credentials $(PREFIX):$(TAG) --nginx-api=$(NGINX_API) --aws-region=$(AWS_REGION) $(PYTEST_ARGS)

tests/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Tests
2+
3+
The project includes automated tests for testing the ASG Sync tool in an AWS environment (Azure support will be added later). The tests are written in Python3, use the pytest framework to run the tests and utilize boto3 to call the AWS API.
4+
5+
Note: for now this is for internal use only, as AWS stack configuration is done outside of testing framework.
6+
7+
Below you will find the instructions on how to run the tests against a cloud provider.
8+
9+
## Running Tests
10+
11+
### Prerequisites:
12+
13+
* AWS stack prepared.
14+
* AWS access key and AWS secret access key.
15+
* Python3 or Docker.
16+
17+
#### Step 1 - Set up the environment
18+
19+
* Either create|update ~/.aws/credentials file or set the AWS_SHARED_CREDENTIALS_FILE environment variable pointing to your own location. This file is an INI formatted file with section names corresponding to profiles. Tests use 'default' profile. The file [credentials](data/credentials) is a minimal example of such a file.
20+
21+
#### Step 2 - Run the Tests
22+
23+
Run the tests:
24+
* Use local Python3 installation:
25+
```bash
26+
$ cd tests
27+
$ pip3 install -r requirements.txt
28+
$ python3 -m pytest --nginx-api=nginx_plus_api_url
29+
```
30+
* Use Docker:
31+
```bash
32+
$ cd tests
33+
$ make run-tests AWS_CREDENTIALS=abs_path_to_creds_file NGINX_API=nginx_plus_api_url
34+
```
35+
36+
## Configuring the Tests
37+
38+
The table below shows various configuration options for the tests. If you use Python3 to run the tests, use the command-line arguments. If you use Docker, use the [Makefile](Makefile) variables.
39+
40+
41+
| Command-line Argument | Makefile Variable | Description | Default |
42+
| :----------------------- | :------------ | :------------ | :----------------------- |
43+
| `--nginx-api` | `NGINX_API` | The NGINX Plus API url. | `N/A` |
44+
| `--aws-region` | `AWS_REGION` | The AWS stack region. | `us-east-2` |
45+
| `N/A` | `PYTEST_ARGS` | Any additional pytest command-line arguments (i.e `-k TestSmoke`) | `""` |
46+
47+
If you would like to use an IDE (such as PyCharm) to run the tests, use the [pytest.ini](pytest.ini) file to set the command-line arguments.

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""Describe overall framework configuration."""
2+
import pytest
3+
from boto3 import Session
4+
from botocore.client import BaseClient
5+
6+
from tests.settings import DEFAULT_AWS_REGION
7+
8+
9+
def pytest_addoption(parser) -> None:
10+
"""Get cli-arguments.
11+
12+
:param parser: pytest parser
13+
:return:
14+
"""
15+
parser.addoption("--nginx-api", action="store", default="", help="The NGINX Plus API url.")
16+
parser.addoption("--aws-region", action="store", default=DEFAULT_AWS_REGION, help="The AWS region name.")
17+
18+
19+
class CLIArguments:
20+
"""
21+
Encapsulate CLI arguments.
22+
23+
Attributes:
24+
nginx_api (str): NGINX Plus API url
25+
aws_region (str): AWS region name
26+
"""
27+
def __init__(self, nginx_api: str, aws_region: str):
28+
self.nginx_api = nginx_api
29+
self.aws_region = aws_region
30+
31+
32+
@pytest.fixture(scope="session", autouse=True)
33+
def cli_arguments(request) -> CLIArguments:
34+
"""
35+
Verify the CLI arguments.
36+
37+
:param request: pytest fixture
38+
:return: CLIArguments
39+
"""
40+
nginx_api = request.config.getoption("--nginx-api")
41+
assert nginx_api != "", "Empty NGINX Plus API url is not allowed"
42+
print(f"\nTests will use NGINX Plus API url: {nginx_api}")
43+
44+
aws_region = request.config.getoption("--aws-region")
45+
print(f"\nTests will use AWS region: {aws_region}")
46+
47+
return CLIArguments(nginx_api, aws_region)
48+
49+
50+
@pytest.fixture(scope="session")
51+
def autoscaling_client(cli_arguments) -> BaseClient:
52+
"""
53+
Set up kubernets-client to operate in cluster.
54+
55+
boto3 looks for AWS credentials file and uses a `default` profile from it.
56+
57+
:param cli_arguments: a set of command-line arguments
58+
:return:
59+
"""
60+
session = Session(profile_name='default',
61+
region_name=cli_arguments.aws_region)
62+
return session.client('autoscaling')

tests/data/credentials

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[default]
2+
aws_access_key_id=foo
3+
aws_secret_access_key=bar

tests/docker/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM python:3.7.2-slim
2+
3+
RUN mkdir /workspace
4+
5+
WORKDIR /workspace
6+
7+
COPY tests tests
8+
9+
WORKDIR /workspace/tests
10+
11+
RUN pip install -r requirements.txt
12+
13+
ENTRYPOINT [ "python3", "-m", "pytest"]

tests/pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
addopts = --tb=native -r fsxX --disable-warnings
3+
log_cli=true

0 commit comments

Comments
 (0)