Skip to content

Commit 7816d2b

Browse files
authored
Merge pull request #656 from snyk/chore/pass-process-env-in-exec
chore: pass process.env when calling child_process.exec()
2 parents 099331a + fe5b3cc commit 7816d2b

File tree

10 files changed

+24
-11
lines changed

10 files changed

+24
-11
lines changed

test/helpers/exec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { exec, PromiseResult } from 'child-process-promise';
2+
3+
/**
4+
* It seems like there is an issue with Jest where changes to process.env are not persisted through
5+
* calls to child_process.exec(). This causes env vars like KUBECONFIG to be lost and to fail our tests.
6+
* For now use this wrapper to explicitly pass process.env as a workaround.
7+
* https://github.com/facebook/jest/issues/9341
8+
* https://github.com/facebook/jest/issues/9264
9+
* https://snyk.slack.com/archives/CLW30N31V/p1612860708027800?thread_ts=1612856703.026500&cid=CLW30N31V
10+
*/
11+
export async function execWrapper(command: string): Promise<PromiseResult<string>> {
12+
return await exec(command, { env: process.env });
13+
}

test/helpers/kubectl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { exec } from 'child-process-promise';
1+
import { execWrapper as exec } from './exec';
22
import { chmodSync, writeFileSync, existsSync, unlinkSync } from 'fs';
33
import { platform } from 'os';
44
import { resolve } from 'path';

test/integration/kubernetes.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { CoreV1Api, KubeConfig, AppsV1Api } from '@kubernetes/client-node';
2-
import { exec } from 'child-process-promise';
32
import { Fact, ScanResult } from 'snyk-docker-plugin';
43
import * as setup from '../setup';
54
import { WorkloadKind } from '../../src/supervisor/types';
@@ -11,6 +10,7 @@ import {
1110
validateUpstreamStoredScanResults,
1211
} from '../helpers/kubernetes-upstream';
1312
import * as kubectl from '../helpers/kubectl';
13+
import { execWrapper as exec } from '../helpers/exec';
1414

1515
let integrationId: string;
1616
let namespace: string;

test/setup/deployers/helm-with-proxy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { exec } from 'child-process-promise';
21
import { platform } from 'os';
32
import { existsSync, chmodSync } from 'fs';
43

54
import { IDeployer, IImageOptions } from './types';
65
import * as kubectl from '../../helpers/kubectl';
6+
import { execWrapper as exec } from '../../helpers/exec';
77

88
const helmVersion = '3.0.0';
99
const helmPath = './helm';

test/setup/deployers/helm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { exec } from 'child-process-promise';
21
import { platform } from 'os';
2+
import { existsSync, chmodSync } from 'fs';
33

44
import { IDeployer, IImageOptions } from './types';
5-
import { existsSync, chmodSync } from 'fs';
5+
import { execWrapper as exec } from '../../helpers/exec';
66

77
const helmVersion = '3.0.0';
88
const helmPath = './helm';

test/setup/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as fs from 'fs';
22
import * as sleep from 'sleep-promise';
33
import * as uuidv4 from 'uuid/v4';
4-
import { exec } from 'child-process-promise';
54

65
import platforms, { getKubernetesVersionForPlatform } from './platforms';
76
import deployers from './deployers';
87
import { IImageOptions } from './deployers/types';
98
import * as kubectl from '../helpers/kubectl';
9+
import { execWrapper as exec } from '../helpers/exec';
1010

1111
const testPlatform = process.env['TEST_PLATFORM'] || 'kind';
1212
const createCluster = process.env['CREATE_CLUSTER'] === 'true';

test/setup/platforms/eks.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { exec } from 'child-process-promise';
2-
31
import { throwIfEnvironmentVariableUnset } from './helpers';
42
import * as kubectl from '../../helpers/kubectl';
3+
import { execWrapper as exec } from '../../helpers/exec';
54

65
export async function validateRequiredEnvironment(): Promise<void> {
76
console.log(

test/setup/platforms/kind.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { exec } from 'child-process-promise';
21
import { accessSync, chmodSync, constants, writeFileSync } from 'fs';
32
import { platform } from 'os';
43
import { resolve } from 'path';
4+
import { execWrapper as exec } from '../../helpers/exec';
55

66
const clusterName = 'kind';
77

test/setup/platforms/openshift4.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Writable } from 'stream';
2-
import { exec } from 'child-process-promise';
32
import { chmodSync, writeFileSync, existsSync } from 'fs';
43
import { platform, tmpdir } from 'os';
54
import { resolve } from 'path';
65
import * as needle from 'needle';
76

87
import { throwIfEnvironmentVariableUnset } from './helpers';
98
import * as kubectl from '../../helpers/kubectl';
9+
import { execWrapper as exec } from '../../helpers/exec';
1010

1111
const OPENSHIFT_CLI_VERSION = '4.3.0';
1212

test/system/kind.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as fsExtra from 'fs-extra';
22
import * as nock from 'nock';
3-
import { exec } from 'child-process-promise';
43

54
import * as kubectl from '../helpers/kubectl';
65
import * as kind from '../setup/platforms/kind';
76
import * as transmitterTypes from '../../src/transmitter/types';
7+
import { execWrapper as exec } from '../helpers/exec';
8+
89
/**
910
* TODO graceful shutdown
1011
* We abruptly close the connection to the K8s API server during shutdown, which can result in exceptions.

0 commit comments

Comments
 (0)