-
Notifications
You must be signed in to change notification settings - Fork 983
/
Copy pathtests.ts
182 lines (166 loc) · 5.38 KB
/
tests.ts
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
import * as fs from "fs";
import * as path from "path";
import { expect } from "chai";
import * as cli from "../functions-deploy-tests/cli";
import { cases, Step } from "./cases";
import * as client from "../../src/dataconnect/client";
import { deleteDatabase } from "../../src/gcp/cloudsql/cloudsqladmin";
import { requireAuth } from "../../src/requireAuth";
const FIREBASE_PROJECT = process.env.FBTOOLS_TARGET_PROJECT || "";
const FIREBASE_DEBUG = process.env.FIREBASE_DEBUG || "";
function expected(
serviceId: string,
databaseId: string,
schemaUpdateTime: string,
connectorLastUpdated: string,
) {
return {
serviceId,
location: "us-central1",
datasource: `CloudSQL Instance: dataconnect-test\nDatabase: ${databaseId}`,
schemaUpdateTime,
connectors: [
{
connectorId: "1",
connectorLastUpdated,
},
],
};
}
async function cleanUpService(projectId: string, serviceId: string, databaseId: string) {
await client.deleteServiceAndChildResources(
`projects/${projectId}/locations/us-central1/services/${serviceId}`,
);
await deleteDatabase(projectId, "dataconnect-test", databaseId);
}
async function list() {
return await cli.exec(
"dataconnect:services:list",
FIREBASE_PROJECT,
["--json"],
__dirname,
/** quiet=*/ false,
{
FIREBASE_CLI_EXPERIMENTS: "dataconnect",
},
);
}
async function migrate(force: boolean) {
const args = force ? ["--force"] : [];
if (FIREBASE_DEBUG) {
args.push("--debug");
}
return await cli.exec(
"dataconnect:sql:migrate",
FIREBASE_PROJECT,
args,
__dirname,
/** quiet=*/ false,
{
FIREBASE_CLI_EXPERIMENTS: "dataconnect",
},
);
}
async function deploy(force: boolean) {
const args = ["--only", "dataconnect"];
if (force) {
args.push("--force");
}
if (FIREBASE_DEBUG) {
args.push("--debug");
}
return await cli.exec("deploy", FIREBASE_PROJECT, args, __dirname, /** quiet=*/ false, {
FIREBASE_CLI_EXPERIMENTS: "dataconnect",
});
}
function toPath(p: string) {
return path.join(__dirname, p);
}
function getRandomString(length: number): string {
const SUFFIX_CHAR_SET = "abcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
for (let i = 0; i < length; i++) {
result += SUFFIX_CHAR_SET.charAt(Math.floor(Math.random() * SUFFIX_CHAR_SET.length));
}
return result;
}
const fdcTest = toPath("fdc-test");
// Each test run should use a random serviceId and databaseId.
function newTestRun(): { serviceId: string; databaseId: string } {
const id = getRandomString(6);
const serviceId = `cli-e2e-service-${id}`;
const databaseId = `cli-e2e-database-${id}`;
const dataconnectYamlTemplate = fs.readFileSync(toPath("templates/dataconnect.yaml")).toString();
const connectorYamlTemplate = fs.readFileSync(toPath("templates/connector.yaml")).toString();
const subbedDataconnectYaml = dataconnectYamlTemplate
.replace("__serviceId__", serviceId)
.replace("__databaseId__", databaseId);
if (!fs.existsSync(fdcTest)) {
fs.mkdirSync(fdcTest);
}
if (!fs.existsSync(toPath("fdc-test/connector"))) {
fs.mkdirSync(toPath("fdc-test/connector"));
}
if (!fs.existsSync(toPath("fdc-test/schema"))) {
fs.mkdirSync(toPath("fdc-test/schema"));
}
fs.writeFileSync(toPath("fdc-test/dataconnect.yaml"), subbedDataconnectYaml, {
mode: 420 /* 0o644 */,
});
fs.writeFileSync(toPath("fdc-test/connector/connector.yaml"), connectorYamlTemplate, {
mode: 420 /* 0o644 */,
});
return { serviceId, databaseId };
}
function prepareStep(step: Step) {
fs.writeFileSync(toPath("fdc-test/schema/schema.gql"), step.schemaGQL, { mode: 420 /* 0o644 */ });
fs.writeFileSync(toPath("fdc-test/connector/connector.gql"), step.connectorGQL, {
mode: 420 /* 0o644 */,
});
}
describe("firebase deploy", () => {
let serviceId: string;
let databaseId: string;
beforeEach(async function (this) {
this.timeout(10000);
expect(FIREBASE_PROJECT).not.to.equal("", "No FBTOOLS_TARGET_PROJECT env var set.");
const info = newTestRun();
serviceId = info.serviceId;
databaseId = info.databaseId;
await requireAuth({});
});
afterEach(async function (this) {
this.timeout(100000); // Need to wait a long time for cleanup.
fs.rmSync(fdcTest, { recursive: true, force: true });
await cleanUpService(FIREBASE_PROJECT, serviceId, databaseId);
});
for (const c of cases) {
it(c.description, async () => {
for (const step of c.sequence) {
prepareStep(step);
try {
await deploy(false);
await migrate(true);
await deploy(true);
} catch (err: any) {
expect(err.expectErr, `Unexpected error: ${err.message}`).to.be.true;
}
expect(step.expectErr).to.be.false;
const result = await list();
const out = JSON.parse(result.stdout);
expect(out?.status).to.equal("success");
expect(out?.result?.services?.length).to.gte(1);
const service = out.result.services.find((s: any) => s.serviceId === serviceId);
// Don't need to check update times.
expect(service).to.deep.equal(
expected(
serviceId,
databaseId,
service["schemaUpdateTime"],
service["connectors"]?.[0]?.["connectorLastUpdated"],
),
);
}
}).timeout(2000000); // Insanely long timeout in case of cSQL deploy. Should almost never be hit.
}
});