11import * as tap from 'tap' ;
2- import { getImagesWithFileSystemPath , pullImages } from '../../../src/scanner/images' ;
2+
33import { IPullableImage } from '../../../src/scanner/images/types' ;
44import config = require( '../../../src/common/config' ) ;
5+ import * as scannerImages from '../../../src/scanner/images' ;
6+
57
68tap . test ( 'getImagesWithFileSystemPath()' , async ( t ) => {
79 const noImages : string [ ] = [ ] ;
8- const noImagesResult = getImagesWithFileSystemPath ( noImages ) ;
10+ const noImagesResult = scannerImages . getImagesWithFileSystemPath ( noImages ) ;
911 t . same ( noImagesResult , [ ] , 'correctly maps an empty array' ) ;
1012
1113 const image = [ 'nginx:latest' ] ;
12- const imageResult = getImagesWithFileSystemPath ( image ) ;
14+ const imageResult = scannerImages . getImagesWithFileSystemPath ( image ) ;
1315 t . same ( imageResult . length , 1 , 'expected 1 item' ) ;
1416
1517 const resultWithExpectedPath = imageResult [ 0 ] ;
@@ -27,8 +29,8 @@ tap.test('getImagesWithFileSystemPath()', async (t) => {
2729
2830 // Ensure that two consecutive calls do not return the same file system path
2931 const someImage = [ 'centos:latest' ] ;
30- const firstCallResult = getImagesWithFileSystemPath ( someImage ) [ 0 ] ;
31- const secondCallResult = getImagesWithFileSystemPath ( someImage ) [ 0 ] ;
32+ const firstCallResult = scannerImages . getImagesWithFileSystemPath ( someImage ) [ 0 ] ;
33+ const secondCallResult = scannerImages . getImagesWithFileSystemPath ( someImage ) [ 0 ] ;
3234 t . ok (
3335 firstCallResult . fileSystemPath !== secondCallResult . fileSystemPath ,
3436 'consecutive calls to the function with the same data return different file system paths' ,
@@ -37,6 +39,49 @@ tap.test('getImagesWithFileSystemPath()', async (t) => {
3739
3840tap . test ( 'pullImages() skips on missing file system path' , async ( t ) => {
3941 const badImage = [ { imageName : 'nginx:latest' } ] ;
40- const result = await pullImages ( badImage as IPullableImage [ ] ) ;
42+ const result = await scannerImages . pullImages ( badImage as IPullableImage [ ] ) ;
4143 t . same ( result , [ ] , 'expect to skip images missing file system path' ) ;
4244} ) ;
45+
46+ tap . test ( 'constructStaticAnalysisOptions() tests' , async ( t ) => {
47+ t . plan ( 1 ) ;
48+
49+ const somePath = '/var/tmp/file.tar' ;
50+ const options = scannerImages . constructStaticAnalysisOptions ( somePath ) ;
51+ const expectedResult = {
52+ staticAnalysisOptions : {
53+ imagePath : somePath ,
54+ imageType : 'docker-archive' ,
55+ tmpDirPath : '/var/tmp' ,
56+ } ,
57+ } ;
58+
59+ t . deepEqual ( options , expectedResult , 'returned options match expectations' ) ;
60+ } ) ;
61+
62+ tap . test ( 'getImageTag() tests' , async ( t ) => {
63+ t . plan ( 4 ) ;
64+
65+ const imageWithSha = 'nginx@sha256:1234567890abcdef' ;
66+ const imageWithShaResult = scannerImages . getImageTag ( imageWithSha ) ;
67+ t . same ( imageWithShaResult , '1234567890abcdef' , 'image sha is returned' ) ;
68+
69+ const imageWithTag = 'nginx:latest' ;
70+ const imageWithTagResult = scannerImages . getImageTag ( imageWithTag ) ;
71+ t . same ( imageWithTagResult , 'latest' , 'image tag is returned' ) ;
72+
73+ const imageWithoutTag = 'nginx' ;
74+ const imageWithoutTagResult = scannerImages . getImageTag ( imageWithoutTag ) ;
75+ t . same ( imageWithoutTagResult , '' , 'empty tag returned when no tag is specified' ) ;
76+
77+ const imageWithManySeparators = 'nginx@abc:tag@bad:reallybad' ;
78+ const imageWithManySeparatorsResult = scannerImages . getImageTag ( imageWithManySeparators ) ;
79+ t . same ( imageWithManySeparatorsResult , '' , 'empty tag is returned on malformed image name and tag' ) ;
80+ } ) ;
81+
82+ tap . test ( 'removeTagFromImage() tests' , async ( t ) => {
83+ t . plan ( 2 ) ;
84+
85+ t . same ( scannerImages . removeTagFromImage ( 'nginx:latest' ) , 'nginx' , 'removed image:tag' ) ;
86+ t . same ( scannerImages . removeTagFromImage ( 'nginx:@sha256:1234567890abcdef' ) , 'nginx' , 'removed image@sha:hex' ) ;
87+ } ) ;
0 commit comments