|
| 1 | +/* eslint-env jest */ |
| 2 | +// mock config for testing |
| 3 | +jest.mock('../src/config', () => require('./__mocks__/config')); |
| 4 | +const config = require('../src/config'); |
| 5 | +// switch config to normal |
| 6 | +config.__load('normal'); |
| 7 | + |
| 8 | +// npm packages |
| 9 | +const getPort = require('get-port'); |
| 10 | +const path = require('path'); |
| 11 | +const tar = require('tar-fs'); |
| 12 | + |
| 13 | +// our packages |
| 14 | +const authToken = require('./fixtures/authToken'); |
| 15 | +const {startServer} = require('../src'); |
| 16 | +const {getSecretsCollection} = require('../src/db/secrets'); |
| 17 | +const docker = require('../src/docker/docker'); |
| 18 | + |
| 19 | +// create tar streams |
| 20 | +const streamDocker = tar.pack(path.join(__dirname, 'fixtures', 'secrets-project')); |
| 21 | + |
| 22 | +// test secret |
| 23 | +const testSecret = { |
| 24 | + secretName: 'test-secret', |
| 25 | + secretValue: 'test-secret-value', |
| 26 | +}; |
| 27 | + |
| 28 | +// container vars |
| 29 | +let fastify; |
| 30 | + |
| 31 | +// set timeout to 60s |
| 32 | +jest.setTimeout(60000); |
| 33 | + |
| 34 | +beforeAll(async () => { |
| 35 | + // start server |
| 36 | + const port = await getPort(); |
| 37 | + fastify = await startServer(port); |
| 38 | + return fastify; |
| 39 | +}); |
| 40 | + |
| 41 | +afterAll(() => fastify.close()); |
| 42 | + |
| 43 | +test('Should create new secret', async done => { |
| 44 | + // options base |
| 45 | + const options = { |
| 46 | + method: 'POST', |
| 47 | + url: '/secrets', |
| 48 | + headers: { |
| 49 | + Authorization: `Bearer ${authToken}`, |
| 50 | + }, |
| 51 | + payload: testSecret, |
| 52 | + }; |
| 53 | + |
| 54 | + const response = await fastify.inject(options); |
| 55 | + const result = JSON.parse(response.payload); |
| 56 | + |
| 57 | + // check response |
| 58 | + expect(response.statusCode).toEqual(200); |
| 59 | + expect(result.name).toEqual(testSecret.secretName); |
| 60 | + expect(result.value).toEqual(testSecret.secretValue); |
| 61 | + expect(result.user).toEqual('admin'); |
| 62 | + |
| 63 | + done(); |
| 64 | +}); |
| 65 | + |
| 66 | +test('Should get list with new secret', async done => { |
| 67 | + // options base |
| 68 | + const options = { |
| 69 | + method: 'GET', |
| 70 | + url: '/secrets', |
| 71 | + headers: { |
| 72 | + Authorization: `Bearer ${authToken}`, |
| 73 | + }, |
| 74 | + }; |
| 75 | + |
| 76 | + const response = await fastify.inject(options); |
| 77 | + const result = JSON.parse(response.payload); |
| 78 | + |
| 79 | + // check response |
| 80 | + expect(response.statusCode).toEqual(200); |
| 81 | + expect(result.secrets).toBeDefined(); |
| 82 | + expect(result.secrets.length).toEqual(1); |
| 83 | + expect(result.secrets[0].user).toEqual('admin'); |
| 84 | + expect(result.secrets[0].name).toEqual(testSecret.secretName); |
| 85 | + expect(result.secrets[0].value).toBeUndefined(); |
| 86 | + |
| 87 | + done(); |
| 88 | +}); |
| 89 | + |
| 90 | +test('Should deploy simple docker project with secret', async done => { |
| 91 | + const options = { |
| 92 | + method: 'POST', |
| 93 | + url: '/deploy', |
| 94 | + headers: { |
| 95 | + Authorization: `Bearer ${authToken}`, |
| 96 | + 'Content-Type': 'application/octet-stream', |
| 97 | + }, |
| 98 | + payload: streamDocker, |
| 99 | + }; |
| 100 | + |
| 101 | + const response = await fastify.inject(options); |
| 102 | + // parse result into lines |
| 103 | + const result = response.payload |
| 104 | + .split('\n') |
| 105 | + .filter(l => l && l.length) |
| 106 | + .map(line => JSON.parse(line)); |
| 107 | + |
| 108 | + // find deployments |
| 109 | + const completeDeployments = result.find(it => it.deployments && it.deployments.length).deployments; |
| 110 | + |
| 111 | + // check response |
| 112 | + expect(response.statusCode).toEqual(200); |
| 113 | + expect(completeDeployments.length).toEqual(1); |
| 114 | + expect(completeDeployments[0].Name.startsWith('/exo-admin-test-secrets-deploy-')).toBeTruthy(); |
| 115 | + |
| 116 | + // check docker services |
| 117 | + const allContainers = await docker.listContainers(); |
| 118 | + const containerInfo = allContainers.find(c => c.Names.includes(completeDeployments[0].Name)); |
| 119 | + expect(containerInfo).toBeDefined(); |
| 120 | + |
| 121 | + const containerData = docker.getContainer(containerInfo.Id); |
| 122 | + const container = await containerData.inspect(); |
| 123 | + |
| 124 | + // check secrets replacement in env vars |
| 125 | + const [key, value] = container.Config.Env.map(v => v.split('=')).find(([key]) => key === 'test'); |
| 126 | + expect(key).toEqual('test'); |
| 127 | + expect(value).toEqual(testSecret.secretValue); |
| 128 | + |
| 129 | + // cleanup |
| 130 | + const instance = docker.getContainer(containerInfo.Id); |
| 131 | + await instance.remove({force: true}); |
| 132 | + |
| 133 | + done(); |
| 134 | +}); |
| 135 | + |
| 136 | +test('Should delete new secret', async done => { |
| 137 | + // options base |
| 138 | + const options = { |
| 139 | + method: 'DELETE', |
| 140 | + url: '/secrets', |
| 141 | + headers: { |
| 142 | + Authorization: `Bearer ${authToken}`, |
| 143 | + }, |
| 144 | + payload: { |
| 145 | + secretName: testSecret.secretName, |
| 146 | + }, |
| 147 | + }; |
| 148 | + |
| 149 | + // check response |
| 150 | + const response = await fastify.inject(options); |
| 151 | + expect(response.statusCode).toEqual(204); |
| 152 | + |
| 153 | + // make sure it's no longer in db |
| 154 | + expect(getSecretsCollection().find()).toEqual([]); |
| 155 | + |
| 156 | + done(); |
| 157 | +}); |
0 commit comments