1
1
import { expect } from 'chai' ;
2
2
import { createNoopLogger } from '@mongodb-js/compass-logging/provider' ;
3
- import {
4
- buildConnectionInfoFromClusterDescription ,
5
- AtlasCloudConnectionStorage ,
6
- } from './connection-storage' ;
7
- import type { ClusterDescriptionWithDataProcessingRegion } from './connection-storage' ;
8
-
9
- const deployment = {
10
- replicaSets : [
11
- {
12
- _id : 'replicaSet-xxx' ,
13
- state : {
14
- clusterId : '123abc' ,
15
- } ,
16
- } ,
17
- ] ,
18
- sharding : [
19
- {
20
- name : 'sharded-xxx' ,
21
- state : {
22
- clusterId : '123abc' ,
23
- } ,
24
- } ,
25
- ] ,
26
- } ;
27
-
28
- type Test = [ string , ClusterDescriptionWithDataProcessingRegion , string ] ;
29
-
30
- describe ( 'buildConnectionInfoFromClusterDescription' , function ( ) {
31
- const tests : Test [ ] = [
32
- [
33
- 'replicaSet' ,
34
- {
35
- '@provider' : 'AWS' ,
36
- uniqueId : '123abc' ,
37
- groupId : 'abc' ,
38
- name : 'Cluster0-replicaSet' ,
39
- clusterType : 'REPLICASET' ,
40
- srvAddress : 'replicaSet.mongodb.com' ,
41
- state : 'IDLE' ,
42
- deploymentItemName : 'replicaSet-xxx' ,
43
- dataProcessingRegion : {
44
- regionalUrl : 'https://example.com' ,
45
- } ,
46
- replicationSpecList : [
47
- {
48
- regionConfigs : [
49
- {
50
- priority : 1 ,
51
- electableSpecs : {
52
- instanceSize : 'M0' , // free tier
53
- } ,
54
- } ,
55
- ] ,
56
- } ,
57
- ] ,
58
- } ,
59
- 'mongodb+srv://replicaSet.mongodb.com/?tls=true&authMechanism=MONGODB-X509&authSource=%24external&serverMonitoringMode=poll&maxIdleTimeMS=30000&minPoolSize=0&maxPoolSize=5' ,
60
- ] ,
61
- [
62
- 'sharded' ,
63
- {
64
- '@provider' : 'AWS' ,
65
- uniqueId : '123abc' ,
66
- groupId : 'abc' ,
67
- name : 'Cluster0-sharded' ,
68
- clusterType : 'SHARDED' ,
69
- srvAddress : 'sharded.mongodb.com' ,
70
- state : 'IDLE' ,
71
- deploymentItemName : 'sharded-xxx' ,
72
- dataProcessingRegion : {
73
- regionalUrl : 'https://example.com' ,
74
- } ,
75
- geoSharding : {
76
- selfManagedSharding : true ,
77
- } ,
78
- replicationSpecList : [
79
- {
80
- regionConfigs : [
81
- {
82
- priority : 1 ,
83
- electableSpecs : {
84
- instanceSize : 'M10' , // dedicated
85
- } ,
86
- } ,
87
- {
88
- priority : 2 ,
89
- electableSpecs : {
90
- instanceSize : 'M12' , // dedicated
91
- } ,
92
- } ,
93
- ] ,
94
- } ,
95
- ] ,
96
- } ,
97
- 'mongodb+srv://sharded.mongodb.com/?tls=true&authMechanism=MONGODB-X509&authSource=%24external&serverMonitoringMode=poll&maxIdleTimeMS=30000&minPoolSize=0&maxPoolSize=5&srvMaxHosts=1' ,
98
- ] ,
99
- [
100
- 'serverless' ,
101
- {
102
- '@provider' : 'SERVERLESS' ,
103
- uniqueId : '123abc' ,
104
- groupId : 'abc' ,
105
- name : 'Cluster0-serverless' ,
106
- clusterType : 'REPLICASET' ,
107
- srvAddress : 'serverless.mongodb.com' ,
108
- state : 'IDLE' ,
109
- deploymentItemName : 'serverless-xxx' ,
110
- dataProcessingRegion : {
111
- regionalUrl : 'https://example.com' ,
112
- } ,
113
- replicationSpecList : [
114
- {
115
- regionConfigs : [
116
- {
117
- priority : 1 ,
118
- electableSpecs : {
119
- instanceSize : 'SERVERLESS_V2' ,
120
- } ,
121
- } ,
122
- ] ,
123
- } ,
124
- ] ,
125
- } ,
126
- 'mongodb+srv://serverless.mongodb.com/?tls=true&authMechanism=MONGODB-X509&authSource=%24external&serverMonitoringMode=poll&maxIdleTimeMS=30000&minPoolSize=0&maxPoolSize=5' ,
127
- ] ,
128
- [
129
- 'flex' ,
130
- {
131
- '@provider' : 'FLEX' ,
132
- uniqueId : '123abc' ,
133
- groupId : 'abc' ,
134
- name : 'Cluster0-flex' ,
135
- clusterType : 'REPLICASET' ,
136
- srvAddress : 'flex.mongodb.com' ,
137
- state : 'IDLE' ,
138
- deploymentItemName : 'flex-xxx' ,
139
- dataProcessingRegion : {
140
- regionalUrl : 'https://example.com' ,
141
- } ,
142
- replicationSpecList : [
143
- {
144
- regionConfigs : [
145
- {
146
- priority : 1 ,
147
- electableSpecs : {
148
- instanceSize : 'FLEX' ,
149
- } ,
150
- } ,
151
- ] ,
152
- } ,
153
- ] ,
154
- } ,
155
- 'mongodb+srv://flex.mongodb.com/?tls=true&authMechanism=MONGODB-X509&authSource=%24external&serverMonitoringMode=poll&maxIdleTimeMS=30000&minPoolSize=0&maxPoolSize=5' ,
156
- ] ,
157
- ] ;
158
-
159
- for ( const [ type , clusterDescription , connectionString ] of tests ) {
160
- it ( `should build connection info for ${ type } cluster` , function ( ) {
161
- const connectionInfo = buildConnectionInfoFromClusterDescription (
162
- 'ws://test' ,
163
- '123' ,
164
- 'abc' ,
165
- clusterDescription ,
166
- deployment
167
- ) ;
168
-
169
- expect ( connectionInfo ) . to . have . property (
170
- 'id' ,
171
- clusterDescription . uniqueId
172
- ) ;
173
-
174
- expect ( connectionInfo ) . to . have . nested . property (
175
- 'connectionOptions.connectionString' ,
176
- connectionString
177
- ) ;
178
-
179
- expect ( connectionInfo . connectionOptions . lookup ?.( ) ) . to . deep . eq ( {
180
- wsURL : 'ws://test' ,
181
- projectId : 'abc' ,
182
- clusterName : `Cluster0-${ type } ` ,
183
- srvAddress : `${ type } .mongodb.com` ,
184
- } ) ;
185
-
186
- // just assume the last regionConfig in our test data is the highest
187
- // priority one
188
- const expectedInstanceSize =
189
- clusterDescription . replicationSpecList ?. [ 0 ] . regionConfigs . slice ( ) . pop ( )
190
- ?. electableSpecs . instanceSize ;
191
-
192
- // We test these separately in another test
193
- if ( connectionInfo . atlasMetadata ?. supports ) {
194
- delete ( connectionInfo . atlasMetadata as { supports ?: any } ) . supports ;
195
- }
196
-
197
- expect ( connectionInfo )
198
- . to . have . property ( 'atlasMetadata' )
199
- . deep . eq ( {
200
- orgId : '123' ,
201
- projectId : 'abc' ,
202
- metricsId :
203
- type === 'serverless' || type === 'flex'
204
- ? `Cluster0-${ type } `
205
- : '123abc' ,
206
- clusterName : `Cluster0-${ type } ` ,
207
- clusterUniqueId : '123abc' ,
208
- metricsType : type === 'sharded' ? 'cluster' : type ,
209
- instanceSize : expectedInstanceSize ,
210
- regionalBaseUrl : null ,
211
- clusterType : clusterDescription . clusterType ,
212
- clusterState : 'IDLE' ,
213
- } ) ;
214
- } ) ;
215
- }
216
-
217
- it ( 'should throw if deployment item is missing' , function ( ) {
218
- try {
219
- buildConnectionInfoFromClusterDescription (
220
- 'ws://test' ,
221
- '123' ,
222
- 'abc' ,
223
- {
224
- '@provider' : 'mock' ,
225
- uniqueId : 'abc' ,
226
- groupId : 'abc' ,
227
- name : 'Cluster0' ,
228
- clusterType : 'REPLICASET' ,
229
- srvAddress : 'test' ,
230
- state : 'test' ,
231
- deploymentItemName : 'test' ,
232
- dataProcessingRegion : { regionalUrl : 'test' } ,
233
- } ,
234
- deployment
235
- ) ;
236
- expect . fail ( 'Expected method to throw' ) ;
237
- } catch ( err ) {
238
- expect ( err ) . to . have . property (
239
- 'message' ,
240
- "Can't build metrics info when deployment item is not found"
241
- ) ;
242
- }
243
- } ) ;
244
- } ) ;
3
+ import { AtlasCloudConnectionStorage } from './connection-storage' ;
245
4
246
5
describe ( 'AtlasCloudConnectionStorage' , function ( ) {
247
- const testClusters : Record <
248
- string ,
249
- Partial < ClusterDescriptionWithDataProcessingRegion >
250
- > = {
251
- Cluster0 : {
252
- '@provider' : 'AWS' ,
253
- groupId : 'abc' ,
254
- name : 'Cluster0' ,
255
- clusterType : 'REPLICASET' ,
256
- srvAddress : 'test' ,
257
- state : 'test' ,
258
- deploymentItemName : 'replicaSet-xxx' ,
259
- dataProcessingRegion : { regionalUrl : 'test' } ,
260
- } ,
261
- NoDeploymentItem : {
262
- '@provider' : 'AWS' ,
263
- groupId : 'abc' ,
264
- name : 'NoDeploymentItem' ,
265
- clusterType : 'REPLICASET' ,
266
- srvAddress : 'test' ,
267
- state : 'test' ,
268
- deploymentItemName : 'not-found' ,
269
- dataProcessingRegion : { regionalUrl : 'test' } ,
270
- } ,
271
- NoSrvAddress : {
272
- '@provider' : 'AWS' ,
273
- name : 'NoSrvAddress' ,
274
- } ,
275
- Paused : {
276
- '@provider' : 'AWS' ,
277
- name : 'Paused' ,
278
- isPaused : true ,
279
- } ,
280
- WillThrowOnFetch : {
281
- '@provider' : 'AWS' ,
282
- name : 'WillThrowOnFetch' ,
283
- } ,
284
- } ;
285
-
286
6
describe ( '#loadAll' , function ( ) {
287
- it ( 'should load connection descriptions filtering out the ones that failed to fetch ' , async function ( ) {
7
+ it ( 'should load connection descriptions filtering out the ones that are in the unsupported state ' , async function ( ) {
288
8
const atlasService = {
289
9
cloudEndpoint ( path : string ) {
290
10
return path ;
@@ -293,25 +13,30 @@ describe('AtlasCloudConnectionStorage', function () {
293
13
return path ;
294
14
} ,
295
15
authenticatedFetch ( path : string ) {
296
- let payload : any ;
297
- if ( path === '/deployment/abc' ) {
298
- payload = deployment ;
299
- }
300
- if ( path === '/nds/clusters/abc' ) {
301
- payload = Array . from ( Object . values ( testClusters ) ) ;
302
- }
303
- const { groups } =
304
- / ^ \/ n d s \/ c l u s t e r s \/ a b c \/ (?< clusterName > .+ ?) \/ .+ ?$ / . exec ( path ) ?? {
305
- groups : undefined ,
306
- } ;
307
- if ( groups ?. clusterName ) {
308
- if ( groups ?. clusterName === 'WillThrowOnFetch' ) {
309
- return Promise . reject (
310
- new Error ( 'Failed to fetch cluster description' )
311
- ) ;
312
- }
313
- payload = testClusters [ groups . clusterName ] ;
16
+ if ( ! path . endsWith ( '/clusters/connectionInfo' ) ) {
17
+ throw new Error ( 'Unsupported URL' ) ;
314
18
}
19
+ const payload = [
20
+ {
21
+ id : 'foo' ,
22
+ connectionOptions : { } ,
23
+ atlasMetadata : { clusterName : 'Cluster0' , clusterState : 'IDLE' } ,
24
+ } ,
25
+ // No metadata, will filter this out
26
+ {
27
+ id : 'bar' ,
28
+ connectionOptions : { } ,
29
+ } ,
30
+ // Cluster state not supported
31
+ {
32
+ id : 'buz' ,
33
+ connectionOptions : { } ,
34
+ atlasMetadata : {
35
+ clusterName : 'Cluster2' ,
36
+ clusterState : 'WEIRD_ONE' ,
37
+ } ,
38
+ } ,
39
+ ] ;
315
40
return Promise . resolve ( {
316
41
json ( ) {
317
42
return payload ;
@@ -339,7 +64,10 @@ describe('AtlasCloudConnectionStorage', function () {
339
64
// We expect all other clusters to be filtered out for one reason or
340
65
// another
341
66
expect ( connections ) . to . have . lengthOf ( 1 ) ;
342
- expect ( connections [ 0 ] ) . to . have . property ( 'id' , 'Cluster0' ) ;
67
+ expect ( connections [ 0 ] ) . to . have . nested . property (
68
+ 'atlasMetadata.clusterName' ,
69
+ 'Cluster0'
70
+ ) ;
343
71
} ) ;
344
72
} ) ;
345
73
} ) ;
0 commit comments