Skip to content

Commit 299ddd3

Browse files
authored
Add Integration tests (#211)
* Add integration tests * Update travis CI to include integration tests
1 parent 4311e96 commit 299ddd3

25 files changed

Lines changed: 362 additions & 34 deletions

.travis.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@ install:
1313
- pip install -r requirements-dev.txt
1414
- pip install .
1515

16+
1617
script:
17-
- pip install coveralls
18-
- python -m mypy alertaclient/
19-
- pytest --cov=alertaclient
20-
- coveralls
18+
- make test.unit
19+
- make test.integration
20+
21+
stages:
22+
- lint
23+
24+
jobs:
25+
include:
26+
- stage: lint
27+
script: make lint

Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM python:3.7
2+
RUN apt-get update
3+
4+
RUN apt-get install -y \
5+
build-essential \
6+
python3-dev \
7+
libffi-dev \
8+
libssl-dev
9+
10+
WORKDIR /usr/src/app
11+
12+
COPY requirements*.txt ./
13+
RUN pip install --no-cache-dir \
14+
-r requirements.txt \
15+
-r requirements-dev.txt
16+
17+
COPY . .

Makefile

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,62 @@
11

22
PYTHON=python
33
VERSION=`cut -d "'" -f 2 alertaclient/version.py`
4+
.DEFAULT_GOAL:=help
45

56
all: help
67

7-
help:
8-
@echo ""
9-
@echo "Usage: make <command>"
10-
@echo ""
11-
@echo "Commands:"
12-
@echo " init Initialise environment"
13-
@echo " dev Initialise dev environment"
14-
@echo " pylint Lint source code"
15-
@echo " mypy Type checking"
16-
@echo " hooks Run pre-commit hooks"
17-
@echo " clean Clean source"
18-
@echo " test Run tests"
19-
@echo " run Run application"
20-
@echo " tag Git tag with current version"
21-
@echo " upload Upload package to PyPI"
22-
@echo ""
23-
8+
## init - Initialise environment.
249
init:
2510
pip install -r requirements.txt --upgrade
2611
pip install -e .
2712

13+
## dev - Initialise dev environment.
2814
dev:
2915
pip install -r requirements-dev.txt --upgrade
3016
pre-commit install
3117
pre-commit autoupdate
3218

33-
pylint:
19+
## hooks - Run pre-commit hooks.
20+
hooks:
21+
pre-commit run --all-files
22+
23+
## lint - Lint and type checking.
24+
lint:
3425
@pip -q install pylint
3526
pylint --rcfile pylintrc alertaclient
36-
37-
mypy:
3827
@pip -q install mypy==0.620
3928
mypy alertaclient/
4029

41-
hooks:
42-
pre-commit run --all-files
43-
30+
## clean - Clean source.
4431
clean:
4532
find . -name "*.pyc" -exec rm {} \;
4633
rm -Rf build dist *.egg-info
4734

48-
test:
49-
pytest
35+
## test.unit - Run unit tests.
36+
test.unit:
37+
pip install coveralls
38+
pytest --cov=alertaclient tests/unit
5039

40+
## test.integration - Run integration tests.
41+
test.integration:
42+
docker-compose -f docker-compose.ci.yaml build sut
43+
docker-compose -f docker-compose.ci.yaml up --exit-code-from sut
44+
docker-compose -f docker-compose.ci.yaml rm --stop --force
45+
46+
## run - Run application.
5147
run:
5248
alerta top
5349

50+
## tag - Git tag with current version.
5451
tag:
5552
git tag -a v$(VERSION) -m v$(VERSION)
5653
git push --tags
5754

55+
## upload - Upload package to PyPI.
5856
upload:
5957
$(PYTHON) setup.py sdist bdist_wheel
6058
twine upload dist/*
59+
60+
## help - Show this help.
61+
help: Makefile
62+
@sed -n 's/^##//p' $<

alertaclient/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from alertaclient.cli import cli
22

3-
cli()
3+
cli() # pylint: disable=no-value-for-parameter

alertaclient/auth/token.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def get_access_token(self, xsrf_token):
5757
httpd = HTTPServer(server_address, lambda request, address,
5858
server: HTTPServerHandler(request, address, server, xsrf_token))
5959
httpd.handle_request()
60-
return httpd.access_token
60+
return httpd.access_token # pylint: disable=no-member
6161

6262

6363
class Jwt:

alertaclient/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
class CustomJsonEncoder(json.JSONEncoder):
12-
def default(self, o):
12+
def default(self, o): # pylint: disable=method-hidden
1313
if isinstance(o, (datetime.date, datetime.datetime)):
1414
return o.replace(microsecond=0).strftime('%Y-%m-%dT%H:%M:%S') + '.%03dZ' % (o.microsecond // 1000)
1515
elif isinstance(o, datetime.timedelta):

docker-compose.ci.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
version: '3.7'
2+
3+
services:
4+
api:
5+
image: alerta/alerta-web # use API in "alerta-web" image
6+
ports:
7+
- "8080:8080"
8+
depends_on:
9+
- db
10+
environment:
11+
- DEBUG=1 # remove this line to turn DEBUG off
12+
- DATABASE_URL=postgres://postgres:postgres@db:5432/monitoring
13+
- AUTH_REQUIRED=True
14+
- ADMIN_USERS=admin@alerta.io,devops@alerta.io #default password: alerta
15+
- ADMIN_KEY=demo-key # assigned to first user in ADMIN_USERS list
16+
# - PLUGINS=reject,blackout,normalise,enhance
17+
networks:
18+
net:
19+
aliases:
20+
- api
21+
22+
db:
23+
image: postgres:9.6
24+
environment:
25+
- POSTGRES_DB=monitoring
26+
- POSTGRES_USER=postgres
27+
- POSTGRES_PASSWORD=postgres
28+
restart: always
29+
networks:
30+
net:
31+
aliases:
32+
- db
33+
34+
sut:
35+
build: .
36+
depends_on:
37+
- api
38+
command: ["./wait-for-it.sh", "api:8080", "-t", "60", "--", "pytest", "tests/integration/test_sdk.py"]
39+
networks:
40+
net:
41+
aliases:
42+
- sut
43+
44+
networks:
45+
net: {}

pylintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ load-plugins=
3333
# can either give multiple identifier separated by comma (,) or put this option
3434
# multiple time (only on the command line, not in the configuration file where
3535
# it should appear only once).
36-
disable=C0111,C0301,E1101
36+
#disable=C0111,C0301,E1101
37+
disable=W,R,C
3738

3839

3940
[REPORTS]

tests/integration/__init__.py

Whitespace-only changes.

tests/integration/test_sdk.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import unittest
2+
3+
from alertaclient.api import Client
4+
5+
6+
class AlertTestCase(unittest.TestCase):
7+
8+
def setUp(self):
9+
self.client = Client(endpoint='http://api:8080/api', key='demo-key')
10+
11+
def test_alert(self):
12+
id, alert, message = self.client.send_alert(
13+
environment='Production', resource='web01', event='node_down', correlated=['node_up', 'node_down'],
14+
service=['Web', 'App'], severity='critical', tags=['london', 'linux'], value=4
15+
)
16+
self.assertEqual(alert.value, '4') # values cast to string
17+
self.assertEqual(alert.timeout, 86400) # timeout returned as int
18+
self.assertIn('london', alert.tags)
19+
20+
def test_blackout(self):
21+
blackout = self.client.create_blackout(
22+
environment='Production', service=['Web', 'App'], resource='web01', event='node_down', group='Network', tags=['london', 'linux']
23+
)
24+
self.assertEqual(blackout.environment, 'Production')
25+
self.assertEqual(blackout.service, ['Web', 'App'])
26+
self.assertIn('london', blackout.tags)
27+
self.assertIn('linux', blackout.tags)
28+
29+
def test_customer(self):
30+
customer = self.client.create_customer(customer='ACME Corp.', match='example.com')
31+
self.assertEqual(customer.customer, 'ACME Corp.')
32+
self.assertEqual(customer.match, 'example.com')
33+
34+
def test_group(self):
35+
group = self.client.create_group(name='myGroup', text='test group')
36+
self.assertEqual(group.name, 'myGroup')
37+
self.assertEqual(group.text, 'test group')
38+
39+
def test_heartbeat(self):
40+
hb = self.client.heartbeat(origin='app/web01', timeout=10, tags=['london', 'linux'])
41+
self.assertEqual(hb.origin, 'app/web01')
42+
self.assertEqual(hb.event_type, 'Heartbeat')
43+
self.assertEqual(hb.timeout, 10)
44+
self.assertIn('linux', hb.tags)
45+
46+
def test_history(self):
47+
hist = self.client.get_history()
48+
self.assertEqual(hist[0].environment, 'Production')
49+
self.assertEqual(hist[0].service, ['Web', 'App'])
50+
self.assertEqual(hist[0].resource, 'web01')
51+
self.assertIn('london', hist[0].tags)
52+
self.assertEqual(hist[0].change_type, 'new')
53+
54+
def test_key(self):
55+
api_key = self.client.create_key(
56+
username='johndoe@example.com', scopes=['write:alerts', 'admin:keys'], text='Ops API Key'
57+
)
58+
self.assertEqual(api_key.user, 'johndoe@example.com')
59+
self.assertEqual(sorted(api_key.scopes), sorted(['write:alerts', 'admin:keys']))
60+
61+
# def test_note(self):
62+
# n = self.client.alert_note(id='e7020428-5dad-4a41-9bfe-78e9d55cda06', note='this is a test note')
63+
# self.assertEqual(n.text, 'this is a test note')
64+
65+
def test_permission(self):
66+
perm = self.client.create_perm(role='websys', scopes=['admin:users', 'admin:keys', 'write'])
67+
self.assertEqual(perm.match, 'websys')
68+
self.assertEqual(sorted(perm.scopes), sorted(['admin:users', 'admin:keys', 'write']))
69+
70+
def test_user(self):
71+
users = self.client.get_users()
72+
self.assertEqual(users[0].name, 'admin@alerta.io')
73+
self.assertEqual(sorted(users[0].roles), sorted(['admin']))
74+
self.assertEqual(users[0].status, 'active')

0 commit comments

Comments
 (0)