Skip to content

Commit

Permalink
Merge bee2729 into 4311e96
Browse files Browse the repository at this point in the history
  • Loading branch information
satterly committed Apr 16, 2020
2 parents 4311e96 + bee2729 commit 42a7a5c
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 1 deletion.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ python:
- "3.5"
- "3.6"
- "3.7"
- "3.8"

install:
- pip install -r requirements.txt
Expand Down
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM python:3.7
RUN apt-get update

RUN apt-get install -y \
build-essential \
python3-dev \
libffi-dev \
libssl-dev

WORKDIR /usr/src/app

COPY requirements*.txt ./
RUN pip install --no-cache-dir \
-r requirements.txt \
-r requirements-dev.txt

COPY . .
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ clean:
test:
pytest

test.integration:
docker-compose -f docker-compose.ci.yaml build sut
docker-compose -f docker-compose.ci.yaml up --exit-code-from sut
docker-compose -f docker-compose.ci.yaml rm --stop --force

run:
alerta top

Expand Down
45 changes: 45 additions & 0 deletions docker-compose.ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: '3.7'

services:
api:
image: alerta/alerta-web # use API in "alerta-web" image
ports:
- "8080:8080"
depends_on:
- db
environment:
- DEBUG=1 # remove this line to turn DEBUG off
- DATABASE_URL=postgres://postgres:postgres@db:5432/monitoring
- AUTH_REQUIRED=True
- ADMIN_USERS=admin@alerta.io,devops@alerta.io #default password: alerta
- ADMIN_KEY=demo-key # assigned to first user in ADMIN_USERS list
# - PLUGINS=reject,blackout,normalise,enhance
networks:
net:
aliases:
- api

db:
image: postgres:9.6
environment:
- POSTGRES_DB=monitoring
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
restart: always
networks:
net:
aliases:
- db

sut:
build: .
depends_on:
- api
command: ["./wait-for-it.sh", "api:8080", "-t", "60", "--", "pytest", "tests/integration/test_sdk.py"]
networks:
net:
aliases:
- sut

networks:
net: {}
Empty file added tests/integration/__init__.py
Empty file.
74 changes: 74 additions & 0 deletions tests/integration/test_sdk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import unittest

from alertaclient.api import Client


class AlertTestCase(unittest.TestCase):

def setUp(self):
self.client = Client(endpoint='http://api:8080/api', key='demo-key')

def test_alert(self):
id, alert, message = self.client.send_alert(
environment='Production', resource='web01', event='node_down', correlated=['node_up', 'node_down'],
service=['Web', 'App'], severity='critical', tags=['london', 'linux'], value=4
)
self.assertEqual(alert.value, '4') # values cast to string
self.assertEqual(alert.timeout, 86400) # timeout returned as int
self.assertIn('london', alert.tags)

def test_blackout(self):
blackout = self.client.create_blackout(
environment='Production', service=['Web', 'App'], resource='web01', event='node_down', group='Network', tags=['london', 'linux']
)
self.assertEqual(blackout.environment, 'Production')
self.assertEqual(blackout.service, ['Web', 'App'])
self.assertIn('london', blackout.tags)
self.assertIn('linux', blackout.tags)

def test_customer(self):
customer = self.client.create_customer(customer='ACME Corp.', match='example.com')
self.assertEqual(customer.customer, 'ACME Corp.')
self.assertEqual(customer.match, 'example.com')

def test_group(self):
group = self.client.create_group(name='myGroup', text='test group')
self.assertEqual(group.name, 'myGroup')
self.assertEqual(group.text, 'test group')

def test_heartbeat(self):
hb = self.client.heartbeat(origin='app/web01', timeout=10, tags=['london', 'linux'])
self.assertEqual(hb.origin, 'app/web01')
self.assertEqual(hb.event_type, 'Heartbeat')
self.assertEqual(hb.timeout, 10)
self.assertIn('linux', hb.tags)

def test_history(self):
hist = self.client.get_history()
self.assertEqual(hist[0].environment, 'Production')
self.assertEqual(hist[0].service, ['Web', 'App'])
self.assertEqual(hist[0].resource, 'web01')
self.assertIn('london', hist[0].tags)
self.assertEqual(hist[0].change_type, 'new')

def test_key(self):
api_key = self.client.create_key(
username='johndoe@example.com', scopes=['write:alerts', 'admin:keys'], text='Ops API Key'
)
self.assertEqual(api_key.user, 'johndoe@example.com')
self.assertEqual(sorted(api_key.scopes), sorted(['write:alerts', 'admin:keys']))

# def test_note(self):
# n = self.client.alert_note(id='e7020428-5dad-4a41-9bfe-78e9d55cda06', note='this is a test note')
# self.assertEqual(n.text, 'this is a test note')

def test_permission(self):
perm = self.client.create_perm(role='websys', scopes=['admin:users', 'admin:keys', 'write'])
self.assertEqual(perm.match, 'websys')
self.assertEqual(sorted(perm.scopes), sorted(['admin:users', 'admin:keys', 'write']))

