diff --git a/docker-compose.yaml b/docker-compose.yaml deleted file mode 100644 index 3d952320ea..0000000000 --- a/docker-compose.yaml +++ /dev/null @@ -1,46 +0,0 @@ -version: "3" -networks: - vote: - driver: bridge - -volumes: - db-data: - -services: - vote: - image: schoolofdevops/vote - ports: - - 80 - networks: - - vote - depends_on: - - redis - - redis: - image: redis:alpine - networks: - - vote - - worker: - image: initcron/worker:v2 - networks: - - vote - depends_on: - - redis - - db: - image: postgres:9.4 - networks: - - vote - volumes: - - "db-data:/var/lib/postgresql/data" - - results: - image: schoolofdevops/vote-result - ports: - - 5001:80 - depends_on: - - db - networks: - - vote - diff --git a/e2e.sh b/e2e.sh new file mode 100755 index 0000000000..0862d42c36 --- /dev/null +++ b/e2e.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +cd e2e + +docker-compose down > /dev/null 2>&1 + +#sleep 10 + +docker-compose build +docker-compose up -d + +docker-compose ps + +docker-compose run --rm e2e + +docker-compose down + diff --git a/e2e/.env b/e2e/.env new file mode 100644 index 0000000000..ba78b2ea4c --- /dev/null +++ b/e2e/.env @@ -0,0 +1,3 @@ +VOTE_IMAGE=lfs261/vote:master +WORKER_IMAGE=lfs261/worker:master +RESULT_IMAGE=lfs261/result:master diff --git a/e2e/docker-compose.test.yml b/e2e/docker-compose.yml similarity index 82% rename from e2e/docker-compose.test.yml rename to e2e/docker-compose.yml index 0df964e646..1427ba2256 100644 --- a/e2e/docker-compose.test.yml +++ b/e2e/docker-compose.yml @@ -2,17 +2,19 @@ version: '2' services: - sut: + e2e: build: ./tests/ depends_on: - vote - result - worker + - db + - redis networks: - front-tier vote: - build: ../vote/ + image: ${VOTE_IMAGE} ports: ["80"] depends_on: - redis @@ -22,8 +24,8 @@ services: - back-tier result: - build: . - ports: ["80"] + image: ${RESULT_IMAGE} + ports: ["4000"] depends_on: - redis - db @@ -32,7 +34,7 @@ services: - back-tier worker: - build: ../worker/ + image: ${WORKER_IMAGE} depends_on: - redis - db @@ -47,8 +49,6 @@ services: db: image: postgres:9.4 - volumes: - - "db-data:/var/lib/postgresql/data" networks: - back-tier diff --git a/e2e/tests/Dockerfile b/e2e/tests/Dockerfile index 6fca727f94..2b0e82210b 100644 --- a/e2e/tests/Dockerfile +++ b/e2e/tests/Dockerfile @@ -9,4 +9,4 @@ RUN apt-get update -qq && apt-get install -qy \ RUN yarn global add phantomjs-prebuilt ADD . /app WORKDIR /app -CMD ["/app/tests.sh"] +CMD ./tests.sh diff --git a/e2e/tests/tests.sh b/e2e/tests/tests.sh index 448159454b..e76354f5f8 100755 --- a/e2e/tests/tests.sh +++ b/e2e/tests/tests.sh @@ -1,13 +1,37 @@ -#!/bin/sh +#!/bin/bash + +current="" +next="" while ! timeout 1 bash -c "echo > /dev/tcp/vote/80"; do sleep 1 done +# add initial vote +curl -sS -X POST --data "vote=a" http://vote > /dev/null + +current=`phantomjs render.js "http://result:4000/" | grep -i vote | cut -d ">" -f 4 | cut -d " " -f1` +next=`echo "$(($current + 1))"` + + echo -e "\n\n-----------------" + echo -e "Current Votes Count: $current" + echo -e "-----------------\n" + +echo -e " I: Submitting one more vote...\n" + curl -sS -X POST --data "vote=b" http://vote > /dev/null -sleep 10 +sleep 3 + +new=`phantomjs render.js "http://result:4000/" | grep -i vote | cut -d ">" -f 4 | cut -d " " -f1` + + + echo -e "\n\n-----------------" + echo -e "New Votes Count: $new" + echo -e "-----------------\n" + +echo -e "I: Checking if votes tally......\n" -if phantomjs render.js http://result | grep -q '1 vote'; then +if [ "$next" -eq "$new" ]; then echo -e "\\e[42m------------" echo -e "\\e[92mTests passed" echo -e "\\e[42m------------" diff --git a/result/Dockerfile b/result/Dockerfile index 2bca93313a..4f3713fc7a 100644 --- a/result/Dockerfile +++ b/result/Dockerfile @@ -1,18 +1,12 @@ -FROM node:8.9-alpine +FROM node:8.16.0-alpine -RUN mkdir -p /app -WORKDIR /app +WORKDIR /app -RUN npm install -g nodemon -RUN npm config set registry https://registry.npmjs.org -COPY package.json /app/package.json -RUN npm install \ - && npm ls \ - && npm cache clean --force \ - && mv /app/node_modules /node_modules -COPY . /app +COPY . . -ENV PORT 80 -EXPOSE 80 +RUN npm install && \ + npm audit fix + +EXPOSE 4000 -CMD ["node", "server.js"] +CMD npm start diff --git a/result/package.json b/result/package.json index f7863e1a1d..f0ac5fb3a6 100644 --- a/result/package.json +++ b/result/package.json @@ -1,10 +1,10 @@ { "name": "result", - "version": "1.0.0", + "version": "1.1.0", "description": "", "main": "server.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "node_modules/.bin/mocha" }, "author": "", "license": "MIT", @@ -16,5 +16,9 @@ "async": "^1.5.0", "pg": "^4.4.3", "socket.io": "^1.3.7" + }, + "devDependencies": { + "chai": "^4.0.2", + "mocha": "^3.4.2" } } diff --git a/result/test/mock.test.js b/result/test/mock.test.js new file mode 100644 index 0000000000..f99dfdae97 --- /dev/null +++ b/result/test/mock.test.js @@ -0,0 +1,29 @@ +const expect = require('chai').expect; + +describe('mock test 1', () => { + it('unit test 1', () => { + expect(true).to.be.true; + }); +}); + + +describe('mock test 2', () => { + it('unit test 2', () => { + expect(true).to.be.true; + }); +}); + +describe('mock test 3', () => { + it('unit test 3', () => { + expect(true).to.be.true; + }); +}); + + +describe('mock test 4', () => { + it('unit test 4', () => { + expect(true).to.be.true; + }); +}); + + diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000000..fa91ad96cc --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,11 @@ +# Uncomment and update Org matching your configurations on Sonarcloud +#sonar.organization=your-org +sonar.projectKey=your-org_example-voting-app +sonar.projectName=Instavote AIO +sonar.projectVersion=1.0 +# Comma-separated paths to directories with sources (required) +sonar.sources=worker +# Encoding of the source files +sonar.sourceEncoding=UTF-8 +sonar.java.binaries=. +#sonar.coverage.jacoco.xmlReportPaths=worker/target diff --git a/vote/integration/Dockerfile b/vote/integration/Dockerfile new file mode 100644 index 0000000000..c44c018e5f --- /dev/null +++ b/vote/integration/Dockerfile @@ -0,0 +1,5 @@ +FROM alpine:3.9.4 +WORKDIR /test +COPY . . +RUN apk add curl +CMD sh diff --git a/vote/integration/docker-compose.yaml b/vote/integration/docker-compose.yaml new file mode 100644 index 0000000000..dc73e9eb85 --- /dev/null +++ b/vote/integration/docker-compose.yaml @@ -0,0 +1,26 @@ +version: "3" + +services: + + integration: + build: ./ + networks: + - integration + + vote: + build: ../ + ports: ["80"] + depends_on: + - redis + networks: + - integration + + redis: + image: redis:alpine + ports: ["6379"] + networks: + - integration + +networks: + integration: + diff --git a/vote/integration/test.sh b/vote/integration/test.sh new file mode 100755 index 0000000000..f906424e87 --- /dev/null +++ b/vote/integration/test.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +echo "I: Checking if frontend vote app is available..." + +curl http://vote > /dev/null 2>&1 + +if [ $? -eq 0 ] +then + echo "---------------------------------------" + echo "Vote app is available....proceeding" + echo "---------------------------------------" +else + echo "---------------------------------------" + echo "Vote app is not avilable....aborting" + echo "---------------------------------------" + exit 2 +fi + + +echo "I: Launching integration test..." + +# submit a vote. Will return an error if it fails to submit or store vote in redis +# Fail integration test if it returns exit code 0 (error state) + +curl -sS -X POST --data "vote=b" http://vote | grep -i erro + +if [ $? -eq 0 ] +then + # error, failed + echo "-----------------------------" + echo "INTEGRATION TEST FAILED" + echo "-----------------------------" + exit 1 +else + # passed + echo "-----------------------------" + echo "INTEGRATION TEST PASSED" + echo "-----------------------------" + exit 0 +fi + + diff --git a/vote/integration_test.sh b/vote/integration_test.sh new file mode 100755 index 0000000000..956dcdea8a --- /dev/null +++ b/vote/integration_test.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +cd integration + +echo "I: Creating environment to run integration tests..." + +docker-compose build +docker-compose up -d + + +echo "I: Launching Integration Test ..." + +docker-compose run --rm integration /test/test.sh + +if [ $? -eq 0 ] +then + echo "---------------------------------------" + echo "INTEGRATION TESTS PASSED....." + echo "---------------------------------------" + docker-compose down + cd .. + exit 0 +else + echo "---------------------------------------" + echo "INTEGRATION TESTS FAILED....." + echo "---------------------------------------" + docker-compose down + cd .. + exit 1 +fi diff --git a/vote/requirements.txt b/vote/requirements.txt index 430bfdcd89..d3cbfb8f41 100644 --- a/vote/requirements.txt +++ b/vote/requirements.txt @@ -1,3 +1,4 @@ Flask Redis gunicorn +nose