Skip to content

Commit 1d2b585

Browse files
committed
chore: add function to test for required env vars in tests
Allows to bail early if we forgot to set up a required environment variables for certain test environments.
1 parent 4df71c1 commit 1d2b585

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

test/setup/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ export async function deployMonitor(): Promise<string> {
106106
console.log('Begin deploying the snyk-monitor...');
107107

108108
try {
109+
await platforms[testPlatform].validateRequiredEnvironment();
110+
109111
const imageNameAndTag = getEnvVariableOrDefault(
110112
'KUBERNETES_MONITOR_IMAGE_NAME_AND_TAG',
111113
// the default, determined by ./script/build-image.sh

test/setup/platforms/eks.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import { exec } from 'child-process-promise';
2+
3+
import { throwIfEnvironmentVariableUnset } from './helpers';
24
import * as kubectl from '../../helpers/kubectl';
35

6+
export async function validateRequiredEnvironment(): Promise<void> {
7+
console.log(
8+
'Checking for the required environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION',
9+
);
10+
throwIfEnvironmentVariableUnset('AWS_ACCESS_KEY_ID');
11+
throwIfEnvironmentVariableUnset('AWS_SECRET_ACCESS_KEY');
12+
throwIfEnvironmentVariableUnset('AWS_REGION');
13+
}
14+
415
export async function setupTester(): Promise<void> {
516
// update the `aws` CLI, the one in CircleCI's default image is outdated and doens't support eks
617
await exec('pip install awscli --ignore-installed six');

test/setup/platforms/helpers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function throwIfEnvironmentVariableUnset(variableName: string) {
2+
if (!process.env[variableName]) {
3+
throw new Error(`Missing required environment variable ${variableName}`);
4+
}
5+
}

test/setup/platforms/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ interface IPlatformSetup {
1414
config: () => Promise<void>;
1515
// clean up whatever we littered an existing cluster with
1616
clean: () => Promise<void>;
17+
// ensure the environment is configured properly for the platform setup; can throw
18+
validateRequiredEnvironment: () => Promise<void>;
1719
// set up host requirements specific to this platform
1820
setupTester: () => Promise<void>;
1921
}
@@ -25,6 +27,7 @@ const kindSetup: IPlatformSetup = {
2527
config: kind.exportKubeConfig,
2628
clean: kind.clean,
2729
setupTester: kind.setupTester,
30+
validateRequiredEnvironment: () => Promise.resolve(),
2831
};
2932

3033
const eksSetup: IPlatformSetup = {
@@ -34,6 +37,7 @@ const eksSetup: IPlatformSetup = {
3437
config: eks.exportKubeConfig,
3538
clean: eks.clean,
3639
setupTester: eks.setupTester,
40+
validateRequiredEnvironment: eks.validateRequiredEnvironment,
3741
};
3842

3943
// Use a kind cluster pinned to a specific Kubernetes version to mimic OS3.
@@ -44,6 +48,7 @@ const openshift3Setup: IPlatformSetup = {
4448
config: kind.exportKubeConfig,
4549
clean: kind.clean,
4650
setupTester: openshift3.setupTester,
51+
validateRequiredEnvironment: () => Promise.resolve(),
4752
};
4853

4954
const openshift4Setup: IPlatformSetup = {
@@ -53,6 +58,7 @@ const openshift4Setup: IPlatformSetup = {
5358
config: openshift4.exportKubeConfig,
5459
clean: openshift4.clean,
5560
setupTester: openshift4.setupTester,
61+
validateRequiredEnvironment: openshift4.validateRequiredEnvironment,
5662
};
5763

5864
export function getKubernetesVersionForPlatform(testPlatform: string): string {

test/setup/platforms/openshift4.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@ import { platform, tmpdir } from 'os';
55
import { resolve } from 'path';
66
import * as needle from 'needle';
77

8+
import { throwIfEnvironmentVariableUnset } from './helpers';
89
import * as kubectl from '../../helpers/kubectl';
910

1011
const OPENSHIFT_CLI_VERSION = '4.3.0';
1112

13+
export async function validateRequiredEnvironment(): Promise<void> {
14+
console.log(
15+
'Checking for the required environment variables: OPENSHIFT4_USER, OPENSHIFT4_PASSWORD, OPENSHIFT4_CLUSTER_URL',
16+
);
17+
throwIfEnvironmentVariableUnset('OPENSHIFT4_USER');
18+
throwIfEnvironmentVariableUnset('OPENSHIFT4_PASSWORD');
19+
throwIfEnvironmentVariableUnset('OPENSHIFT4_CLUSTER_URL');
20+
}
21+
1222
export async function setupTester(): Promise<void> {
1323
if (existsSync(resolve(process.cwd(), 'oc'))) {
1424
console.log('OpenShift CLI exists locally, skipping download');

0 commit comments

Comments
 (0)