From 8abaec0953bcb517808defe5d019c70f1effb4b2 Mon Sep 17 00:00:00 2001 From: Gourav Shah Date: Wed, 5 Jun 2019 20:56:59 +0530 Subject: [PATCH 1/9] added unit tests for results nodejs app --- result/package.json | 8 ++++++-- result/test/mock.test.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 result/test/mock.test.js 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; + }); +}); + + From e115e2884455734d3308b9ed8f04158b5b75e762 Mon Sep 17 00:00:00 2001 From: Gourav Shah Date: Thu, 6 Jun 2019 11:55:56 +0530 Subject: [PATCH 2/9] - refactored and renamed docker-compose for e2e tests - compose now reads list of images from .env file - refactored e2e tests script, which can now run from any starting state of vote counts - e2e tests now run with bash insteaead of sh --- e2e/.env | 3 +++ ...er-compose.test.yml => docker-compose.yml} | 12 +++++----- e2e/tests/tests.sh | 22 +++++++++++++++++-- 3 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 e2e/.env rename e2e/{docker-compose.test.yml => docker-compose.yml} (85%) 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 85% rename from e2e/docker-compose.test.yml rename to e2e/docker-compose.yml index 0df964e646..2715543309 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,7 +24,7 @@ services: - back-tier result: - build: . + image: ${RESULT_IMAGE} ports: ["80"] depends_on: - redis @@ -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/tests.sh b/e2e/tests/tests.sh index 448159454b..4044cc63a7 100755 --- a/e2e/tests/tests.sh +++ b/e2e/tests/tests.sh @@ -1,13 +1,31 @@ -#!/bin/sh +#!/bin/bash while ! timeout 1 bash -c "echo > /dev/tcp/vote/80"; do sleep 1 done +current=`phantomjs render.js http://result | 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 -if phantomjs render.js http://result | grep -q '1 vote'; then +new=`phantomjs render.js http://result | 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 [ "$next" -eq "$new" ]; then echo -e "\\e[42m------------" echo -e "\\e[92mTests passed" echo -e "\\e[42m------------" From fde26ef450d11528eee98c21d81175288e1e6db9 Mon Sep 17 00:00:00 2001 From: Gourav Shah Date: Tue, 25 Jun 2019 15:06:21 +0530 Subject: [PATCH 3/9] remove compose spec --- docker-compose.yaml | 46 --------------------------------------------- 1 file changed, 46 deletions(-) delete mode 100644 docker-compose.yaml 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 - From 8b442e42fbfcdb776316583a110f145450c307c3 Mon Sep 17 00:00:00 2001 From: Gourav Shah Date: Fri, 5 Jul 2019 19:36:28 +0530 Subject: [PATCH 4/9] added integration test script --- vote/integration/Dockerfile | 5 ++++ vote/integration/docker-compose.yaml | 26 +++++++++++++++++ vote/integration/test.sh | 42 ++++++++++++++++++++++++++++ vote/integration_test.sh | 30 ++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 vote/integration/Dockerfile create mode 100644 vote/integration/docker-compose.yaml create mode 100755 vote/integration/test.sh create mode 100755 vote/integration_test.sh 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 From 2a35fe88806956c5ecc407640edced36174a0fd5 Mon Sep 17 00:00:00 2001 From: Gourav Shah Date: Mon, 8 Jul 2019 20:14:31 +0530 Subject: [PATCH 5/9] fixed e2e test run --- e2e.sh | 17 +++++++++++++++++ e2e/docker-compose.yml | 2 +- e2e/tests/Dockerfile | 2 +- e2e/tests/tests.sh | 12 +++++++++--- sonar-project.properties | 9 +++++++++ 5 files changed, 37 insertions(+), 5 deletions(-) create mode 100755 e2e.sh create mode 100644 sonar-project.properties 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/docker-compose.yml b/e2e/docker-compose.yml index 2715543309..1427ba2256 100644 --- a/e2e/docker-compose.yml +++ b/e2e/docker-compose.yml @@ -25,7 +25,7 @@ services: result: image: ${RESULT_IMAGE} - ports: ["80"] + ports: ["4000"] depends_on: - redis - db 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 4044cc63a7..e76354f5f8 100755 --- a/e2e/tests/tests.sh +++ b/e2e/tests/tests.sh @@ -1,10 +1,16 @@ #!/bin/bash +current="" +next="" + while ! timeout 1 bash -c "echo > /dev/tcp/vote/80"; do sleep 1 done -current=`phantomjs render.js http://result | grep -i vote | cut -d ">" -f 4 | cut -d " " -f1` +# 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-----------------" @@ -14,9 +20,9 @@ next=`echo "$(($current + 1))"` 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 | grep -i vote | cut -d ">" -f 4 | cut -d " " -f1` +new=`phantomjs render.js "http://result:4000/" | grep -i vote | cut -d ">" -f 4 | cut -d " " -f1` echo -e "\n\n-----------------" diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000000..b2717edddd --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,9 @@ +sonar.projectKey=instavoteaio +sonar.projectName=Instavote AIO +sonar.projectVersion=1.0 +# Comma-separated paths to directories with sources (required) +sonar.sources=. +# Encoding of the source files +sonar.sourceEncoding=UTF-8 +sonar.java.binaries=. +#sonar.coverage.jacoco.xmlReportPaths=worker/target From cd570f3458b6daf0fff7f5e32f5c4a43817aa228 Mon Sep 17 00:00:00 2001 From: Gourav Shah Date: Fri, 2 Aug 2019 16:20:50 +0530 Subject: [PATCH 6/9] added dep on python nose install python nose --- vote/requirements.txt | 1 + 1 file changed, 1 insertion(+) 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 From 396837485420cfa73151f3cff7e8e6b93e77b09f Mon Sep 17 00:00:00 2001 From: Gourav Shah Date: Wed, 30 Dec 2020 11:58:05 +0530 Subject: [PATCH 7/9] updated Dockerfile with my fixes --- result/Dockerfile | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) 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 From c34e083ad3d5d8a34183cfcd2dae133233edd336 Mon Sep 17 00:00:00 2001 From: Gourav Shah Date: Thu, 7 Jan 2021 18:32:58 +0530 Subject: [PATCH 8/9] Update sonar-project.properties --- sonar-project.properties | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/sonar-project.properties b/sonar-project.properties index b2717edddd..87b267dc19 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,9 +1,10 @@ -sonar.projectKey=instavoteaio +sonar.organization=gshah-org +sonar.projectKey=gshah-org_example-voting-app sonar.projectName=Instavote AIO -sonar.projectVersion=1.0 -# Comma-separated paths to directories with sources (required) -sonar.sources=. -# Encoding of the source files +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 From 7549c81b1854ce5e464ae98a38427342560baac2 Mon Sep 17 00:00:00 2001 From: Gourav Shah Date: Thu, 7 Jan 2021 18:34:54 +0530 Subject: [PATCH 9/9] update sonar project properties - add sonar org to make it compliant with sonar cloud - scan only worker repo --- sonar-project.properties | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sonar-project.properties b/sonar-project.properties index 87b267dc19..fa91ad96cc 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,5 +1,6 @@ -sonar.organization=gshah-org -sonar.projectKey=gshah-org_example-voting-app +# 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)