From 16e4be0a470c6b4d7975d1aac1a02633c7d35f73 Mon Sep 17 00:00:00 2001 From: Richard Simpson Date: Sun, 24 Nov 2019 15:39:33 -0600 Subject: [PATCH] chore(test): organize tests a bit better --- .github/workflows/build.yml | 16 ++++---- docker-compose.yml | 13 ++++-- .../basic}/integration.test.js | 33 +++++++-------- .../basic}/jest.config.js | 0 integrationTests/e2e/README.md | 4 ++ {e2e => integrationTests/e2e}/e2e.test.js | 0 .../e2e}/jest.config.js | 0 {e2e => integrationTests/e2e}/setup.js | 8 ++-- .../enterprise/enterprise.test.js | 41 +++++++++---------- .../enterprise}/jest.config.js | 0 jest.config.js | 2 +- package.json | 6 +-- 12 files changed, 66 insertions(+), 57 deletions(-) rename {integration => integrationTests/basic}/integration.test.js (72%) rename {e2e => integrationTests/basic}/jest.config.js (100%) create mode 100644 integrationTests/e2e/README.md rename {e2e => integrationTests/e2e}/e2e.test.js (100%) rename {integration-ent => integrationTests/e2e}/jest.config.js (100%) rename {e2e => integrationTests/e2e}/setup.js (88%) rename integration-ent/integration.test.js => integrationTests/enterprise/enterprise.test.js (70%) rename {integration => integrationTests/enterprise}/jest.config.js (100%) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9e49f71..80d6357 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,7 +1,7 @@ on: [push] jobs: - test: + integration-basic: runs-on: ubuntu-latest services: @@ -27,14 +27,14 @@ jobs: run: npm run test env: CI: true - - name: npm run test:integration - run: npm run test:integration + - name: npm run test:integration:basic + run: npm run test:integration:basic env: VAULT_HOST: localhost VAULT_PORT: ${{ job.services.vault.ports[8200] }} CI: true - test-ent: + integration-enterprise: runs-on: ubuntu-latest services: @@ -60,8 +60,8 @@ jobs: run: npm run test env: CI: true - - name: npm run test:integration-ent - run: npm run test:integration-ent + - name: npm run test:integration:enterprise + run: npm run test:integration:enterprise env: VAULT_HOST: localhost VAULT_PORT: ${{ job.services.vault.ports[8200] }} @@ -72,7 +72,7 @@ jobs: services: vault: - image: vault:1.2.3 + image: vault:1.3.0 ports: - 8200/tcp env: @@ -108,7 +108,7 @@ jobs: publish: runs-on: ubuntu-latest - needs: [test, e2e] + needs: [integration-basic, integration-enterprise, e2e] steps: - uses: actions/checkout@v1 - name: Use Node.js 10.x diff --git a/docker-compose.yml b/docker-compose.yml index 041702d..ef01eca 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,12 +1,17 @@ -# Start vault server locally -# You can run integration tests against server by running -# `VAULT_HOST=localhost VAULT_PORT=8200 CI=true npm run test:integration-ent` +# Start vault server locally for the purposes of integration tests. version: "3.0" services: vault: - image: hashicorp/vault-enterprise:1.3.0_ent + image: vault:1.3.0 environment: VAULT_DEV_ROOT_TOKEN_ID: testtoken ports: - 8200:8200 + privileged: true + vault-enterprise: + image: hashicorp/vault-enterprise:1.3.0_ent + environment: + VAULT_DEV_ROOT_TOKEN_ID: testtoken + ports: + - 8201:8201 privileged: true \ No newline at end of file diff --git a/integration/integration.test.js b/integrationTests/basic/integration.test.js similarity index 72% rename from integration/integration.test.js rename to integrationTests/basic/integration.test.js index eb71c5b..e4dbcc4 100644 --- a/integration/integration.test.js +++ b/integrationTests/basic/integration.test.js @@ -1,55 +1,56 @@ jest.mock('@actions/core'); jest.mock('@actions/core/lib/command'); -const core = require('@actions/core'); +const core = require('./@actions/core'); -const got = require('got'); -const { when } = require('jest-when'); +const got = require('./got'); +const { when } = require('./jest-when'); -const { exportSecrets } = require('../action'); +const { exportSecrets } = require('../../action'); + +const vaultUrl = `http://${process.env.VAULT_HOST || 'localhost'}:${process.env.VAULT_PORT || '8200'}`; describe('integration', () => { - beforeAll(async () => { // Verify Connection - await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret/config`, { + await got(`${vaultUrl}/v1/secret/config`, { headers: { 'X-Vault-Token': 'testtoken', }, }); - await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret/data/test`, { + await got(`${vaultUrl}/v1/secret/data/test`, { method: 'POST', headers: { 'X-Vault-Token': 'testtoken', }, body: { data: { - secret: "SUPERSECRET", + secret: 'SUPERSECRET', }, }, json: true, }); - await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret/data/nested/test`, { + await got(`${vaultUrl}/v1/secret/data/nested/test`, { method: 'POST', headers: { 'X-Vault-Token': 'testtoken', }, body: { data: { - otherSecret: "OTHERSUPERSECRET", + otherSecret: 'OTHERSUPERSECRET', }, }, json: true, }); - }) + }); beforeEach(() => { jest.resetAllMocks(); when(core.getInput) .calledWith('url') - .mockReturnValue(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}`); + .mockReturnValue(`${vaultUrl}`); when(core.getInput) .calledWith('token') @@ -63,7 +64,7 @@ describe('integration', () => { } it('get simple secret', async () => { - mockInput('test secret') + mockInput('test secret'); await exportSecrets(); @@ -71,7 +72,7 @@ describe('integration', () => { }); it('re-map secret', async () => { - mockInput('test secret | TEST_KEY') + mockInput('test secret | TEST_KEY'); await exportSecrets(); @@ -79,7 +80,7 @@ describe('integration', () => { }); it('get nested secret', async () => { - mockInput('nested/test otherSecret') + mockInput('nested/test otherSecret'); await exportSecrets(); @@ -100,4 +101,4 @@ describe('integration', () => { expect(core.exportVariable).toBeCalledWith('NAMED_SECRET', 'SUPERSECRET'); expect(core.exportVariable).toBeCalledWith('OTHERSECRET', 'OTHERSUPERSECRET'); }); -}); \ No newline at end of file +}); diff --git a/e2e/jest.config.js b/integrationTests/basic/jest.config.js similarity index 100% rename from e2e/jest.config.js rename to integrationTests/basic/jest.config.js diff --git a/integrationTests/e2e/README.md b/integrationTests/e2e/README.md new file mode 100644 index 0000000..f7c319c --- /dev/null +++ b/integrationTests/e2e/README.md @@ -0,0 +1,4 @@ +# e2e tests + +This test suite runs `vault-action` as a GitHub Action in the context of a live build, and then verifies that the appropriate environmental variables are set. +These tests are intended to mostly be very simple smoke tests to verify that the action is being compiled and run correctly in context. diff --git a/e2e/e2e.test.js b/integrationTests/e2e/e2e.test.js similarity index 100% rename from e2e/e2e.test.js rename to integrationTests/e2e/e2e.test.js diff --git a/integration-ent/jest.config.js b/integrationTests/e2e/jest.config.js similarity index 100% rename from integration-ent/jest.config.js rename to integrationTests/e2e/jest.config.js diff --git a/e2e/setup.js b/integrationTests/e2e/setup.js similarity index 88% rename from e2e/setup.js rename to integrationTests/e2e/setup.js index 5ca354c..5930216 100644 --- a/e2e/setup.js +++ b/integrationTests/e2e/setup.js @@ -1,4 +1,4 @@ -const got = require('got'); +const got = require('./got'); (async () => { try { @@ -16,7 +16,7 @@ const got = require('got'); }, body: { data: { - secret: "SUPERSECRET", + secret: 'SUPERSECRET', }, }, json: true, @@ -29,7 +29,7 @@ const got = require('got'); }, body: { data: { - otherSecret: "OTHERSUPERSECRET", + otherSecret: 'OTHERSUPERSECRET', }, }, json: true, @@ -38,4 +38,4 @@ const got = require('got'); console.log(error); process.exit(1); } -})(); \ No newline at end of file +})(); diff --git a/integration-ent/integration.test.js b/integrationTests/enterprise/enterprise.test.js similarity index 70% rename from integration-ent/integration.test.js rename to integrationTests/enterprise/enterprise.test.js index ede9c59..c180ef2 100644 --- a/integration-ent/integration.test.js +++ b/integrationTests/enterprise/enterprise.test.js @@ -1,24 +1,25 @@ jest.mock('@actions/core'); jest.mock('@actions/core/lib/command'); -const core = require('@actions/core'); +const core = require('./@actions/core'); -const got = require('got'); -const { when } = require('jest-when'); +const got = require('./got'); +const { when } = require('./jest-when'); -const { exportSecrets } = require('../action'); +const { exportSecrets } = require('../../action'); + +const vaultUrl = `http://${process.env.VAULT_HOST || 'localhost'}:${process.env.VAULT_PORT || '8201'}`; describe('integration', () => { - beforeAll(async () => { // Verify Connection - await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret/config`, { + await got(`${vaultUrl}/v1/secret/config`, { headers: { 'X-Vault-Token': 'testtoken', }, }); // Create namespace - await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/sys/namespaces/ns1`, { + await got(`${vaultUrl}/v1/sys/namespaces/ns1`, { method: 'POST', headers: { 'X-Vault-Token': 'testtoken', @@ -27,17 +28,17 @@ describe('integration', () => { }); // Enable secret engine - await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/sys/mounts/secret`, { + await got(`${vaultUrl}/v1/sys/mounts/secret`, { method: 'POST', headers: { 'X-Vault-Token': 'testtoken', 'X-Vault-Namespace': 'ns1', }, - body: {"path":"secret","type":"kv","config":{},"options":{"version":2},"generate_signing_key":true}, + body: { path: 'secret', type: 'kv', config: {}, options: { version: 2 }, generate_signing_key: true }, json: true, }); - await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret/data/test`, { + await got(`${vaultUrl}/v1/secret/data/test`, { method: 'POST', headers: { 'X-Vault-Token': 'testtoken', @@ -45,13 +46,13 @@ describe('integration', () => { }, body: { data: { - secret: "SUPERSECRET_IN_NAMESPACE", + secret: 'SUPERSECRET_IN_NAMESPACE', }, }, json: true, }); - await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret/data/nested/test`, { + await got(`${vaultUrl}/v1/secret/data/nested/test`, { method: 'POST', headers: { 'X-Vault-Token': 'testtoken', @@ -59,21 +60,19 @@ describe('integration', () => { }, body: { data: { - otherSecret: "OTHERSUPERSECRET_IN_NAMESPACE", + otherSecret: 'OTHERSUPERSECRET_IN_NAMESPACE', }, }, json: true, }); + }); - - - }) beforeEach(() => { jest.resetAllMocks(); when(core.getInput) .calledWith('url') - .mockReturnValue(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}`); + .mockReturnValue(`${vaultUrl}`); when(core.getInput) .calledWith('token') @@ -91,7 +90,7 @@ describe('integration', () => { } it('get simple secret', async () => { - mockInput('test secret') + mockInput('test secret'); await exportSecrets(); @@ -99,7 +98,7 @@ describe('integration', () => { }); it('re-map secret', async () => { - mockInput('test secret | TEST_KEY') + mockInput('test secret | TEST_KEY'); await exportSecrets(); @@ -107,7 +106,7 @@ describe('integration', () => { }); it('get nested secret', async () => { - mockInput('nested/test otherSecret') + mockInput('nested/test otherSecret'); await exportSecrets(); @@ -128,4 +127,4 @@ describe('integration', () => { expect(core.exportVariable).toBeCalledWith('NAMED_SECRET', 'SUPERSECRET_IN_NAMESPACE'); expect(core.exportVariable).toBeCalledWith('OTHERSECRET', 'OTHERSUPERSECRET_IN_NAMESPACE'); }); -}); \ No newline at end of file +}); diff --git a/integration/jest.config.js b/integrationTests/enterprise/jest.config.js similarity index 100% rename from integration/jest.config.js rename to integrationTests/enterprise/jest.config.js diff --git a/jest.config.js b/jest.config.js index df9fa1c..7916d69 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,3 +1,3 @@ module.exports = { - testPathIgnorePatterns: ['/node_modules/', '/integration/', '/e2e/','/integration-ent'], + testPathIgnorePatterns: ['/node_modules/', '/integrationTests/'], }; diff --git a/package.json b/package.json index c294fa6..44514bd 100644 --- a/package.json +++ b/package.json @@ -6,9 +6,9 @@ "scripts": { "build": "ncc build index.js -o dist", "test": "jest", - "test:integration": "jest -c integration/jest.config.js", - "test:integration-ent": "jest -c integration-ent/jest.config.js", - "test:e2e": "jest -c e2e/jest.config.js" + "test:integration:basic": "jest -c integrationTests/basic/jest.config.js", + "test:integration:enterprise": "jest -c integrationTests/enterprise/jest.config.js", + "test:e2e": "jest -c integrationTests/e2e/jest.config.js" }, "release": { "branch": "master",