diff --git a/.gcloudignore b/.gcloudignore index a3f0c76..9ac9672 100644 --- a/.gcloudignore +++ b/.gcloudignore @@ -14,4 +14,10 @@ .gitignore # Node.js dependencies: -node_modules/ \ No newline at end of file +node_modules/ + +# ignore test directory +test/ + +# ignore github actions yaml files +.github/ \ No newline at end of file diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 13300ce..e45a88e 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -1,18 +1,12 @@ # overall CI pipeline name name: Build and test export server -# only run on master and stage branches -on: - push: - branches: - - master - - stage +# Run tests on EVERY branch and push +on: [push] # Environment variables available to all jobs and steps in this workflow env: CI: true - PROJECT_ID: ${{secrets.GCLOUD_PROJECT_ID}} - APPLICATION_CREDENTIALS: ${{secrets.GOOGLE_APPLICATION_CREDENTIALS}} # jobs are the different pipelines jobs: @@ -22,20 +16,21 @@ jobs: strategy: matrix: - node-version: [10.x, 12.x] + node-version: [10.14.2] # define all the steps in the "build" stage steps: - # use the checkout action + # use the checkout action to get repository contents - uses: actions/checkout@v1 + - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} + # Try to build and run tests again export server - - name: npm install, build, and test + - name: Run Tests run: | - npm ci - npm run build --if-present + npm ci npm test diff --git a/.github/workflows/deploy-gae.yml b/.github/workflows/deploy-gae.yml new file mode 100644 index 0000000..681343f --- /dev/null +++ b/.github/workflows/deploy-gae.yml @@ -0,0 +1,46 @@ +# overall CI pipeline name +name: Deploy to GAE + +# only run on master and stage branches +on: + push: + branches: + - master + - stage + +# Environment variables available to all jobs and steps in this workflow +env: + # Project id + PROJECT_ID: ${{secrets.GCLOUD_PROJECT_ID}} + # GCP authorization credentials + APPLICATION_CREDENTIALS: ${{secrets.GOOGLE_APPLICATION_CREDENTIALS}} + # define to use gcloud over gsutil + CLI: gcloud + +# jobs are the different pipelines +jobs: + build: + + # Define the OS and name + runs-on: ubuntu-latest + name: Publish and Deploy + + # define all the steps in the "build" stage + steps: + # This step checks out a copy of your repository. + - uses: actions/checkout@v1 + # This step checks out and logs in to gcloud + - name: Deploy Stage + if: github.ref == 'refs/heads/stage' + uses: actions-hub/gcloud@master + with: + # This step deploys to stage and the --no-promote flag prevents + # traffic from being split to this version + args: app deploy app-stage.yaml --version stage --no-promote + # This step checks out and logs in to gcloud + - name: Deploy Production + if: github.ref == 'refs/heads/master' + uses: actions-hub/gcloud@master + with: + # This step deploys to production + args: app deploy app.yaml --version master \ No newline at end of file diff --git a/.gitlab-ci.yaml b/.gitlab-ci.yaml deleted file mode 100644 index 6c34cab..0000000 --- a/.gitlab-ci.yaml +++ /dev/null @@ -1,44 +0,0 @@ -# define docker image to use -image: devillex/docker-firebase:slim - -# define what to cache between stages AND pipelines -# this is a shortcut for passing node_modules around -cache: - paths: - - node_modules/ - -before_script: - # Path to NVM install location - - export NVM_DIR="$HOME/.nvm" - - . ${NVM_DIR}/nvm.sh - - gitlab-runner --version - # Install dependent node version via NVM - - nvm --version - - nvm install 10.14 - - node --version - -# define the stages of our pipeline -stages: - - test - - deployment - -###################################################### -### COMMANDS ALWAYS RUN -###################################################### - -## run unit tests -test:unit: - stage: test - script: - - npm install - - npm run test - allow_failure: true - -# deploy:stage: -# stage: deployment -# script: -# - npm install -# - npm run test -# allow_failure: true -# dependencies: -# - test:unit diff --git a/app-stage.yaml b/app-stage.yaml new file mode 100644 index 0000000..3c756d9 --- /dev/null +++ b/app-stage.yaml @@ -0,0 +1,9 @@ +runtime: nodejs10 +instance_class: F4_1G +service: default +env_variables: + NODE_ENV: stage +# prevents all traffic from splitting immediately +# sends a "warmup" request to new version +inbound_services: +- warmup \ No newline at end of file diff --git a/app.yaml b/app.yaml index 821443b..af35182 100644 --- a/app.yaml +++ b/app.yaml @@ -3,3 +3,7 @@ instance_class: F4_1G service: default env_variables: NODE_ENV: production +# prevents all traffic from splitting immediately +# sends a "warmup" request to new version +inbound_services: +- warmup \ No newline at end of file diff --git a/server.js b/server.js index 7106109..8afff5e 100644 --- a/server.js +++ b/server.js @@ -141,45 +141,35 @@ app.post('/', async (req, res) => { // }; // }); - // read file asynchronously in case it never returns - // fs.readFile(readFilePath, (err, imageFile) => { - - // catch err - // if (err) { - // console.error(err); - // return res.status(500).send('Issue Reading File From Server'); - // } - - // request headers - let rHeaders = { - 'Content-Type': '', - // 'Content-Length': '', - // 'Content-Disposition': `attachment; filename="${fileName}.${imgType}"` - }; - - // set appropriate mime/types - switch (imgType) { - case 'svg': - rHeaders['Content-Type'] = 'image/svg+xml'; - // rHeaders['Content-Length'] = svgData.length; - break; - case 'pdf': - rHeaders['Content-Type'] = 'application/pdf'; - // rHeaders['Content-Length'] = imageFile.length; - break; - case 'png': - case 'jpeg': - default: - rHeaders['Content-Type'] = `image/${imgType}`; - // rHeaders['Content-Length'] = imageFile.length; - } + // request headers + let rHeaders = { + 'Content-Type': '', + // 'Content-Length': '', + // 'Content-Disposition': `attachment; filename="${fileName}.${imgType}"` + }; + + // set appropriate mime/types + switch (imgType) { + case 'svg': + rHeaders['Content-Type'] = 'image/svg+xml'; + // rHeaders['Content-Length'] = svgData.length; + break; + case 'pdf': + rHeaders['Content-Type'] = 'application/pdf'; + // rHeaders['Content-Length'] = imageFile.length; + break; + case 'png': + case 'jpeg': + default: + rHeaders['Content-Type'] = `image/${imgType}`; + // rHeaders['Content-Length'] = imageFile.length; + } - // write headers - res.writeHead(200, rHeaders); + // write headers + res.writeHead(200, rHeaders); - // end the buffer and send - return res.end(tmpBuffer); - // }); + // end the buffer and send + return res.end(tmpBuffer); } catch(e) { console.error(e); @@ -299,41 +289,31 @@ app.post('/json', async (req, res) => { // }; // }); - // read file asynchronously in case it never returns - // fs.readFile(readFilePath, (err, imageFile) => { - - // catch err - // if (err) { - // console.error(err); - // return res.status(500).send('Issue Reading File From Server'); - // } - - // request headers - let rHeaders = { - 'Content-Type': '', - // 'Content-Length': '', - // 'Content-Disposition': `attachment; filename="${fileName}.${imgType}"` - }; - - // set appropriate mime/types - switch (imgType) { - case 'pdf': - rHeaders['Content-Type'] = 'application/pdf'; - // rHeaders['Content-Length'] = imageFile.length; - break; - case 'png': - case 'jpeg': - default: - rHeaders['Content-Type'] = `image/${imgType}`; - // rHeaders['Content-Length'] = imageFile.length; - } - - // write headers - res.writeHead(200, rHeaders); - - // end the buffer and send - return res.end(tmpBuffer); - // }); + // request headers + let rHeaders = { + 'Content-Type': '', + // 'Content-Length': '', + 'Content-Disposition': `attachment; filename="${fileName}.${imgType}"` + }; + + // set appropriate mime/types + switch (imgType) { + case 'pdf': + rHeaders['Content-Type'] = 'application/pdf'; + // rHeaders['Content-Length'] = imageFile.length; + break; + case 'png': + case 'jpeg': + default: + rHeaders['Content-Type'] = `image/${imgType}`; + // rHeaders['Content-Length'] = imageFile.length; + } + + // write headers + res.writeHead(200, rHeaders); + + // end the buffer and send + return res.end(tmpBuffer); } }); diff --git a/test.js b/test.js deleted file mode 100644 index 1a9b465..0000000 --- a/test.js +++ /dev/null @@ -1 +0,0 @@ -// all tests rendered from gallery at width 725 and height 500 diff --git a/test/base-test.spec.js b/test/base-test.spec.js index cee761c..15ec9b7 100644 --- a/test/base-test.spec.js +++ b/test/base-test.spec.js @@ -3,6 +3,7 @@ const axios = require('axios'); const fs = require('fs'); describe(`Server Route Test's`, function() { + // before(function(done) { // // Waits for quickbooks to initialize // done(); @@ -10,81 +11,63 @@ describe(`Server Route Test's`, function() { // test the response status's // --------------------------------- - describe.skip('Get Base Routes', function() { - it(' Post / Url is OK', function(done) { - axios.post('http://localhost:8080/') - .then(function (response) { - // console.log(response); - expect(response.statusText).to.equal('OK'); - expect(response.status).to.equal(200); - done(); - }) - .catch(function (error) { - console.log(error); - }); + describe('Get Base Routes', function() { + it(' Post / Url is OK', async function() { + try { + let response = await axios.post('http://localhost:8080/'); + expect(response.statusText).to.equal('OK'); + expect(response.status).to.equal(200); + } catch(e) { + if (process.env.DEBUG) console.error(e); + } }); - - it('Post /json Url is OK', function(done) { - axios.post('http://localhost:8080/json') - .then(function (response) { - // console.log(response); - expect(response.statusText).to.equal('OK'); - expect(response.status).to.equal(200); - done(); - }) - .catch(function (error) { - console.log(error); - }); + + it('Post /json Url is OK', async function() { + try { + let response = await axios.post('http://localhost:8080/json'); + expect(response.statusText).to.equal('OK'); + expect(response.status).to.equal(200); + } catch(e) { + if (process.env.DEBUG) console.error(e); + } }); }); // test the output for PNG creation // --------------------------------- describe('PNG Creation', function() { - it.skip(' Post / PNG output is OK', function(done) { - axios.post('http://localhost:8080/') - .then(function (response) { - // console.log(response); - expect(response.statusText).to.equal('OK'); - expect(response.status).to.equal(200); - done(); - }) - .catch(function (error) { - console.log(error); - }); - }); - it('Post /json PNG output is exact match', function(done) { - let chartConfig = require('./configs/plain-chart.js'); - axios({ - method: 'post', - url: 'http://localhost:8080/json', - responseType: 'stream', - data: { - chartConfig, - t: 'png', - height: '500px', - width: '500px' - } - }) - .then(async function (response) { - // pipe results into dist folder - response.data.pipe(fs.createWriteStream(`${__dirname}/dist/test1.png`)); - // check results again reference image and pipe to dist/diff - let blinkDiffResults = await utils.compareScreenshotsBlinkDiff( - `${__dirname}/dist/test1.png`, // file 1 - `${__dirname}/references/test1-500x500.png`, // file 2 - `${__dirname}/dist/diff/test1.png`, // output image path - ); - // verify pass's threshold and the the differences are zero (COULD Loosen this restriction) - expect(blinkDiffResults.pass).to.equal(true); - expect(blinkDiffResults.differences).to.equal(0); - done(); - }) - .catch(function (error) { - console.log(error); + // test to make sure the image matches + it('Post /json PNG output is exact match', async function() { + try { + let chartConfig = require('./configs/plain-chart.js'); + + let response = await axios({ + method: 'post', + url: 'http://localhost:8080/json', + responseType: 'stream', + data: { + chartConfig, + t: 'png', + height: '500px', + width: '500px' + } }); + // pipe results into dist folder + response.data.pipe(await fs.createWriteStream(`${__dirname}/dist/test1.png`)); + // check results again reference image and pipe to dist/diff + let blinkDiffResults = await utils.compareScreenshotsBlinkDiff( + `${__dirname}/dist/test1.png`, // file 1 + `${__dirname}/references/test1-500x500.png`, // file 2 + `${__dirname}/dist/diff/test1.png`, // output image path + ); + // verify pass's threshold and the the differences are zero (COULD Loosen this restriction) + expect(blinkDiffResults.pass).to.equal(true); + expect(blinkDiffResults.differences).to.equal(0); + } catch(e) { + if (process.env.DEBUG) console.error(e); + } }); }); });