Skip to content

Commit 51281e5

Browse files
shaimendelArthur Granado
authored andcommitted
test: operator on kind
1 parent f8e65c5 commit 51281e5

File tree

11 files changed

+163
-9
lines changed

11 files changed

+163
-9
lines changed

.circleci/config/@config.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ workflows:
3838
requires:
3939
- build_image
4040
<<: *main_branches_filter
41+
- integration_tests_proxy:
42+
requires:
43+
- build_image
44+
<<: *main_branches_filter
4145

4246
MERGE_TO_STAGING:
4347
jobs:
@@ -73,11 +77,18 @@ workflows:
7377
requires:
7478
- build_image
7579
<<: *staging_branch_only_filter
80+
- integration_tests_operator_on_k8s:
81+
requires:
82+
- build_image
83+
- build_operator
84+
- upload_operator
85+
<<: *staging_branch_only_filter
7686
- openshift4_integration_tests:
7787
requires:
7888
- build_image
7989
- build_operator
8090
- upload_operator
91+
- integration_tests_operator_on_k8s
8192
<<: *staging_branch_only_filter
8293
- tag_and_push:
8394
requires:

.circleci/config/jobs/@jobs.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,36 @@ integration_tests_helm:
174174
- store_artifacts:
175175
path: /tmp/logs/test/integration/kind-helm
176176

177+
integration_tests_operator_on_k8s:
178+
machine:
179+
docker_layer_caching: true
180+
enabled: true
181+
steps:
182+
- checkout
183+
- setup_node12
184+
- install_python_requests
185+
- run:
186+
command: mkdir -p /tmp/logs/test/integration/kind-olm-operator
187+
name: Create temporary directory for logs
188+
- run:
189+
command: |
190+
export KUBERNETES_MONITOR_IMAGE_NAME_AND_TAG=$(./scripts/circleci-jobs/setup-integration-tests.py)
191+
npm run test:integration:kindolm:operator
192+
name: Operator integration tests on plain k8s
193+
- run:
194+
name: Delete Operator from Quay
195+
command: |
196+
./scripts/operator/delete-operator-from-quay.sh
197+
when: on_fail # it will be deleted on success in the openshift4 tests
198+
- run:
199+
command: |
200+
./scripts/slack/notify_failure_on_branch.py "staging-integration-operator-k8s-tests-${CIRCLE_SHA1}"
201+
name: Notify Slack on failure
202+
when: on_fail
203+
- store_artifacts:
204+
path: /tmp/logs/test/integration/kind-olm-operator
205+
working_directory: ~/kubernetes-monitor
206+
177207
integration_tests_proxy:
178208
machine:
179209
enabled: true

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"test:integration:kind:yaml": "DEPLOYMENT_TYPE=YAML TEST_PLATFORM=kind CREATE_CLUSTER=true tap test/integration/kubernetes.test.ts --timeout=900",
1111
"test:integration:kind:helm": "DEPLOYMENT_TYPE=Helm TEST_PLATFORM=kind CREATE_CLUSTER=true tap test/integration/kubernetes.test.ts --timeout=900",
1212
"test:integration:kind:proxy": "DEPLOYMENT_TYPE=Proxy TEST_PLATFORM=kind CREATE_CLUSTER=true tap test/integration/kubernetes.test.ts --timeout=900",
13+
"test:integration:kindolm:operator": "DEPLOYMENT_TYPE=Operator TEST_PLATFORM=kindolm CREATE_CLUSTER=true tap test/integration/kubernetes.test.ts --timeout=900",
1314
"test:integration:eks:yaml": "DEPLOYMENT_TYPE=YAML TEST_PLATFORM=eks CREATE_CLUSTER=false tap test/integration/kubernetes.test.ts --timeout=900",
1415
"test:integration:openshift3:yaml": "DEPLOYMENT_TYPE=YAML TEST_PLATFORM=openshift3 CREATE_CLUSTER=true tap test/integration/kubernetes.test.ts --timeout=900",
1516
"test:integration:openshift4:operator": "DEPLOYMENT_TYPE=Operator TEST_PLATFORM=openshift4 CREATE_CLUSTER=false tap test/integration/kubernetes.test.ts --timeout=900",

