|
| 1 | +import nock from 'nock'; |
| 2 | + |
| 3 | +import { config } from '../../../src/common/config'; |
| 4 | +import { scrapeData } from '../../../src/data-scraper'; |
| 5 | +import * as transmitterTypes from '../../../src/transmitter/types'; |
| 6 | + |
| 7 | +describe('dataScraper()', () => { |
| 8 | + beforeAll(() => { |
| 9 | + config.SYSDIG_ENDPOINT = 'https://sysdig'; |
| 10 | + config.SYSDIG_TOKEN = 'token123'; |
| 11 | + }); |
| 12 | + |
| 13 | + afterAll(() => { |
| 14 | + delete config.SYSDIG_ENDPOINT; |
| 15 | + delete config.SYSDIG_TOKEN; |
| 16 | + }); |
| 17 | + |
| 18 | + it('correctly sends data to kubernetes-upstream', async (jestDoneCallback) => { |
| 19 | + const bodyWithToken = { |
| 20 | + data: [ |
| 21 | + { |
| 22 | + imageID: 'something', |
| 23 | + namespace: 'sysdig', |
| 24 | + workloadName: 'workload', |
| 25 | + workloadKind: 'Deployment', |
| 26 | + container: 'box', |
| 27 | + packages: [], |
| 28 | + }, |
| 29 | + ], |
| 30 | + page: { |
| 31 | + returned: 10, |
| 32 | + next: 'xxx', |
| 33 | + }, |
| 34 | + }; |
| 35 | + const bodyNoToken = { |
| 36 | + data: [ |
| 37 | + { |
| 38 | + imageID: 'something', |
| 39 | + namespace: 'sysdig', |
| 40 | + workloadName: 'workload', |
| 41 | + workloadKind: 'Deployment', |
| 42 | + container: 'box', |
| 43 | + packages: [], |
| 44 | + }, |
| 45 | + ], |
| 46 | + page: { |
| 47 | + returned: 10, |
| 48 | + next: '', |
| 49 | + }, |
| 50 | + }; |
| 51 | + const expectedHeader = 'Bearer token123'; |
| 52 | + nock('https://sysdig', { reqheaders: { authorization: expectedHeader } }) |
| 53 | + .get('/v1/runtimeimages?limit=10&cursor=') |
| 54 | + .times(1) |
| 55 | + .reply(200, bodyWithToken); |
| 56 | + |
| 57 | + nock('https://sysdig', { reqheaders: { authorization: expectedHeader } }) |
| 58 | + .get('/v1/runtimeimages?limit=10&cursor=xxx') |
| 59 | + .times(1) |
| 60 | + .reply(200, bodyNoToken); |
| 61 | + |
| 62 | + nock('https://kubernetes-upstream.snyk.io') |
| 63 | + .post('/api/v1/runtime-results') |
| 64 | + .times(1) |
| 65 | + .reply(200, (uri, requestBody: transmitterTypes.IRuntimeDataPayload) => { |
| 66 | + try { |
| 67 | + expect(requestBody).toEqual<transmitterTypes.IRuntimeDataPayload>({ |
| 68 | + identity: { |
| 69 | + type: 'sysdig', |
| 70 | + }, |
| 71 | + target: { |
| 72 | + userLocator: expect.any(String), |
| 73 | + cluster: 'Default cluster', |
| 74 | + agentId: expect.any(String), |
| 75 | + }, |
| 76 | + facts: [ |
| 77 | + { |
| 78 | + type: 'loadedPackages', |
| 79 | + data: bodyWithToken.data, |
| 80 | + }, |
| 81 | + ], |
| 82 | + }); |
| 83 | + } catch (error) { |
| 84 | + jestDoneCallback(error); |
| 85 | + } |
| 86 | + }) |
| 87 | + .post('/api/v1/runtime-results') |
| 88 | + .times(1) |
| 89 | + .reply(200, (uri, requestBody: transmitterTypes.IRuntimeDataPayload) => { |
| 90 | + try { |
| 91 | + expect(requestBody).toEqual<transmitterTypes.IRuntimeDataPayload>({ |
| 92 | + identity: { |
| 93 | + type: 'sysdig', |
| 94 | + }, |
| 95 | + target: { |
| 96 | + userLocator: expect.any(String), |
| 97 | + cluster: 'Default cluster', |
| 98 | + agentId: expect.any(String), |
| 99 | + }, |
| 100 | + facts: [ |
| 101 | + { |
| 102 | + type: 'loadedPackages', |
| 103 | + data: bodyNoToken.data, |
| 104 | + }, |
| 105 | + ], |
| 106 | + }); |
| 107 | + jestDoneCallback(); |
| 108 | + } catch (error) { |
| 109 | + jestDoneCallback(error); |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + await scrapeData(); |
| 114 | + }); |
| 115 | +}); |
0 commit comments