Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
275 changes: 273 additions & 2 deletions src/extensions/export.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import * as sinon from "sinon";

import {
functionsEnvFromInstance,
memoryToMb,
parameterizeProject,
parseMemory,
resolveMigratedMemory,
setSecretParamsToLatest,
ejectSecretsFromInstance,
} from "./export";
import { DeploymentInstanceSpec } from "../deploy/extensions/planner";
import { ExtensionInstance, ParamType } from "./types";
import * as secretsModule from "../deploy/extensions/secrets";
import { FirebaseError } from "../error";
import { MemoryOption } from "firebase-functions/v2/options";

describe("ext:export helpers", () => {
describe("parameterizeProject", () => {
Expand Down Expand Up @@ -125,6 +129,124 @@ describe("ext:export helpers", () => {
});
}
});

describe("memoryToMb", () => {
const testCases: { input?: string; expected: number }[] = [
{ input: "256", expected: 256 },
{ input: "512", expected: 512 },
{ input: "1024", expected: 1024 },
{ input: "256Mi", expected: 256 },
{ input: "512Mi", expected: 512 },
{ input: "512MiB", expected: 512 },
{ input: "1Gi", expected: 1024 },
{ input: "1GiB", expected: 1024 },
{ input: "2Gi", expected: 2048 },
{ input: "2GiB", expected: 2048 },
{ input: "1G", expected: 1024 },
{ input: "1GB", expected: 1024 },
{ input: "0.5Gi", expected: 512 },
{ input: "-256", expected: 0 },
{ input: "invalid", expected: 0 },
{ input: "", expected: 0 },
{ input: " ", expected: 0 },
{ input: undefined, expected: 0 },
];

for (const { input, expected } of testCases) {
it(`should parse "${String(input)}" to ${expected} MB`, () => {
expect(memoryToMb(input)).to.equal(expected);
});
}
});

describe("parseMemory", () => {
const testCases: { input?: string; expected?: MemoryOption }[] = [
{ input: "256", expected: "256MiB" },
{ input: "512", expected: "512MiB" },
{ input: "1024", expected: "1GiB" },
{ input: "2048", expected: "2GiB" },
{ input: "4096", expected: "4GiB" },
{ input: "8192", expected: "8GiB" },
{ input: "16384", expected: "16GiB" },
{ input: "32768", expected: "32GiB" },
{ input: "256Mi", expected: "256MiB" },
{ input: "512Mi", expected: "512MiB" },
{ input: "512MiB", expected: "512MiB" },
{ input: "1024Mi", expected: "1GiB" },
{ input: "2048Mi", expected: "2GiB" },
{ input: "1Gi", expected: "1GiB" },
{ input: "1GiB", expected: "1GiB" },
{ input: "2Gi", expected: "2GiB" },
{ input: "2GiB", expected: "2GiB" },
{ input: "1G", expected: "1GiB" },
{ input: "1GB", expected: "1GiB" },
{ input: "0.5Gi", expected: "512MiB" },
{ input: "300", expected: undefined },
{ input: "-256", expected: undefined },
{ input: "invalid", expected: undefined },
{ input: "", expected: undefined },
{ input: " ", expected: undefined },
{ input: undefined, expected: undefined },
];

for (const { input, expected } of testCases) {
it(`should parse "${String(input)}" to ${String(expected)}`, () => {
expect(parseMemory(input)).to.equal(expected);
});
}
});

describe("resolveMigratedMemory", () => {
it("should return undefined if no memory params are present", () => {
expect(resolveMigratedMemory({}, [])).to.be.undefined;
});

it("should return V1 memory if only V1 is present", () => {
expect(
resolveMigratedMemory({ "firebaseextensions.v1beta.function/memory": "256" }, []),
).to.equal("256");
});

it("should return V2 memory if only V2 is present", () => {
expect(
resolveMigratedMemory({ "firebaseextensions.v1beta.v2function/memory": "512Mi" }, []),
).to.equal("512Mi");
});

it("should select the highest value when both V1 and V2 are present", () => {
expect(
resolveMigratedMemory(
{
"firebaseextensions.v1beta.function/memory": "1024",
"firebaseextensions.v1beta.v2function/memory": "512Mi",
},
[],
),
).to.equal("1024");

expect(
resolveMigratedMemory(
{
"firebaseextensions.v1beta.function/memory": "256",
"firebaseextensions.v1beta.v2function/memory": "512Mi",
},
[],
),
).to.equal("512Mi");
});

it("should fall back to spec defaults when live params are missing", () => {
expect(
resolveMigratedMemory({ "firebaseextensions.v1beta.function/memory": "1024" }, [
{
param: "firebaseextensions.v1beta.v2function/memory",
label: "Memory",
default: "256Mi",
},
]),
).to.equal("1024");
});
});
});

