|
| 1 | +import * as tap from 'tap'; |
| 2 | +import * as nock from 'nock'; |
| 3 | +import * as sleep from 'sleep-promise'; |
| 4 | +import { exec } from 'child-process-promise'; |
| 5 | + |
| 6 | +import * as kubectl from '../helpers/kubectl'; |
| 7 | +import * as kind from '../setup/platforms/kind'; |
| 8 | + |
| 9 | +// let integrationId: string; |
| 10 | + |
| 11 | +async function tearDown() { |
| 12 | + console.log('Begin removing the snyk-monitor...'); |
| 13 | + await kind.deleteCluster(); |
| 14 | + console.log('Removed the snyk-monitor!'); |
| 15 | +} |
| 16 | + |
| 17 | +tap.tearDown(tearDown); |
| 18 | + |
| 19 | +tap.test('Kubernetes-Monitor with KinD', async (t) => { |
| 20 | + |
| 21 | + // Start fresh |
| 22 | + try { |
| 23 | + await tearDown(); |
| 24 | + } catch (error) { |
| 25 | + console.log(`could not start with a clean environment: ${error.message}`); |
| 26 | + } |
| 27 | + |
| 28 | + // install Skopeo |
| 29 | + // TODO: this thing should probably be in a setup test environment script |
| 30 | + // not in this file |
| 31 | + try { |
| 32 | + await exec('which skopeo'); |
| 33 | + console.log('Skopeo already installed :tada:'); |
| 34 | + } catch (err) { |
| 35 | + // linux-oriented, not mac |
| 36 | + // for mac, install skopeo with brew |
| 37 | + console.log('installing Skopeo'); |
| 38 | + await exec('git clone https://github.com/containers/skopeo'); |
| 39 | + await exec('(cd skopeo && make binary-static DISABLE_CGO=1)'); |
| 40 | + await exec('sudo mkdir -p /etc/containers'); |
| 41 | + await exec('sudo chown circleci:circleci /etc/containers'); |
| 42 | + await exec('cp ./skopeo/default-policy.json /etc/containers/policy.json'); |
| 43 | + |
| 44 | + process.env['PATH'] = process.env['PATH'] + ':./skopeo'; |
| 45 | + } |
| 46 | + |
| 47 | + // kubectl |
| 48 | + await kubectl.downloadKubectl(); |
| 49 | + |
| 50 | + // KinD |
| 51 | + await kind.createCluster(); |
| 52 | + await kind.exportKubeConfig(); |
| 53 | + Promise.all([ |
| 54 | + kubectl.createNamespace('snyk-monitor'), |
| 55 | + kubectl.createNamespace('services'), |
| 56 | + ]); |
| 57 | + // wait for default service account |
| 58 | + await kubectl.waitForServiceAccount('default', 'default'); |
| 59 | + |
| 60 | + // Services |
| 61 | + await Promise.all([ |
| 62 | + kubectl.applyK8sYaml('./test/fixtures/java-deployment.yaml'), |
| 63 | + ]); |
| 64 | + |
| 65 | + // TODO: wait for the services to start? |
| 66 | + |
| 67 | + // Setup nocks |
| 68 | + nock('https://kubernetes-upstream.snyk.io') |
| 69 | + .post('/api/v1/workload') |
| 70 | + .times(1) |
| 71 | + .reply(200, (uri, requestBody) => { |
| 72 | + // TODO assert POST payload |
| 73 | + }); |
| 74 | + |
| 75 | + nock('https://kubernetes-upstream.snyk.io') |
| 76 | + .post('/api/v1/dependency-graph') |
| 77 | + .times(1) |
| 78 | + .reply(200, (uri, requestBody) => { |
| 79 | + // TODO assert POST payload |
| 80 | + }); |
| 81 | + |
| 82 | + // Start the monitor |
| 83 | + require('../../src'); |
| 84 | + |
| 85 | + // TODO: replace with being event driven? |
| 86 | + // will still need SOME timeout |
| 87 | + while (true) { |
| 88 | + if (nock.isDone()) { |
| 89 | + break; |
| 90 | + } else { |
| 91 | + await sleep(5 * 1000); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // additional asserts? |
| 96 | + t.ok(nock.isDone(), 'all outgoing calls were made'); |
| 97 | + |
| 98 | + // TODO cleanup the images we saved to /var/tmp? |
| 99 | +}); |
0 commit comments