Skip to content

Commit b2da3a4

Browse files
committed
test: add image-scanner tests
1 parent 71d0c0a commit b2da3a4

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/kube-scanner/image-scanner.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ export interface IScanResult {
1010
pluginResult: any;
1111
}
1212

13-
function removeTagFromImage(imageWithTag: string): string {
13+
/**
14+
* Exported for testing
15+
*/
16+
export function removeTagFromImage(imageWithTag: string): string {
1417
return imageWithTag.split('@')[0].split(':')[0];
1518
}
1619

17-
function getImageTag(imageWithTag: string): string {
20+
/**
21+
* Exported for testing
22+
*/
23+
export function getImageTag(imageWithTag: string): string {
1824
const imageParts: string[] = imageWithTag.split(':');
1925
if (imageParts.length === 2) { // image@sha256:hash or image:tag
2026
return imageParts[1];
@@ -23,7 +29,10 @@ function getImageTag(imageWithTag: string): string {
2329
return '';
2430
}
2531

26-
function constructStaticAnalysisOptions(
32+
/**
33+
* Exported for testing
34+
*/
35+
export function constructStaticAnalysisOptions(
2736
fileSystemPath: string,
2837
): { staticAnalysisOptions: IStaticAnalysisOptions } {
2938
return {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { test } from 'tap';
2+
import {
3+
constructStaticAnalysisOptions,
4+
getImageTag,
5+
removeTagFromImage,
6+
} from '../../../src/kube-scanner/image-scanner';
7+
8+
test('constructStaticAnalysisOptions() tests', async (t) => {
9+
t.plan(1);
10+
11+
const somePath = '/var/tmp/file.tar';
12+
const options = constructStaticAnalysisOptions(somePath);
13+
const expectedResult = {
14+
staticAnalysisOptions: {
15+
imagePath: somePath,
16+
imageType: 'docker-archive',
17+
tmpDirPath: '/var/tmp',
18+
},
19+
};
20+
21+
t.deepEqual(options, expectedResult, 'returned options match expectations');
22+
});
23+
24+
test('getImageTag() tests', async (t) => {
25+
t.plan(4);
26+
27+
const imageWithSha = 'nginx@sha256:1234567890abcdef';
28+
const imageWithShaResult = getImageTag(imageWithSha);
29+
t.same(imageWithShaResult, '1234567890abcdef', 'image sha is returned');
30+
31+
const imageWithTag = 'nginx:latest';
32+
const imageWithTagResult = getImageTag(imageWithTag);
33+
t.same(imageWithTagResult, 'latest', 'image tag is returned');
34+
35+
const imageWithoutTag = 'nginx';
36+
const imageWithoutTagResult = getImageTag(imageWithoutTag);
37+
t.same(imageWithoutTagResult, '', 'empty tag returned when no tag is specified');
38+
39+
const imageWithManySeparators = 'nginx@abc:tag@bad:reallybad';
40+
const imageWithManySeparatorsResult = getImageTag(imageWithManySeparators);
41+
t.same(imageWithManySeparatorsResult, '', 'empty tag is returned on malformed image name and tag');
42+
});
43+
44+
test('removeTagFromImage() tests', async (t) => {
45+
t.plan(2);
46+
47+
t.same(removeTagFromImage('nginx:latest'), 'nginx', 'removed image:tag');
48+
t.same(removeTagFromImage('nginx:@sha256:1234567890abcdef'), 'nginx', 'removed image@sha:hex');
49+
});

0 commit comments

Comments
 (0)