From 783713affe9c30b2ced1d7d5a4e349fad0b05e1d Mon Sep 17 00:00:00 2001 From: "Chris Bass (aa6164)" Date: Thu, 7 Nov 2019 09:12:48 +0000 Subject: [PATCH 1/3] Update test.sh --- exercises/03_acceptance/todo/test.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/exercises/03_acceptance/todo/test.sh b/exercises/03_acceptance/todo/test.sh index 2b9116b..49e14bd 100755 --- a/exercises/03_acceptance/todo/test.sh +++ b/exercises/03_acceptance/todo/test.sh @@ -1,5 +1,10 @@ #!/usr/bin/env bash +echo hello +mkdir -p screenshots +mkdir -p trace node index.js& node_modules/.bin/jest --runInBand --detectOpenHandles acceptance\ tests/* kill %1 +read -p "Press enter to continue" + From 40b2dd197a5ececce1650e66741144da88dc2d81 Mon Sep 17 00:00:00 2001 From: Mark Tyers Date: Sun, 10 Nov 2019 14:02:12 +0000 Subject: [PATCH 2/3] completed the lab exercise --- 03 Acceptance Testing.md | 25 ++++++++++++++++--- .../03_acceptance/cucumber/cucumberTest.sh | 2 +- .../todo/acceptance tests/ui.test.js | 2 +- exercises/03_acceptance/todo/public/style.css | 4 +++ exercises/03_acceptance/todo/test.sh | 3 +++ exercises/05_mqtt/chat/Messages.js | 19 ++++++++++++++ exercises/05_mqtt/chat/module.js | 1 + 7 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 exercises/05_mqtt/chat/Messages.js diff --git a/03 Acceptance Testing.md b/03 Acceptance Testing.md index 82b35cf..0923ece 100644 --- a/03 Acceptance Testing.md +++ b/03 Acceptance Testing.md @@ -94,9 +94,25 @@ You have seen how to use Puppeteer to write a test to see that we can add a sing ### 1.2 Snapshots +The acceptance tests in the previous section interact with the [Document Model Object](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction), they have no way to understand the appearance of the page as this is influenced by the [Cascading Stylesheets](https://developer.mozilla.org/en-US/docs/Web/CSS) that are being applied to the page. Clearly it is important that our acceptance tests should also check that the page renders as it should and [Snapshot Testing](https://jestjs.io/docs/en/snapshot-testing) enables us to achieve this. + +In essence we identify the important screens in the app (the ones we wish to monitor), get the layout the way we expect it to be and capture a screenshot of the page which is stored in a `__image_snapshots_/` directory then used as a reference whenever the test suite is subsequently run. Since you have already triggered an inital test run you should be able to locate this directory and examine the image file it contains. + +The snapshot is captured using the following two lines of code: + +```javascript +const image = await page.screenshot() +expect(image).toMatchImageSnapshot() +``` + +Sometimes you will need to modify the page layout however this will cause the snapshot test to fail since there will no longer be a match. The solution is to run the test suite but add a `-u` flag. This will update the snapshot files. + #### 1.2.1 Test Your Understanding -xxx +1. Modify the stylesheet and make the top level heading larger (perhaps `28pt`). +2. Now re-run the acceptance test suite, this will fail because the layout is now different. +3. Edit the `test.sh` script, adding the `-u` flag, save this change and rerun the test suite. +4. Remove the `-u` flag and rerun the tests to check these are still passing. ## 2 Profiling @@ -129,7 +145,9 @@ There are many more settings you can experiment with to help understand the perf ### 2.1 Test Your Understanding -xxx +1. Look at the graphs and figures that are produced for the todo code profiling and identify the different stages and different time taken for each. Are there any stages where you feel the timing is significantly long. +2. Run the todo code three times and compare the time taken for each of stages involved. Do they different? Is there any reason why this might be the case? +3. Using the har analyzer how long does it take for the server to respond. ### 2.2 Flame Graphs @@ -174,7 +192,8 @@ Hovering over a box will reveal more information. ### 2.2.1 Test Your Understanding -xxx +1. Looking at the flame graph which process is consuming the most CPU cycles? Is it possible to optimise this process to reduce the cycles? +2. Looking at the flame graph which process what is the hot path for the code. ## 3 Cucumber diff --git a/exercises/03_acceptance/cucumber/cucumberTest.sh b/exercises/03_acceptance/cucumber/cucumberTest.sh index cb5dab2..d4a900a 100755 --- a/exercises/03_acceptance/cucumber/cucumberTest.sh +++ b/exercises/03_acceptance/cucumber/cucumberTest.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash - +set -e node index.js& node_modules/.bin/cucumber-js ./features -r ./steps & sleep 2 diff --git a/exercises/03_acceptance/todo/acceptance tests/ui.test.js b/exercises/03_acceptance/todo/acceptance tests/ui.test.js index 0f0a964..4be8bfc 100644 --- a/exercises/03_acceptance/todo/acceptance tests/ui.test.js +++ b/exercises/03_acceptance/todo/acceptance tests/ui.test.js @@ -81,7 +81,7 @@ describe('todo list', () => { const image = await page.screenshot() // compare to the screenshot from the previous test run expect(image).toMatchImageSnapshot() - // stop logging to the trace file + // stop logging to the trace files await page.tracing.stop() await har.stop() done() diff --git a/exercises/03_acceptance/todo/public/style.css b/exercises/03_acceptance/todo/public/style.css index 8a5442c..d38f05f 100644 --- a/exercises/03_acceptance/todo/public/style.css +++ b/exercises/03_acceptance/todo/public/style.css @@ -3,6 +3,10 @@ body { font-family: Arial, Helvetica, sans-serif; } +h1 { + font-size: 18pt +} + .msg { border: 1px solid red; font-weight: bold; diff --git a/exercises/03_acceptance/todo/test.sh b/exercises/03_acceptance/todo/test.sh index 2b9116b..fb6c0b2 100755 --- a/exercises/03_acceptance/todo/test.sh +++ b/exercises/03_acceptance/todo/test.sh @@ -1,5 +1,8 @@ #!/usr/bin/env bash +set -e +mkdir -p screenshots +mkdir -p trace node index.js& node_modules/.bin/jest --runInBand --detectOpenHandles acceptance\ tests/* kill %1 diff --git a/exercises/05_mqtt/chat/Messages.js b/exercises/05_mqtt/chat/Messages.js new file mode 100644 index 0000000..8457de4 --- /dev/null +++ b/exercises/05_mqtt/chat/Messages.js @@ -0,0 +1,19 @@ + +'use strict' + +module.exports = class Messages { + constructor() { + console.log('constructor') + this.messages = [] + return this + } + extractData() { + + } + add() { + + } + getAll() { + + } +} diff --git a/exercises/05_mqtt/chat/module.js b/exercises/05_mqtt/chat/module.js index 947f4c7..d9b8421 100755 --- a/exercises/05_mqtt/chat/module.js +++ b/exercises/05_mqtt/chat/module.js @@ -7,6 +7,7 @@ const messages = ( function() { let msgs = [] return { extractData: payloadString => { + console.log('extracData') return payloadString }, add: data => { From 2b2eb4b864b1ea221294ba44371532006882c4e0 Mon Sep 17 00:00:00 2001 From: Mark Tyers Date: Mon, 11 Nov 2019 15:40:50 +0000 Subject: [PATCH 3/3] Started the DevOps Lab --- 04 Deployment.md | 2 - 04 DevOps.md | 28 ++++ exercises/03_acceptance/todo/test.sh | 2 + exercises/04_devops/.cpd.yml | 8 ++ exercises/04_devops/.eslintignore | 2 + exercises/04_devops/.eslintrc.json | 57 ++++++++ exercises/04_devops/.gitignore | 3 + exercises/04_devops/.gitlab-ci.yml | 80 +++++++++++ exercises/04_devops/.istanbul.yml | 10 ++ exercises/04_devops/README.md | 11 ++ exercises/04_devops/acceptance/api-spec.js | 21 +++ exercises/04_devops/acceptance/jasmine.json | 6 + exercises/04_devops/acceptance/testRunner.js | 20 +++ exercises/04_devops/index.js | 51 +++++++ exercises/04_devops/integration/jasmine.json | 6 + exercises/04_devops/integration/notes-spec.js | 18 +++ exercises/04_devops/integration/testRunner.js | 20 +++ exercises/04_devops/modules/notes.js | 41 ++++++ exercises/04_devops/modules/request.js | 14 ++ exercises/04_devops/notes.js | 12 ++ exercises/04_devops/package.json | 46 +++++++ exercises/04_devops/spec/jasmine.json | 7 + exercises/04_devops/spec/notes-spec.js | 125 ++++++++++++++++++ exercises/04_devops/spec/request-spec.js | 50 +++++++ exercises/04_devops/spec/testRunner.js | 20 +++ exercises/04_devops/status-codes.json | 64 +++++++++ 26 files changed, 722 insertions(+), 2 deletions(-) delete mode 100644 04 Deployment.md create mode 100644 04 DevOps.md create mode 100644 exercises/04_devops/.cpd.yml create mode 100644 exercises/04_devops/.eslintignore create mode 100644 exercises/04_devops/.eslintrc.json create mode 100644 exercises/04_devops/.gitignore create mode 100644 exercises/04_devops/.gitlab-ci.yml create mode 100644 exercises/04_devops/.istanbul.yml create mode 100644 exercises/04_devops/README.md create mode 100644 exercises/04_devops/acceptance/api-spec.js create mode 100644 exercises/04_devops/acceptance/jasmine.json create mode 100644 exercises/04_devops/acceptance/testRunner.js create mode 100644 exercises/04_devops/index.js create mode 100644 exercises/04_devops/integration/jasmine.json create mode 100644 exercises/04_devops/integration/notes-spec.js create mode 100644 exercises/04_devops/integration/testRunner.js create mode 100644 exercises/04_devops/modules/notes.js create mode 100644 exercises/04_devops/modules/request.js create mode 100644 exercises/04_devops/notes.js create mode 100644 exercises/04_devops/package.json create mode 100644 exercises/04_devops/spec/jasmine.json create mode 100644 exercises/04_devops/spec/notes-spec.js create mode 100644 exercises/04_devops/spec/request-spec.js create mode 100644 exercises/04_devops/spec/testRunner.js create mode 100644 exercises/04_devops/status-codes.json diff --git a/04 Deployment.md b/04 Deployment.md deleted file mode 100644 index f72fe44..0000000 --- a/04 Deployment.md +++ /dev/null @@ -1,2 +0,0 @@ - -# Deployment diff --git a/04 DevOps.md b/04 DevOps.md new file mode 100644 index 0000000..c7f5ea2 --- /dev/null +++ b/04 DevOps.md @@ -0,0 +1,28 @@ + +# DevOps + +In this lab you will learn how to deploy your app to a cloud server and how to set up a complete devops build chain. For this tutorial you will need to have an account on the [GitLab](https://gitlab.com) server as we will be learning how to use the GitLab CI continuous integration tools. (Note that GitHub has recently introduced a rival service called [GitHub Actions](https://github.com/features/actions) however at the time of writing this was still in Beta). + +Create an account using your University email address and log in. Create an empty private repository called devops and make a note of the git clone URL. + +You should now clone the `template-dynamic-website` repository (the original template, not the one you have been using for your assignment) into a temporary location on your computer. + +```shell +git clone https://github.coventry.ac.uk/web/template-dynamic-websites.git temp +``` + +Once cloned you need to _mirror push_ to the empty gitlab repository. + +```shell +cd temp/ && git push --mirror xxx +``` + +## Deploying to the Cloud + +Using Heroku + +## Running a CI Workflow + +Introduction to CI and gitlab Ci + +Committing and pushing to trigger the workflow. diff --git a/exercises/03_acceptance/todo/test.sh b/exercises/03_acceptance/todo/test.sh index 2b9116b..10fae29 100755 --- a/exercises/03_acceptance/todo/test.sh +++ b/exercises/03_acceptance/todo/test.sh @@ -1,5 +1,7 @@ #!/usr/bin/env bash +mkdir screenshots +mkdir trace node index.js& node_modules/.bin/jest --runInBand --detectOpenHandles acceptance\ tests/* kill %1 diff --git a/exercises/04_devops/.cpd.yml b/exercises/04_devops/.cpd.yml new file mode 100644 index 0000000..012ebd2 --- /dev/null +++ b/exercises/04_devops/.cpd.yml @@ -0,0 +1,8 @@ + +path: + - "/." +languages: + - javascript +exclude: + - "**/node_modules/**" +reporter: json diff --git a/exercises/04_devops/.eslintignore b/exercises/04_devops/.eslintignore new file mode 100644 index 0000000..55f99ce --- /dev/null +++ b/exercises/04_devops/.eslintignore @@ -0,0 +1,2 @@ +node_modules/ +docs/ \ No newline at end of file diff --git a/exercises/04_devops/.eslintrc.json b/exercises/04_devops/.eslintrc.json new file mode 100644 index 0000000..b8e04d0 --- /dev/null +++ b/exercises/04_devops/.eslintrc.json @@ -0,0 +1,57 @@ + +{ + "env": { + "es6": true, + "jasmine": true, + "node": true + }, + "parserOptions": { + "ecmaVersion": 8 + }, + "rules": { + "arrow-body-style": 2, + "arrow-spacing": [1, { "before": true, "after": true }], + "brace-style": 2, + "camelcase": [2, {"properties": "never"}], + "complexity": [2, {"max": 3}], + "eol-last": 1, + "eqeqeq": 2, + "global-require": 2, + "handle-callback-err": 2, + "indent": [1, "tab", { "SwitchCase": 1 }], + "key-spacing": ["error", { "beforeColon": false, "afterColon": true }], + "linebreak-style": [1, "unix"], + "max-depth": ["error", {"max": 4}], + "max-len": ["error", { "code": 80 }], + "max-nested-callbacks": ["error", {"max": 3}], + "max-params": ["error", {"max": 3}], + "max-statements": ["error", {"max": 10}], + "no-cond-assign": 2, + "no-dupe-args": 2, + "no-dupe-keys": 2, + "no-duplicate-case": 2, + "no-empty": 1, + "no-empty-function": 1, + "no-multiple-empty-lines": 1, + "no-extra-parens": 2, + "no-func-assign": 2, + "no-irregular-whitespace": 1, + "no-magic-numbers": [1, { "ignore": [-1, 0, 1, 2] }], + "no-multi-spaces": 1, + "no-multi-str": 1, + "no-unexpected-multiline": 2, + "no-unreachable": 2, + "no-self-assign": 2, + "no-trailing-spaces": 1, + "no-undef": 2, + "no-unused-vars": 1, + "no-var": 2, + "prefer-arrow-callback": 1, + "prefer-const": 2, + "quotes": [1, "single"], + "semi": [1, "never"], + "space-before-function-paren": [1, "never"], + "strict": [2, "global"], + "yoda": 2 + } +} diff --git a/exercises/04_devops/.gitignore b/exercises/04_devops/.gitignore new file mode 100644 index 0000000..9ca4b2a --- /dev/null +++ b/exercises/04_devops/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +node_modules/ +docs/ \ No newline at end of file diff --git a/exercises/04_devops/.gitlab-ci.yml b/exercises/04_devops/.gitlab-ci.yml new file mode 100644 index 0000000..2ccb1e5 --- /dev/null +++ b/exercises/04_devops/.gitlab-ci.yml @@ -0,0 +1,80 @@ +image: node:latest + +stages: + - code-testing + - staging-server + - acceptance-testing + +linting: + stage: code-testing + script: + - npm install + - npm run linter + +dependency-checks: + stage: code-testing + script: + - npm install + - npm run dependency + +code-duplication: + stage: code-testing + script: + - npm install + - npm run duplication + +unit-testing: + stage: code-testing + script: + - npm install + - npm test + +code-coverage: + stage: code-testing + script: + - npm install + - npm run coverage + - npm run check-coverage + +coverage-report: + stage: staging-server + script: + - npm install + - npm run coverage + #- mv ./docs/coverage/ public/ + artifacts: + paths: + - docs + expire_in: 30 days + only: + - master + +deploy-staging-server: + stage: staging-server + script: + - apt-get update -qy + - apt-get install -y ruby ruby-dev rubygems-integration + - gem install dpl + - dpl --provider=heroku --app=notes-api-test --api-key=95023c27-5a9d-4250-a3fd-d2e19e0dac02 + only: + - master + +acceptance-tests: + stage: acceptance-testing + script: + - npm install --only=dev + - npm run acceptance + only: + - master + +# https://docs.gitlab.com/ce/ci/examples/test-and-deploy-python-application-to-heroku.html + +# production: +# type: deploy +# script: +# - apt-get update -qy +# - apt-get install -y ruby-dev +# - gem install dpl +# - dpl --provider=heroku --app=gitlab-ci-python-test-prod --api-key=$HEROKU_PRODUCTION_API_KEY +# only: +# - tags \ No newline at end of file diff --git a/exercises/04_devops/.istanbul.yml b/exercises/04_devops/.istanbul.yml new file mode 100644 index 0000000..344a204 --- /dev/null +++ b/exercises/04_devops/.istanbul.yml @@ -0,0 +1,10 @@ + +instrumentation: + excludes: ['*-spec.js', 'spec/*', 'integration/*'] +reporting: + dir: ./docs/coverage/ + watermarks: + statements: [50, 80] + lines: [50, 80] + functions: [50, 80] + branches: [50, 80] diff --git a/exercises/04_devops/README.md b/exercises/04_devops/README.md new file mode 100644 index 0000000..aa0388a --- /dev/null +++ b/exercises/04_devops/README.md @@ -0,0 +1,11 @@ + +# Notes + +This code is designed to demonstrate the use of Continuous Integration. + +## Dependencies +``` +npm install +npm install --only=dev +npm install --only=production +``` \ No newline at end of file diff --git a/exercises/04_devops/acceptance/api-spec.js b/exercises/04_devops/acceptance/api-spec.js new file mode 100644 index 0000000..32b8041 --- /dev/null +++ b/exercises/04_devops/acceptance/api-spec.js @@ -0,0 +1,21 @@ + +'use strict' + +const frisby = require('frisby') +const status = require('../status-codes.json') +const url = 'https://notes-api-test.herokuapp.com' +console.log(url) +console.log(status.ok) + +it('should be a teapot', done => { + frisby.get('http://httpbin.org/status/200') + .expect('status', status.ok) + .done(done) +}); + +it ('should return a status of 200', done => { + frisby + .get(`${url}/notes`) + .expect('status', status.ok) + .done(done) +}) diff --git a/exercises/04_devops/acceptance/jasmine.json b/exercises/04_devops/acceptance/jasmine.json new file mode 100644 index 0000000..57f45b2 --- /dev/null +++ b/exercises/04_devops/acceptance/jasmine.json @@ -0,0 +1,6 @@ + +{ + "spec_files": [ + "acceptance/api-spec.js" + ] +} \ No newline at end of file diff --git a/exercises/04_devops/acceptance/testRunner.js b/exercises/04_devops/acceptance/testRunner.js new file mode 100644 index 0000000..9d211e9 --- /dev/null +++ b/exercises/04_devops/acceptance/testRunner.js @@ -0,0 +1,20 @@ + +'use strict' + +const Jasmine = require('jasmine') +const jasmine = new Jasmine() + +jasmine.loadConfigFile('acceptance/jasmine.json') + +const JasmineConsoleReporter = require('jasmine-console-reporter') +const reporter = new JasmineConsoleReporter({ + colors: true, + cleanStack: true, + verbosity: 3, + listStyle: 'indent', + activity: true +}) + +jasmine.addReporter(reporter) + +jasmine.execute() diff --git a/exercises/04_devops/index.js b/exercises/04_devops/index.js new file mode 100644 index 0000000..4510fc4 --- /dev/null +++ b/exercises/04_devops/index.js @@ -0,0 +1,51 @@ + +'use strict' + +// const etag = require('etag') +const restify = require('restify') +const server = restify.createServer() + +const restifyBodyParser = require('restify-plugins').bodyParser +server.use(restifyBodyParser()) +const restifyQueryParser = require('restify-plugins').queryParser +server.use(restifyQueryParser()) +const restifyAuthParser = require('restify-plugins').authorizationParser +server.use(restifyAuthParser()) + +const notes = require('./notes.js') +const defaultPort = 8080 + +const status = { + 'OK': 200, + 'badRequest': 400 +} + +server.get('/notes', async(req, res) => { + try { + const allNotes = notes.getAllNotes() + res.setHeader('content-type', 'application/json') + res.setHeader('accepts', 'GET') + res.send(status.OK, {notes: allNotes}) + } catch(err) { + res.send(status.badRequest, {error: err.message}) + } +}) + +server.post('/notes', async(req, res) => { + try { + //TODO + } catch(err) { + //TODO + } +}) + +console.log(`NODE_ENV: ${process.env['NODE_ENV']}`) +const port = process.env.PORT || defaultPort + +server.listen(port, err => { + if (err) { + console.error(err) + } else { + console.log(`App is ready on port ${port}`) + } +}) diff --git a/exercises/04_devops/integration/jasmine.json b/exercises/04_devops/integration/jasmine.json new file mode 100644 index 0000000..6b4b15a --- /dev/null +++ b/exercises/04_devops/integration/jasmine.json @@ -0,0 +1,6 @@ + +{ + "spec_files": [ + "integration/notes-spec.js" + ] +} \ No newline at end of file diff --git a/exercises/04_devops/integration/notes-spec.js b/exercises/04_devops/integration/notes-spec.js new file mode 100644 index 0000000..6e30fdd --- /dev/null +++ b/exercises/04_devops/integration/notes-spec.js @@ -0,0 +1,18 @@ + +'use strict' + +const notes = require('../notes.js') + +describe('integration module', () => { + describe('add notes', () => { + //TODO + }) + describe('get all notes', () => { + it('should return list of 2 notes', () => { + //TODO + }) + it('should throw an error if no notes', () => { + //TODO + }) + }) +}) diff --git a/exercises/04_devops/integration/testRunner.js b/exercises/04_devops/integration/testRunner.js new file mode 100644 index 0000000..cb95069 --- /dev/null +++ b/exercises/04_devops/integration/testRunner.js @@ -0,0 +1,20 @@ + +'use strict' + +const Jasmine = require('jasmine') +const jasmine = new Jasmine() + +jasmine.loadConfigFile('spec/jasmine.json') + +const JasmineConsoleReporter = require('jasmine-console-reporter') +const reporter = new JasmineConsoleReporter({ + colors: true, + cleanStack: true, + verbosity: 3, + listStyle: 'indent', + activity: true +}) + +jasmine.addReporter(reporter) + +jasmine.execute() diff --git a/exercises/04_devops/modules/notes.js b/exercises/04_devops/modules/notes.js new file mode 100644 index 0000000..84b6090 --- /dev/null +++ b/exercises/04_devops/modules/notes.js @@ -0,0 +1,41 @@ + +'use strict' + +let noteArray = [] + +exports.add = note => { + console.log(note) + validateNote(note) + noteArray.push(note) + console.log(noteArray.length) + return note +} + +exports.get = index => { + if (index < 0 || index > noteArray.length -1) { + throw new Error(`no note at index ${index}`) + } + return noteArray[index] +} + +exports.delete = index => { + if (index < 0 || index > noteArray.length -1) { + throw new Error(`no note at index ${index}`) + } + noteArray.splice(index, 1) +} + +exports.clear = () => { + noteArray = [] +} + +exports.getAll = () => noteArray + +exports.count = () => noteArray.length + +function validateNote(note) { + if (note.title === undefined || note.text === undefined) { + throw new Error('invalid note') + } + return +} diff --git a/exercises/04_devops/modules/request.js b/exercises/04_devops/modules/request.js new file mode 100644 index 0000000..40eb47a --- /dev/null +++ b/exercises/04_devops/modules/request.js @@ -0,0 +1,14 @@ + +'use strict' + +exports.getParameter = (request, param) => { + if (request.params === undefined || request.params[param] === undefined) + throw new Error(`parameter ${param} missing`) + return request.params[param] +} + +exports.extractBodyKey = (req, key) => { + if (req.body === undefined || req.body[key] === undefined) + throw new Error(`missing key ${key} in request body`) + return req.body[key] +} diff --git a/exercises/04_devops/notes.js b/exercises/04_devops/notes.js new file mode 100644 index 0000000..b074436 --- /dev/null +++ b/exercises/04_devops/notes.js @@ -0,0 +1,12 @@ + +'use strict' + +const notes = require('./modules/notes.js') +const request = require('./modules/request.js') + +exports.getAllNotes = () => notes.getAll() + +exports.addNote = req => { + //TODO + const note = request.extractBodyKey(req, note) +} diff --git a/exercises/04_devops/package.json b/exercises/04_devops/package.json new file mode 100644 index 0000000..df263a4 --- /dev/null +++ b/exercises/04_devops/package.json @@ -0,0 +1,46 @@ +{ + "name": "continuous-integration-example", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "node index.js", + "run": "node index.js", + "test": "node spec/testRunner", + "linter": "node_modules/.bin/eslint init && node_modules/.bin/eslint .", + "duplication": "node_modules/.bin/jscpd -p . --config .cpd.yml", + "dependency": "node_modules/.bin/dependency-check --unused --no-dev . && node_modules/.bin/dependency-check -i modules --missing .", + "coverage": "node_modules/.bin/istanbul cover spec/testRunner.js", + "check-coverage": "node_modules/.bin/istanbul check-coverage --statement 100 --branch 100 --function 100 --line 100", + "acceptance": "node acceptance/testRunner" + }, + "pre-commit": [ + "linter", + "duplication", + "dependency" + ], + "repository": { + "type": "git", + "url": "git+ssh://git@gitlab.com/covcom/continuous-integration-example.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://gitlab.com/covcom/continuous-integration-example/issues" + }, + "homepage": "https://gitlab.com/covcom/continuous-integration-example#README", + "devDependencies": { + "dependency-check": "^2.9.2", + "eslint": "^4.12.1", + "frisby": "^2.0.10", + "istanbul": "^0.4.5", + "jasmine": "^2.8.0", + "jasmine-console-reporter": "^2.0.1", + "jscpd": "^0.6.17", + "pre-commit": "^1.2.2" + }, + "dependencies": { + "restify": "^6.3.4", + "restify-plugins": "^1.6.0" + } +} diff --git a/exercises/04_devops/spec/jasmine.json b/exercises/04_devops/spec/jasmine.json new file mode 100644 index 0000000..78443c5 --- /dev/null +++ b/exercises/04_devops/spec/jasmine.json @@ -0,0 +1,7 @@ + +{ + "spec_files": [ + "spec/notes-spec.js", + "spec/request-spec.js" + ] +} diff --git a/exercises/04_devops/spec/notes-spec.js b/exercises/04_devops/spec/notes-spec.js new file mode 100644 index 0000000..633d754 --- /dev/null +++ b/exercises/04_devops/spec/notes-spec.js @@ -0,0 +1,125 @@ + +'use strict' + +const notes = require('../modules/notes.js') + +describe('notes module', () => { + + describe('add', () => { + + beforeEach( () => { + console.log('clearing notes') + notes.clear() + }) + + it('should be able to add a valid note', () => { + try { + expect(notes.count()).toBe(0) + notes.add({title: 'Hello', text: 'World'}) + expect(notes.count()).toBe(1) + } catch(err) { + expect(1).toBe(0) // this should never run! + } + }) + + it('should throw error if title missing', () => { + try { + expect(notes.count()).toBe(0) + notes.add({text: 'Hello'}) + expect(1).toBe(0) + } catch(err) { + expect(notes.count()).toBe(0) + expect(err.message).toBe('invalid note') + } + }) + + it('should throw error if text missing', () => { + try { + expect(notes.count()).toBe(0) + notes.add({title: 'Hello'}) + expect(1).toBe(0) + } catch(err) { + expect(notes.count()).toBe(0) + expect(err.message).toBe('invalid note') + } + }) + + }) + + describe('get', () => { + + beforeEach( () => { + notes.clear() + notes.add({title: 'Hello', text: 'World'}) + }) + + it('should retrieve a single note', () => { + try { + expect(notes.count()).toBe(1) + const note = notes.get(0) + expect(note.title).toBe('Hello') + expect(note.text).toBe('World') + } catch(err) { + expect(1).toBe(0) // this should never run! + } + }) + + it('should reject a negative index', () => { + try { + expect(notes.count()).toBe(1) + notes.get(-1) + expect(1).toBe(0) + } catch(err) { + expect(err.message).toBe('no note at index -1') + } + }) + + }) + + describe('delete', () => { + + beforeEach( () => { + notes.clear() + notes.add({title: 'Hello', text: 'World'}) + notes.add({title: 'Note Two', text: 'text for note two'}) + }) + + it('should delete a valid note index', () => { + try { + expect(notes.count()).toBe(2) + notes.delete(0) + expect(notes.count()).toBe(1) + const note = notes.get(0) + expect(note.title).toBe('Note Two') + } catch(err) { + expect(1).toBe(0) + } + }) + + it('should reject a negative index', () => { + try { + expect(notes.count()).toBe(2) + notes.delete(-1) + } catch(err) { + expect(notes.count()).toBe(2) + const note = notes.get(1) + expect(note.title).toBe('Note Two') + } + }) + + }) + + describe('getAll', () => { + beforeEach( () => { + notes.clear() + notes.add({title: 'Note One', text: 'text for note one'}) + notes.add({title: 'Note Two', text: 'text for note two'}) + }) + it('retrieves all notes', () => { + const allNotes = notes.getAll() + expect(allNotes.length).toBe(2) + expect(allNotes[0].title).toBe('Note One') + expect(allNotes[1].title).toBe('Note Two') + }) + }) +}) diff --git a/exercises/04_devops/spec/request-spec.js b/exercises/04_devops/spec/request-spec.js new file mode 100644 index 0000000..de384aa --- /dev/null +++ b/exercises/04_devops/spec/request-spec.js @@ -0,0 +1,50 @@ + +'use strict' + +const request = require('../modules/request.js') + +describe('request module', () => { + describe('getParameter', () => { + + it('should extract valid parameter', () => { + try { + const req = {params: {param1: 'hello'}} + const param = request.getParameter(req, 'param1') + expect(param).toBe('hello') + } catch(err) { + expect(1).toBe(0) // this should never run! + } + }) + + it('should throw error if param is missing', () => { + try { + const req = {params: {param2: 'hello'}} + request.getParameter(req, 'param1') + expect(1).toBe(0) + } catch(err) { + expect(err.message).toBe('parameter param1 missing') + } + }) + }) + describe('extractBodyKey', () => { + it('should extract valid body key', () => { + try { + const req = {body: {key1: 'hello'}} + const key = request.extractBodyKey(req, 'key1') + expect(key).toBe('hello') + } catch(err) { + expect(1).toBe(0) // this should never run! + } + }) + + it('should throw error if key is missing', () => { + try { + const req = {body: {key2: 'hello'}} + request.extractBodyKey(req, 'key1') + expect(1).toBe(0) // this should never run! + } catch(err) { + expect(err.message).toBe('missing key key1 in request body') + } + }) + }) +}) diff --git a/exercises/04_devops/spec/testRunner.js b/exercises/04_devops/spec/testRunner.js new file mode 100644 index 0000000..cb95069 --- /dev/null +++ b/exercises/04_devops/spec/testRunner.js @@ -0,0 +1,20 @@ + +'use strict' + +const Jasmine = require('jasmine') +const jasmine = new Jasmine() + +jasmine.loadConfigFile('spec/jasmine.json') + +const JasmineConsoleReporter = require('jasmine-console-reporter') +const reporter = new JasmineConsoleReporter({ + colors: true, + cleanStack: true, + verbosity: 3, + listStyle: 'indent', + activity: true +}) + +jasmine.addReporter(reporter) + +jasmine.execute() diff --git a/exercises/04_devops/status-codes.json b/exercises/04_devops/status-codes.json new file mode 100644 index 0000000..7a46ef0 --- /dev/null +++ b/exercises/04_devops/status-codes.json @@ -0,0 +1,64 @@ + +{ + "continue": 100, + "switchingProtocols": 101, + "processing": 102, + "earlyHints": 103, + "ok": 200, + "created": 201, + "accepted": 202, + "nonAuthoritativeInformation": 203, + "noContent": 204, + "resetContent": 205, + "partialContent": 206, + "multiStatus": 207, + "alreadyReported": 208, + "imUsed": 226, + "multipleChoices": 300, + "movedPermanently": 301, + "Found": 302, + "seeOther": 303, + "notModified": 304, + "useProxy": 305, + "temporaryRedirect": 307, + "permanentRedirect": 308, + "badRequest": 400, + "unauthorized": 401, + "paymentRequired": 402, + "forbidden": 403, + "notFound": 404, + "methodNotAllowed": 405, + "notAcceptable": 406, + "proxyAuthenticationRequired": 407, + "requestTimeout": 408, + "conflict": 409, + "gone": 410, + "lengthRequired": 411, + "preconditionFailed": 412, + "payloadTooLarge": 413, + "uriTooLong": 414, + "unsupportedMediaType": 415, + "rangeNotSatisfiable": 416, + "expectationFailed": 417, + "imaTeapot": 418, + "misdirectedRequest": 421, + "unprocessableEntity": 422, + "locked": 423, + "failedDependency": 424, + "upgradeRequired": 426, + "preconditionRequired": 428, + "tooManyRequests": 429, + "requestHeaderFieldsTooLarge": 431, + "unavailableForLegalReasons": 451, + "internalServerError": 500, + "notImplemented": 501, + "badGateway": 502, + "serviceUnavailable": 503, + "gatewayTimeout": 504, + "httpVersionNotSupported": 505, + "variantAlsoNegotiates": 506, + "insufficientStorage": 507, + "loopDetected": 508, + "notExtended": 510, + "networkAuthenticationRequired": 511 +} \ No newline at end of file