def test_user(self):
users = self.client.get_users()
self.assertEqual(users[0].name, 'admin@alerta.io')
self.assertEqual(sorted(users[0].roles), sorted(['admin']))
self.assertEqual(users[0].status, 'active')
2 changes: 1 addition & 1 deletion tests/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def setUp(self):
"""

@requests_mock.mock()
def test_key(self, m):
def test_group(self, m):
m.post('http://localhost:8080/group', text=self.key)
group = self.client.create_group(name='myGroup', text='test group')
self.assertEqual(group.name, 'myGroup')
Expand Down
182 changes: 182 additions & 0 deletions wait-for-it.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#!/usr/bin/env bash
# Use this script to test if a given TCP host/port are available

WAITFORIT_cmdname=${0##*/}

echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }

usage()
{
cat << USAGE >&2
Usage:
$WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args]
-h HOST | --host=HOST Host or IP under test
-p PORT | --port=PORT TCP port under test
Alternatively, you specify the host and port as host:port
-s | --strict Only execute subcommand if the test succeeds
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT
Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
USAGE
exit 1
}

wait_for()
{
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
else
echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout"
fi
WAITFORIT_start_ts=$(date +%s)
while :
do
if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then
nc -z $WAITFORIT_HOST $WAITFORIT_PORT
WAITFORIT_result=$?
else
(echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1
WAITFORIT_result=$?
fi
if [[ $WAITFORIT_result -eq 0 ]]; then
WAITFORIT_end_ts=$(date +%s)
echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds"
break
fi
sleep 1
done
return $WAITFORIT_result
}

wait_for_wrapper()
{
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
if [[ $WAITFORIT_QUIET -eq 1 ]]; then
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
else
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
fi
WAITFORIT_PID=$!
trap "kill -INT -$WAITFORIT_PID" INT
wait $WAITFORIT_PID
WAITFORIT_RESULT=$?
if [[ $WAITFORIT_RESULT -ne 0 ]]; then
echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
fi
return $WAITFORIT_RESULT
}

# process arguments
while [[ $# -gt 0 ]]
do
case "$1" in
*:* )
WAITFORIT_hostport=(${1//:/ })
WAITFORIT_HOST=${WAITFORIT_hostport[0]}
WAITFORIT_PORT=${WAITFORIT_hostport[1]}
shift 1
;;
--child)
WAITFORIT_CHILD=1
shift 1
;;
-q | --quiet)
WAITFORIT_QUIET=1
shift 1
;;
-s | --strict)
WAITFORIT_STRICT=1
shift 1
;;
-h)
WAITFORIT_HOST="$2"
if [[ $WAITFORIT_HOST == "" ]]; then break; fi
shift 2
;;
--host=*)
WAITFORIT_HOST="${1#*=}"
shift 1
;;
-p)
WAITFORIT_PORT="$2"
if [[ $WAITFORIT_PORT == "" ]]; then break; fi
shift 2
;;
--port=*)
WAITFORIT_PORT="${1#*=}"
shift 1
;;
-t)
WAITFORIT_TIMEOUT="$2"
if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi
shift 2
;;
--timeout=*)
WAITFORIT_TIMEOUT="${1#*=}"
shift 1
;;
--)
shift
WAITFORIT_CLI=("$@")
break
;;
--help)
usage
;;
*)
echoerr "Unknown argument: $1"
usage
;;
esac
done

if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then
echoerr "Error: you need to provide a host and port to test."
usage
fi

WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15}
WAITFORIT_STRICT=${WAITFORIT_STRICT:-0}
WAITFORIT_CHILD=${WAITFORIT_CHILD:-0}
WAITFORIT_QUIET=${WAITFORIT_QUIET:-0}

# Check to see if timeout is from busybox?
WAITFORIT_TIMEOUT_PATH=$(type -p timeout)
WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH)

WAITFORIT_BUSYTIMEFLAG=""
if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then
WAITFORIT_ISBUSY=1
# Check if busybox timeout uses -t flag
# (recent Alpine versions don't support -t anymore)
if timeout &>/dev/stdout | grep -q -e '-t '; then
WAITFORIT_BUSYTIMEFLAG="-t"
fi
else
WAITFORIT_ISBUSY=0
fi

if [[ $WAITFORIT_CHILD -gt 0 ]]; then
wait_for
WAITFORIT_RESULT=$?
exit $WAITFORIT_RESULT
else
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
wait_for_wrapper
WAITFORIT_RESULT=$?
else
wait_for
WAITFORIT_RESULT=$?
fi
fi

if [[ $WAITFORIT_CLI != "" ]]; then
if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then
echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess"
exit $WAITFORIT_RESULT
fi
exec "${WAITFORIT_CLI[@]}"
else
exit $WAITFORIT_RESULT
fi

0 comments on commit 42a7a5c

Please sign in to comment.