Skip to content

Commit

Permalink
test: Use ruby rspec to run unit tests (#347)
Browse files Browse the repository at this point in the history
* Use ruby rspec to run unit tests
  • Loading branch information
satterly authored Dec 3, 2021
1 parent dc20c3d commit 0fd2590
Show file tree
Hide file tree
Showing 22 changed files with 320 additions and 248 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ jobs:
- name: Run tests
id: smoketest
run: |
docker-compose -f docker-compose.postgres.yml build \
docker-compose -f tests/docker-compose.test.postgres.yml build \
--build-arg VCS_REF=${{ github.sha }} \
--build-arg VERSION=$(cat VERSION)
docker-compose -f docker-compose.postgres.yml up --exit-code-from tester
docker-compose -f tests/docker-compose.test.postgres.yml up --exit-code-from tester
- uses: act10ns/slack@v1
with:
Expand All @@ -66,10 +66,10 @@ jobs:
- name: Run tests
id: smoketest
run: |
docker-compose -f docker-compose.mongo.yml build \
docker-compose -f tests/docker-compose.test.mongodb.yml build \
--build-arg VCS_REF=${{ github.sha }} \
--build-arg VERSION=$(cat VERSION)
docker-compose -f docker-compose.mongo.yml up --exit-code-from tester
docker-compose -f tests/docker-compose.test.mongodb.yml up --exit-code-from tester
- uses: act10ns/slack@v1
with:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ contrib/kubernetes/helm/alerta/charts
.env.*.local

.idea/*

/.bundle/
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014-2020 Nick Satterly
Copyright (c) 2014-2021 Nick Satterly

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
21 changes: 7 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ VCS_REF=$(shell git rev-parse --short HEAD)
BUILD_DATE=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
VERSION:=$(shell cat VERSION)

BACKEND ?= postgres

ifndef IMAGE_NAME
$(error IMAGE_NAME is not set)
endif
Expand All @@ -23,23 +25,14 @@ all: help
lint:
docker run --rm -i hadolint/hadolint < Dockerfile

## test.postgres - Run unit tests (Postgres).
test.postgres:
IMAGE_NAME=${IMAGE_NAME} \
VCS_REF=${VCS_REF} \
VERSION=${VERSION} \
$(DOCKER_COMPOSE) -f docker-compose.postgres.yml up \
--build \
--renew-anon-volumes \
--no-color \
--exit-code-from tester

## test.mongodb - Run unit tests (MongoDB).
test.mongodb:
## test.unit - Run unit tests.
test.unit:
IMAGE_NAME=${IMAGE_NAME} \
VCS_REF=${VCS_REF} \
VERSION=${VERSION} \
$(DOCKER_COMPOSE) -f docker-compose.mongodb.yml up \
$(DOCKER_COMPOSE) \
-f tests/docker-compose.test.${BACKEND}.yml \
up \
--build \
--renew-anon-volumes \
--no-color \
Expand Down
2 changes: 2 additions & 0 deletions config/templates/app/nginx.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ http {

include /etc/nginx/mime.types;

server_tokens off;

gzip on;
gzip_disable "msie6";

Expand Down
1 change: 0 additions & 1 deletion docker-compose.test.yml

This file was deleted.

5 changes: 0 additions & 5 deletions hooks/build

This file was deleted.

5 changes: 0 additions & 5 deletions hooks/post_push

This file was deleted.

4 changes: 0 additions & 4 deletions hooks/pre_build

This file was deleted.

5 changes: 0 additions & 5 deletions hooks/test

This file was deleted.

10 changes: 0 additions & 10 deletions tests.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ version: '3'
services:
sut:
build:
context: .
context: ..
args:
VCS_REF: ${VCS_REF}
VERSION: ${VERSION}
# image: alerta/alerta-web
ports:
- 8080
depends_on:
Expand All @@ -31,16 +32,15 @@ services:
# - HEARTBEAT_SEVERITY=

tester:
image: curlimages/curl:latest
build:
context: docker
dockerfile: Dockerfile
volumes:
- ./wait-for:/wait-for
- ./tests.sh:/tests.sh
- ./spec:/spec
depends_on:
- sut
command: sh -c './wait-for sut:8080 -t 240 -- ./tests.sh'
command: sh -c './wait-for sut:8080 -t 240 -- bundle exec rspec ./spec'

db:
image: mongo
volumes:
- ./mongodb:/data/db
restart: always
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3'
services:
sut:
build:
context: .
context: ..
args:
VCS_REF: ${VCS_REF}
VERSION: ${VERSION}
Expand Down Expand Up @@ -32,20 +32,18 @@ services:
# - HEARTBEAT_SEVERITY=

tester:
image: curlimages/curl:latest
build:
context: docker
dockerfile: Dockerfile
volumes:
- ./wait-for:/wait-for
- ./tests.sh:/tests.sh
- ./spec:/spec
depends_on:
- sut
command: sh -c './wait-for sut:8080 -t 240 -- ./tests.sh'
command: sh -c './wait-for sut:8080 -t 240 -- bundle exec rspec ./spec'

db:
image: postgres
volumes:
- ./pg-data:/var/lib/postgresql/data
environment:
POSTGRES_DB: monitoring
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres

3 changes: 3 additions & 0 deletions tests/docker/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--color
--require ./spec/helpers/spec_helper
--require ./spec/helpers/client
10 changes: 10 additions & 0 deletions tests/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM ruby:2.6

COPY Gemfile* ./
RUN gem install bundler && bundle install --jobs=3 --retry=3

RUN apt-get -q update && apt-get -qy install netcat
RUN wget https://raw.githubusercontent.com/eficode/wait-for/v2.2.0/wait-for && chmod +x wait-for

COPY .rspec .
CMD ["bundle", "exec", "rspec"]
6 changes: 6 additions & 0 deletions tests/docker/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Gemfile
source "https://rubygems.org"

gem "rspec"
gem 'rest-client'
gem 'nokogiri'
44 changes: 44 additions & 0 deletions tests/docker/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.4.4)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
http-accept (1.7.0)
http-cookie (1.0.4)
domain_name (~> 0.5)
mime-types (3.4.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2021.1115)
netrc (0.11.0)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.3)
unf (0.1.4)
unf_ext
unf_ext (0.0.8)

PLATFORMS
ruby

DEPENDENCIES
rest-client
rspec

BUNDLED WITH
1.17.2
70 changes: 70 additions & 0 deletions tests/spec/api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'json'

describe "api" do
GATEWAY_BASE_URL = "http://sut:8080/api"
context "/_" do
context "get" do
result = Client.get "#{GATEWAY_BASE_URL}/_"
it "return 200" do
expect(result.code).to eq(200)
end
it "return OK" do
expect(result.body).to eq("OK")
end
end
end
context "/config" do
context "get" do
result = Client.get "#{GATEWAY_BASE_URL}/config"
it "return 200" do
expect(result.code).to eq(200)
end
it "response header content-type is application/json" do
expect(result.headers[:content_type]).to eq("application/json")
end
it "config is valid json" do
expect(JSON.parse(result.body)['provider']).to eq("basic")
end
end
context "/management" do
context "get properties" do
result = Client.get "#{GATEWAY_BASE_URL}/management/properties", {'x-api-key' => 'demo-key'}
it "return 200" do
expect(result.code).to eq(200)
end
it "X-Forwarded-For header is set" do
expect(result.body).to include("X-Forwarded-For")
end
end
context "get healthcheck" do
result = Client.get "#{GATEWAY_BASE_URL}/management/gtg"
it "return 200" do
expect(result.code).to eq(200)
end
it "good-to-go is OK" do
expect(result.body).to eq("OK")
end
it "response header does not contain nginx version" do
expect(result.headers[:server]).to eq("nginx")
end
end
end
context "/alerts" do
context "get without auth" do
result = Client.get "#{GATEWAY_BASE_URL}/alerts"
it "return 401" do
expect(result.code).to eq(401)
end
end
context "get" do
result = Client.get "#{GATEWAY_BASE_URL}/alerts?api-key=demo-key"
it "return 200" do
expect(result.code).to eq(200)
end
it "total is 0" do
expect(JSON.parse(result.body)['total']).to eq(0)
end
end
end
end
end
37 changes: 37 additions & 0 deletions tests/spec/helpers/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'rest-client'

module Client
include RestClient

def self.get(*args)
begin
RestClient.get(*args)
rescue => e
e.response
end
end

def self.post(*args)
begin
RestClient.post(*args)
rescue => e
e.response
end
end

def self.put(*args)
begin
RestClient.put(*args)
rescue => e
e.response
end
end

def self.delete(*args)
begin
RestClient.delete(*args)
rescue => e
e.response
end
end
end
Loading

0 comments on commit 0fd2590

Please sign in to comment.