oncall-engine/grafana-plugin/e2e-tests/globalSetup.ts
Dominik Broj 344cd0efde
Add missing labels permissions, fix tilt ci from ops-devenv, fix expensive e2e tests (#4842)
# What this PR does

- add missing labels-related permissions for external service account
used by new oncall init process
- fix expensive e2e tests in new oncall init process
- unify Grafana versions between standard and expensive e2e tests
- fix running tilt through ops-devenv in new oncall init process
- avoid duplicated standard e2e tests on workflows that run daily and on
merges to main

## Which issue(s) this PR closes

Related to https://github.com/grafana/oncall-private/issues/2656

<!--
*Note*: If you want the issue to be auto-closed once the PR is merged,
change "Related to" to "Closes" in the line above.
If you have more than one GitHub issue that this PR closes, be sure to
preface
each issue link with a [closing
keyword](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests#linking-a-pull-request-to-an-issue).
This ensures that the issue(s) are auto-closed once the PR has been
merged.
-->

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.

---------

Co-authored-by: Joey Orlando <joseph.t.orlando@gmail.com>
2024-08-19 18:17:10 +00:00

136 lines
4.3 KiB
TypeScript

import {
test as setup,
chromium,
type BrowserContext,
type FullConfig,
type APIRequestContext,
Page,
} from '@playwright/test';
import { VIEWER_USER_STORAGE_STATE, EDITOR_USER_STORAGE_STATE, ADMIN_USER_STORAGE_STATE } from '../playwright.config';
import grafanaApiClient from './utils/clients/grafana';
import {
GRAFANA_ADMIN_PASSWORD,
GRAFANA_ADMIN_USERNAME,
GRAFANA_EDITOR_PASSWORD,
GRAFANA_EDITOR_USERNAME,
GRAFANA_VIEWER_PASSWORD,
GRAFANA_VIEWER_USERNAME,
IS_CLOUD,
IS_OPEN_SOURCE,
OrgRole,
isGrafanaVersionLowerThan,
} from './utils/constants';
import { goToOnCallPage } from './utils/navigation';
type UserCreationSettings = {
adminAuthedRequest: APIRequestContext;
role: OrgRole;
};
const generateLoginStorageStateAndOptionallCreateUser = async (
config: FullConfig,
userName: string,
password: string,
storageStateFileLocation: string,
userCreationSettings?: UserCreationSettings,
closeContext = false
): Promise<BrowserContext> => {
if (userCreationSettings !== undefined && IS_OPEN_SOURCE) {
const { adminAuthedRequest, role } = userCreationSettings;
await grafanaApiClient.idempotentlyCreateUserWithRole(adminAuthedRequest, userName, password, role);
}
const { headless } = config.projects[0]!.use;
const browser = await chromium.launch({ headless, slowMo: headless ? 0 : 100 });
const browserContext = await browser.newContext();
await grafanaApiClient.login(browserContext.request, userName, password);
await browserContext.storageState({ path: storageStateFileLocation });
if (closeContext) {
await browserContext.close();
}
return browserContext;
};
const idempotentlyInitializePlugin = async (page: Page) => {
await goToOnCallPage(page, 'alert-groups');
await page.waitForTimeout(1000);
const openPluginConfigurationButton = page.getByRole('button', { name: 'Open configuration' });
if (await openPluginConfigurationButton.isVisible()) {
await openPluginConfigurationButton.click();
// Before 10.3 Admin user needs to create service account manually
if (isGrafanaVersionLowerThan('10.3.0')) {
await page.getByTestId('recreate-service-account').click();
}
await page.getByTestId('connect-plugin').click();
await page.waitForLoadState('networkidle');
await page.getByText('Plugin is connected').waitFor();
}
};
const determineGrafanaVersion = async (adminAuthedRequest: APIRequestContext) => {
/**
* determine the current Grafana version of the stack in question and set it such that it can be used in the tests
* to conditionally skip certain tests.
*
* According to the Playwright docs, the best way to set config like this on the fly, is to set values
* on process.env https://playwright.dev/docs/test-global-setup-teardown#example
*
* TODO: when this bug is fixed in playwright https://github.com/microsoft/playwright/issues/29608
* move this to the currentGrafanaVersion fixture
*/
const currentGrafanaVersion = await grafanaApiClient.getGrafanaVersion(adminAuthedRequest);
process.env.CURRENT_GRAFANA_VERSION = currentGrafanaVersion;
};
/**
* Borrowed from our friends on the Incident team
* https://github.com/grafana/incident/blob/main/plugin/e2e/global-setup.ts
*/
setup('Configure Grafana OnCall plugin', async ({ request }, { config }) => {
if (IS_CLOUD) {
await grafanaApiClient.pollInstanceUntilItIsHealthy(request);
}
const adminBrowserContext = await generateLoginStorageStateAndOptionallCreateUser(
config,
GRAFANA_ADMIN_USERNAME,
GRAFANA_ADMIN_PASSWORD,
ADMIN_USER_STORAGE_STATE
);
const adminPage = await adminBrowserContext.newPage();
const { request: adminAuthedRequest } = adminBrowserContext;
await determineGrafanaVersion(adminAuthedRequest);
await idempotentlyInitializePlugin(adminPage);
await generateLoginStorageStateAndOptionallCreateUser(
config,
GRAFANA_EDITOR_USERNAME,
GRAFANA_EDITOR_PASSWORD,
EDITOR_USER_STORAGE_STATE,
{
adminAuthedRequest,
role: OrgRole.Editor,
},
true
);
await generateLoginStorageStateAndOptionallCreateUser(
config,
GRAFANA_VIEWER_USERNAME,
GRAFANA_VIEWER_PASSWORD,
VIEWER_USER_STORAGE_STATE,
{
adminAuthedRequest,
role: OrgRole.Viewer,
},
true
);
await adminBrowserContext.close();
});