@@ -3,6 +3,7 @@ import * as nock from 'nock';
33import { copyFile , readFile , mkdir , exists } from 'fs' ;
44import { promisify } from 'util' ;
55import { resolve as resolvePath } from 'path' ;
6+ import { v4 as uuid } from 'uuid' ;
67
78import * as kubectl from '../helpers/kubectl' ;
89import * as kind from '../setup/platforms/kind' ;
@@ -25,6 +26,7 @@ const existsAsync = promisify(exists);
2526 * Error: Client network socket disconnected before secure TLS connection was established
2627 */
2728import { state as kubernetesMonitorState } from '../../src/state' ;
29+ import * as kubernetesApiWrappers from '../../src/supervisor/kuberenetes-api-wrappers' ;
2830
2931async function tearDown ( ) {
3032 console . log ( 'Begin removing the snyk-monitor...' ) ;
@@ -38,6 +40,8 @@ async function tearDown() {
3840
3941beforeAll ( tearDown ) ;
4042afterAll ( async ( ) => {
43+ jest . restoreAllMocks ( ) ;
44+
4145 kubernetesMonitorState . shutdownInProgress = true ;
4246 await tearDown ( ) ;
4347 // TODO cleanup the images we saved to /var/tmp?
@@ -48,6 +52,17 @@ test('Kubernetes-Monitor with KinD', async (jestDoneCallback) => {
4852 . spyOn ( fsExtra , 'emptyDirSync' )
4953 . mockReturnValue ( { } ) ;
5054
55+ const agentId = uuid ( ) ;
56+ const retryKubernetesApiRequestMock = jest
57+ . spyOn ( kubernetesApiWrappers , 'retryKubernetesApiRequest' )
58+ . mockResolvedValueOnce ( {
59+ body : {
60+ metadata : {
61+ uid : agentId ,
62+ } ,
63+ } ,
64+ } ) ;
65+
5166 try {
5267 await exec ( 'which skopeo' ) ;
5368 console . log ( 'Skopeo already installed :tada:' ) ;
@@ -102,7 +117,7 @@ test('Kubernetes-Monitor with KinD', async (jestDoneCallback) => {
102117 expect (
103118 requestBody ,
104119 ) . toEqual < transmitterTypes . IWorkloadEventsPolicyPayload > ( {
105- agentId : expect . any ( String ) ,
120+ agentId,
106121 cluster : expect . any ( String ) ,
107122 userLocator : expect . any ( String ) ,
108123 policy : regoPolicyContents ,
@@ -123,7 +138,7 @@ test('Kubernetes-Monitor with KinD', async (jestDoneCallback) => {
123138 expect ( requestBody ) . toEqual <
124139 Partial < transmitterTypes . IClusterMetadataPayload >
125140 > ( {
126- agentId : expect . any ( String ) ,
141+ agentId,
127142 cluster : expect . any ( String ) ,
128143 userLocator : expect . any ( String ) ,
129144 // also should have version here but due to test limitation it is undefined
@@ -199,7 +214,7 @@ test('Kubernetes-Monitor with KinD', async (jestDoneCallback) => {
199214 ] ) ,
200215 } ) ,
201216 } ) ,
202- agentId : expect . any ( String ) ,
217+ agentId,
203218 } ) ;
204219 } catch ( error ) {
205220 jestDoneCallback ( error ) ;
@@ -230,8 +245,7 @@ test('Kubernetes-Monitor with KinD', async (jestDoneCallback) => {
230245 . reply ( 500 , ( uri , requestBody : transmitterTypes . ScanResultsPayload ) => {
231246 try {
232247 expect ( requestBody ) . toEqual < transmitterTypes . ScanResultsPayload > ( {
233- metadata : expect . any ( Object ) ,
234- agentId : expect . any ( String ) ,
248+ agentId,
235249 imageLocator : expect . objectContaining ( {
236250 imageId : expect . any ( String ) ,
237251 } ) ,
@@ -277,7 +291,7 @@ test('Kubernetes-Monitor with KinD', async (jestDoneCallback) => {
277291 try {
278292 expect ( requestBody ) . toEqual < transmitterTypes . IDependencyGraphPayload > (
279293 {
280- agentId : expect . any ( String ) ,
294+ agentId,
281295 dependencyGraph : expect . stringContaining ( 'docker-image|java' ) ,
282296 imageLocator : {
283297 userLocator : expect . any ( String ) ,
@@ -288,11 +302,10 @@ test('Kubernetes-Monitor with KinD', async (jestDoneCallback) => {
288302 type : expect . any ( String ) ,
289303 imageWithDigest : expect . any ( String ) ,
290304 } ,
291- metadata : expect . objectContaining ( {
292- agentId : expect . any ( String ) ,
293- } ) ,
294305 } ,
295306 ) ;
307+
308+ expect ( retryKubernetesApiRequestMock ) . toHaveBeenCalled ( ) ;
296309 jestDoneCallback ( ) ;
297310 } catch ( error ) {
298311 jestDoneCallback ( error ) ;
0 commit comments