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 9 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
48 changes: 28 additions & 20 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions tests/e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"lint:fix": "eslint . --fix && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -w --log-level error"
},
"devDependencies": {
"@types/lodash": "^4.17.7",
shibeshduw marked this conversation as resolved.
Show resolved Hide resolved
"dotenv": "^16.4.5"
}
}
139 changes: 130 additions & 9 deletions tests/e2e-tests/src/ai-api.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';

// 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', () => {
shibeshduw marked this conversation as resolved.
Show resolved Hide resolved
describe('DeploymentAPI', () => {
shibeshduw marked this conversation as resolved.
Show resolved Hide resolved
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' }
shibeshduw marked this conversation as resolved.
Show resolved Hide resolved
).execute();
if (deploymentDetail.status === targetStatus) {
return deploymentDetail;
}
await new Promise(resolve => setTimeout(resolve, RETRY_INTERVAL));
shibeshduw marked this conversation as resolved.
Show resolved Hide resolved
}
throw new Error(
`Deployment ${deploymentId} did not reach ${targetStatus} status within the expected time. ` +
'Please manually stop and delete the deployment.'
shibeshduw marked this conversation as resolved.
Show resolved Hide resolved
);
}

const sanitizedState = (state: AiDeploymentList | undefined) => ({
...state,
resources: state?.resources.map(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
({ modifiedAt, ...rest }) => rest
)
});

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' }
shibeshduw marked this conversation as resolved.
Show resolved Hide resolved
).execute();

expect(createResponse).toBeDefined();
expect(createResponse.message).toBe('Deployment scheduled.');
expect(createResponse.id).toBeDefined();
shibeshduw marked this conversation as resolved.
Show resolved Hide resolved

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pp] I think it would be better if we could have a way to run the tests independently. In case something is not working, I would always have to run all tests in order, so local testing is a bit difficult: I couldn't run one test at a time...

My thoughts:

  1. I expect that we have no running deployments before we run the tests. This means when I would like to run the modification or deletion test, I couldn't run this test individually.
    A fix could be that I have to run the creation manually before (e.g. running the creation test) and then to run modification individually, query the deployment instead of keeping a global state.
    This is still inconvenient, so the tests could try to retrieve the global state, if not set query the deployment, if not there create it as needed.

  2. Another thought is - if tests fail or are cancelled before they are finished, it is possible that we accumulate more and more deployments - I guess we should consider some kind of a clean up step.

I guess 1. could be a bit overkill or could be done later. I think 2. could be useful.
Thoughts @deekshas8 @MatKuhr @ZhongpinWang @KavithaSiva @tomfrenken

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could use jest's pattern matching to run individual tests but also keep this flow? Plus we also add a cleanup step as you mentioned.


const modifyResponse = await DeploymentApi.deploymentModify(
createdDeploymentId!,
shibeshduw marked this conversation as resolved.
Show resolved Hide resolved
{ 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();

await new Promise(r => setTimeout(r, 20000));
shibeshduw marked this conversation as resolved.
Show resolved Hide resolved
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', () => {
shibeshduw marked this conversation as resolved.
Show resolved Hide resolved
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();
});
});
});