test/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Please note that `docker` needs to be installed in order for this script to succ
4040
As part of these tests, we attempt pulling and scanning an image hosted on a private GCR registry. For this test case to work, one has to define the following environment variables: `GCR_IO_SERVICE_ACCOUNT`, `GCR_IO_DOCKERCFG`.
4141

4242
Our integration tests may use different Kubernetes platforms to host the Kubernetes-Monitor. These platforms may use an existing cluster, or create a new one. Both decisions are based on the environment variables:
43-
* `TEST_PLATFORM` (`kind`, `eks`)
43+
* `TEST_PLATFORM` (`kind`, `kindolm`, `eks`)
4444
* `CREATE_CLUSTER` (`true`, `false`).
4545

4646
Additionally, the deployment of the Kubernetes-Monitor can be configured through an environment variable:
@@ -83,6 +83,14 @@ This test runs whenever we commit to our `staging` branch, and at the moment may
8383

8484
Run with `npm run test:integration:openshift4`.
8585

86+
### KinD with OLM ###
87+
88+
This test helps us ensure our operator is installable and functioning on a plain k8s cluster.
89+
90+
This test runs whenever we commit to any branch.
91+
92+
Run with `npm run test:integration:kindolm:operator`.
93+
8694
### Package Managers ###
8795

8896
These tests attempt to provide some more thorough coverage for our scans of specific package manager: APK, APT and RPM.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: operators.coreos.com/v1
2+
kind: OperatorGroup
3+
metadata:
4+
name: snyk-operator
5+
namespace: snyk-monitor
6+
spec:
7+
targetNamespaces:
8+
- snyk-monitor
9+
---
10+
apiVersion: operators.coreos.com/v1alpha1
11+
kind: Subscription
12+
metadata:
13+
name: snyk-operator
14+
namespace: snyk-monitor
15+
spec:
16+
channel: stable
17+
name: snyk-operator
18+
installPlanApproval: Automatic
19+
source: snyk-operator
20+
sourceNamespace: marketplace
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: operators.coreos.com/v1
2+
kind: OperatorSource
3+
metadata:
4+
name: snyk-operator
5+
namespace: marketplace
6+
spec:
7+
type: appregistry
8+
endpoint: https://quay.io/cnr
9+
registryNamespace: snyk-runtime
10+
displayName: "Snyk Operator"
11+
publisher: "Snyk Ltd."

test/helpers/kubectl.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,14 @@ export async function createConfigMap(
7474
console.log(`Created config map ${configMapName}`);
7575
}
7676

77-
export async function applyK8sYaml(pathToYamlDeployment: string): Promise<void> {
77+
export async function applyK8sYaml(pathToYamlDeployment: string, namespace?: string): Promise<void> {
78+
if (namespace) {
79+
console.log(`Applying ${pathToYamlDeployment} to namespace ${namespace}...`);
80+
await exec(`./kubectl apply -f ${pathToYamlDeployment} -n ${namespace}`);
81+
console.log(`Applied ${pathToYamlDeployment} to namespace ${namespace}`);
82+
return;
83+
}
84+
7885
console.log(`Applying ${pathToYamlDeployment}...`);
7986
await exec(`./kubectl apply -f ${pathToYamlDeployment}`);
8087
console.log(`Applied ${pathToYamlDeployment}`);
@@ -122,6 +129,13 @@ export async function getPodNames(namespace: string): Promise<string[]> {
122129
return podsOutput.stdout.split('\n');
123130
}
124131

132+
export async function getNamespaces(): Promise<string[]> {
133+
const commandPrefix = `./kubectl get ns`;
134+
const onlyNames = ' --template \'{{range .items}}{{.metadata.name}}{{"\\n"}}{{end}}\'';
135+
const nsOutput = await exec(commandPrefix+onlyNames);
136+
return nsOutput.stdout.split('\n');
137+
}
138+
125139
export async function getPodLogs(podName: string, namespace: string): Promise<any> {
126140
const logsOutput = await exec(`./kubectl -n ${namespace} logs ${podName}`);
127141
return logsOutput.stdout;
@@ -155,6 +169,18 @@ export async function waitForServiceAccount(name: string, namespace: string): Pr
155169
}
156170
}
157171

172+
export async function waitForCRD(name: string): Promise<void> {
173+
// TODO: add some timeout
174+
while (true) {
175+
try {
176+
await exec(`./kubectl get crd ${name}`);
177+
break;
178+
} catch (err) {
179+
await sleep(500);
180+
}
181+
}
182+
}
183+
158184
export async function waitForJob(name: string, namespace: string): Promise<void> {
159185
console.log(`Trying to find job ${name} in namespace ${namespace}`);
160186
for (let attempt = 0; attempt < 60; attempt++) {

test/setup/deployers/operator.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,21 @@ export const operatorDeployer: IDeployer = {
88
async function deployKubernetesMonitor(
99
_imageOptions: IImageOptions,
1010
): Promise<void> {
11-
await kubectl.applyK8sYaml('./test/fixtures/operator/operator-source.yaml');
12-
await kubectl.applyK8sYaml('./test/fixtures/operator/installation.yaml');
13-
14-
// Await for the Operator to become available, only then
15-
// the Operator can start processing the custom resource.
16-
await kubectl.waitForDeployment('snyk-operator', 'snyk-monitor');
11+
const namespaces = await kubectl.getNamespaces();
12+
if (namespaces.includes('openshift-marketplace')) {
13+
await kubectl.applyK8sYaml('./test/fixtures/operator/operator-source.yaml');
14+
await kubectl.applyK8sYaml('./test/fixtures/operator/installation.yaml');
15+
// Await for the Operator to become available, only then
16+
// the Operator can start processing the custom resource.
17+
await kubectl.waitForDeployment('snyk-operator', 'openshift-marketplace');
18+
} else {
19+
await kubectl.applyK8sYaml('./test/fixtures/operator/operator-source-k8s.yaml');
20+
await kubectl.applyK8sYaml('./test/fixtures/operator/installation-k8s.yaml');
21+
// Await for the Operator to become available, only then
22+
// the Operator can start processing the custom resource.
23+
await kubectl.waitForDeployment('snyk-operator', 'marketplace');
24+
await kubectl.waitForCRD('snykmonitors.charts.helm.k8s.io');
25+
}
1726

1827
await kubectl.applyK8sYaml('./test/fixtures/operator/custom-resource.yaml');
1928
}

test/setup/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export async function deployMonitor(): Promise<string> {
133133
await predeploy(integrationId);
134134

135135
// TODO: hack, rewrite this
136-
const imagePullPolicy = testPlatform === 'kind' || testPlatform === 'openshift3' ? 'Never' : 'Always';
136+
const imagePullPolicy = testPlatform === 'kind' || testPlatform === 'kindolm' || testPlatform === 'openshift3' ? 'Never' : 'Always';
137137
const deploymentImageOptions: IImageOptions = {
138138
nameAndTag: remoteImageName,
139139
pullPolicy: imagePullPolicy,

test/setup/platforms/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as kind from './kind';
2+
import * as kindOlm from './kind-olm';
23
import * as eks from './eks';
34
import * as openshift3 from './openshift3';
45
import * as openshift4 from './openshift4';
@@ -27,6 +28,15 @@ const kindSetup: IPlatformSetup = {
2728
setupTester: kind.setupTester,
2829
};
2930

31+
const kindOlmSetup: IPlatformSetup = {
32+
create: kindOlm.createCluster,
33+
loadImage: kind.loadImageInCluster,
34+
delete: kind.deleteCluster,
35+
config: kind.exportKubeConfig,
36+
clean: kind.clean,
37+
setupTester: kind.setupTester,
38+
};
39+
3040
const eksSetup: IPlatformSetup = {
3141
create: eks.createCluster,
3242
loadImage: eks.loadImageInCluster,
@@ -66,6 +76,7 @@ export function getKubernetesVersionForPlatform(testPlatform: string): string {
6676

6777
export default {
6878
kind: kindSetup,
79+
kindolm: kindOlmSetup,
6980
eks: eksSetup,
7081
openshift3: openshift3Setup,
7182
openshift4: openshift4Setup,

0 commit comments

Comments
 (0)