|
1 | 1 | import { CoreV1Api, KubeConfig, AppsV1Api } from '@kubernetes/client-node'; |
| 2 | +import { exec } from 'child-process-promise'; |
2 | 3 | import setup = require('../setup'); |
3 | 4 | import * as tap from 'tap'; |
4 | 5 | import { WorkloadKind } from '../../src/supervisor/types'; |
@@ -65,6 +66,23 @@ tap.test('snyk-monitor container started', async (t) => { |
65 | 66 | console.log('Done -- snyk-monitor exists!'); |
66 | 67 | }); |
67 | 68 |
|
| 69 | +tap.test('create local container registry and push an image', async (t) => { |
| 70 | + if (process.env['TEST_PLATFORM'] !== 'kind') { |
| 71 | + t.pass('Not testing local container registry because we\'re not running in KinD'); |
| 72 | + return; |
| 73 | + } |
| 74 | + console.log('Creating local container registry...'); |
| 75 | + await exec('docker run -d --restart=always -p "5000:5000" --name "kind-registry" registry:2'); |
| 76 | + await exec('docker network connect "kind" "kind-registry"'); |
| 77 | + |
| 78 | + console.log('Pushing python:rc-buster image to the local registry'); |
| 79 | + //Note: this job takes a while and waitForJob() should be called before trying to access local registry image, |
| 80 | + //to make sure it completed |
| 81 | + await kubectl.applyK8sYaml('./test/fixtures/insecure-registries/push-dockerhub-image-to-local-registry.yaml'); |
| 82 | + |
| 83 | + t.pass('successfully started a job to push image to a local registry'); |
| 84 | +}); |
| 85 | + |
68 | 86 | tap.test('snyk-monitor sends data to kubernetes-upstream', async (t) => { |
69 | 87 | t.plan(7); |
70 | 88 |
|
@@ -226,6 +244,59 @@ tap.test('snyk-monitor pulls images from a private ECR and sends data to kuberne |
226 | 244 | 'snyk-monitor sent expected data to upstream in the expected timeframe'); |
227 | 245 | }); |
228 | 246 |
|
| 247 | +tap.test('snyk-monitor pulls images from a local registry and sends data to kubernetes-upstream', async (t) => { |
| 248 | + t.teardown(async () => { |
| 249 | + console.log('Begin removing local container registry...'); |
| 250 | + await setup.removeLocalContainerRegistry(); |
| 251 | + console.log('Removed local container registry'); |
| 252 | + console.log('Begin removing "kind" network...'); |
| 253 | + await setup.removeUnusedKindNetwork(); |
| 254 | + console.log('Removed "kind" network'); |
| 255 | + }); |
| 256 | + |
| 257 | + if (process.env['TEST_PLATFORM'] !== 'kind') { |
| 258 | + t.pass('Not testing local container registry because we\'re not running in KinD'); |
| 259 | + return; |
| 260 | + } |
| 261 | + t.plan(4); |
| 262 | + |
| 263 | + const deploymentName = 'python-local'; |
| 264 | + const namespace = 'services'; |
| 265 | + const clusterName = 'Default cluster'; |
| 266 | + const deploymentType = WorkloadKind.Deployment; |
| 267 | + const imageName = 'kind-registry:5000/python:rc-buster'; |
| 268 | + |
| 269 | + await kubectl.waitForJob('push-to-local-registry', 'default'); |
| 270 | + |
| 271 | + console.log('Applying local registry workload...'); |
| 272 | + await kubectl.applyK8sYaml('./test/fixtures/insecure-registries/python-local-deployment.yaml'); |
| 273 | + |
| 274 | + console.log(`Begin polling upstream for the expected kind-registry:5000 image with integration ID ${integrationId}...`); |
| 275 | + |
| 276 | + const validatorFn: WorkloadLocatorValidator = (workloads) => { |
| 277 | + return workloads !== undefined && |
| 278 | + workloads.find((workload) => workload.name === deploymentName && |
| 279 | + workload.type === deploymentType) !== undefined; |
| 280 | + }; |
| 281 | + |
| 282 | + const testResult = await validateUpstreamStoredData( |
| 283 | + validatorFn, `api/v2/workloads/${integrationId}/${clusterName}/${namespace}`); |
| 284 | + t.ok(testResult, 'snyk-monitor sent expected data to upstream in the expected timeframe'); |
| 285 | + |
| 286 | + const depGraphResult = await getUpstreamResponseBody( |
| 287 | + `api/v1/dependency-graphs/${integrationId}/${clusterName}/${namespace}/${deploymentType}/${deploymentName}`); |
| 288 | + |
| 289 | + t.ok('dependencyGraphResults' in depGraphResult, |
| 290 | + 'expected dependencyGraphResults field to exist in /dependency-graphs response'); |
| 291 | + |
| 292 | + /* Because of a bug in removeTagFromImage() func in src/scanner/images/index.ts, |
| 293 | + which chops off everything after ':' from the image name, we store a wrong image name |
| 294 | + and the result does not exist in the object referred below */ |
| 295 | + t.same(depGraphResult.dependencyGraphResults[imageName], null, |
| 296 | + 'expected result for image kind-registry:5000/python:rc-buster does not exist'); |
| 297 | + t.ok('kind-registry' in depGraphResult.dependencyGraphResults, 'BUG: the full image name is not stored in kubernetes-upstream'); |
| 298 | +}); |
| 299 | + |
229 | 300 | tap.test('snyk-monitor sends deleted workload to kubernetes-upstream', async (t) => { |
230 | 301 | // First ensure the deployment exists from the previous test |
231 | 302 | const deploymentValidatorFn: WorkloadLocatorValidator = (workloads) => { |
|
0 commit comments