From fbf8cc2d0303bf46dc664a2ec8f9fd4cf411d4f9 Mon Sep 17 00:00:00 2001 From: James Allen Date: Thu, 23 Nov 2017 16:01:32 +0000 Subject: [PATCH 01/12] Run acceptance tests via docker compose --- services/web/Jenkinsfile | 11 ++-- services/web/config/settings.defaults.coffee | 15 +++-- services/web/docker-compose.ci.yml | 12 ++++ services/web/docker-compose.yml | 42 ++++++++++++ services/web/makefile | 64 +++++++++++++++++++ services/web/package.json | 11 ++++ .../coffee/helpers/MockDocUpdaterApi.coffee | 2 +- .../coffee/helpers/MockDocstoreApi.coffee | 2 +- .../acceptance/coffee/helpers/redis.coffee | 1 - .../acceptance/coffee/helpers/request.coffee | 2 +- 10 files changed, 144 insertions(+), 18 deletions(-) create mode 100644 services/web/docker-compose.ci.yml create mode 100644 services/web/docker-compose.yml create mode 100644 services/web/makefile diff --git a/services/web/Jenkinsfile b/services/web/Jenkinsfile index 33aa807fdd..85ecafef58 100644 --- a/services/web/Jenkinsfile +++ b/services/web/Jenkinsfile @@ -117,19 +117,16 @@ pipeline { } } steps { + sh 'make install' + sh 'make test_unit MOCHA_ARGS="--reporter=tap"' sh 'env NODE_ENV=development ./node_modules/.bin/grunt mochaTest:unit --reporter=tap' } } stage('Acceptance Tests') { steps { - // This tagged relase of the acceptance test runner is a temporary fix - // to get the acceptance tests working before we move to a - // docker-compose workflow. See: - // https://github.com/sharelatex/web-sharelatex-internal/pull/148 - - sh 'docker pull sharelatex/sl-acceptance-test-runner:node-6.9-mongo-3.4' - sh 'docker run --rm -v $(pwd):/app --env SHARELATEX_ALLOW_PUBLIC_ACCESS=true sharelatex/sl-acceptance-test-runner:node-6.9-mongo-3.4 || (cat forever/app.log && false)' + sh 'make install' + sh "make test_acceptance MOCHA_ARGS="--reporter=tap"' } } diff --git a/services/web/config/settings.defaults.coffee b/services/web/config/settings.defaults.coffee index 69bf0a3b7c..fccd197a83 100644 --- a/services/web/config/settings.defaults.coffee +++ b/services/web/config/settings.defaults.coffee @@ -35,12 +35,12 @@ module.exports = settings = # Databases # --------- mongo: - url : 'mongodb://127.0.0.1/sharelatex' + url : "mongodb://#{process.env['MONGO_HOST'] || '127.0.0.1'}/sharelatex" redis: web: - host: "localhost" - port: "6379" + host: process.env['REDIS_HOST'] || "localhost" + port: process.env['REDIS_PORT'] || "6379" password: "" # websessions: @@ -74,8 +74,8 @@ module.exports = settings = # ] api: - host: "localhost" - port: "6379" + host: process.env['REDIS_HOST'] || "localhost" + port: process.env['REDIS_PORT'] || "6379" password: "" # Service locations @@ -87,6 +87,7 @@ module.exports = settings = internal: web: port: webPort = 3000 + host: process.env['LISTEN_ADDRESS'] or 'localhost' documentupdater: port: docUpdaterPort = 3003 @@ -99,7 +100,7 @@ module.exports = settings = user: httpAuthUser pass: httpAuthPass documentupdater: - url : "http://localhost:#{docUpdaterPort}" + url : "http://#{process.env['DOCUPDATER_HOST'] or 'localhost'}:#{docUpdaterPort}" thirdPartyDataStore: url : "http://localhost:3002" emptyProjectFlushDelayMiliseconds: 5 * seconds @@ -113,7 +114,7 @@ module.exports = settings = enabled: false url : "http://localhost:3054" docstore: - url : "http://localhost:3016" + url : "http://#{process.env['DOCSTORE_HOST'] or 'localhost'}:3016" pubUrl: "http://localhost:3016" chat: url: "http://localhost:3010" diff --git a/services/web/docker-compose.ci.yml b/services/web/docker-compose.ci.yml new file mode 100644 index 0000000000..0bb7af5f74 --- /dev/null +++ b/services/web/docker-compose.ci.yml @@ -0,0 +1,12 @@ +version: "2" + +services: + test_unit: + image: quay.io/sharelatex/$PROJECT_NAME:$BRANCH_NAME-$BUILD_NUMBER + user: root + volumes: [] + + test_acceptance: + image: quay.io/sharelatex/$PROJECT_NAME:$BRANCH_NAME-$BUILD_NUMBER + user: root + volumes: [] diff --git a/services/web/docker-compose.yml b/services/web/docker-compose.yml new file mode 100644 index 0000000000..fe30bb8f57 --- /dev/null +++ b/services/web/docker-compose.yml @@ -0,0 +1,42 @@ +version: "2" + +volumes: + node_modules: + +services: + npm: + image: node:6.9.5 + volumes: + - .:/app + - node_modules:/app/node_modules + working_dir: /app + + test_unit: + image: node:6.9.5 + volumes: + - .:/app + - node_modules:/app/node_modules + working_dir: /app + command: npm run test:unit + + test_acceptance: + image: node:6.9.5 + volumes: + - .:/app + - node_modules:/app/node_modules + environment: + REDIS_HOST: redis + MONGO_HOST: mongo + SHARELATEX_ALLOW_PUBLIC_ACCESS: 'true' + LISTEN_ADDRESS: 0.0.0.0 + depends_on: + - redis + - mongo + working_dir: /app + command: npm run start + + redis: + image: redis + + mongo: + image: mongo:3.4.6 diff --git a/services/web/makefile b/services/web/makefile new file mode 100644 index 0000000000..c6e1516598 --- /dev/null +++ b/services/web/makefile @@ -0,0 +1,64 @@ +NPM := docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} run --rm npm npm +BUILD_NUMBER ?= local +BRANCH_NAME ?= $(shell git rev-parse --abbrev-ref HEAD) +PROJECT_NAME = web + +all: install test + @echo "Run:" + @echo " make install to set up the project dependencies (in docker)" + @echo " make test to run all the tests for the project (in docker)" + @echo " make run to run the app (natively)" + +add: + $(NPM) install --save ${P} + +add_dev: + $(NPM) install --save-dev ${P} + +install: + $(NPM) install + +clean: + rm app.js + rm -r app/js + rm -r test/unit/js + rm -r test/acceptance/js + +test: test_unit test_acceptance + +test_unit: + docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} run --rm test_unit npm run test:unit -- ${MOCHA_ARGS} + +test_acceptance: test_acceptance_start_service test_acceptance_run test_acceptance_stop_service + +test_acceptance_start_service: + docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} up -d test_acceptance + +test_acceptance_stop_service: + docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} stop test_acceptance + +test_acceptance_run: + docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} exec -T test_acceptance npm run test:acceptance -- ${MOCHA_ARGS} + +build: + docker build --pull --tag quay.io/sharelatex/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER) . + +publish: + docker push quay.io/sharelatex/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER) + +ci: + # When we run the tests locally we mount the local directory as a volumne + # and use a persistent node_modules folder (see docker-compose.yml). + # However, on the CI server, we want to run our tests in the image that we + # have built for deployment, which is what the docker-compose.ci.yml + # override does. + PROJECT_NAME=$(PROJECT_NAME) \ + BRANCH_NAME=$(BRANCH_NAME) \ + BUILD_NUMBER=$(BUILD_NUMBER) \ + DOCKER_COMPOSE_FLAGS="-f docker-compose.ci.yml" \ + $(MAKE) build test publish + +.PHONY: + add install update test test_unit test_acceptance \ + test_acceptance_start_service test_acceptance_stop_service \ + test_acceptance_run build publish ci diff --git a/services/web/package.json b/services/web/package.json index e831d929e1..f4ea19a49d 100644 --- a/services/web/package.json +++ b/services/web/package.json @@ -9,6 +9,17 @@ "directories": { "public": "./public" }, + "scripts": { + "test:acceptance:wait_for_app": "echo 'Waiting for app to be accessible' && while (! curl -s -o /dev/null localhost:3000/status) do sleep 1; done", + "test:acceptance:run": "mocha --recursive --reporter spec --timeout 15000 $@ test/acceptance/js", + "test:acceptance": "npm run compile:acceptance_tests && npm run test:acceptance:wait_for_app && npm run test:acceptance:run -- $@", + "test:unit": "npm run compile:app && npm run compile:unit_tests && mocha --recursive --reporter spec $@ test/unit/js", + "compile:unit_tests": "coffee -o test/unit/js -c test/unit/coffee", + "compile:acceptance_tests": "coffee -o test/acceptance/js -c test/acceptance/coffee", + "compile:app": "coffee -o app/js -c app/coffee && coffee -c app.coffee", + "start": "npm run compile:app && node app.js", + "echo": "echo $@" + }, "dependencies": { "archiver": "0.9.0", "async": "0.6.2", diff --git a/services/web/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee b/services/web/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee index aefcd4513a..6f307b0810 100644 --- a/services/web/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee +++ b/services/web/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee @@ -6,7 +6,7 @@ module.exports = MockDocUpdaterApi = app.post "/project/:project_id/flush", (req, res, next) => res.sendStatus 200 - app.listen 3003, (error) -> + app.listen 3003, '0.0.0.0', (error) -> throw error if error? .on "error", (error) -> console.error "error starting MockDocUpdaterApi:", error.message diff --git a/services/web/test/acceptance/coffee/helpers/MockDocstoreApi.coffee b/services/web/test/acceptance/coffee/helpers/MockDocstoreApi.coffee index 2133d40b9f..7c8f5dbe50 100644 --- a/services/web/test/acceptance/coffee/helpers/MockDocstoreApi.coffee +++ b/services/web/test/acceptance/coffee/helpers/MockDocstoreApi.coffee @@ -22,7 +22,7 @@ module.exports = MockDocStoreApi = docs = (doc for doc_id, doc of @docs[req.params.project_id]) res.send JSON.stringify docs - app.listen 3016, (error) -> + app.listen 3016, '0.0.0.0', (error) -> throw error if error? .on "error", (error) -> console.error "error starting MockDocStoreApi:", error.message diff --git a/services/web/test/acceptance/coffee/helpers/redis.coffee b/services/web/test/acceptance/coffee/helpers/redis.coffee index 9aecf6b387..7c48f97d2e 100644 --- a/services/web/test/acceptance/coffee/helpers/redis.coffee +++ b/services/web/test/acceptance/coffee/helpers/redis.coffee @@ -1,5 +1,4 @@ Settings = require('settings-sharelatex') -redis = require('redis-sharelatex') logger = require("logger-sharelatex") Async = require('async') diff --git a/services/web/test/acceptance/coffee/helpers/request.coffee b/services/web/test/acceptance/coffee/helpers/request.coffee index 879acd843a..1c7120d141 100644 --- a/services/web/test/acceptance/coffee/helpers/request.coffee +++ b/services/web/test/acceptance/coffee/helpers/request.coffee @@ -1,4 +1,4 @@ -BASE_URL = "http://localhost:3000" +BASE_URL = "http://#{process.env["HTTP_TEST_HOST"] or "localhost"}:3000" module.exports = require("request").defaults({ baseUrl: BASE_URL, followRedirect: false From d9d7c9695887c0d1ad72bb644f8602246f5771ab Mon Sep 17 00:00:00 2001 From: James Allen Date: Thu, 23 Nov 2017 16:45:46 +0000 Subject: [PATCH 02/12] Get module unit tests running inside Docker as well as main tests --- services/web/Jenkinsfile | 12 +----- services/web/README.md | 63 ++++++++++++++++++++++++++++++ services/web/docker-compose.ci.yml | 12 ------ services/web/makefile | 21 +--------- services/web/package.json | 10 ++--- 5 files changed, 71 insertions(+), 47 deletions(-) delete mode 100644 services/web/docker-compose.ci.yml diff --git a/services/web/Jenkinsfile b/services/web/Jenkinsfile index 85ecafef58..59347a266c 100644 --- a/services/web/Jenkinsfile +++ b/services/web/Jenkinsfile @@ -109,23 +109,15 @@ pipeline { } } - stage('Unit Test') { - agent { - docker { - image 'node:6.9.5' - reuseNode true - } - } + stage('Unit Tests') { steps { sh 'make install' - sh 'make test_unit MOCHA_ARGS="--reporter=tap"' - sh 'env NODE_ENV=development ./node_modules/.bin/grunt mochaTest:unit --reporter=tap' + sh "make test_unit MOCHA_ARGS="--reporter=tap"' } } stage('Acceptance Tests') { steps { - sh 'make install' sh "make test_acceptance MOCHA_ARGS="--reporter=tap"' } } diff --git a/services/web/README.md b/services/web/README.md index f777e7e5f5..51f73d02ec 100644 --- a/services/web/README.md +++ b/services/web/README.md @@ -17,6 +17,69 @@ web-sharelatex uses [Grunt](http://gruntjs.com/) to build its front-end related Image processing tasks are commented out in the gruntfile and the needed packages aren't presently in the project's `package.json`. If the images need to be processed again (minified and sprited), start by fetching the packages (`npm install grunt-contrib-imagemin grunt-sprity`), then *decomment* the tasks in `Gruntfile.coffee`. After this, the tasks can be called (explicitly, via `grunt imagemin` and `grunt sprity`). +New Docker-based build process +------------------------------ + +Note that the Grunt workflow from above should still work, but we are transitioning to a +Docker based testing workflow, which is documented below: + +### Running the app + +The app runs natively using npm and Node on the local system: + +``` +$ npm install +$ npm run start +``` + +*Ideally the app would run in Docker like the tests below, but with host networking not supported in OS X, we need to run it natively until all services are Dockerised.* + +### Unit Tests + +The test suites run in Docker. + +Unit tests can be run in the `test_unit` container defined in `docker-compose.tests.yml`. + +The makefile contains a short cut to run these: + +``` +make install # Only needs running once, or when npm packages are updated +make unit_test +``` + +During development it is often useful to only run a subset of tests, which can be configured with arguments to the mocha CLI: + +``` +make unit_test MOCHA_ARGS='--grep=AuthorizationManager' +``` + +### Acceptance Tests + +Acceptance tests are run against a live service, which runs in the `acceptance_test` container defined in `docker-compose.tests.yml`. + +To run the tests out-of-the-box, the makefile defines: + +``` +make install # Only needs running once, or when npm packages are updated +make acceptance_test +``` + +However, during development it is often useful to leave the service running for rapid iteration on the acceptance tests. This can be done with: + +``` +make acceptance_test_start_service +make acceptance_test_run # Run as many times as needed during development +make acceptance_test_stop_service +``` + +`make acceptance_test` just runs these three commands in sequence. + +During development it is often useful to only run a subset of tests, which can be configured with arguments to the mocha CLI: + +``` +make acceptance_test_run MOCHA_ARGS='--grep=AuthorizationManager' +``` + Unit test status ---------------- diff --git a/services/web/docker-compose.ci.yml b/services/web/docker-compose.ci.yml deleted file mode 100644 index 0bb7af5f74..0000000000 --- a/services/web/docker-compose.ci.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: "2" - -services: - test_unit: - image: quay.io/sharelatex/$PROJECT_NAME:$BRANCH_NAME-$BUILD_NUMBER - user: root - volumes: [] - - test_acceptance: - image: quay.io/sharelatex/$PROJECT_NAME:$BRANCH_NAME-$BUILD_NUMBER - user: root - volumes: [] diff --git a/services/web/makefile b/services/web/makefile index c6e1516598..8d6ebf7c2e 100644 --- a/services/web/makefile +++ b/services/web/makefile @@ -7,7 +7,6 @@ all: install test @echo "Run:" @echo " make install to set up the project dependencies (in docker)" @echo " make test to run all the tests for the project (in docker)" - @echo " make run to run the app (natively)" add: $(NPM) install --save ${P} @@ -40,25 +39,7 @@ test_acceptance_stop_service: test_acceptance_run: docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} exec -T test_acceptance npm run test:acceptance -- ${MOCHA_ARGS} -build: - docker build --pull --tag quay.io/sharelatex/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER) . - -publish: - docker push quay.io/sharelatex/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER) - -ci: - # When we run the tests locally we mount the local directory as a volumne - # and use a persistent node_modules folder (see docker-compose.yml). - # However, on the CI server, we want to run our tests in the image that we - # have built for deployment, which is what the docker-compose.ci.yml - # override does. - PROJECT_NAME=$(PROJECT_NAME) \ - BRANCH_NAME=$(BRANCH_NAME) \ - BUILD_NUMBER=$(BUILD_NUMBER) \ - DOCKER_COMPOSE_FLAGS="-f docker-compose.ci.yml" \ - $(MAKE) build test publish - .PHONY: add install update test test_unit test_acceptance \ test_acceptance_start_service test_acceptance_stop_service \ - test_acceptance_run build publish ci + test_acceptance_run diff --git a/services/web/package.json b/services/web/package.json index f4ea19a49d..a795df999e 100644 --- a/services/web/package.json +++ b/services/web/package.json @@ -11,12 +11,12 @@ }, "scripts": { "test:acceptance:wait_for_app": "echo 'Waiting for app to be accessible' && while (! curl -s -o /dev/null localhost:3000/status) do sleep 1; done", - "test:acceptance:run": "mocha --recursive --reporter spec --timeout 15000 $@ test/acceptance/js", + "test:acceptance:run": "bin/acceptance_test $@", "test:acceptance": "npm run compile:acceptance_tests && npm run test:acceptance:wait_for_app && npm run test:acceptance:run -- $@", - "test:unit": "npm run compile:app && npm run compile:unit_tests && mocha --recursive --reporter spec $@ test/unit/js", - "compile:unit_tests": "coffee -o test/unit/js -c test/unit/coffee", - "compile:acceptance_tests": "coffee -o test/acceptance/js -c test/acceptance/coffee", - "compile:app": "coffee -o app/js -c app/coffee && coffee -c app.coffee", + "test:unit": "npm run compile:app && npm run compile:unit_tests && bin/unit_test $@", + "compile:unit_tests": "bin/compile_unit_tests", + "compile:acceptance_tests": "bin/compile_acceptance_tests", + "compile:app": "bin/compile_app", "start": "npm run compile:app && node app.js", "echo": "echo $@" }, From 7efef129814052733c4b2360a51627cf9e5be19c Mon Sep 17 00:00:00 2001 From: James Allen Date: Thu, 23 Nov 2017 16:46:49 +0000 Subject: [PATCH 03/12] Fix Jenkinsfile syntax --- services/web/Jenkinsfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/web/Jenkinsfile b/services/web/Jenkinsfile index 59347a266c..0650eb4edb 100644 --- a/services/web/Jenkinsfile +++ b/services/web/Jenkinsfile @@ -112,13 +112,13 @@ pipeline { stage('Unit Tests') { steps { sh 'make install' - sh "make test_unit MOCHA_ARGS="--reporter=tap"' + sh 'make test_unit MOCHA_ARGS="--reporter=tap"' } } stage('Acceptance Tests') { steps { - sh "make test_acceptance MOCHA_ARGS="--reporter=tap"' + sh 'make test_acceptance MOCHA_ARGS="--reporter=tap"' } } From 492b37aa6e040f5e250d908311805b471b456880 Mon Sep 17 00:00:00 2001 From: James Allen Date: Thu, 23 Nov 2017 16:52:43 +0000 Subject: [PATCH 04/12] Add missing bin/ files --- services/web/bin/acceptance_test | 18 ++++++++++++++++++ services/web/bin/compile_acceptance_tests | 16 ++++++++++++++++ services/web/bin/compile_app | 23 +++++++++++++++++++++++ services/web/bin/compile_unit_tests | 15 +++++++++++++++ services/web/bin/unit_test | 14 ++++++++++++++ 5 files changed, 86 insertions(+) create mode 100755 services/web/bin/acceptance_test create mode 100755 services/web/bin/compile_acceptance_tests create mode 100755 services/web/bin/compile_app create mode 100755 services/web/bin/compile_unit_tests create mode 100755 services/web/bin/unit_test diff --git a/services/web/bin/acceptance_test b/services/web/bin/acceptance_test new file mode 100755 index 0000000000..717e8542ad --- /dev/null +++ b/services/web/bin/acceptance_test @@ -0,0 +1,18 @@ +#!/bin/bash +set -e; + +MOCHA="node_modules/.bin/mocha --recursive --reporter spec --timeout 15000" + +$MOCHA "$@" test/acceptance/js + +# TODO: Module acceptance tests are hard to get working, +# because they typically require the server to be instantiated +# with a different config. + +# for dir in modules/*; +# do +# if [ -d $dir/test/acceptance/js ]; then +# $MOCHA "$@" $dir/test/acceptance/js +# fi +# done + diff --git a/services/web/bin/compile_acceptance_tests b/services/web/bin/compile_acceptance_tests new file mode 100755 index 0000000000..d60ba0cc46 --- /dev/null +++ b/services/web/bin/compile_acceptance_tests @@ -0,0 +1,16 @@ +#!/bin/bash +set -e; + +COFFEE=node_modules/.bin/coffee + +echo Compiling test/acceptance/coffee; +$COFFEE -o test/acceptance/js -c test/acceptance/coffee; + +for dir in modules/*; +do + + if [ -d $dir/test/acceptance ]; then + echo Compiling $dir/test/acceptance/coffee; + $COFFEE -o $dir/test/acceptance/js -c $dir/test/acceptance/coffee; + fi +done \ No newline at end of file diff --git a/services/web/bin/compile_app b/services/web/bin/compile_app new file mode 100755 index 0000000000..b218b01a5a --- /dev/null +++ b/services/web/bin/compile_app @@ -0,0 +1,23 @@ +#!/bin/bash +set -e; + +COFFEE=node_modules/.bin/coffee + +echo Compiling app.coffee; +$COFFEE -c app.coffee; + +echo Compiling app/coffee; +$COFFEE -o app/js -c app/coffee; + +for dir in modules/*; +do + if [ -d $dir/app/coffee ]; then + echo Compiling $dir/app/coffee; + $COFFEE -o $dir/app/js -c $dir/app/coffee; + fi + + if [ -e $dir/index.coffee ]; then + echo Compiling $dir/index.coffee; + $COFFEE -c $dir/index.coffee; + fi +done \ No newline at end of file diff --git a/services/web/bin/compile_unit_tests b/services/web/bin/compile_unit_tests new file mode 100755 index 0000000000..780a189bda --- /dev/null +++ b/services/web/bin/compile_unit_tests @@ -0,0 +1,15 @@ +#!/bin/bash +set -e; + +COFFEE=node_modules/.bin/coffee + +echo Compiling test/unit/coffee; +$COFFEE -o test/unit/js -c test/unit/coffee; + +for dir in modules/*; +do + if [ -d $dir/test/unit ]; then + echo Compiling $dir/test/unit/coffee; + $COFFEE -o $dir/test/unit/js -c $dir/test/unit/coffee; + fi +done \ No newline at end of file diff --git a/services/web/bin/unit_test b/services/web/bin/unit_test new file mode 100755 index 0000000000..da13a441fa --- /dev/null +++ b/services/web/bin/unit_test @@ -0,0 +1,14 @@ +#!/bin/bash +set -e; + +MOCHA="node_modules/.bin/mocha --recursive --reporter spec" + +$MOCHA "$@" test/unit/js + +for dir in modules/*; +do + if [ -d $dir/test/unit/js ]; then + $MOCHA "$@" $dir/test/unit/js + fi +done + From 49057a5ab7e59d2ca7cded79248ce33087817cd9 Mon Sep 17 00:00:00 2001 From: James Allen Date: Fri, 24 Nov 2017 16:28:24 +0000 Subject: [PATCH 05/12] Only mount coffee and needed files into Docker so js isn't written back to local system --- services/web/.gitignore | 1 + services/web/{makefile => Makefile} | 36 +++++++++++++++---------- services/web/bin/generate_volumes_file | 26 ++++++++++++++++++ services/web/docker-compose.yml | 25 +++++++---------- services/web/docker-shared.template.yml | 21 +++++++++++++++ 5 files changed, 80 insertions(+), 29 deletions(-) rename services/web/{makefile => Makefile} (61%) create mode 100755 services/web/bin/generate_volumes_file create mode 100644 services/web/docker-shared.template.yml diff --git a/services/web/.gitignore b/services/web/.gitignore index 2a29f414d1..a48481690a 100644 --- a/services/web/.gitignore +++ b/services/web/.gitignore @@ -73,3 +73,4 @@ Gemfile.lock app/views/external /modules/ +docker-shared.yml diff --git a/services/web/makefile b/services/web/Makefile similarity index 61% rename from services/web/makefile rename to services/web/Makefile index 8d6ebf7c2e..25c0eb2cca 100644 --- a/services/web/makefile +++ b/services/web/Makefile @@ -8,38 +8,46 @@ all: install test @echo " make install to set up the project dependencies (in docker)" @echo " make test to run all the tests for the project (in docker)" -add: +add: docker-shared.yml $(NPM) install --save ${P} -add_dev: +add_dev: docker-shared.yml $(NPM) install --save-dev ${P} -install: +install: docker-shared.yml $(NPM) install -clean: - rm app.js - rm -r app/js - rm -r test/unit/js - rm -r test/acceptance/js +clean: docker-shared.yml + rm -f app.js + rm -rf app/js + rm -rf test/unit/js + rm -rf test/acceptance/js + # Deletes node_modules volume + docker-compose down --volumes + # Delete after docker-compose command + rm -f docker-shared.yml + +# Need regenerating if you change the web modules you have installed +docker-shared.yml: + bin/generate_volumes_file test: test_unit test_acceptance -test_unit: +test_unit: docker-shared.yml docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} run --rm test_unit npm run test:unit -- ${MOCHA_ARGS} test_acceptance: test_acceptance_start_service test_acceptance_run test_acceptance_stop_service -test_acceptance_start_service: +test_acceptance_start_service: docker-shared.yml docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} up -d test_acceptance -test_acceptance_stop_service: - docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} stop test_acceptance +test_acceptance_stop_service: docker-shared.yml + docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} stop test_acceptance redis mongo -test_acceptance_run: +test_acceptance_run: docker-shared.yml docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} exec -T test_acceptance npm run test:acceptance -- ${MOCHA_ARGS} .PHONY: - add install update test test_unit test_acceptance \ + all add install update test test_unit test_acceptance \ test_acceptance_start_service test_acceptance_stop_service \ test_acceptance_run diff --git a/services/web/bin/generate_volumes_file b/services/web/bin/generate_volumes_file new file mode 100755 index 0000000000..2117ff5342 --- /dev/null +++ b/services/web/bin/generate_volumes_file @@ -0,0 +1,26 @@ +#!/usr/bin/env python2 + +from os import listdir +from os.path import isfile, isdir, join + +volumes = [] + +for module in listdir("modules/"): + if module[0] != '.': + if isfile(join("modules", module, 'index.coffee')): + volumes.append(join("modules", module, 'index.coffee')) + for directory in ['app/coffee', 'app/views', 'public/coffee', 'test/unit/coffee', 'test/acceptance/coffee']: + if isdir(join("modules", module, directory)): + volumes.append(join("modules", module, directory)) + +volumes_string = map(lambda vol: "- ./" + vol + ":/app/" + vol + ":ro", volumes) +volumes_string = "\n ".join(volumes_string) + +with open("docker-shared.template.yml", "r") as f: + docker_shared_file = f.read() + +docker_shared_file = docker_shared_file.replace("MODULE_VOLUMES", volumes_string) + +with open("docker-shared.yml", "w") as f: + f.write(docker_shared_file) + diff --git a/services/web/docker-compose.yml b/services/web/docker-compose.yml index fe30bb8f57..3658a87b7f 100644 --- a/services/web/docker-compose.yml +++ b/services/web/docker-compose.yml @@ -5,25 +5,21 @@ volumes: services: npm: - image: node:6.9.5 - volumes: - - .:/app - - node_modules:/app/node_modules - working_dir: /app + extends: + file: docker-shared.yml + service: app + command: npm install test_unit: - image: node:6.9.5 - volumes: - - .:/app - - node_modules:/app/node_modules - working_dir: /app + extends: + file: docker-shared.yml + service: app command: npm run test:unit test_acceptance: - image: node:6.9.5 - volumes: - - .:/app - - node_modules:/app/node_modules + extends: + file: docker-shared.yml + service: app environment: REDIS_HOST: redis MONGO_HOST: mongo @@ -32,7 +28,6 @@ services: depends_on: - redis - mongo - working_dir: /app command: npm run start redis: diff --git a/services/web/docker-shared.template.yml b/services/web/docker-shared.template.yml new file mode 100644 index 0000000000..dca6b5a224 --- /dev/null +++ b/services/web/docker-shared.template.yml @@ -0,0 +1,21 @@ +version: "2" + +services: + app: + image: node:6.9.5 + volumes: + - ./package.json:/app/package.json + - ./npm-shrinkwrap.json:/app/npm-shrinkwrap.json + - node_modules:/app/node_modules + - ./bin:/app/bin + - ./public:/app/public + - ./app.coffee:/app/app.coffee:ro + - ./app/coffee:/app/app/coffee:ro + - ./app/templates:/app/app/templates:ro + - ./app/views:/app/app/views:ro + - ./config:/app/config + - ./test/unit/coffee:/app/test/unit/coffee:ro + - ./test/acceptance/coffee:/app/test/acceptance/coffee:ro + - ./test/smoke/coffee:/app/test/smoke/coffee:ro + MODULE_VOLUMES + working_dir: /app \ No newline at end of file From 2bc0f666bac9c48aa5ecaa7cdb19c3159d68e28e Mon Sep 17 00:00:00 2001 From: James Allen Date: Fri, 24 Nov 2017 17:04:22 +0000 Subject: [PATCH 06/12] Add some documentation --- services/web/docker-shared.template.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/services/web/docker-shared.template.yml b/services/web/docker-shared.template.yml index dca6b5a224..48adb7a036 100644 --- a/services/web/docker-shared.template.yml +++ b/services/web/docker-shared.template.yml @@ -1,5 +1,10 @@ version: "2" +# We mount all the directories explicitly so that we are only mounting +# the coffee directories, so that the compiled js is only written inside +# the container, and not back to the local filesystem, where it would be +# root owned, and conflict with working outside of the container. + services: app: image: node:6.9.5 @@ -8,6 +13,10 @@ services: - ./npm-shrinkwrap.json:/app/npm-shrinkwrap.json - node_modules:/app/node_modules - ./bin:/app/bin + # Copying the whole public dir is fine for now, and needed for + # some unit tests to pass, but we will want to isolate the coffee + # and vendor js files, so that the compiled js files are not written + # back to the local filesystem. - ./public:/app/public - ./app.coffee:/app/app.coffee:ro - ./app/coffee:/app/app/coffee:ro From 5e0fc24c1a014bd5852debf198fbbdf345969147 Mon Sep 17 00:00:00 2001 From: James Allen Date: Fri, 24 Nov 2017 17:40:24 +0000 Subject: [PATCH 07/12] Allow modules to specific their own acceptance tests --- services/web/Makefile | 21 +++++++++++++++++---- services/web/bin/acceptance_test | 16 +--------------- services/web/bin/generate_volumes_file | 2 +- services/web/package.json | 3 ++- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/services/web/Makefile b/services/web/Makefile index 25c0eb2cca..3955fe1d52 100644 --- a/services/web/Makefile +++ b/services/web/Makefile @@ -36,17 +36,30 @@ test: test_unit test_acceptance test_unit: docker-shared.yml docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} run --rm test_unit npm run test:unit -- ${MOCHA_ARGS} -test_acceptance: test_acceptance_start_service test_acceptance_run test_acceptance_stop_service +test_acceptance: test_acceptance_app test_acceptance_modules -test_acceptance_start_service: docker-shared.yml +test_acceptance_app: test_acceptance_app_start_service test_acceptance_app_run test_acceptance_app_stop_service + +test_acceptance_app_start_service: docker-shared.yml docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} up -d test_acceptance -test_acceptance_stop_service: docker-shared.yml +test_acceptance_app_stop_service: docker-shared.yml docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} stop test_acceptance redis mongo -test_acceptance_run: docker-shared.yml +test_acceptance_app_run: docker-shared.yml docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} exec -T test_acceptance npm run test:acceptance -- ${MOCHA_ARGS} +test_acceptance_modules: docker-shared.yml + for dir in modules/*; \ + do \ + if [ -e $$dir/makefile ]; then \ + (make test_acceptance_module MODULE=$$dir) \ + fi \ + done + +test_acceptance_module: docker-shared.yml + cd $(MODULE) && make test_acceptance + .PHONY: all add install update test test_unit test_acceptance \ test_acceptance_start_service test_acceptance_stop_service \ diff --git a/services/web/bin/acceptance_test b/services/web/bin/acceptance_test index 717e8542ad..fd2e5137b5 100755 --- a/services/web/bin/acceptance_test +++ b/services/web/bin/acceptance_test @@ -1,18 +1,4 @@ #!/bin/bash set -e; - MOCHA="node_modules/.bin/mocha --recursive --reporter spec --timeout 15000" - -$MOCHA "$@" test/acceptance/js - -# TODO: Module acceptance tests are hard to get working, -# because they typically require the server to be instantiated -# with a different config. - -# for dir in modules/*; -# do -# if [ -d $dir/test/acceptance/js ]; then -# $MOCHA "$@" $dir/test/acceptance/js -# fi -# done - +$MOCHA "$@" diff --git a/services/web/bin/generate_volumes_file b/services/web/bin/generate_volumes_file index 2117ff5342..d70ac11c3d 100755 --- a/services/web/bin/generate_volumes_file +++ b/services/web/bin/generate_volumes_file @@ -9,7 +9,7 @@ for module in listdir("modules/"): if module[0] != '.': if isfile(join("modules", module, 'index.coffee')): volumes.append(join("modules", module, 'index.coffee')) - for directory in ['app/coffee', 'app/views', 'public/coffee', 'test/unit/coffee', 'test/acceptance/coffee']: + for directory in ['app/coffee', 'app/views', 'public/coffee', 'test/unit/coffee', 'test/acceptance/coffee', 'test/acceptance/config']: if isdir(join("modules", module, directory)): volumes.append(join("modules", module, directory)) diff --git a/services/web/package.json b/services/web/package.json index a795df999e..b14cdf8b20 100644 --- a/services/web/package.json +++ b/services/web/package.json @@ -12,7 +12,8 @@ "scripts": { "test:acceptance:wait_for_app": "echo 'Waiting for app to be accessible' && while (! curl -s -o /dev/null localhost:3000/status) do sleep 1; done", "test:acceptance:run": "bin/acceptance_test $@", - "test:acceptance": "npm run compile:acceptance_tests && npm run test:acceptance:wait_for_app && npm run test:acceptance:run -- $@", + "test:acceptance:dir": "npm run compile:acceptance_tests && npm run test:acceptance:wait_for_app && npm run test:acceptance:run -- $@", + "test:acceptance": "npm run test:acceptance:dir test/acceptance/js", "test:unit": "npm run compile:app && npm run compile:unit_tests && bin/unit_test $@", "compile:unit_tests": "bin/compile_unit_tests", "compile:acceptance_tests": "bin/compile_acceptance_tests", From 4c504ad8ebcbfc25aab78b6492694396ea5b65a6 Mon Sep 17 00:00:00 2001 From: James Allen Date: Mon, 27 Nov 2017 16:51:20 +0000 Subject: [PATCH 08/12] Remove debugging command --- services/web/package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/web/package.json b/services/web/package.json index b14cdf8b20..3e6789920e 100644 --- a/services/web/package.json +++ b/services/web/package.json @@ -18,8 +18,7 @@ "compile:unit_tests": "bin/compile_unit_tests", "compile:acceptance_tests": "bin/compile_acceptance_tests", "compile:app": "bin/compile_app", - "start": "npm run compile:app && node app.js", - "echo": "echo $@" + "start": "npm run compile:app && node app.js" }, "dependencies": { "archiver": "0.9.0", From 054964dd8551851b8ca779073c0358101ef9bd82 Mon Sep 17 00:00:00 2001 From: James Allen Date: Mon, 27 Nov 2017 16:55:11 +0000 Subject: [PATCH 09/12] Clean out module js on make clean --- services/web/Makefile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/services/web/Makefile b/services/web/Makefile index 3955fe1d52..88b09ce609 100644 --- a/services/web/Makefile +++ b/services/web/Makefile @@ -22,6 +22,13 @@ clean: docker-shared.yml rm -rf app/js rm -rf test/unit/js rm -rf test/acceptance/js + for dir in modules/*; \ + do \ + rm -f $$dir/index.js; \ + rm -rf $$dir/app/js; \ + rm -rf $$dir/test/unit/js; \ + rm -rf $$dir/test/acceptance/js; \ + done # Deletes node_modules volume docker-compose down --volumes # Delete after docker-compose command From 3e90103d9cb3523fdc34a24e2bc856196e38895a Mon Sep 17 00:00:00 2001 From: James Allen Date: Mon, 27 Nov 2017 17:04:54 +0000 Subject: [PATCH 10/12] No need to bind to 0.0.0.0 when running in same container --- .../web/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee | 2 +- .../web/test/acceptance/coffee/helpers/MockDocstoreApi.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/web/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee b/services/web/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee index 6f307b0810..aefcd4513a 100644 --- a/services/web/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee +++ b/services/web/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee @@ -6,7 +6,7 @@ module.exports = MockDocUpdaterApi = app.post "/project/:project_id/flush", (req, res, next) => res.sendStatus 200 - app.listen 3003, '0.0.0.0', (error) -> + app.listen 3003, (error) -> throw error if error? .on "error", (error) -> console.error "error starting MockDocUpdaterApi:", error.message diff --git a/services/web/test/acceptance/coffee/helpers/MockDocstoreApi.coffee b/services/web/test/acceptance/coffee/helpers/MockDocstoreApi.coffee index 7c8f5dbe50..2133d40b9f 100644 --- a/services/web/test/acceptance/coffee/helpers/MockDocstoreApi.coffee +++ b/services/web/test/acceptance/coffee/helpers/MockDocstoreApi.coffee @@ -22,7 +22,7 @@ module.exports = MockDocStoreApi = docs = (doc for doc_id, doc of @docs[req.params.project_id]) res.send JSON.stringify docs - app.listen 3016, '0.0.0.0', (error) -> + app.listen 3016, (error) -> throw error if error? .on "error", (error) -> console.error "error starting MockDocStoreApi:", error.message From 50b30455482d0c1a56052f1cc339d256d1def206 Mon Sep 17 00:00:00 2001 From: James Allen Date: Wed, 29 Nov 2017 13:49:36 +0000 Subject: [PATCH 11/12] Tidy up docker-compose and makefile --- services/web/Makefile | 13 +++++++------ services/web/config/settings.defaults.coffee | 2 +- services/web/docker-compose.yml | 3 +-- services/web/package.json | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/services/web/Makefile b/services/web/Makefile index 88b09ce609..cd724038e6 100644 --- a/services/web/Makefile +++ b/services/web/Makefile @@ -1,4 +1,5 @@ -NPM := docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} run --rm npm npm +DOCKER_COMPOSE_FLAGS ?= -f docker-compose.yml +NPM := docker-compose ${DOCKER_COMPOSE_FLAGS} run --rm npm npm BUILD_NUMBER ?= local BRANCH_NAME ?= $(shell git rev-parse --abbrev-ref HEAD) PROJECT_NAME = web @@ -41,20 +42,20 @@ docker-shared.yml: test: test_unit test_acceptance test_unit: docker-shared.yml - docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} run --rm test_unit npm run test:unit -- ${MOCHA_ARGS} + docker-compose ${DOCKER_COMPOSE_FLAGS} run --rm test_unit npm run test:unit -- ${MOCHA_ARGS} test_acceptance: test_acceptance_app test_acceptance_modules test_acceptance_app: test_acceptance_app_start_service test_acceptance_app_run test_acceptance_app_stop_service -test_acceptance_app_start_service: docker-shared.yml - docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} up -d test_acceptance +test_acceptance_app_start_service: test_acceptance_app_stop_service docker-shared.yml + docker-compose ${DOCKER_COMPOSE_FLAGS} up -d test_acceptance test_acceptance_app_stop_service: docker-shared.yml - docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} stop test_acceptance redis mongo + docker-compose ${DOCKER_COMPOSE_FLAGS} stop test_acceptance redis mongo test_acceptance_app_run: docker-shared.yml - docker-compose -f docker-compose.yml ${DOCKER_COMPOSE_FLAGS} exec -T test_acceptance npm run test:acceptance -- ${MOCHA_ARGS} + docker-compose ${DOCKER_COMPOSE_FLAGS} exec -T test_acceptance npm run test:acceptance -- ${MOCHA_ARGS} test_acceptance_modules: docker-shared.yml for dir in modules/*; \ diff --git a/services/web/config/settings.defaults.coffee b/services/web/config/settings.defaults.coffee index fccd197a83..98d1c9e031 100644 --- a/services/web/config/settings.defaults.coffee +++ b/services/web/config/settings.defaults.coffee @@ -35,7 +35,7 @@ module.exports = settings = # Databases # --------- mongo: - url : "mongodb://#{process.env['MONGO_HOST'] || '127.0.0.1'}/sharelatex" + url : process.env['MONGO_URL'] || "mongodb://127.0.0.1/sharelatex" redis: web: diff --git a/services/web/docker-compose.yml b/services/web/docker-compose.yml index 3658a87b7f..aaa8666a16 100644 --- a/services/web/docker-compose.yml +++ b/services/web/docker-compose.yml @@ -22,9 +22,8 @@ services: service: app environment: REDIS_HOST: redis - MONGO_HOST: mongo + MONGO_URL: "mongodb://mongo/sharelatex" SHARELATEX_ALLOW_PUBLIC_ACCESS: 'true' - LISTEN_ADDRESS: 0.0.0.0 depends_on: - redis - mongo diff --git a/services/web/package.json b/services/web/package.json index 3e6789920e..c221dc1052 100644 --- a/services/web/package.json +++ b/services/web/package.json @@ -13,7 +13,7 @@ "test:acceptance:wait_for_app": "echo 'Waiting for app to be accessible' && while (! curl -s -o /dev/null localhost:3000/status) do sleep 1; done", "test:acceptance:run": "bin/acceptance_test $@", "test:acceptance:dir": "npm run compile:acceptance_tests && npm run test:acceptance:wait_for_app && npm run test:acceptance:run -- $@", - "test:acceptance": "npm run test:acceptance:dir test/acceptance/js", + "test:acceptance": "npm run test:acceptance:dir -- $@ test/acceptance/js", "test:unit": "npm run compile:app && npm run compile:unit_tests && bin/unit_test $@", "compile:unit_tests": "bin/compile_unit_tests", "compile:acceptance_tests": "bin/compile_acceptance_tests", From bb74f8318ab9bbc7603722b39fd094b69b72e7d6 Mon Sep 17 00:00:00 2001 From: James Allen Date: Wed, 29 Nov 2017 14:16:29 +0000 Subject: [PATCH 12/12] Support `make clean install` usage --- services/web/Makefile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/services/web/Makefile b/services/web/Makefile index cd724038e6..7ab8b89781 100644 --- a/services/web/Makefile +++ b/services/web/Makefile @@ -18,7 +18,7 @@ add_dev: docker-shared.yml install: docker-shared.yml $(NPM) install -clean: docker-shared.yml +clean: rm -f app.js rm -rf app/js rm -rf test/unit/js @@ -32,8 +32,9 @@ clean: docker-shared.yml done # Deletes node_modules volume docker-compose down --volumes - # Delete after docker-compose command - rm -f docker-shared.yml + # Regenerate docker-shared.yml - not stictly a 'clean', + # but lets `make clean install` work nicely + bin/generate_volumes_file # Need regenerating if you change the web modules you have installed docker-shared.yml: