5
0
Fork 0
mirror of https://github.com/hashicorp/vault-action.git synced 2025-11-07 15:16:56 +00:00

feat: fix unit and add e2e test

This commit is contained in:
Richard Simpson 2019-09-20 17:11:09 -05:00
parent 0b17727b1c
commit 33ba690ebb
No known key found for this signature in database
GPG key ID: 0CECAF50D013D1E2
8 changed files with 180 additions and 97 deletions

View file

@ -20,8 +20,7 @@ jobs:
env:
CI: true
e2e:
integration:
runs-on: ubuntu-latest
services:
@ -41,9 +40,38 @@ jobs:
node-version: 10.x
- name: npm install
run: npm ci
- name: npm run test:e2e
run: npm run test:e2e
- name: npm run test:integration
run: npm run test:integration
env:
VAULT_HOST: localhost
VAULT_PORT: ${{ job.services.vault.ports[8200] }}
e2e:
runs-on: ubuntu-latest
services:
vault:
image: vault:1.2.3
ports:
- 8200/tcp
env:
VAULT_DEV_ROOT_TOKEN_ID: testtoken
options: --cap-add=IPC_LOCK
steps:
- uses: actions/checkout@v1
- name: Use Node.js 10.x
uses: actions/setup-node@v1
with:
node-version: 10.x
- name: npm install
run: npm ci
- name: setup vault
run: node ./e2e/setup.js
- name: use vault actions
uses: ./
- name: verify
run: npm run test:e2e

View file

@ -89,8 +89,9 @@ describe('exportSecrets', () => {
function mockVaultData(data) {
got.mockResolvedValue({
body: JSON.stringify({
data,
meta: {}
data: {
data
}
})
});
}

View file

@ -1,93 +1,7 @@
jest.mock('@actions/core');
const core = require('@actions/core');
const got = require('got');
const { when } = require('jest-when');
const { exportSecrets } = require('../action');
describe('e2e', () => {
beforeAll(async () => {
console.debug(`Testing against: http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret`)
// Verify Connection
await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret/config`, {
headers: {
'X-Vault-Token': 'testtoken',
},
});
await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret/data/test`, {
method: 'POST',
headers: {
'X-Vault-Token': 'testtoken',
},
body: {
data: {
a: 1,
b: 2,
c: 3,
},
},
json: true,
});
await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret/data/nested/test`, {
method: 'POST',
headers: {
'X-Vault-Token': 'testtoken',
},
body: {
data: {
e: 4,
f: 5,
g: 6,
},
},
json: true,
});
})
beforeEach(() => {
jest.resetAllMocks();
when(core.getInput)
.calledWith('vaultUrl')
.mockReturnValue(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}`);
when(core.getInput)
.calledWith('vaultToken')
.mockReturnValue('testtoken');
});
function mockInput(key) {
when(core.getInput)
.calledWith('keys')
.mockReturnValue(key);
}
it('get simple secret', async () => {
mockInput('test a')
await exportSecrets();
expect(core.exportSecret).toBeCalledWith('A', 1);
});
it('re-map secret', async () => {
mockInput('test a | TEST_KEY')
await exportSecrets();
expect(core.exportSecret).toBeCalledWith('TEST_KEY', 1);
});
it('get nested secret', async () => {
mockInput('nested/test e')
await exportSecrets();
expect(core.exportSecret).toBeCalledWith('E', 4);
it('verify', () => {
expect(process.env.A).toBe("1");
expect(process.env.NAMED_TOKEN).toBe("1");
expect(process.env.E).toBe("4");
});
});

45
e2e/setup.js Normal file
View file

@ -0,0 +1,45 @@
const got = require('got');
(async () => {
try {
// Verify Connection
await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret/config`, {
headers: {
'X-Vault-Token': 'testtoken',
},
});
await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret/data/test`, {
method: 'POST',
headers: {
'X-Vault-Token': 'testtoken',
},
body: {
data: {
a: 1,
b: 2,
c: 3,
},
},
json: true,
});
await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret/data/nested/test`, {
method: 'POST',
headers: {
'X-Vault-Token': 'testtoken',
},
body: {
data: {
e: 4,
f: 5,
g: 6,
},
},
json: true,
});
} catch (error) {
console.log(error);
process.exit(1);
}
})();

View file

@ -0,0 +1,91 @@
jest.mock('@actions/core');
const core = require('@actions/core');
const got = require('got');
const { when } = require('jest-when');
const { exportSecrets } = require('../action');
describe('integration', () => {
beforeAll(async () => {
// Verify Connection
await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret/config`, {
headers: {
'X-Vault-Token': 'testtoken',
},
});
await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret/data/test`, {
method: 'POST',
headers: {
'X-Vault-Token': 'testtoken',
},
body: {
data: {
a: 1,
b: 2,
c: 3,
},
},
json: true,
});
await got(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}/v1/secret/data/nested/test`, {
method: 'POST',
headers: {
'X-Vault-Token': 'testtoken',
},
body: {
data: {
e: 4,
f: 5,
g: 6,
},
},
json: true,
});
})
beforeEach(() => {
jest.resetAllMocks();
when(core.getInput)
.calledWith('vaultUrl')
.mockReturnValue(`http://${process.env.VAULT_HOST}:${process.env.VAULT_PORT}`);
when(core.getInput)
.calledWith('vaultToken')
.mockReturnValue('testtoken');
});
function mockInput(key) {
when(core.getInput)
.calledWith('keys')
.mockReturnValue(key);
}
it('get simple secret', async () => {
mockInput('test a')
await exportSecrets();
expect(core.exportSecret).toBeCalledWith('A', 1);
});
it('re-map secret', async () => {
mockInput('test a | TEST_KEY')
await exportSecrets();
expect(core.exportSecret).toBeCalledWith('TEST_KEY', 1);
});
it('get nested secret', async () => {
mockInput('nested/test e')
await exportSecrets();
expect(core.exportSecret).toBeCalledWith('E', 4);
});
});

View file

@ -0,0 +1,3 @@
module.exports = {
verbose: true
};

View file

@ -1,3 +1,3 @@
module.exports = {
testPathIgnorePatterns: ['/node_modules/', '<rootDir>/e2e/'],
testPathIgnorePatterns: ['/node_modules/', '<rootDir>/integration/', '<rootDir>/e2e/'],
};

View file

@ -5,6 +5,7 @@
"main": "index.js",
"scripts": {
"test": "jest",
"test:integration": "jest -c integration/jest.config.js",
"test:e2e": "jest -c e2e/jest.config.js"
},
"repository": {