Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add AI Core e2e tests #128

Merged
merged 19 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion tests/e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"lint:fix": "eslint . --fix && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -w --log-level error"
},
"devDependencies": {
"dotenv": "^16.4.5"
"@types/lodash": "^4.17.7",
shibeshduw marked this conversation as resolved.
Show resolved Hide resolved
"dotenv": "^16.4.5",
"lodash": "^4.17.21"
}
}
139 changes: 130 additions & 9 deletions tests/e2e-tests/src/ai-core.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,141 @@
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
import { DeploymentApi } from '@sap-ai-sdk/ai-api';
import 'dotenv/config';
import {
AiDeployment,
AiDeploymentList,
DeploymentApi,
ScenarioApi
} from '@sap-ai-sdk/ai-api';
import _ from 'lodash';

// Pick .env file from root directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
dotenv.config({ path: path.resolve(__dirname, '../.env') });

describe('ai-api', () => {
it('should get deployments', async () => {
const deployments = await DeploymentApi.deploymentQuery(
{},
{ 'AI-Resource-Group': 'default' }
).execute();
expect(deployments).toBeDefined();
describe('AI Core APIs', () => {
describe('DeploymentAPI', () => {
const MAX_RETRIES = 30;
const RETRY_INTERVAL = 5000;
let createdDeploymentId: string | undefined;
let initialState: AiDeploymentList | undefined;

async function waitForDeploymentToReachStatus(
deploymentId: string,
targetStatus: 'RUNNING' | 'STOPPED'
): Promise<AiDeployment> {
for (let i = 0; i < MAX_RETRIES; i++) {
const deploymentDetail = await DeploymentApi.deploymentGet(
deploymentId,
{},
{ 'AI-Resource-Group': 'i745181' }
).execute();
if (deploymentDetail.status === targetStatus) {
return deploymentDetail;
}
await new Promise(resolve => setTimeout(resolve, RETRY_INTERVAL));
}
throw new Error(
`Deployment ${deploymentId} did not reach ${targetStatus} status within the expected time. ` +
'Please manually stop and delete the deployment.'
);
}

const sanitizedState = (state: AiDeploymentList | undefined) => ({
MatKuhr marked this conversation as resolved.
Show resolved Hide resolved
...state,
resources: state?.resources.map(resource =>
_.omit(resource, ['modifiedAt'])
shibeshduw marked this conversation as resolved.
Show resolved Hide resolved
)
});

beforeAll(async () => {
const queryResponse = await DeploymentApi.deploymentQuery(
{},
{ 'AI-Resource-Group': 'i745181' }
).execute();
expect(queryResponse).toBeDefined();
initialState = queryResponse;
});

it('should create a deployment and wait for it to run', async () => {
const createResponse = await DeploymentApi.deploymentCreate(
{ configurationId: '8e2ff27f-d65e-48c4-9b21-1bb2709abfd1' },
{ 'AI-Resource-Group': 'i745181' }
).execute();

expect(createResponse).toBeDefined();
expect(createResponse.message).toBe('Deployment scheduled.');
expect(createResponse.id).toBeDefined();

const runningDeployment = await waitForDeploymentToReachStatus(
createResponse.id,
'RUNNING'
);
expect(runningDeployment.status).toBe('RUNNING');
expect(runningDeployment.deploymentUrl).toBeTruthy();
createdDeploymentId = runningDeployment.id;
}, 180000);

it('should modify the deployment to stop it', async () => {
expect(createdDeploymentId).toBeDefined();

const modifyResponse = await DeploymentApi.deploymentModify(
createdDeploymentId!,
{ targetStatus: 'STOPPED' },
{ 'AI-Resource-Group': 'i745181' }
).execute();

expect(modifyResponse).toBeDefined();
expect(modifyResponse.message).toBe('Deployment modification scheduled');

const stoppedDeployment = await waitForDeploymentToReachStatus(
createdDeploymentId!,
'STOPPED'
);

expect(stoppedDeployment.status).toBe('STOPPED');
}, 180000);

it('should delete the deployment', async () => {
expect(createdDeploymentId).toBeDefined();

const deleteResponse = await DeploymentApi.deploymentDelete(
createdDeploymentId!,
{ 'AI-Resource-Group': 'i745181' }
).execute();

expect(deleteResponse).toBeDefined();
expect(deleteResponse.message).toBe('Deletion scheduled');
});

afterAll(async () => {
expect(createdDeploymentId).toBeDefined();
MatKuhr marked this conversation as resolved.
Show resolved Hide resolved

await new Promise(r => setTimeout(r, 20000));
const queryResponse = await DeploymentApi.deploymentQuery(
{},
{ 'AI-Resource-Group': 'i745181' }
).execute();
expect(queryResponse).toBeDefined();

const sanitizedInitialState = sanitizedState(initialState);
const sanitizedEndState = sanitizedState(queryResponse);
expect(sanitizedEndState).toStrictEqual(sanitizedInitialState);
}, 30000);
});

describe('ScenarioAPI', () => {
it('should get list of available scenarios', async () => {
const scenarios = await ScenarioApi.scenarioQuery({
'AI-Resource-Group': 'default'
}).execute();

expect(scenarios).toBeDefined();
const foundationModel = scenarios.resources.find(
scenario => scenario.id === 'foundation-models'
);
expect(foundationModel).toBeDefined();
});
});
});
Loading