|
| 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