Skip to content

Commit f550278

Browse files
author
Amir Moualem
authored
Merge pull request #244 from snyk/fix/queue-workload-metadata
Fix/queue workload metadata
2 parents bbb947b + 2e71d12 commit f550278

File tree

5 files changed

+55
-25
lines changed

5 files changed

+55
-25
lines changed

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
"pretest": "./scripts/build-image.sh",
77
"test": "npm run lint && npm run build && npm run test:unit && npm run test:integration",
88
"test:unit": "./tap test/unit -R spec",
9-
"test:integration": "./tap test/integration/kubernetes.test.ts -R spec --timeout=1200",
9+
"test:integration": "TEST_PLATFORM=kind CREATE_CLUSTER=true ./tap test/integration/kubernetes.test.ts -R spec --timeout=1200",
10+
"test:integration:kind": "TEST_PLATFORM=kind CREATE_CLUSTER=true ./tap test/integration/kubernetes.test.ts -R spec --timeout=1200",
11+
"test:integration:eks": "TEST_PLATFORM=eks CREATE_CLUSTER=false ./tap test/integration/kubernetes.test.ts -R spec --timeout=1200",
1012
"test:coverage": "npm run test:unit -- --coverage",
1113
"test:watch": "tsc-watch --onSuccess 'npm run test:unit'",
12-
"test:apk": "./scripts/build-image.sh && PACKAGE_MANAGER=apk ./tap test/integration/package-manager.test.ts -R spec --timeout=7200",
13-
"test:apt": "./scripts/build-image.sh && PACKAGE_MANAGER=apt ./tap test/integration/package-manager.test.ts -R spec --timeout=7200",
14-
"test:rpm": "./scripts/build-image.sh && PACKAGE_MANAGER=rpm ./tap test/integration/package-manager.test.ts -R spec --timeout=7200",
14+
"test:apk": "./scripts/build-image.sh && TEST_PLATFORM=kind CREATE_CLUSTER=true PACKAGE_MANAGER=apk ./tap test/integration/package-manager.test.ts -R spec --timeout=7200",
15+
"test:apt": "./scripts/build-image.sh && TEST_PLATFORM=kind CREATE_CLUSTER=true PACKAGE_MANAGER=apt ./tap test/integration/package-manager.test.ts -R spec --timeout=7200",
16+
"test:rpm": "./scripts/build-image.sh && TEST_PLATFORM=kind CREATE_CLUSTER=true PACKAGE_MANAGER=rpm ./tap test/integration/package-manager.test.ts -R spec --timeout=7200",
1517
"start": "bin/start",
1618
"prepare": "npm run build",
1719
"build": "tsc",

test/setup/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ export async function removeMonitor(): Promise<void> {
7171
}
7272
}
7373

