Skip to content

Commit 6a136c8

Browse files
authored
Merge pull request #416 from snyk/test/openshift3-yaml
test/openshift3 with yaml deployment
2 parents 07cd896 + 8dc5422 commit 6a136c8

File tree

8 files changed

+124
-32
lines changed

8 files changed

+124
-32
lines changed

.circleci/config.yml

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 2
1+
version: 2.1
22

33
default_machine_config: &default_machine_config
44
machine:
@@ -29,6 +29,20 @@ main_branches_filter: &main_branches_filter
2929
- staging
3030
- master
3131

32+
commands:
33+
setup_node12:
34+
description: Setup Node 12
35+
steps:
36+
- run:
37+
command: |
38+
export NVM_DIR="/opt/circleci/.nvm"
39+
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
40+
nvm install v12
41+
npm install
42+
echo 'export NVM_DIR="/opt/circleci/.nvm"' >> $BASH_ENV
43+
echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> $BASH_ENV
44+
echo 'nvm use 12' >> $BASH_ENV
45+
3246
jobs:
3347
######################## PR OR MERGE TO STAGING ########################
3448
build_image:
@@ -227,6 +241,26 @@ jobs:
227241
- store_artifacts:
228242
path: /tmp/logs/test/integration/eks
229243

244+
openshift3_integration_tests:
245+
<<: *default_machine_config
246+
steps:
247+
- checkout
248+
- setup_node12
249+
- run:
250+
name: Create temporary directory for logs
251+
command: mkdir -p /tmp/logs/test/integration/openshift3
252+
- run:
253+
name: Integration tests OpenShift 3
254+
command: |
255+
export KUBERNETES_MONITOR_IMAGE_NAME_AND_TAG=$(./scripts/circleci-jobs/setup-openshift3-integration-tests.py)
256+
npm run test:integration:openshift3
257+
- run:
258+
name: Notify Slack on failure
259+
command: ./scripts/slack-notify-failure.sh "staging-openshift3-integration-tests-${CIRCLE_SHA1}"
260+
when: on_fail
261+
- store_artifacts:
262+
path: /tmp/logs/test/integration/openshift3
263+
230264
openshift4_integration_tests:
231265
<<: *default_machine_config
232266
steps:
@@ -423,6 +457,10 @@ workflows:
423457
requires:
424458
- build_image
425459
<<: *staging_branch_only_filter
460+
- openshift3_integration_tests:
461+
requires:
462+
- build_image
463+
<<: *staging_branch_only_filter
426464
- openshift4_integration_tests:
427465
requires:
428466
- build_image

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"test:integration": "DEPLOYMENT_TYPE=YAML TEST_PLATFORM=kind CREATE_CLUSTER=true tap test/integration/kubernetes.test.ts --timeout=900",
1111
"test:integration:kind": "DEPLOYMENT_TYPE=YAML TEST_PLATFORM=kind CREATE_CLUSTER=true tap test/integration/kubernetes.test.ts --timeout=900",
1212
"test:integration:eks": "DEPLOYMENT_TYPE=YAML TEST_PLATFORM=eks CREATE_CLUSTER=false tap test/integration/kubernetes.test.ts --timeout=900",
13+
"test:integration:openshift3": "DEPLOYMENT_TYPE=YAML TEST_PLATFORM=openshift3 CREATE_CLUSTER=true tap test/integration/kubernetes.test.ts --timeout=900",
1314
"test:integration:openshift4": "DEPLOYMENT_TYPE=Operator TEST_PLATFORM=openshift4 CREATE_CLUSTER=false tap test/integration/kubernetes.test.ts --timeout=900",
1415
"test:coverage": "npm run test:unit -- --coverage",
1516
"test:watch": "tsc-watch --onSuccess 'npm run test:unit'",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/python3
2+
3+
import os
4+
import subprocess
5+
6+
dockerhub_user = os.getenv('DOCKERHUB_USER')
7+
dockerhub_password = os.getenv('DOCKERHUB_PASSWORD')
8+
9+
subprocess.getoutput("docker login --username " + dockerhub_user + " --password " + dockerhub_password)
10+
11+
circle_branch = os.getenv("CIRCLE_BRANCH")
12+
if circle_branch == "staging":
13+
image_tag = "staging-candidate"
14+
else:
15+
image_tag = "discardable"
16+
circle_sha1 = os.getenv("CIRCLE_SHA1")
17+
kubernetes_monitor_image_name_and_tag = "snyk/kubernetes-monitor:" + image_tag + "-" + circle_sha1
18+
19+
subprocess.getoutput("docker pull " + kubernetes_monitor_image_name_and_tag)
20+
21+
print(kubernetes_monitor_image_name_and_tag)

test/helpers/kubectl.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
import { exec } from 'child-process-promise';
2-
import { accessSync, chmodSync, constants, writeFileSync } from 'fs';
2+
import { chmodSync, writeFileSync, existsSync, unlinkSync } from 'fs';
33
import { platform } from 'os';
44
import { resolve } from 'path';
55
import * as needle from 'needle';
66
import * as sleep from 'sleep-promise';
77

8-
export async function downloadKubectl(): Promise<void> {
9-
try {
10-
accessSync(resolve(process.cwd(), 'kubectl'), constants.R_OK);
11-
} catch (error) {
12-
console.log('Downloading kubectl...');
13-
14-
// eslint-disable-next-line @typescript-eslint/camelcase
15-
const requestOptions = { follow_max: 2 };
16-
const k8sRelease = await getLatestStableK8sRelease();
17-
const osDistro = platform();
18-
const bodyData = null;
19-
await needle('get', 'https://storage.googleapis.com/kubernetes-release/release/' +
20-
`${k8sRelease}/bin/${osDistro}/amd64/kubectl`,
21-
bodyData,
22-
requestOptions,
23-
).then((response) => {
24-
writeFileSync('kubectl', response.body);
25-
chmodSync('kubectl', 0o755); // rwxr-xr-x
26-
});
27-
28-
console.log('kubectl downloaded');
8+
/**
9+
* @param version For example: "v1.18.0"
10+
*/
11+
export async function downloadKubectl(version: string): Promise<void> {
12+
const kubectlPath = resolve(process.cwd(), 'kubectl');
13+
if (existsSync(kubectlPath)) {
14+
if (version === 'latest') {
15+
return;
16+
}
17+
18+
// Always start clean when requesting a specific version.
19+
unlinkSync(kubectlPath);
2920
}
21+
22+
console.log(`Downloading kubectl ${version}...`);
23+
24+
// eslint-disable-next-line @typescript-eslint/camelcase
25+
const requestOptions = { follow_max: 2 };
26+
const k8sRelease = version === 'latest' ? await getLatestStableK8sRelease() : version;
27+
const osDistro = platform();
28+
const bodyData = null;
29+
await needle('get', 'https://storage.googleapis.com/kubernetes-release/release/' +
30+
`${k8sRelease}/bin/${osDistro}/amd64/kubectl`,
31+
bodyData,
32+
requestOptions,
33+
).then((response) => {
34+
writeFileSync('kubectl', response.body);
35+
chmodSync('kubectl', 0o755); // rwxr-xr-x
36+
});
37+
38+
console.log('kubectl downloaded');
3039
}
3140

3241
export async function createNamespace(namespace: string): Promise<void> {
@@ -63,7 +72,7 @@ export async function applyK8sYaml(pathToYamlDeployment: string): Promise<void>
6372

6473
export async function createPodFromImage(name: string, image: string, namespace: string) {
6574
console.log(`Letting Kubernetes decide how to manage image ${image} with name ${name}`);
66-
await exec(`./kubectl run ${name} --image=${image} -n ${namespace} -- sleep 999999999`);
75+
await exec(`./kubectl run ${name} --generator=run-pod/v1 --image=${image} -n ${namespace} -- sleep 999999999`);
6776
console.log(`Done Letting Kubernetes decide how to manage image ${image} with name ${name}`);
6877
}
6978

test/setup/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from 'fs';
22
import * as sleep from 'sleep-promise';
33
import * as uuidv4 from 'uuid/v4';
44

5-
import platforms from './platforms';
5+
import platforms, { getKubernetesVersionForPlatform } from './platforms';
66
import deployers from './deployers';
77
import { IImageOptions } from './deployers/types';
88
import * as kubectl from '../helpers/kubectl';
@@ -92,10 +92,12 @@ export async function deployMonitor(): Promise<string> {
9292

9393
console.log(`platform chosen is ${testPlatform}, createCluster===${createCluster}`);
9494

95-
await kubectl.downloadKubectl();
95+
const kubernetesVersion = getKubernetesVersionForPlatform(testPlatform);
96+
await kubectl.downloadKubectl(kubernetesVersion);
97+
9698
await platforms[testPlatform].setupTester();
9799
if (createCluster) {
98-
await platforms[testPlatform].create();
100+
await platforms[testPlatform].create(kubernetesVersion);
99101
await platforms[testPlatform].config();
100102
} else {
101103
await platforms[testPlatform].config();

test/setup/platforms/index.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as openshift4 from './openshift4';
44

55
interface IPlatformSetup {
66
// create a Kubernetes cluster
7-
create: () => Promise<void>;
7+
create: (version: string) => Promise<void>;
88
// loads the image so Kubernetes may run it, return the name of the image in its registry's format
99
loadImage: (imageNameAndTag: string) => Promise<string>;
1010
// delete a Kubernetes cluster
@@ -35,6 +35,16 @@ const eksSetup: IPlatformSetup = {
3535
setupTester: eks.setupTester,
3636
};
3737

38+
// Use a kind cluster pinned to a specific Kubernetes version to mimic OS3.
39+
const openshift3Setup: IPlatformSetup = {
40+
create: kind.createCluster,
41+
loadImage: kind.loadImageInCluster,
42+
delete: kind.deleteCluster,
43+
config: kind.exportKubeConfig,
44+
clean: kind.clean,
45+
setupTester: kind.setupTester,
46+
};
47+
3848
const openshift4Setup: IPlatformSetup = {
3949
create: openshift4.createCluster,
4050
loadImage: openshift4.returnUnchangedImageNameAndTag,
@@ -44,8 +54,18 @@ const openshift4Setup: IPlatformSetup = {
4454
setupTester: openshift4.setupTester,
4555
};
4656

57+
export function getKubernetesVersionForPlatform(testPlatform: string): string {
58+
switch (testPlatform) {
59+
case 'openshift3':
60+
return 'v1.11.10';
61+
default:
62+
return 'latest';
63+
}
64+
}
65+
4766
export default {
4867
kind: kindSetup,
4968
eks: eksSetup,
69+
openshift3: openshift3Setup,
5070
openshift4: openshift4Setup,
5171
};

test/setup/platforms/kind.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ export async function setupTester(): Promise<void> {
1111
await download(osDistro);
1212
}
1313

14-
export async function createCluster(): Promise<void> {
14+
export async function createCluster(version: string): Promise<void> {
1515
// available tags may be viewed at https://hub.docker.com/r/kindest/node/tags
16-
const kindImageTag = 'latest';
16+
const kindImageTag = version;
1717
console.log(`Creating cluster "${clusterName}" with Kind image tag ${kindImageTag}...`);
1818

1919
let kindImageArgument = '';

test/system/kind.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ tap.test('Kubernetes-Monitor with KinD', async (t) => {
4444
process.env['PATH'] = process.env['PATH'] + ':./skopeo';
4545
}
4646

47+
const kubernetesVersion = 'latest';
4748
// kubectl
48-
await kubectl.downloadKubectl();
49+
await kubectl.downloadKubectl(kubernetesVersion);
4950

5051
// KinD
5152
await kind.setupTester();
52-
await kind.createCluster();
53+
await kind.createCluster(kubernetesVersion);
5354
await kind.exportKubeConfig();
5455

5556
await Promise.all([

0 commit comments

Comments
 (0)