Skip to content

Commit 2dde4b1

Browse files
committed
test: validate sysdig integration
1 parent a3a9f50 commit 2dde4b1

File tree

7 files changed

+221
-4
lines changed

7 files changed

+221
-4
lines changed

test/common/config.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ describe('extractNamespaceName()', () => {
4848
) => {
4949
if (clusterNameEnvVar) {
5050
process.env.SNYK_CLUSTER_NAME = clusterNameEnvVar;
51+
process.env.SNYK_SYSDIG_ENDPOINT = 'https://api/v1/images/';
52+
process.env.SNYK_SYSDIG_TOKEN = '1432gtrhtrw32raf';
5153
}
5254

5355
const consoleSpy = jest.spyOn(console, 'log').mockReturnValue();
@@ -73,5 +75,15 @@ describe('extractNamespaceName()', () => {
7375
expect(config.SKIP_K8S_JOBS).toEqual(false);
7476
expect(config.WORKERS_COUNT).toEqual(10);
7577
expect(config.SKOPEO_COMPRESSION_LEVEL).toEqual(6);
78+
expect(config.SYSDIG_ENDPOINT).toEqual('https://api/v1/images/');
79+
expect(config.SYSDIG_TOKEN).toEqual('1432gtrhtrw32raf');
80+
delete process.env.SNYK_SYSDIG_ENDPOINT;
81+
delete process.env.SNYK_SYSDIG_TOKEN;
82+
});
83+
84+
it('cannot load sysdig API and JWT values if it is not enabled', () => {
85+
const { config } = require('../../src/common/config');
86+
expect(config.SYSDIG_ENDPOINT).toBeUndefined();
87+
expect(config.SYSDIG_TOKEN).toBeUndefined();
7688
});
7789
});

test/setup/deployers/helm.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ async function deployKubernetesMonitor(
3939
'--set volumes.projected.serviceAccountToken=true ' +
4040
'--set securityContext.fsGroup=65534 ' +
4141
'--set skopeo.compression.level=1 ' +
42-
'--set workers.count=5 ',
42+
'--set workers.count=5 ' +
43+
'--set sysdig.enabled=true ',
4344
);
4445
console.log(
4546
`Deployed ${imageOptions.nameAndTag} with pull policy ${imageOptions.pullPolicy}`,

test/setup/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export async function deployMonitor(): Promise<string> {
159159
const imageNameAndTag = getEnvVariableOrDefault(
160160
'KUBERNETES_MONITOR_IMAGE_NAME_AND_TAG',
161161
// the default, determined by ./script/build-image.sh
162-
'snyk/kubernetes-monitor:local',
162+
'snyk/kubernetes-monitor-private-fork:local',
163163
);
164164

165165
console.log(

test/setup/platforms/eks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export async function loadImageInCluster(
6060
throw new Error('aws ecr get-login-password returned an unexpected output');
6161
}
6262

63-
const targetImage = `${ecrURL}/snyk/kubernetes-monitor:local`;
63+
const targetImage = `${ecrURL}/snyk/kubernetes-monitor-private-fork:local`;
6464

6565
await exec(`docker tag ${imageNameAndTag} ${targetImage}`);
6666
await exec(`docker push ${targetImage}`);
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
});

test/unit/supervisor/pod-watch-handler-caches.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as async from 'async';
22
import * as fs from 'fs';
33
import sleep from 'sleep-promise';
44
import * as YAML from 'yaml';
5-
65
// NOTE: Very important that the mock is set up before application code is imported!
76
let pushCallCount = 0;
87
const asyncQueueSpy = jest

test/unit/transmitter-payload.spec.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
IDeleteWorkloadPayload,
1010
IImageLocator,
1111
ILocalWorkloadLocator,
12+
IRuntimeDataPayload,
13+
IRuntimeImage,
1214
IWorkload,
1315
IWorkloadLocator,
1416
IWorkloadMetadata,
@@ -208,4 +210,92 @@ describe('transmitter payload tests', () => {
208210
type: 'wl-type',
209211
});
210212
});
213+
214+
test.concurrent('constructRuntimeData happy flow', async () => {
215+
const runtimeDataPayload = payload.constructRuntimeData([
216+
{
217+
imageID: 'something',
218+
namespace: 'sysdig',
219+
workloadName: 'workload',
220+
workloadKind: 'deployment',
221+
container: 'box',
222+
packages: [],
223+
},
224+
]);
225+
expect(runtimeDataPayload).toEqual<IRuntimeDataPayload>({
226+
identity: {
227+
type: 'sysdig',
228+
},
229+
target: {
230+
userLocator: expect.any(String),
231+
cluster: 'Default cluster',
232+
agentId: expect.any(String),
233+
},
234+
facts: [
235+
{
236+
type: 'loadedPackages',
237+
data: [
238+
{
239+
imageID: 'something',
240+
namespace: 'sysdig',
241+
workloadName: 'workload',
242+
workloadKind: 'Deployment',
243+
container: 'box',
244+
packages: [],
245+
},
246+
],
247+
},
248+
],
249+
});
250+
});
251+
252+
test.concurrent(
253+
'constructRuntimeData with excluded namespace happy flow',
254+
async () => {
255+
config.EXCLUDED_NAMESPACES = ['test'];
256+
const runtimeDataPayload = payload.constructRuntimeData([
257+
{
258+
imageID: 'something',
259+
namespace: 'sysdig',
260+
workloadName: 'workload',
261+
workloadKind: 'deployment',
262+
container: 'box',
263+
packages: [],
264+
},
265+
{
266+
imageID: 'something',
267+
namespace: 'test',
268+
workloadName: 'workload',
269+
workloadKind: 'deployment',
270+
container: 'box',
271+
packages: [],
272+
},
273+
]);
274+
expect(runtimeDataPayload).toEqual<IRuntimeDataPayload>({
275+
identity: {
276+
type: 'sysdig',
277+
},
278+
target: {
279+
userLocator: expect.any(String),
280+
cluster: 'Default cluster',
281+
agentId: expect.any(String),
282+
},
283+
facts: [
284+
{
285+
type: 'loadedPackages',
286+
data: [
287+
{
288+
imageID: 'something',
289+
namespace: 'sysdig',
290+
workloadName: 'workload',
291+
workloadKind: 'Deployment',
292+
container: 'box',
293+
packages: [],
294+
},
295+
],
296+
},
297+
],
298+
});
299+
},
300+
);
211301
});

0 commit comments

Comments
 (0)