describe("functionsEnvFromInstance", () => {
Expand Down Expand Up @@ -255,7 +377,7 @@ describe("functionsEnvFromInstance", () => {
};
const output = functionsEnvFromInstance(instance);
expect(output).to.deep.equal({
EXT_MIGRATED_SYSTEM_MEMORY: "256",
EXT_MIGRATED_SYSTEM_MEMORY: "256MiB",
EXT_MIGRATED_SYSTEM_MININSTANCES: "10",
});
});
Expand Down Expand Up @@ -298,11 +420,160 @@ describe("functionsEnvFromInstance", () => {
};
const output = functionsEnvFromInstance(instance);
expect(output).to.deep.equal({
EXT_MIGRATED_SYSTEM_MEMORY: "256",
EXT_MIGRATED_SYSTEM_MEMORY: "256MiB",
EXT_MIGRATED_SYSTEM_MININSTANCES: "10",
});
});

it("system params (both v1 and v2 functions, v2 higher)", () => {
const instance: ExtensionInstance = {
name: "",
createTime: "",
updateTime: "",
state: "ACTIVE",
serviceAccountEmail: "",
config: {
name: "",
createTime: "",
params: {},
systemParams: {
"firebaseextensions.v1beta.function/memory": "256",
"firebaseextensions.v1beta.v2function/memory": "512Mi",
},
source: {
name: "",
state: "ACTIVE",
packageUri: "",
hash: "",
spec: {
name: "",
version: "1",
resources: [],
params: [],
systemParams: [],
},
},
},
};
const output = functionsEnvFromInstance(instance);
expect(output).to.deep.equal({
EXT_MIGRATED_SYSTEM_MEMORY: "512MiB",
});
});

it("system params (both v1 and v2 functions, v1 higher)", () => {
const instance: ExtensionInstance = {
name: "",
createTime: "",
updateTime: "",
state: "ACTIVE",
serviceAccountEmail: "",
config: {
name: "",
createTime: "",
params: {},
systemParams: {
"firebaseextensions.v1beta.function/memory": "1024",
"firebaseextensions.v1beta.v2function/memory": "512Mi",
},
source: {
name: "",
state: "ACTIVE",
packageUri: "",
hash: "",
spec: {
name: "",
version: "1",
resources: [],
params: [],
systemParams: [],
},
},
},
};
const output = functionsEnvFromInstance(instance);
expect(output).to.deep.equal({
EXT_MIGRATED_SYSTEM_MEMORY: "1GiB",
});
});

it("system params (both v1 and v2 functions, equal memory)", () => {
const instance: ExtensionInstance = {
name: "",
createTime: "",
updateTime: "",
state: "ACTIVE",
serviceAccountEmail: "",
config: {
name: "",
createTime: "",
params: {},
systemParams: {
"firebaseextensions.v1beta.function/memory": "256",
"firebaseextensions.v1beta.v2function/memory": "256Mi",
},
source: {
name: "",
state: "ACTIVE",
packageUri: "",
hash: "",
spec: {
name: "",
version: "1",
resources: [],
params: [],
systemParams: [],
},
},
},
};
const output = functionsEnvFromInstance(instance);
expect(output).to.deep.equal({
EXT_MIGRATED_SYSTEM_MEMORY: "256MiB",
});
});

it("system params (v1 in live, v2 in spec defaults, v1 higher)", () => {
const instance: ExtensionInstance = {
name: "",
createTime: "",
updateTime: "",
state: "ACTIVE",
serviceAccountEmail: "",
config: {
name: "",
createTime: "",
params: {},
systemParams: {
"firebaseextensions.v1beta.function/memory": "1024",
},
source: {
name: "",
state: "ACTIVE",
packageUri: "",
hash: "",
spec: {
name: "",
version: "1",
resources: [],
params: [],
systemParams: [
{
param: "firebaseextensions.v1beta.v2function/memory",
label: "Memory",
default: "256Mi",
},
],
},
},
},
};
const output = functionsEnvFromInstance(instance);
expect(output).to.deep.equal({
EXT_MIGRATED_SYSTEM_MEMORY: "1GiB",
});
});

it("system params location should map to FUNCTION_DEFAULT_REGION", () => {
const instance: ExtensionInstance = {
name: "projects/1234/instances/ext1",
Expand Down
Loading
Loading