Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bucket filtering in the functions emulator #3893

Merged
merged 10 commits into from Dec 8, 2021
55 changes: 55 additions & 0 deletions scripts/integration-helpers/framework.ts
Expand Up @@ -14,6 +14,14 @@ const STORAGE_FUNCTION_V2_ARCHIVED_LOG = "========== STORAGE V2 FUNCTION ARCHIVE
const STORAGE_FUNCTION_V2_DELETED_LOG = "========== STORAGE V2 FUNCTION DELETED ==========";
const STORAGE_FUNCTION_V2_FINALIZED_LOG = "========== STORAGE V2 FUNCTION FINALIZED ==========";
const STORAGE_FUNCTION_V2_METADATA_LOG = "========== STORAGE V2 FUNCTION METADATA ==========";
const STORAGE_BUCKET_FUNCTION_V2_ARCHIVED_LOG =
"========== STORAGE BUCKET V2 FUNCTION ARCHIVED ==========";
const STORAGE_BUCKET_FUNCTION_V2_DELETED_LOG =
"========== STORAGE BUCKET V2 FUNCTION DELETED ==========";
const STORAGE_BUCKET_FUNCTION_V2_FINALIZED_LOG =
"========== STORAGE BUCKET V2 FUNCTION FINALIZED ==========";
const STORAGE_BUCKET_FUNCTION_V2_METADATA_LOG =
"========== STORAGE BUCKET V2 FUNCTION METADATA ==========";
/* Functions V1 */
const RTDB_FUNCTION_LOG = "========== RTDB FUNCTION ==========";
const FIRESTORE_FUNCTION_LOG = "========== FIRESTORE FUNCTION ==========";
Expand All @@ -23,6 +31,13 @@ const STORAGE_FUNCTION_ARCHIVED_LOG = "========== STORAGE FUNCTION ARCHIVED ====
const STORAGE_FUNCTION_DELETED_LOG = "========== STORAGE FUNCTION DELETED ==========";
const STORAGE_FUNCTION_FINALIZED_LOG = "========== STORAGE FUNCTION FINALIZED ==========";
const STORAGE_FUNCTION_METADATA_LOG = "========== STORAGE FUNCTION METADATA ==========";
const STORAGE_BUCKET_FUNCTION_ARCHIVED_LOG =
"========== STORAGE BUCKET FUNCTION ARCHIVED ==========";
const STORAGE_BUCKET_FUNCTION_DELETED_LOG = "========== STORAGE BUCKET FUNCTION DELETED ==========";
const STORAGE_BUCKET_FUNCTION_FINALIZED_LOG =
"========== STORAGE BUCKET FUNCTION FINALIZED ==========";
const STORAGE_BUCKET_FUNCTION_METADATA_LOG =
"========== STORAGE BUCKET FUNCTION METADATA ==========";
const ALL_EMULATORS_STARTED_LOG = "All emulators ready";

