Skip to content

Commit 9b86790

Browse files
committed
fix: container test should detect python package files on windows
1 parent 951bab0 commit 9b86790

4 files changed

Lines changed: 101 additions & 82 deletions

File tree

package-lock.json

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
"semver": "^6.0.0",
117117
"snyk-config": "^5.0.0",
118118
"snyk-cpp-plugin": "^2.24.3",
119-
"snyk-docker-plugin": "9.3.0",
119+
"snyk-docker-plugin": "9.5.2",
120120
"snyk-go-plugin": "2.0.3",
121121
"snyk-gradle-plugin": "5.1.1",
122122
"snyk-module": "3.1.0",
Binary file not shown.

test/jest/acceptance/snyk-container/container.spec.ts

Lines changed: 96 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
import * as os from 'os';
22
import { startSnykCLI, TestCLI } from '../../util/startSnykCLI';
33
import { runSnykCLI } from '../../util/runSnykCLI';
4-
import { FakeServer, fakeServer } from '../../../acceptance/fake-server';
4+
import {
5+
FakeServer,
6+
fakeServer,
7+
getFirstIPv4Address,
8+
} from '../../../acceptance/fake-server';
59
import { RunCommandOptions, RunCommandResult } from '../../util/runCommand';
6-
import { getServerPort } from '../../util/getServerPort';
7-
import { isWindowsOperatingSystem } from '../../../utils';
10+
import { getAvailableServerPort } from '../../util/getServerPort';
11+
import { describeIf, isWindowsOperatingSystem } from '../../../utils';
812

913
jest.setTimeout(1000 * 300);
1014

