|
| 1 | +import * as tap from 'tap'; |
| 2 | +import { getImagesWithFileSystemPath, pullImages } from '../../src/images'; |
| 3 | +const config = require('../../src/common/config'); |
| 4 | + |
| 5 | +tap.test('getImagesWithFileSystemPath()', async (t) => { |
| 6 | + const noImages: string[] = []; |
| 7 | + const noImagesResult = getImagesWithFileSystemPath(noImages); |
| 8 | + t.same(noImagesResult, [], 'correctly maps an empty array'); |
| 9 | + |
| 10 | + // Cache the last value of STATIC_ANALYSIS, we return it back to normal once the test completes |
| 11 | + const lastStaticAnalysisValue = config.STATIC_ANALYSIS; |
| 12 | + |
| 13 | + // First try without static analysis set |
| 14 | + config.STATIC_ANALYSIS = false; |
| 15 | + const imageWithoutStaticAnalysis = ['redis:latest']; |
| 16 | + const imageWithoutStaticAnalysisResult = getImagesWithFileSystemPath(imageWithoutStaticAnalysis); |
| 17 | + t.same( |
| 18 | + imageWithoutStaticAnalysisResult, |
| 19 | + [{ imageName: 'redis:latest' }], |
| 20 | + 'correctly returns an image without a file system path', |
| 21 | + ); |
| 22 | + |
| 23 | + // Next try with static analysis set |
| 24 | + config.STATIC_ANALYSIS = true; |
| 25 | + const imageWithStaticAnalysis = ['nginx:latest']; |
| 26 | + const imageWithStaticAnalysisResult = getImagesWithFileSystemPath(imageWithStaticAnalysis); |
| 27 | + t.same(imageWithStaticAnalysisResult.length, 1, 'expected 1 item'); |
| 28 | + |
| 29 | + const resultWithExpectedPath = imageWithStaticAnalysisResult[0]; |
| 30 | + t.same( |
| 31 | + resultWithExpectedPath.imageName, |
| 32 | + 'nginx:latest', |
| 33 | + 'correctly returns an image without a file system path', |
| 34 | + ); |
| 35 | + const fileSystemPath = resultWithExpectedPath.fileSystemPath!; |
| 36 | + t.ok(fileSystemPath, 'file system path exists on the result'); |
| 37 | + t.ok(fileSystemPath.endsWith('.tar'), 'file system path ends in .tar'); |
| 38 | + |
| 39 | + const expectedPattern = fileSystemPath.indexOf(`${config.IMAGE_STORAGE_ROOT}/nginx_latest_`) !== -1; |
| 40 | + t.ok(expectedPattern, 'the file system path starts with an expected pattern'); |
| 41 | + |
| 42 | + // Finally ensure that two consecutive calls do not return the same file system path |
| 43 | + config.STATIC_ANALYSIS = true; |
| 44 | + const someImage = ['centos:latest']; |
| 45 | + const firstCallResult = getImagesWithFileSystemPath(someImage)[0]; |
| 46 | + const secondCallResult = getImagesWithFileSystemPath(someImage)[0]; |
| 47 | + t.ok( |
| 48 | + firstCallResult.fileSystemPath !== secondCallResult.fileSystemPath, |
| 49 | + 'consecutive calls to the function with the same data return different file system paths', |
| 50 | + ); |
| 51 | + |
| 52 | + // Restore the old value |
| 53 | + config.STATIC_ANALYSIS = lastStaticAnalysisValue; |
| 54 | +}); |
| 55 | + |
| 56 | +tap.test('pullImages() skips on missing file system path in static analysis', async (t) => { |
| 57 | + // Cache the last value of STATIC_ANALYSIS, we return it back to normal once the test completes |
| 58 | + const lastStaticAnalysisValue = config.STATIC_ANALYSIS; |
| 59 | + |
| 60 | + config.STATIC_ANALYSIS = true; |
| 61 | + const badStaticAnalysisImage = [ |
| 62 | + { |
| 63 | + imageName: 'nginx:latest', |
| 64 | + }, |
| 65 | + ]; |
| 66 | + const result = await pullImages(badStaticAnalysisImage); |
| 67 | + t.same(result, [], 'expect to skip images on static analysis set but missing file system path'); |
| 68 | + |
| 69 | + // Restore the old value |
| 70 | + config.STATIC_ANALYSIS = lastStaticAnalysisValue; |
| 71 | +}); |
0 commit comments