1- import * as tap from 'tap' ;
2- import * as sinon from 'sinon' ;
3- import * as sleep from 'sleep-promise' ;
1+ import * as async from 'async' ;
42import * as fs from 'fs' ;
3+ import * as sleep from 'sleep-promise' ;
54import * as YAML from 'yaml' ;
6- import async = require( 'async' ) ;
75
86import { V1PodSpec , V1Pod } from '@kubernetes/client-node' ;
97import { IWorkload } from '../../../src/transmitter/types' ;
108import * as metadataExtractor from '../../../src/supervisor/metadata-extractor' ;
119
1210let pushCallCount = 0 ;
13- sinon . stub ( async , 'queue' ) . returns ( { error : ( ) => { } , push : ( ) => pushCallCount ++ } as any ) ;
11+ const asyncQueueSpy = jest . spyOn ( async , 'queue' ) . mockReturnValue ( { error : ( ) => { } , push : ( ) => pushCallCount ++ } as any ) ;
1412
1513import * as pod from '../../../src/supervisor/watchers/handlers/pod' ;
1614
17- tap . test ( 'image and workload image cache' , async ( t ) => {
15+ describe ( 'image and workload image cache' , ( ) => {
1816 const podSpecFixture = fs . readFileSync ( './test/fixtures/pod-spec.json' , 'utf8' ) ;
1917 const podSpec : V1PodSpec = YAML . parse ( podSpecFixture ) ;
2018 const workloadMetadata : IWorkload [ ] = [
@@ -36,25 +34,33 @@ tap.test('image and workload image cache', async (t) => {
3634 } ,
3735 ] ;
3836
39- sinon . stub ( metadataExtractor , 'buildMetadataForWorkload' ) . resolves ( workloadMetadata ) ;
4037
41- t . teardown ( ( ) => {
42- ( async [ 'queue' ] as any ) . restore ( ) ;
43- ( metadataExtractor [ 'buildMetadataForWorkload' ] as any ) . restore ( ) ;
38+ const buildMetadataSpy = jest . spyOn ( metadataExtractor , 'buildMetadataForWorkload' ) . mockResolvedValue ( workloadMetadata ) ;
39+
40+ afterAll ( ( ) => {
41+ asyncQueueSpy . mockRestore ( ) ;
42+ buildMetadataSpy . mockRestore ( ) ;
4443 } ) ;
4544
4645 const podFixture = fs . readFileSync ( './test/fixtures/sidecar-containers/pod.yaml' , 'utf8' ) ;
4746 const podObject : V1Pod = YAML . parse ( podFixture ) ;
48- await pod . podWatchHandler ( podObject ) ;
49- await sleep ( 500 ) ;
50- t . equals ( pushCallCount , 1 , 'pushed data to send' ) ;
51-
52- await pod . podWatchHandler ( podObject ) ;
53- await sleep ( 500 ) ;
54- t . equals ( pushCallCount , 1 , 'cached info, no data pushed to send' ) ;
55-
56- workloadMetadata [ 0 ] . imageId = 'newImageName' ;
57- await pod . podWatchHandler ( podObject ) ;
58- await sleep ( 1000 ) ;
59- t . equals ( pushCallCount , 2 , 'new image parsed, workload is cached' ) ;
47+
48+ it ( 'pushed data to send' , async ( ) => {
49+ await pod . podWatchHandler ( podObject ) ;
50+ await sleep ( 500 ) ;
51+ expect ( pushCallCount ) . toEqual ( 1 ) ;
52+ } ) ;
53+
54+ it ( 'cached info, no data pushed to send' , async ( ) => {
55+ await pod . podWatchHandler ( podObject ) ;
56+ await sleep ( 500 ) ;
57+ expect ( pushCallCount ) . toEqual ( 1 ) ;
58+ } ) ;
59+
60+ it ( 'new image parsed, workload is cached' , async ( ) => {
61+ workloadMetadata [ 0 ] . imageId = 'newImageName' ;
62+ await pod . podWatchHandler ( podObject ) ;
63+ await sleep ( 1000 ) ;
64+ expect ( pushCallCount ) . toEqual ( 2 ) ;
65+ } ) ;
6066} ) ;
0 commit comments