@@ -17,17 +17,14 @@ import { EC2 } from 'aws-sdk'
1717import { AsyncCollection } from '../../../../shared/utilities/asyncCollection'
1818import * as FakeTimers from '@sinonjs/fake-timers'
1919import { installFakeClock } from '../../../testUtil'
20- import { PollingSet } from '../../../../shared/utilities/pollingSet'
2120
2221describe ( 'ec2ParentNode' , function ( ) {
2322 let testNode : Ec2ParentNode
24- let defaultInstances : SafeEc2Instance [ ]
2523 let client : Ec2Client
2624 let getInstanceStub : sinon . SinonStub < [ filters ?: EC2 . Filter [ ] | undefined ] , Promise < AsyncCollection < EC2 . Instance > > >
2725 let clock : FakeTimers . InstalledClock
2826 let refreshStub : sinon . SinonStub < [ ] , Promise < void > >
29- let clearTimerStub : sinon . SinonStub < [ ] , void >
30-
27+ let statusUpdateStub : sinon . SinonStub < [ status : string ] , Promise < string > >
3128 const testRegion = 'testRegion'
3229 const testPartition = 'testPartition'
3330
@@ -45,36 +42,19 @@ describe('ec2ParentNode', function () {
4542 client = new Ec2Client ( testRegion )
4643 clock = installFakeClock ( )
4744 refreshStub = sinon . stub ( Ec2InstanceNode . prototype , 'refreshNode' )
48- clearTimerStub = sinon . stub ( PollingSet . prototype , 'clearTimer' )
49- defaultInstances = [
50- { Name : 'firstOne' , InstanceId : '0' , LastSeenStatus : 'running' } ,
51- { Name : 'secondOne' , InstanceId : '1' , LastSeenStatus : 'running' } ,
52- ]
45+ statusUpdateStub = sinon . stub ( Ec2Client . prototype , 'getInstanceStatus' )
5346 } )
5447
5548 beforeEach ( function ( ) {
5649 getInstanceStub = sinon . stub ( Ec2Client . prototype , 'getInstances' )
57- defaultInstances = [
58- { Name : 'firstOne' , InstanceId : '0' , LastSeenStatus : 'running' } ,
59- { Name : 'secondOne' , InstanceId : '1' , LastSeenStatus : 'stopped' } ,
60- ]
61-
62- getInstanceStub . callsFake ( async ( ) =>
63- intoCollection (
64- defaultInstances . map ( ( instance ) => ( {
65- InstanceId : instance . InstanceId ,
66- Tags : [ { Key : 'Name' , Value : instance . Name } ] ,
67- } ) )
68- )
69- )
70-
7150 testNode = new Ec2ParentNode ( testRegion , testPartition , client )
7251 refreshStub . resetHistory ( )
73- clearTimerStub . resetHistory ( )
7452 } )
7553
7654 afterEach ( function ( ) {
7755 getInstanceStub . restore ( )
56+ testNode . pollingSet . clear ( )
57+ testNode . pollingSet . clearTimer ( )
7858 } )
7959
8060 after ( function ( ) {
@@ -91,10 +71,14 @@ describe('ec2ParentNode', function () {
9171 } )
9272
9373 it ( 'has instance child nodes' , async function ( ) {
94- getInstanceStub . resolves ( mapToInstanceCollection ( defaultInstances ) )
74+ const instances = [
75+ { Name : 'firstOne' , InstanceId : '0' , LastSeenStatus : 'running' } ,
76+ { Name : 'secondOne' , InstanceId : '1' , LastSeenStatus : 'stopped' } ,
77+ ]
78+ getInstanceStub . resolves ( mapToInstanceCollection ( instances ) )
9579 const childNodes = await testNode . getChildren ( )
9680
97- assert . strictEqual ( childNodes . length , defaultInstances . length , 'Unexpected child count' )
81+ assert . strictEqual ( childNodes . length , instances . length , 'Unexpected child count' )
9882
9983 childNodes . forEach ( ( node ) =>
10084 assert . ok ( node instanceof Ec2InstanceNode , 'Expected child node to be Ec2InstanceNode' )
@@ -151,14 +135,13 @@ describe('ec2ParentNode', function () {
151135 ]
152136
153137 getInstanceStub . resolves ( mapToInstanceCollection ( instances ) )
154-
155138 await testNode . updateChildren ( )
156139 assert . strictEqual ( testNode . pollingSet . size , 1 )
157140 getInstanceStub . restore ( )
158141 } )
159142
160143 it ( 'does not refresh explorer when timer goes off if status unchanged' , async function ( ) {
161- const statusUpdateStub = sinon . stub ( Ec2Client . prototype , 'getInstanceStatus' ) . resolves ( 'pending' )
144+ statusUpdateStub = statusUpdateStub . resolves ( 'pending' )
162145 const instances = [
163146 { Name : 'firstOne' , InstanceId : '0' , LastSeenStatus : 'pending' } ,
164147 { Name : 'secondOne' , InstanceId : '1' , LastSeenStatus : 'stopped' } ,
@@ -170,16 +153,56 @@ describe('ec2ParentNode', function () {
170153 await testNode . updateChildren ( )
171154 await clock . tickAsync ( 6000 )
172155 sinon . assert . notCalled ( refreshStub )
173- statusUpdateStub . restore ( )
174156 getInstanceStub . restore ( )
175157 } )
176158
177159 it ( 'does refresh explorer when timer goes and status changed' , async function ( ) {
160+ statusUpdateStub = statusUpdateStub . resolves ( 'running' )
161+ const instances = [ { Name : 'firstOne' , InstanceId : '0' , LastSeenStatus : 'pending' } ]
162+
163+ getInstanceStub . resolves ( mapToInstanceCollection ( instances ) )
164+ await testNode . updateChildren ( )
165+
178166 sinon . assert . notCalled ( refreshStub )
179- const statusUpdateStub = sinon . stub ( Ec2Client . prototype , 'getInstanceStatus' ) . resolves ( 'running' )
180- testNode . pollingSet . add ( '0' )
181167 await clock . tickAsync ( 6000 )
182168 sinon . assert . called ( refreshStub )
183- statusUpdateStub . restore ( )
169+ } )
170+
171+ it ( 'returns the node when in the map' , async function ( ) {
172+ const instances = [ { Name : 'firstOne' , InstanceId : 'node1' , LastSeenStatus : 'pending' } ]
173+
174+ getInstanceStub . resolves ( mapToInstanceCollection ( instances ) )
175+ await testNode . updateChildren ( )
176+ const node = testNode . getInstanceNode ( 'node1' )
177+ assert . strictEqual ( node . InstanceId , instances [ 0 ] . InstanceId )
178+ getInstanceStub . restore ( )
179+ } )
180+
181+ it ( 'throws error when node not in map' , async function ( ) {
182+ const instances = [ { Name : 'firstOne' , InstanceId : 'node1' , LastSeenStatus : 'pending' } ]
183+
184+ getInstanceStub . resolves ( mapToInstanceCollection ( instances ) )
185+ await testNode . updateChildren ( )
186+ assert . throws ( ( ) => testNode . getInstanceNode ( 'node2' ) )
187+ getInstanceStub . restore ( )
188+ } )
189+
190+ it ( 'adds node to polling set when asked to track it' , async function ( ) {
191+ const instances = [ { Name : 'firstOne' , InstanceId : 'node1' , LastSeenStatus : 'pending' } ]
192+
193+ getInstanceStub . resolves ( mapToInstanceCollection ( instances ) )
194+ await testNode . updateChildren ( )
195+ testNode . trackPendingNode ( 'node1' )
196+ assert . strictEqual ( testNode . pollingSet . size , 1 )
197+ getInstanceStub . restore ( )
198+ } )
199+
200+ it ( 'throws error when asked to track non-child node' , async function ( ) {
201+ const instances = [ { Name : 'firstOne' , InstanceId : 'node1' , LastSeenStatus : 'pending' } ]
202+
203+ getInstanceStub . resolves ( mapToInstanceCollection ( instances ) )
204+ await testNode . updateChildren ( )
205+ assert . throws ( ( ) => testNode . trackPendingNode ( 'node2' ) )
206+ getInstanceStub . restore ( )
184207 } )
185208} )
0 commit comments