11import { CoreV1Api , KubeConfig , AppsV1Api } from '@kubernetes/client-node' ;
2+ import { exec } from 'child-process-promise' ;
23import setup = require( '../setup' ) ;
34import * as tap from 'tap' ;
45import { WorkloadKind } from '../../src/supervisor/types' ;
@@ -21,6 +22,12 @@ tap.tearDown(async() => {
2122 console . log ( 'Begin removing the snyk-monitor...' ) ;
2223 await setup . removeMonitor ( ) ;
2324 console . log ( 'Removed the snyk-monitor!' ) ;
25+ console . log ( 'Begin removing local container registry...' ) ;
26+ await setup . removeLocalContainerRegistry ( ) ;
27+ console . log ( 'Removed local container registry' ) ;
28+ console . log ( 'Begin removing "kind" network...' ) ;
29+ await setup . removeUnusedKindNetwork ( ) ;
30+ console . log ( 'Removed "kind" network' ) ;
2431} ) ;
2532
2633// Make sure this runs first -- deploying the monitor for the next tests
@@ -65,6 +72,19 @@ tap.test('snyk-monitor container started', async (t) => {
6572 console . log ( 'Done -- snyk-monitor exists!' ) ;
6673} ) ;
6774
75+ tap . test ( 'create local container registry and push an image' , async ( t ) => {
76+ console . log ( 'Creating local container registry...' ) ;
77+ await exec ( 'docker run -d --restart=always -p "5000:5000" --name "kind-registry" registry:2' ) ;
78+ await exec ( 'docker network connect "kind" "kind-registry"' ) ;
79+
80+ console . log ( 'Pushing python:rc-buster image to the local registry' ) ;
81+ //Note: this job takes a while and waitForJob() should be called before trying to access local registry image,
82+ //to make sure it completed
83+ await kubectl . applyK8sYaml ( './test/fixtures/insecure-registries/push-dockerhub-image-to-local-registry.yaml' ) ;
84+
85+ t . pass ( 'successfully started a job to push image to a local registry' ) ;
86+ } ) ;
87+
6888tap . test ( 'snyk-monitor sends data to kubernetes-upstream' , async ( t ) => {
6989 t . plan ( 7 ) ;
7090
@@ -226,6 +246,46 @@ tap.test('snyk-monitor pulls images from a private ECR and sends data to kuberne
226246 'snyk-monitor sent expected data to upstream in the expected timeframe' ) ;
227247} ) ;
228248
249+ tap . test ( 'snyk-monitor pulls images from a local registry and sends data to kubernetes-upstream' , async ( t ) => {
250+ t . plan ( 4 ) ;
251+
252+ const deploymentName = 'python-local' ;
253+ const namespace = 'services' ;
254+ const clusterName = 'Default cluster' ;
255+ const deploymentType = WorkloadKind . Deployment ;
256+ const imageName = 'kind-registry:5000/python:rc-buster' ;
257+
258+ await kubectl . waitForJob ( 'push-to-local-registry' , 'default' ) ;
259+
260+ console . log ( 'Applying local registry workload...' ) ;
261+ await kubectl . applyK8sYaml ( './test/fixtures/insecure-registries/python-local-deployment.yaml' ) ;
262+
263+ console . log ( `Begin polling upstream for the expected kind-registry:5000 image with integration ID ${ integrationId } ...` ) ;
264+
265+ const validatorFn : WorkloadLocatorValidator = ( workloads ) => {
266+ return workloads !== undefined &&
267+ workloads . find ( ( workload ) => workload . name === deploymentName &&
268+ workload . type === WorkloadKind . Deployment ) !== undefined ;
269+ } ;
270+
271+ const testResult = await validateUpstreamStoredData (
272+ validatorFn , `api/v2/workloads/${ integrationId } /${ clusterName } /${ namespace } ` ) ;
273+ t . ok ( testResult , 'snyk-monitor sent expected data to upstream in the expected timeframe' ) ;
274+
275+ const depGraphResult = await getUpstreamResponseBody (
276+ `api/v1/dependency-graphs/${ integrationId } /${ clusterName } /${ namespace } /${ deploymentType } /${ deploymentName } ` ) ;
277+
278+ t . ok ( 'dependencyGraphResults' in depGraphResult ,
279+ 'expected dependencyGraphResults field to exist in /dependency-graphs response' ) ;
280+
281+ /* Because of a bug in removeTagFromImage() func in src/scanner/images/index.ts,
282+ which chops off everything after ':' from the image name, we store a wrong image name
283+ and the result does not exist in the object referred below */
284+ t . same ( depGraphResult . dependencyGraphResults [ imageName ] , null ,
285+ 'expected result for image kind-registry:5000/python:rc-buster does not exist' ) ;
286+ t . ok ( 'kind-registry' in depGraphResult . dependencyGraphResults , 'BUG: the full image name is not stored in kubernetes-upstream' ) ;
287+ } ) ;
288+
229289tap . test ( 'snyk-monitor sends deleted workload to kubernetes-upstream' , async ( t ) => {
230290 // First ensure the deployment exists from the previous test
231291 const deploymentValidatorFn : WorkloadLocatorValidator = ( workloads ) => {
0 commit comments