|
| 1 | +import * as tap from 'tap'; |
| 2 | + |
| 3 | +import imageScanner = require('../../src/kube-scanner/image-scanner'); |
| 4 | +import payload = require('../../src/transmitter/payload'); |
| 5 | +import transmitterTypes = require('../../src/transmitter/types'); |
| 6 | + |
| 7 | +tap.test('constructHomebaseWorkloadPayloads breaks when workloadMetadata is missing items', async (t) => { |
| 8 | + const scannedImages: imageScanner.IScanResult[] = [ |
| 9 | + { |
| 10 | + image: 'myImage', |
| 11 | + imageWithTag: 'myImage:tag', |
| 12 | + pluginResult: 'whatever1', |
| 13 | + }, |
| 14 | + { |
| 15 | + image: 'anotherImage', |
| 16 | + imageWithTag: 'anotherImage:1.2.3-alpha', |
| 17 | + pluginResult: 'whatever3', |
| 18 | + }, |
| 19 | + ]; |
| 20 | + |
| 21 | + const workloadMetadata: transmitterTypes.IKubeImage[] = [ |
| 22 | + { |
| 23 | + type: 'type', |
| 24 | + name: 'workloadName', |
| 25 | + namespace: 'spacename', |
| 26 | + labels: undefined, |
| 27 | + annotations: undefined, |
| 28 | + uid: 'udi', |
| 29 | + specLabels: undefined, |
| 30 | + specAnnotations: undefined, |
| 31 | + containerName: 'contener', |
| 32 | + imageName: 'myImage', |
| 33 | + imageId: 'does this matter?', |
| 34 | + cluster: 'grapefruit', |
| 35 | + }, |
| 36 | + ]; |
| 37 | + |
| 38 | + t.throws(() => payload.constructHomebaseWorkloadPayloads(scannedImages, workloadMetadata), |
| 39 | + 'extractNamespaceName throws on undefined name'); |
| 40 | +}); |
| 41 | + |
| 42 | +tap.test('constructHomebaseWorkloadPayloads happy flow', async (t) => { |
| 43 | + const scannedImages: imageScanner.IScanResult[] = [ |
| 44 | + { |
| 45 | + image: 'myImage', |
| 46 | + imageWithTag: 'myImage:tag', |
| 47 | + pluginResult: 'whatever1', |
| 48 | + }, |
| 49 | + ]; |
| 50 | + |
| 51 | + const workloadMetadata: transmitterTypes.IKubeImage[] = [ |
| 52 | + { |
| 53 | + type: 'type', |
| 54 | + name: 'workloadName', |
| 55 | + namespace: 'spacename', |
| 56 | + labels: undefined, |
| 57 | + annotations: undefined, |
| 58 | + uid: 'udi', |
| 59 | + specLabels: undefined, |
| 60 | + specAnnotations: undefined, |
| 61 | + containerName: 'contener', |
| 62 | + imageName: 'myImage:tag', |
| 63 | + imageId: 'does this matter?', |
| 64 | + cluster: 'grapefruit', |
| 65 | + }, |
| 66 | + ]; |
| 67 | + |
| 68 | + const payloads = payload.constructHomebaseWorkloadPayloads(scannedImages, workloadMetadata); |
| 69 | + |
| 70 | + t.equals(payloads.length, 1, 'one payload to send to Homebase'); |
| 71 | + t.equals(payloads[0].dependencyGraph, JSON.stringify('whatever1'), 'dependency graph present in payload'); |
| 72 | + t.equals(payloads[0].imageLocator.cluster, 'grapefruit', 'cluster present in payload'); |
| 73 | + t.equals(payloads[0].imageLocator.imageId, 'myImage', 'image ID present in payload'); |
| 74 | + t.equals(payloads[0].imageLocator.name, 'workloadName', 'workload name present in payload'); |
| 75 | + t.equals(payloads[0].imageLocator.type, 'type', 'workload type present in payload'); |
| 76 | +}); |
0 commit comments