Skip to content

Commit

Permalink
test: e2e test for parsing config.toml and modifying (#2705)
Browse files Browse the repository at this point in the history
### TL;DR

Updated e2e tests for config.toml and added TOML parsing functionality.

### What changed?

- Removed `config-test.toml` file
- Updated `config.test.ts` to include new tests for blocklist and showNonInstalledImages
- Modified `test-util.ts` to include a new `modifyConfigToml` function
- Added `@iarna/toml` dependency to `package.json` and `pnpm-lock.yaml`

The toml parser currently used by webui has a lot of [issues](#2382), so I used a different library, [`@iarna/toml`](https://github.com/iarna/iarna-toml).

### How to test?

1. Run the e2e tests for the config.toml file:
   - Test the blocklist functionality by checking if specified menu items are hidden and their pages are inaccessible
   - Test the showNonInstalledImages setting by verifying the visibility of non-installed images

2. Verify that the `modifyConfigToml` function works correctly by running the tests and checking if the config.toml file is modified as expected

### Why make this change?

This change improves the e2e testing capabilities for the config.toml file, allowing for more dynamic and comprehensive testing of configuration options. The addition of TOML parsing functionality enables easier manipulation of the config file during tests, providing a more flexible and maintainable testing approach.

---

<!--
Please precisely, concisely, and concretely describe what this PR changes, the rationale behind codes,
and how it affects the users and other developers.
-->

**Checklist:** (if applicable)

- [ ] Mention to the original issue
- [ ] Documentation
- [ ] Minium required manager version
- [ ] Specific setting for review (eg., KB link, endpoint or how to setup)
- [ ] Minimum requirements to check during review
- [ ] Test case(s) to demonstrate the difference of before/after
  • Loading branch information
ironAiken2 committed Sep 20, 2024
1 parent be6db95 commit 406b5ac
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 170 deletions.
79 changes: 0 additions & 79 deletions e2e/config-test.toml

This file was deleted.

255 changes: 171 additions & 84 deletions e2e/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,174 @@
import { loginAsAdmin, mockConfigToml, webuiEndpoint } from './test-util';
import { loginAsAdmin, modifyConfigToml, webuiEndpoint } from './test-util';
import { test, expect } from '@playwright/test';

test.describe('config read and test', () => {
test('showNonInstalledImages', async ({ page, context }) => {
// manipulate config file from mocked one
await mockConfigToml(page, './config-test.toml');

await loginAsAdmin(page);

await page.getByRole('group').getByText('Environments').click();
await page
.getByRole('columnheader', { name: 'Status' })
.locator('div')
.click();
await page
.getByRole('columnheader', { name: 'Status' })
.locator('div')
.click();

const registryCell = await page
.locator('.ant-table-row > td:nth-child(3)')
.first();
const registry = await registryCell.textContent();

const architectureCell = await page
.locator('.ant-table-row > td:nth-child(4)')
.first();
const architecture = await architectureCell.textContent();

const namespaceCell = await page
.getByRole('cell', { name: 'community' })
.first();
const namespace = await namespaceCell.textContent();

const languageCell = await page.getByRole('cell', { name: 'afni' }).first();
const language = await languageCell.textContent();

const versionCell = await page
.getByRole('cell', { name: 'ubuntu18.04' })
.first();
const version = await versionCell.textContent();

const uninstalledImageString = `${registry}/${namespace}/${language}:${version}@${architecture}`;

await page.goto(webuiEndpoint);
await page.getByLabel('power_settings_new').click();
await page
.getByRole('button', { name: '2 Environments & Resource' })
.click();
await page
.locator(
'.ant-form-item-control-input-content > .ant-select > .ant-select-selector',
)
.first()
.click();
await page.getByLabel('Environments / Version').fill('AF');
await page
.locator('.rc-virtual-list-holder-inner > div:nth-child(2)')
.click();

await page
.locator('span')
.filter({ hasText: 'ubuntu18.04x86_64' })
.locator('div')
.first()
.click();
await page
.locator(
'div:nth-child(4) > .rc-virtual-list-holder > div > .rc-virtual-list-holder-inner > .ant-select-item > .ant-select-item-option-content > div',
)
.click();
await page
.getByRole('button', { name: 'Skip to review double-right' })
.click();
await page.getByRole('button', { name: 'Copy' }).click();

await context.grantPermissions(['clipboard-read', 'clipboard-write']);

const handle = await page.evaluateHandle(() =>
navigator.clipboard.readText(),
);
const clipboardContent = await handle.jsonValue();

expect(clipboardContent).toEqual(uninstalledImageString);
});
test.describe.parallel('config.toml', () => {
test(
'enableLLMPlayground',
{ tag: ['@serving'] },
async ({ page, request }) => {
// modify config.toml to enable LLM playground
let requestConfig = {
general: {
enableLLMPlayground: true,
},
};
await modifyConfigToml(page, request, requestConfig);
await loginAsAdmin(page);
await page.getByRole('menuitem', { name: 'Serving' }).click();
await expect(
page.getByRole('tab', { name: 'LLM Playground' }),
).toBeVisible();

requestConfig.general.enableLLMPlayground = false;
await modifyConfigToml(page, request, requestConfig);
await page.reload();
await page.getByRole('menuitem', { name: 'Serving' }).click();
await expect(
page.getByRole('tab', { name: 'LLM Playground' }),
).toBeHidden();
},
);

test(
'block list',
{ tag: ['@session', '@summary', '@serving'] },
async ({ page, request }) => {
// modify config.toml to blocklist some menu items
let requestConfig = {
menu: {
blocklist: 'summary, serving, job',
},
};
await modifyConfigToml(page, request, requestConfig);
await loginAsAdmin(page);

// check if the menu items are hidden
await expect(
page.getByRole('menuitem', { name: 'Summary' }),
).toBeHidden();
await expect(
page.getByRole('menuitem', { name: 'Sessions' }),
).toBeHidden();
await expect(
page.getByRole('menuitem', { name: 'Serving' }),
).toBeHidden();

// check if the pages are not accessible
await page.goto(`${webuiEndpoint}/summary`);
await expect(page).toHaveURL(/.*error/);
await page.goto(`${webuiEndpoint}/serving`);
await expect(page).toHaveURL(/.*error/);
await page.goto(`${webuiEndpoint}/job`);
await expect(page).toHaveURL(/.*error/);

requestConfig.menu.blocklist = '';
await modifyConfigToml(page, request, requestConfig);
await page.reload();

// check if the menu items are visible
await expect(
page.getByRole('menuitem', { name: 'Summary' }),
).toBeVisible();
await expect(
page.getByRole('menuitem', { name: 'Sessions' }),
).toBeVisible();
await expect(
page.getByRole('menuitem', { name: 'Serving' }),
).toBeVisible();
},
);

test(
'showNonInstalledImages',
{ tag: ['@session'] },
async ({ page, context, request }) => {
// modify config.toml to show non-installed images
const requestConfig = {
environments: {
showNonInstalledImages: true,
},
};
await modifyConfigToml(page, request, requestConfig);

await loginAsAdmin(page);

await page.getByRole('group').getByText('Environments').click();
await page
.getByRole('columnheader', { name: 'Status' })
.locator('div')
.click();
await page
.getByRole('columnheader', { name: 'Status' })
.locator('div')
.click();

const registryCell = await page
.locator('.ant-table-row > td:nth-child(3)')
.first();
const registry = await registryCell.textContent();

const architectureCell = await page
.locator('.ant-table-row > td:nth-child(4)')
.first();
const architecture = await architectureCell.textContent();

const namespaceCell = await page
.getByRole('cell', { name: 'community' })
.first();
const namespace = await namespaceCell.textContent();

const languageCell = await page
.getByRole('cell', { name: 'afni' })
.first();
const language = await languageCell.textContent();

const versionCell = await page
.getByRole('cell', { name: 'ubuntu18.04' })
.first();
const version = await versionCell.textContent();

const uninstalledImageString = `${registry}/${namespace}/${language}:${version}@${architecture}`;

await page.goto(webuiEndpoint);
await page.getByLabel('power_settings_new').click();
await page
.getByRole('button', { name: '2 Environments & Resource' })
.click();
await page
.locator(
'.ant-form-item-control-input-content > .ant-select > .ant-select-selector',
)
.first()
.click();
await page.getByLabel('Environments / Version').fill('AF');
await page
.locator('.rc-virtual-list-holder-inner > div:nth-child(2)')
.click();

await page
.locator('span')
.filter({ hasText: 'ubuntu18.04x86_64' })
.locator('div')
.first()
.click();
await page
.locator(
'div:nth-child(4) > .rc-virtual-list-holder > div > .rc-virtual-list-holder-inner > .ant-select-item > .ant-select-item-option-content > div',
)
.click();
await page
.getByRole('button', { name: 'Skip to review double-right' })
.click();
await page.getByRole('button', { name: 'Copy' }).click();

await context.grantPermissions(['clipboard-read', 'clipboard-write']);

const handle = await page.evaluateHandle(() =>
navigator.clipboard.readText(),
);
const clipboardContent = await handle.jsonValue();

expect(clipboardContent).toEqual(uninstalledImageString);
},
);
});
38 changes: 32 additions & 6 deletions e2e/test-util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Locator, Page, expect } from '@playwright/test';
import TOML from '@iarna/toml';
import {
APIRequestContext,
Locator,
Page,
Request,
expect,
request,
} from '@playwright/test';
import fs from 'fs/promises';
import path from 'path';

Expand Down Expand Up @@ -229,14 +237,32 @@ export async function deleteSession(page: Page, sessionName: string) {
await expect(page.getByText(sessionName)).toBeHidden();
}

export async function mockConfigToml(page: Page, rawPath) {
const filePath = path.resolve(__dirname, rawPath);
const mockData = await fs.readFile(filePath, 'utf-8');
await page.route('http://127.0.0.1:9081/config.toml', async (route) => {
/**
* Modify specific columns in the webui config.toml file
*
* @param page
* @param request
* @param configColumn
* The object to modify the config.toml file
*
* e.g. { "environments": { "showNonInstalledImages": "true" } }
*/
export async function modifyConfigToml(
page: Page,
request: APIRequestContext,
configColumn: Record<string, Record<string, any>>,
) {
const configToml = await (
await request.get(`${webuiEndpoint}/config.toml`)
).text();
const config = TOML.parse(configToml);
Object.assign(config, configColumn);

await page.route(`${webuiEndpoint}/config.toml`, async (route) => {
await route.fulfill({
status: 200,
contentType: 'text/plain',
body: mockData,
body: TOML.stringify(config),
});
});
}
Loading

0 comments on commit 406b5ac

Please sign in to comment.