interface ConnectionInfo {
Expand Down Expand Up @@ -65,13 +80,21 @@ export class TriggerEndToEndTest {
storageDeletedTriggerCount = 0;
storageFinalizedTriggerCount = 0;
storageMetadataTriggerCount = 0;
storageBucketArchivedTriggerCount = 0;
storageBucketDeletedTriggerCount = 0;
storageBucketFinalizedTriggerCount = 0;
storageBucketMetadataTriggerCount = 0;

/* Functions V2 */
pubsubV2TriggerCount = 0;
storageV2ArchivedTriggerCount = 0;
storageV2DeletedTriggerCount = 0;
storageV2FinalizedTriggerCount = 0;
storageV2MetadataTriggerCount = 0;
storageBucketV2ArchivedTriggerCount = 0;
storageBucketV2DeletedTriggerCount = 0;
storageBucketV2FinalizedTriggerCount = 0;
storageBucketV2MetadataTriggerCount = 0;

rtdbFromFirestore = false;
firestoreFromRtdb = false;
Expand Down Expand Up @@ -138,6 +161,18 @@ export class TriggerEndToEndTest {
if (data.includes(STORAGE_FUNCTION_METADATA_LOG)) {
this.storageMetadataTriggerCount++;
}
if (data.includes(STORAGE_BUCKET_FUNCTION_ARCHIVED_LOG)) {
this.storageBucketArchivedTriggerCount++;
}
if (data.includes(STORAGE_BUCKET_FUNCTION_DELETED_LOG)) {
this.storageBucketDeletedTriggerCount++;
}
if (data.includes(STORAGE_BUCKET_FUNCTION_FINALIZED_LOG)) {
this.storageBucketFinalizedTriggerCount++;
}
if (data.includes(STORAGE_BUCKET_FUNCTION_METADATA_LOG)) {
this.storageBucketMetadataTriggerCount++;
}
/* Functions V2 */
if (data.includes(PUBSUB_FUNCTION_V2_LOG)) {
this.pubsubV2TriggerCount++;
Expand All @@ -154,6 +189,18 @@ export class TriggerEndToEndTest {
if (data.includes(STORAGE_FUNCTION_V2_METADATA_LOG)) {
this.storageV2MetadataTriggerCount++;
}
if (data.includes(STORAGE_BUCKET_FUNCTION_V2_ARCHIVED_LOG)) {
this.storageBucketV2ArchivedTriggerCount++;
}
if (data.includes(STORAGE_BUCKET_FUNCTION_V2_DELETED_LOG)) {
this.storageBucketV2DeletedTriggerCount++;
}
if (data.includes(STORAGE_BUCKET_FUNCTION_V2_FINALIZED_LOG)) {
this.storageBucketV2FinalizedTriggerCount++;
}
if (data.includes(STORAGE_BUCKET_FUNCTION_V2_METADATA_LOG)) {
this.storageBucketV2MetadataTriggerCount++;
}
});

this.cliProcess = cli;
Expand Down Expand Up @@ -217,6 +264,14 @@ export class TriggerEndToEndTest {
return this.invokeHttpFunction("updateDeleteFromStorage");
}

writeToSpecificStorageBucket(): Promise<Response> {
return this.invokeHttpFunction("writeToSpecificStorageBucket");
}

updateDeleteFromSpecificStorageBucket(): Promise<Response> {
return this.invokeHttpFunction("updateDeleteFromSpecificStorageBucket");
}

waitForCondition(
conditionFn: () => boolean,
timeout: number,
Expand Down
211 changes: 168 additions & 43 deletions scripts/triggers-end-to-end-tests/functions/index.js
Expand Up @@ -23,12 +23,27 @@ const STORAGE_FUNCTION_ARCHIVED_LOG = "========== STORAGE FUNCTION ARCHIVED ====
const STORAGE_FUNCTION_DELETED_LOG = "========== STORAGE FUNCTION DELETED ==========";
const STORAGE_FUNCTION_FINALIZED_LOG = "========== STORAGE FUNCTION FINALIZED ==========";
const STORAGE_FUNCTION_METADATA_LOG = "========== STORAGE FUNCTION METADATA ==========";
const STORAGE_BUCKET_FUNCTION_ARCHIVED_LOG =
"========== STORAGE BUCKET FUNCTION ARCHIVED ==========";
const STORAGE_BUCKET_FUNCTION_DELETED_LOG = "========== STORAGE BUCKET FUNCTION DELETED ==========";
const STORAGE_BUCKET_FUNCTION_FINALIZED_LOG =
"========== STORAGE BUCKET FUNCTION FINALIZED ==========";
const STORAGE_BUCKET_FUNCTION_METADATA_LOG =
"========== STORAGE BUCKET FUNCTION METADATA ==========";
/* Functions V2 */
const PUBSUB_FUNCTION_V2_LOG = "========== PUBSUB V2 FUNCTION ==========";
const STORAGE_FUNCTION_V2_ARCHIVED_LOG = "========== STORAGE V2 FUNCTION ARCHIVED ==========";
const STORAGE_FUNCTION_V2_DELETED_LOG = "========== STORAGE V2 FUNCTION DELETED ==========";
const STORAGE_FUNCTION_V2_FINALIZED_LOG = "========== STORAGE V2 FUNCTION FINALIZED ==========";
const STORAGE_FUNCTION_V2_METADATA_LOG = "========== STORAGE V2 FUNCTION METADATA ==========";
const STORAGE_BUCKET_FUNCTION_V2_ARCHIVED_LOG =
"========== STORAGE BUCKET V2 FUNCTION ARCHIVED ==========";
const STORAGE_BUCKET_FUNCTION_V2_DELETED_LOG =
"========== STORAGE BUCKET V2 FUNCTION DELETED ==========";
const STORAGE_BUCKET_FUNCTION_V2_FINALIZED_LOG =
"========== STORAGE BUCKET V2 FUNCTION FINALIZED ==========";
const STORAGE_BUCKET_FUNCTION_V2_METADATA_LOG =
"========== STORAGE BUCKET V2 FUNCTION METADATA ==========";

/*
* We install onWrite triggers for START_DOCUMENT_NAME in both the firestore and
Expand Down Expand Up @@ -100,19 +115,33 @@ exports.writeToAuth = functions.https.onRequest(async (req, res) => {
});

exports.writeToStorage = functions.https.onRequest(async (req, res) => {
await admin.storage().bucket().file(STORAGE_FILE_NAME).save("hello world!");
await admin.storage().bucket("default-bucket").file(STORAGE_FILE_NAME).save("hello world!");
colerogers marked this conversation as resolved.
Show resolved Hide resolved
console.log("Wrote to Storage bucket");
res.json({ created: "ok" });
});

exports.updateDeleteFromStorage = functions.https.onRequest(async (req, res) => {
await admin.storage().bucket().file(STORAGE_FILE_NAME).save("something new!");
await admin.storage().bucket("default-bucket").file(STORAGE_FILE_NAME).save("something new!");
console.log("Wrote to Storage bucket");
await admin.storage().bucket().file(STORAGE_FILE_NAME).delete();
await admin.storage().bucket("default-bucket").file(STORAGE_FILE_NAME).delete();
console.log("Deleted from Storage bucket");
res.json({ done: "ok" });
});

exports.writeToSpecificStorageBucket = functions.https.onRequest(async (req, res) => {
await admin.storage().bucket("test-bucket").file(STORAGE_FILE_NAME).save("hello world!");
console.log("Wrote to a specific Storage bucket");
res.json({ created: "ok" });
});

exports.updateDeleteFromSpecificStorageBucket = functions.https.onRequest(async (req, res) => {
await admin.storage().bucket("test-bucket").file(STORAGE_FILE_NAME).save("something new!");
console.log("Wrote to a specific Storage bucket");
await admin.storage().bucket("test-bucket").file(STORAGE_FILE_NAME).delete();
console.log("Deleted from a specific Storage bucket");
res.json({ done: "ok" });
});

exports.firestoreReaction = functions.firestore
.document(START_DOCUMENT_NAME)
.onWrite(async (/* change, ctx */) => {
Expand Down Expand Up @@ -175,50 +204,146 @@ exports.authReaction = functions.auth.user().onCreate((user, ctx) => {
return true;
});

exports.storageArchiveReaction = functions.storage.object().onArchive((object, context) => {
console.log(STORAGE_FUNCTION_ARCHIVED_LOG);
console.log("Object", JSON.stringify(object));
return true;
});
exports.storageArchiveReaction = functions.storage
.bucket("default-bucket")
colerogers marked this conversation as resolved.
Show resolved Hide resolved
.object()
.onArchive((object, context) => {
console.log(STORAGE_FUNCTION_ARCHIVED_LOG);
console.log("Object", JSON.stringify(object));
return true;
});

exports.storageDeleteReaction = functions.storage.object().onDelete((object, context) => {
console.log(STORAGE_FUNCTION_DELETED_LOG);
console.log("Object", JSON.stringify(object));
return true;
});
exports.storageDeleteReaction = functions.storage
.bucket("default-bucket")
.object()
.onDelete((object, context) => {
console.log(STORAGE_FUNCTION_DELETED_LOG);
console.log("Object", JSON.stringify(object));
return true;
});

exports.storageFinalizeReaction = functions.storage.object().onFinalize((object, context) => {
console.log(STORAGE_FUNCTION_FINALIZED_LOG);
console.log("Object", JSON.stringify(object));
return true;
});
exports.storageFinalizeReaction = functions.storage
.bucket("default-bucket")
.object()
.onFinalize((object, context) => {
console.log(STORAGE_FUNCTION_FINALIZED_LOG);
console.log("Object", JSON.stringify(object));
return true;
});

exports.storageMetadataReaction = functions.storage.object().onMetadataUpdate((object, context) => {
console.log(STORAGE_FUNCTION_METADATA_LOG);
console.log("Object", JSON.stringify(object));
return true;
});
exports.storageMetadataReaction = functions.storage
.bucket("default-bucket")
.object()
.onMetadataUpdate((object, context) => {
console.log(STORAGE_FUNCTION_METADATA_LOG);
console.log("Object", JSON.stringify(object));
return true;
});

exports.storagev2archivedreaction = functionsV2.storage.onObjectArchived((cloudevent) => {
console.log(STORAGE_FUNCTION_V2_ARCHIVED_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
});
exports.storagev2archivedreaction = functionsV2.storage.onObjectArchived(
"default-bucket",
(cloudevent) => {
console.log(STORAGE_FUNCTION_V2_ARCHIVED_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
}
);

exports.storagev2deletedreaction = functionsV2.storage.onObjectDeleted(
"default-bucket",
(cloudevent) => {
console.log(STORAGE_FUNCTION_V2_DELETED_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
}
);

exports.storagev2finalizedreaction = functionsV2.storage.onObjectFinalized(
"default-bucket",
(cloudevent) => {
console.log(STORAGE_FUNCTION_V2_FINALIZED_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
}
);

exports.storagev2metadatareaction = functionsV2.storage.onObjectMetadataUpdated(
"default-bucket",
(cloudevent) => {
console.log(STORAGE_FUNCTION_V2_METADATA_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
}
);

exports.storageArchiveReaction = functions.storage
.bucket("test-bucket")
.object()
.onArchive((object, context) => {
console.log(STORAGE_BUCKET_FUNCTION_ARCHIVED_LOG);
console.log("Object", JSON.stringify(object));
return true;
});

exports.storagev2deletedreaction = functionsV2.storage.onObjectDeleted((cloudevent) => {
console.log(STORAGE_FUNCTION_V2_DELETED_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
});
exports.storageDeleteReaction = functions.storage
.bucket("test-bucket")
.object()
.onDelete((object, context) => {
console.log(STORAGE_BUCKET_FUNCTION_DELETED_LOG);
console.log("Object", JSON.stringify(object));
return true;
});

exports.storagev2finalizedreaction = functionsV2.storage.onObjectFinalized((cloudevent) => {
console.log(STORAGE_FUNCTION_V2_FINALIZED_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
});
exports.storageFinalizeReaction = functions.storage
.bucket("test-bucket")
.object()
.onFinalize((object, context) => {
console.log(STORAGE_BUCKET_FUNCTION_FINALIZED_LOG);
console.log("Object", JSON.stringify(object));
return true;
});

exports.storagev2metadatareaction = functionsV2.storage.onObjectMetadataUpdated((cloudevent) => {
console.log(STORAGE_FUNCTION_V2_METADATA_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
});
exports.storageMetadataReaction = functions.storage
.bucket("test-bucket")
.object()
.onMetadataUpdate((object, context) => {
console.log(STORAGE_BUCKET_FUNCTION_METADATA_LOG);
console.log("Object", JSON.stringify(object));
return true;
});

exports.storagev2archivedreaction = functionsV2.storage.onObjectArchived(
"test-bucket",
(cloudevent) => {
console.log(STORAGE_BUCKET_FUNCTION_V2_ARCHIVED_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
}
);

exports.storagev2deletedreaction = functionsV2.storage.onObjectDeleted(
"test-bucket",
(cloudevent) => {
console.log(STORAGE_BUCKET_FUNCTION_V2_DELETED_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
}
);

exports.storagev2finalizedreaction = functionsV2.storage.onObjectFinalized(
"test-bucket",
(cloudevent) => {
console.log(STORAGE_BUCKET_FUNCTION_V2_FINALIZED_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
}
);

exports.storagev2metadatareaction = functionsV2.storage.onObjectMetadataUpdated(
"test-bucket",
(cloudevent) => {
console.log(STORAGE_BUCKET_FUNCTION_V2_METADATA_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
}
);