1115
describe('snyk container', () => {
12-
if (isWindowsOperatingSystem()) {
13-
// eslint-disable-next-line jest/no-focused-tests
14-
it.only('Windows not yet supported', () => {
15-
console.warn(
16-
"Skipping as we don't have a Windows-compatible image to test against.",
17-
);
18-
});
19-
}
16+
const isWindows = isWindowsOperatingSystem();
2017

2118
const TEST_DISTROLESS_STATIC_IMAGE_NAME = 'gcr.io/distroless/static';
2219
const TEST_DISTROLESS_STATIC_IMAGE = `${TEST_DISTROLESS_STATIC_IMAGE_NAME}@sha256:7198a357ff3a8ef750b041324873960cf2153c11cc50abb9d8d5f8bb089f6b4e`;
2320
const TEST_OS_PACKAGES_AND_APP_VULNS_TAR =
2421
'test/fixtures/container-projects/os-packages-and-app-vulns.tar';
2522
const TEST_MULTI_PROJECT_IMAGE_TAR =
2623
'test/fixtures/container-projects/multi-project-image.tar';
24+
const TEST_PYTHON_WITH_PIP_DEPENDENCIES_TAR =
25+
'test/fixtures/container-projects/python-with-pip-dependencies.tar';
2726
const TEST_DISTROLESS_STATIC_IMAGE_DEPGRAPH = {
2827
schemaVersion: '1.3.0',
2928
pkgManager: {
@@ -113,7 +112,24 @@ describe('snyk container', () => {
113112
jest.resetAllMocks();
114113
});
115114

116-
describe('test', () => {
115+
describe('cross-platform tests', () => {
116+
it('container test finds pip-installed python packages in an archived image', async () => {
117+
const { code, stdout } = await runSnykCLI(
118+
`container test ${TEST_PYTHON_WITH_PIP_DEPENDENCIES_TAR} --json --app-vulns`,
119+
);
120+
121+
expect(code).toEqual(1);
122+
const jsonOutput = JSON.parse(stdout);
123+
expect(jsonOutput.applications).toBeDefined();
124+
const pipApp = jsonOutput.applications.find(
125+
(app) => app.packageManager === 'pip',
126+
);
127+
expect(pipApp).toBeDefined();
128+
expect(jsonOutput.dependencyCount).toBeGreaterThan(0);
129+
});
130+
});
131+
132+
describeIf(!isWindows)('test', () => {
117133
it('finds dependencies in rpm sqlite databases', async () => {
118134
cli = await startSnykCLI(
119135
'container test amazonlinux:2022.0.20220504.1 --print-deps',
@@ -522,7 +538,7 @@ DepGraph end`,
522538
}, 180000);
523539
});
524540

525-
describe('depgraph', () => {
541+
describeIf(!isWindows)('depgraph', () => {
526542
it('should print depgraph for image as JSON', async () => {
527543
const { code, stdout, stderr } = await runSnykCLIWithDebug(
528544
`container depgraph ${TEST_DISTROLESS_STATIC_IMAGE}`,
@@ -538,33 +554,32 @@ DepGraph end`,
538554
});
539555
});
540556

541-
describe('sbom (mock export-sbom service)', () => {
557+
describeIf(!isWindows)('sbom (mock export-sbom service)', () => {
542558
let server: FakeServer;
543559
let env: Record<string, string>;
544560

545-
beforeAll((done) => {
546-
const port = process.env.PORT || process.env.SNYK_PORT || '58584';
561+
beforeAll(async () => {
562+
const port = await getAvailableServerPort(process);
563+
const ipAddress = getFirstIPv4Address();
547564
const baseApi = '/api/v1';
548565
env = {
549566
...process.env,
550-
SNYK_API: 'http://localhost:' + port + baseApi,
567+
SNYK_API: `http://${ipAddress}:${port}${baseApi}`,
568+
SNYK_HOST: `http://${ipAddress}:${port}`,
551569
SNYK_TOKEN: '123456789',
570+
SNYK_HTTP_PROTOCOL_UPGRADE: '0',
552571
SNYK_DISABLE_ANALYTICS: '1',
553572
};
554573
server = fakeServer(baseApi, env.SNYK_TOKEN);
555-
server.listen(port, () => {
556-
done();
557-
});
574+
await server.listenPromise(port);
558575
});
559576

560577
afterEach(() => {
561578
server.restore();
562579
});
563580

564-
afterAll((done) => {
565-
server.close(() => {
566-
done();
567-
});
581+
afterAll(async () => {
582+
await server.closePromise();
568583
});
569584

570585
it('should print sbom for image - spdx', async () => {
@@ -888,7 +903,7 @@ DepGraph end`,
888903
});
889904
});
890905

891-
describe('snyk container monitor --json output', () => {
906+
describeIf(!isWindows)('snyk container monitor --json output', () => {
892907
it('snyk container monitor json produces expected output for a single depgraph', async () => {
893908
const { code, stdout } = await runSnykCLI(
894909
`container monitor --platform=linux/amd64 --json ${TEST_DISTROLESS_STATIC_IMAGE}`,
@@ -989,81 +1004,86 @@ DepGraph end`,
9891004
});
9901005
});
9911006

992-
describe('snyk container monitor supports --target-reference', () => {
993-
let server: ReturnType<typeof fakeServer>;
994-
let env: Record<string, string>;
995-
996-
beforeAll((done) => {
997-
const port = getServerPort(process);
998-
const baseApi = '/api/v1';
999-
env = {
1000-
...process.env,
1001-
SNYK_API: 'http://localhost:' + port + baseApi,
1002-
SNYK_HOST: 'http://localhost:' + port,
1003-
SNYK_TOKEN: '123456789',
1004-
SNYK_DISABLE_ANALYTICS: '1',
1005-
DEBUG: 'snyk*',
1006-
};
1007-
server = fakeServer(baseApi, env.SNYK_TOKEN);
1008-
server.listen(port, () => {
1009-
done();
1007+
describeIf(!isWindows)(
1008+
'snyk container monitor supports --target-reference',
1009+
() => {
1010+
let server: ReturnType<typeof fakeServer>;
1011+
let env: Record<string, string>;
1012+
1013+
beforeAll(async () => {
1014+
const port = await getAvailableServerPort(process);
1015+
const ipAddress = getFirstIPv4Address();
1016+
const baseApi = '/api/v1';
1017+
env = {
1018+
...process.env,
1019+
SNYK_API: `http://${ipAddress}:${port}${baseApi}`,
1020+
SNYK_HOST: `http://${ipAddress}:${port}`,
1021+
SNYK_TOKEN: '123456789',
1022+
SNYK_HTTP_PROTOCOL_UPGRADE: '0',
1023+
SNYK_DISABLE_ANALYTICS: '1',
1024+
DEBUG: 'snyk*',
1025+
};
1026+
server = fakeServer(baseApi, env.SNYK_TOKEN);
1027+
await server.listenPromise(port);
10101028
});
1011-
});
10121029

1013-
afterEach(() => {
1014-
server.restore();
1015-
});
1030+
afterEach(() => {
1031+
server.restore();
1032+
});
10161033

1017-
afterAll((done) => {
1018-
server.close(() => done());
1019-
});
1034+
afterAll(async () => {
1035+
await server.closePromise();
1036+
});
10201037

1021-
it('forwards value of target-reference to monitor-dependencies endpoint', async () => {
1022-
const { code } = await runSnykCLI(
1023-
`container monitor ${TEST_DISTROLESS_STATIC_IMAGE} --target-reference=test-target-ref`,
1024-
{
1025-
env,
1026-
},
1027-
);
1028-
expect(code).toEqual(0);
1038+
it('forwards value of target-reference to monitor-dependencies endpoint', async () => {
1039+
const { code } = await runSnykCLI(
1040+
`container monitor ${TEST_DISTROLESS_STATIC_IMAGE} --target-reference=test-target-ref`,
1041+
{
1042+
env,
1043+
},
1044+
);
1045+
expect(code).toEqual(0);
10291046

1030-
const monitorRequests = server
1031-
.getRequests()
1032-
.filter((request) => request.url?.includes('/monitor-dependencies'));
1047+
const monitorRequests = server
1048+
.getRequests()
1049+
.filter((request) => request.url?.includes('/monitor-dependencies'));
10331050

1034-
expect(monitorRequests.length).toBeGreaterThanOrEqual(1);
1035-
monitorRequests.forEach((request) => {
1036-
expect(request.body.scanResult.targetReference).toBe('test-target-ref');
1051+
expect(monitorRequests.length).toBeGreaterThanOrEqual(1);
1052+
monitorRequests.forEach((request) => {
1053+
expect(request.body.scanResult.targetReference).toBe(
1054+
'test-target-ref',
1055+
);
1056+
});
10371057
});
1038-
});
1039-
});
1058+
},
1059+
);
10401060

1041-
describe('container test deprecation warnings', () => {
1061+
describeIf(!isWindows)('container test deprecation warnings', () => {
10421062
let server: ReturnType<typeof fakeServer>;
10431063
let env: Record<string, string>;
10441064

1045-
beforeAll((done) => {
1046-
const port = getServerPort(process);
1065+
beforeAll(async () => {
1066+
const port = await getAvailableServerPort(process);
1067+
const ipAddress = getFirstIPv4Address();
10471068
const baseApi = '/api/v1';
10481069
env = {
10491070
...process.env,
1050-
SNYK_API: 'http://localhost:' + port + baseApi,
1051-
SNYK_HOST: 'http://localhost:' + port,
1071+
SNYK_API: `http://${ipAddress}:${port}${baseApi}`,
1072+
SNYK_HOST: `http://${ipAddress}:${port}`,
10521073
SNYK_TOKEN: '123456789',
1074+
SNYK_HTTP_PROTOCOL_UPGRADE: '0',
10531075
SNYK_DISABLE_ANALYTICS: '1',
10541076
};
10551077
server = fakeServer(baseApi, env.SNYK_TOKEN);
1056-
server.listen(port, () => {
1057-
done();
1058-
});
1078+
await server.listenPromise(port);
10591079
});
10601080

10611081
afterEach(() => {
10621082
server.restore();
10631083
});
10641084

1065-
afterAll((done) => {
1066-
server.close(() => done());
1085+
afterAll(async () => {
1086+
await server.closePromise();
10671087
});
10681088

10691089
it('should show deprecation warning for --shaded-jars-depth', async () => {

0 commit comments

Comments
 (0)