-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(atlas-service, indexes): implement automation agent request hel…
…per, implement rolling indexes service COMPASS-8215 (#6236) * chore(atlas-service, indexes): implement automation agent request helper, implement rolling indexes service * chore(atlas-service, indexes): add tests and fix depcheck * chore(web): adjust test to account for new prop * chore(atlas-service): refactor automation agent types to clarify the response picking up logic * chore(atlas-service, indexes): more type fixes
- Loading branch information
1 parent
e68050f
commit 94538e6
Showing
10 changed files
with
431 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
packages/atlas-service/src/make-automation-agent-op-request.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import Sinon from 'sinon'; | ||
import { makeAutomationAgentOpRequest } from './make-automation-agent-op-request'; | ||
import { expect } from 'chai'; | ||
|
||
describe('makeAutomationAgentOpRequest', function () { | ||
const successSpecs = [ | ||
[ | ||
'succeeds if backend returned requestId and response', | ||
{ _id: 'abc', requestType: 'listIndexStats' }, | ||
{ | ||
_id: 'abc', | ||
requestType: 'listIndexStats', | ||
response: [{ indexName: 'test' }], | ||
}, | ||
], | ||
] as const; | ||
|
||
const failSpecs = [ | ||
[ | ||
'fails if initial request fails', | ||
new Error('NetworkError'), | ||
{}, | ||
/NetworkError/, | ||
], | ||
[ | ||
'fails if await response fails', | ||
{ _id: 'abc', requestType: 'listIndexStats' }, | ||
new Error('NetworkError'), | ||
/NetworkError/, | ||
], | ||
[ | ||
'fails if backend did not return requestId', | ||
{}, | ||
{}, | ||
/Got unexpected backend response/, | ||
], | ||
[ | ||
'fails if backend returned requestId but no response', | ||
{ _id: 'abc', requestType: 'listIndexStats' }, | ||
{}, | ||
/Got unexpected backend response/, | ||
], | ||
] as const; | ||
|
||
function getMockFetch( | ||
requestResponse: Record<string, unknown> | Error, | ||
awaitResponse: Record<string, unknown> | Error | ||
) { | ||
return Sinon.stub() | ||
.onFirstCall() | ||
.callsFake(() => { | ||
return requestResponse instanceof Error | ||
? Promise.reject(requestResponse) | ||
: Promise.resolve({ | ||
ok: true, | ||
staus: 200, | ||
json() { | ||
return Promise.resolve(requestResponse); | ||
}, | ||
}); | ||
}) | ||
.onSecondCall() | ||
.callsFake(() => { | ||
return awaitResponse instanceof Error | ||
? Promise.reject(awaitResponse) | ||
: Promise.resolve({ | ||
ok: true, | ||
staus: 200, | ||
json() { | ||
return Promise.resolve(awaitResponse); | ||
}, | ||
}); | ||
}); | ||
} | ||
|
||
function getRequestBodyFromFnCall(call: Sinon.SinonSpyCall<any, any>) { | ||
return JSON.parse(call.args[1].body); | ||
} | ||
|
||
for (const [ | ||
successSpecName, | ||
requestResponse, | ||
awaitResponse, | ||
] of successSpecs) { | ||
it(successSpecName, async function () { | ||
const fetchFn = getMockFetch(requestResponse, awaitResponse); | ||
const res = await makeAutomationAgentOpRequest( | ||
fetchFn, | ||
'http://example.com', | ||
'abc', | ||
'listIndexStats', | ||
{ clusterId: 'abc', db: 'db', collection: 'coll' } | ||
); | ||
expect(getRequestBodyFromFnCall(fetchFn.firstCall)).to.deep.eq({ | ||
clusterId: 'abc', | ||
collection: 'coll', | ||
db: 'db', | ||
}); | ||
expect(res).to.deep.eq(awaitResponse.response); | ||
}); | ||
} | ||
|
||
for (const [ | ||
failSpecName, | ||
requestResponse, | ||
awaitResponse, | ||
errorMessage, | ||
] of failSpecs) { | ||
it(failSpecName, async function () { | ||
try { | ||
await makeAutomationAgentOpRequest( | ||
getMockFetch(requestResponse, awaitResponse), | ||
'http://example.com', | ||
'abc', | ||
'listIndexStats', | ||
{ clusterId: 'abc', db: 'db', collection: 'coll' } | ||
); | ||
expect.fail('Expected makeAutomationAgentOpRequest call to fail'); | ||
} catch (err) { | ||
expect((err as any).message).to.match(errorMessage); | ||
} | ||
}); | ||
} | ||
}); |
Oops, something went wrong.