generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* init * Added e2e tests * increase timeout * changes to error msg * use native approach * remove unused deps * small changes * create utils * separate tests * update libs * fix lint issues * whoops
- Loading branch information
1 parent
3467ec7
commit 0924084
Showing
9 changed files
with
206 additions
and
34 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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import retry from 'async-retry'; | ||
import { | ||
AiDeployment, | ||
AiDeploymentList, | ||
DeploymentApi | ||
} from '@sap-ai-sdk/ai-api'; | ||
import { loadEnv } from './utils/load-env.js'; | ||
import { resourceGroup } from './utils/ai-api-utils.js'; | ||
|
||
loadEnv(); | ||
|
||
describe('DeploymentApi', () => { | ||
let createdDeploymentId: string | undefined; | ||
let initialState: AiDeploymentList | undefined; | ||
|
||
beforeAll(async () => { | ||
const queryResponse = await DeploymentApi.deploymentQuery( | ||
{}, | ||
{ 'AI-Resource-Group': resourceGroup } | ||
).execute(); | ||
expect(queryResponse).toBeDefined(); | ||
initialState = queryResponse; | ||
}); | ||
|
||
it('should create a deployment and wait for it to run', async () => { | ||
const createResponse = await DeploymentApi.deploymentCreate( | ||
{ configurationId: '54cc966d-8bc1-44ab-a9dc-658d59ef205d' }, | ||
{ 'AI-Resource-Group': resourceGroup } | ||
).execute(); | ||
|
||
expect(createResponse).toEqual( | ||
expect.objectContaining({ | ||
message: 'Deployment scheduled.', | ||
id: expect.anything() | ||
}) | ||
); | ||
|
||
const runningDeployment = await waitForDeploymentToReachStatus( | ||
createResponse.id, | ||
'RUNNING' | ||
); | ||
|
||
expect(runningDeployment).toEqual( | ||
expect.objectContaining({ | ||
status: 'RUNNING', | ||
deploymentUrl: expect.any(String) | ||
}) | ||
); | ||
|
||
createdDeploymentId = runningDeployment.id; | ||
}, 180000); | ||
|
||
it('should modify the deployment to stop it', async () => { | ||
const deploymentId = getDeploymentId(createdDeploymentId); | ||
|
||
const modifyResponse = await DeploymentApi.deploymentModify( | ||
deploymentId, | ||
{ targetStatus: 'STOPPED' }, | ||
{ 'AI-Resource-Group': resourceGroup } | ||
).execute(); | ||
|
||
expect(modifyResponse).toEqual( | ||
expect.objectContaining({ | ||
message: 'Deployment modification scheduled' | ||
}) | ||
); | ||
|
||
const stoppedDeployment = await waitForDeploymentToReachStatus( | ||
deploymentId, | ||
'STOPPED' | ||
); | ||
|
||
expect(stoppedDeployment).toEqual( | ||
expect.objectContaining({ | ||
status: 'STOPPED' | ||
}) | ||
); | ||
}, 180000); | ||
|
||
it('should delete the deployment', async () => { | ||
const deploymentId = getDeploymentId(createdDeploymentId); | ||
const deleteResponse = await DeploymentApi.deploymentDelete(deploymentId, { | ||
'AI-Resource-Group': resourceGroup | ||
}).execute(); | ||
|
||
expect(deleteResponse).toEqual( | ||
expect.objectContaining({ | ||
message: 'Deletion scheduled' | ||
}) | ||
); | ||
}); | ||
|
||
afterAll(async () => { | ||
getDeploymentId(createdDeploymentId); | ||
// Wait for deletion to complete | ||
await new Promise(r => setTimeout(r, 15000)); | ||
const queryResponse = await DeploymentApi.deploymentQuery( | ||
{}, | ||
{ 'AI-Resource-Group': resourceGroup } | ||
).execute(); | ||
expect(queryResponse).toBeDefined(); | ||
|
||
const sanitizedInitialState = sanitizedState(initialState); | ||
const sanitizedEndState = sanitizedState(queryResponse); | ||
expect(sanitizedEndState).toStrictEqual(sanitizedInitialState); | ||
}, 30000); | ||
}); | ||
|
||
const sanitizedState = (state: AiDeploymentList | undefined) => ({ | ||
...state, | ||
resources: state?.resources.map( | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
({ modifiedAt, ...rest }) => rest | ||
) | ||
}); | ||
|
||
function getDeploymentId(id: string | undefined): string { | ||
if (id === undefined) { | ||
throw new Error('deploymentId is not defined.'); | ||
} | ||
return id; | ||
} | ||
|
||
async function waitForDeploymentToReachStatus( | ||
deploymentId: string, | ||
targetStatus: 'RUNNING' | 'STOPPED' | ||
): Promise<AiDeployment> { | ||
return retry( | ||
async () => { | ||
const deploymentDetail = await DeploymentApi.deploymentGet( | ||
deploymentId, | ||
{}, | ||
{ 'AI-Resource-Group': resourceGroup } | ||
).execute(); | ||
|
||
if (deploymentDetail.status === targetStatus) { | ||
return deploymentDetail; | ||
} | ||
throw new Error(`Deployment has not yet reached ${targetStatus} status.`); | ||
}, | ||
{ | ||
retries: 30, | ||
minTimeout: 5000 | ||
} | ||
); | ||
} |
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
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,19 @@ | ||
import { ScenarioApi } from '@sap-ai-sdk/ai-api'; | ||
import { loadEnv } from './utils/load-env.js'; | ||
import { resourceGroup } from './utils/ai-api-utils.js'; | ||
|
||
loadEnv(); | ||
|
||
describe('ScenarioApi', () => { | ||
it('should get list of available scenarios', async () => { | ||
const scenarios = await ScenarioApi.scenarioQuery({ | ||
'AI-Resource-Group': resourceGroup | ||
}).execute(); | ||
|
||
expect(scenarios).toBeDefined(); | ||
const foundationModel = scenarios.resources.find( | ||
scenario => scenario.id === 'foundation-models' | ||
); | ||
expect(foundationModel).toBeDefined(); | ||
}); | ||
}); |
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,4 @@ | ||
/** | ||
* @internal | ||
*/ | ||
export const resourceGroup = 'ai-sdk-js-e2e'; |
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,13 @@ | ||
import path from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import dotenv from 'dotenv'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const loadEnv = (): void => { | ||
// Pick .env file from e2e root directory | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
dotenv.config({ path: path.resolve(__dirname, '../../.env') }); | ||
}; |