74-
async function createEnvironment(imageNameAndTag: string): Promise<void> {
75-
await kubectl.downloadKubectl();
76-
await platforms.kind.create(imageNameAndTag);
74+
async function createEnvironment(): Promise<void> {
75+
// TODO: we probably want to use k8s-api for that, not kubectl
7776
const servicesNamespace = 'services';
7877
await kubectl.createNamespace(servicesNamespace);
7978
// Small hack to prevent timing problems in CircleCI...
@@ -130,8 +129,16 @@ export async function deployMonitor(): Promise<string> {
130129
'snyk/kubernetes-monitor:local',
131130
);
132131

133-
// this bit is probably where we act upon the decision of which platform we'll use
134-
await createEnvironment(imageNameAndTag);
132+
const testPlatform = process.env['TEST_PLATFORM'] || 'kind';
133+
const createCluster = process.env['CREATE_CLUSTER'] === 'true';
134+
console.log(`platform chosen is ${testPlatform}, createCluster===${createCluster}`);
135+
136+
await kubectl.downloadKubectl();
137+
if (createCluster) {
138+
await platforms[testPlatform].create(imageNameAndTag);
139+
}
140+
await platforms[testPlatform].config();
141+
await createEnvironment();
135142
const integrationId = await installKubernetesMonitor(imageNameAndTag);
136143
await waiters.waitForMonitorToBeReady();
137144
console.log(`Deployed the snyk-monitor with integration ID ${integrationId}`);

test/setup/platforms/eks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
export async function createCluster(imageNameAndTag: string): Promise<void> {
2-
exportKubeConfig('TODO');
2+
exportKubeConfig();
33
throw new Error('Not implemented');
44
// process.env.KUBECONFIG = 'path-to-/kubeconfig-aws';
55
}
66

7-
export async function deleteCluster(clusterName = 'kind'): Promise<void> {
7+
export async function deleteCluster(): Promise<void> {
88
throw new Error('Not implemented');
99
}
1010

11-
async function exportKubeConfig(clusterName): Promise<void> {
11+
export async function exportKubeConfig(): Promise<void> {
1212
throw new Error('Not implemented');
1313
}

test/setup/platforms/index.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
import * as kind from './kind';
22
import * as eks from './eks';
33

4+
interface IPlatformSetup {
5+
// create a Kubernetes cluster
6+
create: (imageNameAndTag: string) => Promise<void>;
7+
// delete a Kubernetes cluster
8+
delete: () => Promise<void>;
9+
// set KUBECONFIG to point at the tested cluster
10+
config: () => Promise<void>;
11+
}
12+
13+
const kindSetup: IPlatformSetup = {
14+
create: kind.createCluster,
15+
delete: kind.deleteCluster,
16+
config: kind.exportKubeConfig,
17+
};
18+
19+
const eksSetup: IPlatformSetup = {
20+
create: eks.createCluster,
21+
delete: eks.deleteCluster,
22+
config: eks.exportKubeConfig,
23+
}
24+
425
export default {
5-
kind: {create: kind.createCluster, delete: kind.deleteCluster},
6-
eks: {create: eks.createCluster, delete: eks.deleteCluster},
26+
kind: kindSetup,
27+
eks: eksSetup,
728
}

test/setup/platforms/kind.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,29 @@ import { platform } from 'os';
44
import { resolve } from 'path';
55
import * as needle from 'needle';
66

7+
const clusterName = 'kind';
8+
79
export async function createCluster(imageNameAndTag: string): Promise<void> {
810
const osDistro = platform();
911
await download(osDistro);
10-
const clusterName = 'kind';
1112
await createKindCluster(clusterName);
12-
await exportKubeConfig(clusterName);
1313
await loadImageInCluster(imageNameAndTag);
1414
}
1515

16-
export async function deleteCluster(clusterName = 'kind'): Promise<void> {
16+
export async function deleteCluster(): Promise<void> {
1717
console.log(`Deleting cluster ${clusterName}...`);
1818
await exec(`./kind delete cluster --name=${clusterName}`);
1919
console.log(`Deleted cluster ${clusterName}!`);
2020
}
2121

22+
export async function exportKubeConfig(): Promise<void> {
23+
console.log('Exporting K8s config...');
24+
const kindResponse = await exec(`./kind get kubeconfig-path --name="${clusterName}"`);
25+
const configPath = kindResponse.stdout.replace(/[\n\t\r]/g, '');
26+
process.env.KUBECONFIG = configPath;
27+
console.log('Exported K8s config!');
28+
}
29+
2230
async function download(osDistro: string): Promise<void> {
2331
try {
2432
accessSync(resolve(process.cwd(), 'kind'), constants.R_OK);
@@ -55,14 +63,6 @@ async function createKindCluster(clusterName, kindImageTag = 'latest'): Promise<
5563
console.log(`Created cluster ${clusterName}!`);
5664
}
5765

58-
async function exportKubeConfig(clusterName): Promise<void> {
59-
console.log('Exporting K8s config...');
60-
const kindResponse = await exec(`./kind get kubeconfig-path --name="${clusterName}"`);
61-
const configPath = kindResponse.stdout.replace(/[\n\t\r]/g, '');
62-
process.env.KUBECONFIG = configPath;
63-
console.log('Exported K8s config!');
64-
}
65-
6666
async function loadImageInCluster(imageNameAndTag): Promise<void> {
6767
console.log(`Loading image ${imageNameAndTag} in cluster...`);
6868
await exec(`./kind load docker-image ${imageNameAndTag}`);

0 commit comments

Comments
 (0)