1
1
import {
2
+ ENV_CMDS_FULL_URI ,
2
3
ENV_CMDS_RELATIVE_URI ,
3
4
fromContainerMetadata
4
5
} from "../lib/fromContainerMetadata" ;
@@ -18,14 +19,17 @@ const mockHttpGet = <MockInstance<HttpGet>><any>httpGet;
18
19
jest . mock ( '../lib/remoteProvider/httpGet' , ( ) => ( { httpGet : jest . fn ( ) } ) ) ;
19
20
20
21
const relativeUri = process . env [ ENV_CMDS_RELATIVE_URI ] ;
22
+ const fullUri = process . env [ ENV_CMDS_FULL_URI ] ;
21
23
22
24
beforeEach ( ( ) => {
23
25
mockHttpGet . mockReset ( ) ;
24
- process . env [ ENV_CMDS_RELATIVE_URI ] = '/relative/uri' ;
26
+ delete process . env [ ENV_CMDS_RELATIVE_URI ] ;
27
+ delete process . env [ ENV_CMDS_FULL_URI ] ;
25
28
} ) ;
26
29
27
30
afterAll ( ( ) => {
28
31
process . env [ ENV_CMDS_RELATIVE_URI ] = relativeUri ;
32
+ process . env [ ENV_CMDS_FULL_URI ] = fullUri ;
29
33
} ) ;
30
34
31
35
describe ( 'fromContainerMetadata' , ( ) => {
@@ -37,9 +41,8 @@ describe('fromContainerMetadata', () => {
37
41
} ) ;
38
42
39
43
it (
40
- 'should reject the promise if the container credentials environment variable is not set' ,
44
+ 'should reject the promise with a terminal error if the container credentials environment variable is not set' ,
41
45
async ( ) => {
42
- delete process . env [ ENV_CMDS_RELATIVE_URI ] ;
43
46
await fromContainerMetadata ( ) ( ) . then (
44
47
( ) => { throw new Error ( 'The promise should have been rejected' ) ; } ,
45
48
err => {
@@ -49,53 +52,142 @@ describe('fromContainerMetadata', () => {
49
52
}
50
53
) ;
51
54
52
- it (
53
- 'should resolve credentials by fetching them from the container metadata service' ,
54
- async ( ) => {
55
- mockHttpGet . mockReturnValue ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
56
- expect ( await fromContainerMetadata ( ) ( ) )
57
- . toEqual ( fromImdsCredentials ( creds ) ) ;
58
- }
59
- ) ;
55
+ describe ( ENV_CMDS_RELATIVE_URI , ( ) => {
56
+ beforeEach ( ( ) => {
57
+ process . env [ ENV_CMDS_RELATIVE_URI ] = '/relative/uri' ;
58
+ } ) ;
60
59
61
- it ( 'should retry the fetching operation up to maxRetries times' , async ( ) => {
62
- const maxRetries = 5 ;
63
- for ( let i = 0 ; i < maxRetries - 1 ; i ++ ) {
64
- mockHttpGet . mockReturnValueOnce ( Promise . reject ( 'No!' ) ) ;
65
- }
66
- mockHttpGet . mockReturnValueOnce (
67
- Promise . resolve ( JSON . stringify ( creds ) )
60
+ it (
61
+ 'should resolve credentials by fetching them from the container metadata service' ,
62
+ async ( ) => {
63
+ mockHttpGet . mockReturnValue ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
64
+ expect ( await fromContainerMetadata ( ) ( ) )
65
+ . toEqual ( fromImdsCredentials ( creds ) ) ;
66
+ }
68
67
) ;
69
68
70
- expect ( await fromContainerMetadata ( { maxRetries} ) ( ) )
71
- . toEqual ( fromImdsCredentials ( creds ) ) ;
72
- expect ( mockHttpGet . mock . calls . length ) . toEqual ( maxRetries ) ;
73
- } ) ;
69
+ it (
70
+ 'should retry the fetching operation up to maxRetries times' ,
71
+ async ( ) => {
72
+ const maxRetries = 5 ;
73
+ for ( let i = 0 ; i < maxRetries - 1 ; i ++ ) {
74
+ mockHttpGet . mockReturnValueOnce ( Promise . reject ( 'No!' ) ) ;
75
+ }
76
+ mockHttpGet . mockReturnValueOnce (
77
+ Promise . resolve ( JSON . stringify ( creds ) )
78
+ ) ;
74
79
75
- it ( 'should retry responses that receive invalid response values' , async ( ) => {
76
- for ( let key of Object . keys ( creds ) ) {
77
- const invalidCreds : any = { ...creds } ;
78
- delete invalidCreds [ key ] ;
79
- mockHttpGet . mockReturnValueOnce (
80
- Promise . resolve ( JSON . stringify ( invalidCreds ) )
81
- ) ;
82
- }
83
- mockHttpGet . mockReturnValueOnce ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
80
+ expect ( await fromContainerMetadata ( { maxRetries} ) ( ) )
81
+ . toEqual ( fromImdsCredentials ( creds ) ) ;
82
+ expect ( mockHttpGet . mock . calls . length ) . toEqual ( maxRetries ) ;
83
+ }
84
+ ) ;
84
85
85
- await fromContainerMetadata ( { maxRetries : 100 } ) ( ) ;
86
- expect ( mockHttpGet . mock . calls . length )
87
- . toEqual ( Object . keys ( creds ) . length + 1 ) ;
86
+ it (
87
+ 'should retry responses that receive invalid response values' ,
88
+ async ( ) => {
89
+ for ( let key of Object . keys ( creds ) ) {
90
+ const invalidCreds : any = { ...creds } ;
91
+ delete invalidCreds [ key ] ;
92
+ mockHttpGet . mockReturnValueOnce (
93
+ Promise . resolve ( JSON . stringify ( invalidCreds ) )
94
+ ) ;
95
+ }
96
+ mockHttpGet . mockReturnValueOnce (
97
+ Promise . resolve ( JSON . stringify ( creds ) )
98
+ ) ;
99
+
100
+ await fromContainerMetadata ( { maxRetries : 100 } ) ( ) ;
101
+ expect ( mockHttpGet . mock . calls . length )
102
+ . toEqual ( Object . keys ( creds ) . length + 1 ) ;
103
+ }
104
+ ) ;
105
+
106
+ it ( 'should pass relevant configuration to httpGet' , async ( ) => {
107
+ const timeout = Math . ceil ( Math . random ( ) * 1000 ) ;
108
+ mockHttpGet . mockReturnValue ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
109
+ await fromContainerMetadata ( { timeout} ) ( ) ;
110
+ expect ( mockHttpGet . mock . calls . length ) . toEqual ( 1 ) ;
111
+ expect ( mockHttpGet . mock . calls [ 0 ] [ 0 ] ) . toEqual ( {
112
+ hostname : '169.254.170.2' ,
113
+ path : process . env [ ENV_CMDS_RELATIVE_URI ] ,
114
+ timeout,
115
+ } ) ;
116
+ } ) ;
88
117
} ) ;
89
118
90
- it ( 'should pass relevant configuration to httpGet' , async ( ) => {
91
- const timeout = Math . ceil ( Math . random ( ) * 1000 ) ;
92
- mockHttpGet . mockReturnValue ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
93
- await fromContainerMetadata ( { timeout} ) ( ) ;
94
- expect ( mockHttpGet . mock . calls . length ) . toEqual ( 1 ) ;
95
- expect ( mockHttpGet . mock . calls [ 0 ] [ 0 ] ) . toEqual ( {
96
- host : '169.254.170.2' ,
97
- path : process . env [ ENV_CMDS_RELATIVE_URI ] ,
98
- timeout,
119
+ describe ( ENV_CMDS_FULL_URI , ( ) => {
120
+ it ( 'should pass relevant configuration to httpGet' , async ( ) => {
121
+ process . env [ ENV_CMDS_FULL_URI ] = 'http://localhost:8080/path' ;
122
+
123
+ const timeout = Math . ceil ( Math . random ( ) * 1000 ) ;
124
+ mockHttpGet . mockReturnValue ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
125
+ await fromContainerMetadata ( { timeout} ) ( ) ;
126
+ expect ( mockHttpGet . mock . calls . length ) . toEqual ( 1 ) ;
127
+ const {
128
+ protocol,
129
+ hostname,
130
+ path,
131
+ port,
132
+ timeout : actualTimeout ,
133
+ } = mockHttpGet . mock . calls [ 0 ] [ 0 ] ;
134
+ expect ( protocol ) . toBe ( 'http:' ) ;
135
+ expect ( hostname ) . toBe ( 'localhost' ) ;
136
+ expect ( path ) . toBe ( '/path' ) ;
137
+ expect ( port ) . toBe ( 8080 ) ;
138
+ expect ( actualTimeout ) . toBe ( timeout ) ;
99
139
} ) ;
140
+
141
+ it (
142
+ `should prefer ${ ENV_CMDS_RELATIVE_URI } to ${ ENV_CMDS_FULL_URI } ` ,
143
+ async ( ) => {
144
+ process . env [ ENV_CMDS_RELATIVE_URI ] = 'foo' ;
145
+ process . env [ ENV_CMDS_FULL_URI ] = 'http://localhost:8080/path' ;
146
+
147
+ const timeout = Math . ceil ( Math . random ( ) * 1000 ) ;
148
+ mockHttpGet . mockReturnValue (
149
+ Promise . resolve ( JSON . stringify ( creds ) )
150
+ ) ;
151
+ await fromContainerMetadata ( { timeout} ) ( ) ;
152
+ expect ( mockHttpGet . mock . calls . length ) . toEqual ( 1 ) ;
153
+ expect ( mockHttpGet . mock . calls [ 0 ] [ 0 ] ) . toEqual ( {
154
+ hostname : '169.254.170.2' ,
155
+ path : 'foo' ,
156
+ timeout,
157
+ } ) ;
158
+ }
159
+ ) ;
160
+
161
+ it (
162
+ 'should reject the promise with a terminal error if a unexpected protocol is specified' ,
163
+ async ( ) => {
164
+ process . env [ ENV_CMDS_FULL_URI ] = 'wss://localhost:8080/path' ;
165
+
166
+ await fromContainerMetadata ( ) ( ) . then (
167
+ ( ) => {
168
+ throw new Error ( 'The promise should have been rejected' ) ;
169
+ } ,
170
+ err => {
171
+ expect ( ( err as any ) . tryNextLink ) . toBeFalsy ( ) ;
172
+ }
173
+ ) ;
174
+ }
175
+ ) ;
176
+
177
+ it (
178
+ 'should reject the promise with a terminal error if a unexpected hostname is specified' ,
179
+ async ( ) => {
180
+ process . env [ ENV_CMDS_FULL_URI ] = 'https://bucket.s3.amazonaws.com/key' ;
181
+
182
+ await fromContainerMetadata ( ) ( ) . then (
183
+ ( ) => {
184
+ throw new Error ( 'The promise should have been rejected' ) ;
185
+ } ,
186
+ err => {
187
+ expect ( ( err as any ) . tryNextLink ) . toBeFalsy ( ) ;
188
+ }
189
+ ) ;
190
+ }
191
+ ) ;
100
192
} ) ;
101
193
} ) ;
0 commit comments