forked from redhat-developer/rhdh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify-schema-mode.spec.ts
More file actions
201 lines (166 loc) · 6.6 KB
/
Copy pathverify-schema-mode.spec.ts
File metadata and controls
201 lines (166 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* E2E test for pluginDivisionMode: schema
*
* Verifies that RHDH can operate with schema-mode enabled when the database user
* has restricted permissions (NOCREATEDB), matching production managed database environments.
*
* Tests are opt-in - they skip when SCHEMA_MODE_* environment variables are not set.
*/
import { ChildProcessWithoutNullStreams, spawn } from "child_process";
import { test, expect } from "@support/coverage/test";
import { Common } from "../../utils/common";
import { getReleaseName, resolveInstallMethod } from "../../utils/helper";
import { KubeClient } from "../../utils/kube-client";
import { ensureRuntimeDeployed } from "../../utils/runtime-deploy";
import { setPortForwardRestarter } from "./schema-mode-db";
import { SchemaModeTestSetup } from "./schema-mode-setup";
function streamDataToString(data: Buffer | string): string {
return typeof data === "string" ? data : data.toString();
}
function startPortForward(
pfNamespace: string,
pfResource: string,
): Promise<ChildProcessWithoutNullStreams> {
return new Promise<ChildProcessWithoutNullStreams>((resolve, reject) => {
const proc = spawn("oc", ["port-forward", "-n", pfNamespace, pfResource, "5432:5432"]);
const timeout = setTimeout(() => {
proc.kill("SIGTERM");
reject(new Error("Port-forward timeout after 30 seconds"));
}, 30000);
let ready = false;
proc.stdout.on("data", (data: Buffer | string) => {
if (ready) return;
if (streamDataToString(data).includes("Forwarding from")) {
ready = true;
clearTimeout(timeout);
resolve(proc);
}
});
proc.stderr.on("data", (data: Buffer | string) => {
const msg = streamDataToString(data).trim();
if (msg) console.error(`Port-forward stderr: ${msg}`);
});
proc.on("error", (err) => {
clearTimeout(timeout);
reject(err);
});
});
}
function killPortForward(proc: ChildProcessWithoutNullStreams | undefined): Promise<void> {
if (!proc || proc.exitCode !== null) return Promise.resolve();
return new Promise<void>((resolve) => {
proc.once("close", () => {
resolve();
});
proc.kill("SIGTERM");
setTimeout(() => {
if (proc.exitCode === null) {
try {
proc.kill("SIGKILL");
} catch {
// already dead
}
}
}, 5000);
});
}
test.describe("Verify pluginDivisionMode: schema", () => {
const namespace = process.env.NAME_SPACE_RUNTIME ?? "showcase-runtime";
const releaseName = getReleaseName();
const installMethod = resolveInstallMethod();
let portForwardProcess: ChildProcessWithoutNullStreams | undefined;
let testSetup: SchemaModeTestSetup;
test.beforeAll(async ({}, testInfo) => {
test.setTimeout(900000);
// Ensure the runtime RHDH instance is deployed (idempotent — no-op if already running).
// Also sets SCHEMA_MODE_* env vars via configureSchemaMode().
await ensureRuntimeDeployed();
const pfNamespace = process.env.SCHEMA_MODE_PORT_FORWARD_NAMESPACE;
const pfResource = process.env.SCHEMA_MODE_PORT_FORWARD_RESOURCE;
const dbHost = process.env.SCHEMA_MODE_DB_HOST;
const adminPassword = process.env.SCHEMA_MODE_DB_ADMIN_PASSWORD;
const dbPassword = process.env.SCHEMA_MODE_DB_PASSWORD;
const hasPortForwardMeta =
pfNamespace !== undefined &&
pfNamespace !== "" &&
pfResource !== undefined &&
pfResource !== "";
const hasDirectHost = dbHost !== undefined && dbHost !== "";
if (
adminPassword === undefined ||
adminPassword === "" ||
dbPassword === undefined ||
dbPassword === "" ||
(!hasPortForwardMeta && !hasDirectHost)
) {
testInfo.skip(
true,
"SCHEMA_MODE_* environment variables not set - schema mode tests are opt-in",
);
return;
}
testInfo.annotations.push(
{ type: "component", description: "data-management" },
{ type: "namespace", description: namespace },
);
if (hasPortForwardMeta) {
console.log(`Starting port-forward: ${pfResource} in ${pfNamespace} -> localhost:5432`);
portForwardProcess = await startPortForward(pfNamespace, pfResource);
console.log("Port-forward established");
process.env.SCHEMA_MODE_DB_HOST = "localhost";
setPortForwardRestarter(async () => {
await killPortForward(portForwardProcess);
console.log("Restarting port-forward...");
portForwardProcess = await startPortForward(pfNamespace, pfResource);
console.log("Port-forward re-established");
});
}
testSetup = new SchemaModeTestSetup(namespace, releaseName, installMethod);
try {
await testSetup.setupDatabase();
await testSetup.configureRHDH();
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
testInfo.skip(true, `Schema mode setup failed: ${errorMsg}`);
}
});
test.afterAll(async () => {
setPortForwardRestarter(null);
await killPortForward(portForwardProcess);
});
// RHDH's frontend opens an SSE (EventSource) connection for live
// updates. With tracing enabled, Playwright's fixture teardown hangs
// waiting for network idle, which never resolves while SSE stays open
// (microsoft/playwright#41513, fixed in v1.62). Navigating away drops
// the connection so teardown completes immediately.
// Requesting `page` creates a context for every test, including non-UI
// ones — acceptable overhead vs per-test conditional logic.
test.afterEach(async ({ page }) => {
await page.goto("about:blank").catch(() => {});
});
test("Verify database user has restricted permissions", async () => {
const hasRestrictedPerms = await testSetup.verifyRestrictedDatabasePermissions();
expect(hasRestrictedPerms).toBe(true);
});
test("Verify RHDH is accessible with schema mode", async ({ page }, testInfo) => {
const kubeClient = new KubeClient();
const deploymentName = testSetup.getDeploymentName();
try {
const deployment = await kubeClient.appsApi.readNamespacedDeployment(
deploymentName,
namespace,
);
const readyReplicas = deployment.body.status?.readyReplicas ?? 0;
if (readyReplicas < 1) {
testInfo.skip(true, "Deployment is not ready (cluster capacity or PVC issue)");
return;
}
} catch (error) {
console.warn("Could not check deployment readiness:", error);
}
const common = new Common(page);
await common.loginAsGuest();
await expect(page.getByRole("heading", { level: 1 })).toBeVisible();
console.log("RHDH is accessible - plugins successfully created schemas in schema mode");
});
});