Skip to content

Commit

Permalink
wip: integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
exaby73 committed Jul 21, 2024
1 parent c16f329 commit 1dba352
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,5 @@ doc/dist
.vscode/*
!.vscode/settings.json
integration_tests/serviceAccount.json
integration_tests/functions/firebase_functions_*.tar.gz
integration_tests/functions/requirements.txt
5 changes: 0 additions & 5 deletions integration_tests/functions/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
# To get started, simply uncomment the below code or create your own.
# Deploy with `firebase deploy`

from firebase_functions import https_fn
from firebase_admin import initialize_app

initialize_app()

@https_fn.on_call()
def hello(_: https_fn.CallableRequest) -> str:
return 'Hello from Firebase!'
2 changes: 2 additions & 0 deletions integration_tests/functions/region.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# FIXME: Change to us-central1
REGION = "asia-south1"
1 change: 0 additions & 1 deletion integration_tests/functions/requirements.txt

This file was deleted.

55 changes: 55 additions & 0 deletions integration_tests/functions/v2/firestore_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from firebase_admin import firestore
from firebase_functions import firestore_fn, logger
from integration_tests.functions.region import REGION


@firestore_fn.on_document_created(document='tests/{documentId}', region=REGION, timeout_sec=540)
def firestoreOnDocumentCreatedTests(event: firestore_fn.Event[firestore_fn.DocumentSnapshot]):
logger.debug(event=event)
documentId = event.params['documentId']

firestore.client().collection('firestoreOnDocumentCreatedTests').document(documentId).set({
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})


@firestore_fn.on_document_deleted(document='tests/{documentId}', region=REGION, timeout_sec=540)
def firestoreOnDocumentDeletedTests(event: firestore_fn.Event[firestore_fn.DocumentSnapshot]):
logger.debug(event=event)
documentId = event.params['documentId']

firestore.client().collection('firestoreOnDocumentDeletedTests').document(documentId).set({
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})


@firestore_fn.on_document_updated(document='tests/{documentId}', region=REGION, timeout_sec=540)
def firestoreOnDocumentUpdatedTests(event: firestore_fn.Event[firestore_fn.DocumentSnapshot]):
logger.debug(event=event)
documentId = event.params['documentId']

firestore.client().collection('firestoreOnDocumentUpdatedTests').document(documentId).set({
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})


@firestore_fn.on_document_written(document='tests/{documentId}', region=REGION, timeout_sec=540)
def firestoreOnDocumentWrittenTests(event: firestore_fn.Event[firestore_fn.DocumentSnapshot]):
logger.debug(event=event)
documentId = event.params['documentId']

firestore.client().collection('firestoreOnDocumentWrittenTests').document(documentId).set({
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})
20 changes: 0 additions & 20 deletions integration_tests/package.json.template

This file was deleted.

2 changes: 2 additions & 0 deletions integration_tests/requirements.txt.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
firebase_admin==__FIREBASE_ADMIN__
__LOCAL_FIREBASE_FUNCTIONS__
8 changes: 3 additions & 5 deletions integration_tests/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ function loadEnv(): void {
loadEnv();

const {
NODE_VERSION = "18",
FIREBASE_ADMIN = "^10.0.0",
PROJECT_ID,
DATABASE_URL,
Expand All @@ -55,7 +54,7 @@ if (
process.exit(1);
}

setup(TEST_RUN_ID, NODE_VERSION, FIREBASE_ADMIN);
setup(TEST_RUN_ID, FIREBASE_ADMIN);

const config = {
projectId: PROJECT_ID,
Expand Down Expand Up @@ -185,7 +184,7 @@ function cleanFiles(): void {
try {
const files = fs.readdirSync(".");
files.forEach((file) => {
if (file.match(`firebase-functions-${TEST_RUN_ID}.tgz`)) {
if (file.match(`firebase_functions_${TEST_RUN_ID}.tar.gz`)) {
fs.rmSync(file);
}
if (file.match("package.json")) {
Expand All @@ -199,8 +198,7 @@ function cleanFiles(): void {
}
});

fs.rmSync("lib", { recursive: true });
// fs.existsSync("node_modules") && fs.rmSync("node_modules", { recursive: true });
fs.rmSync("venv", { recursive: true });
} catch (error) {
console.error("Error occurred while cleaning files:", error);
}
Expand Down
70 changes: 31 additions & 39 deletions integration_tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,81 +7,73 @@ const DIR = process.cwd();
/**
* Build SDK, and Functions
*/
export default function setup(testRunId: string, nodeVersion: string, firebaseAdmin: string) {
export default function setup(testRunId: string, firebaseAdmin: string) {
buildSdk(testRunId);
createPackageJson(testRunId, nodeVersion, firebaseAdmin);
createRequirementsTxt(testRunId, firebaseAdmin);
installDependencies();
buildFunctions();
}

function buildSdk(testRunId: string) {
console.log("Building SDK...");
process.chdir(path.join(DIR, "..")); // go up to root

// remove existing firebase-functions-*.tgz files
const files = fs.readdirSync(".");
files.forEach((file) => {
if (file.match(/^firebase-functions-.*\.tgz$/)) {
fs.rmSync(file);
}
});
// remove existing build
fs.rmdirSync("dist", { recursive: true });

// build the package
execSync("npm run build:pack", { stdio: "inherit" });
execSync("python -m pip install --upgrade build", { stdio: "inherit" });
execSync("python -m build -s", { stdio: "inherit" });

// move the generated tarball package to functions
const generatedFile = fs
.readdirSync(".")
.find((file) => file.match(/^firebase-functions-.*\.tgz$/));
.readdirSync("dist")
.find((file) => file.match(/^firebase_functions-.*\.tar\.gz$/));

if (generatedFile) {
const targetPath = path.join(
"integration_test",
"integration_tests",
"functions",
`firebase-functions-${testRunId}.tgz`
`firebase_functions_${testRunId}.tar.gz`
);
fs.renameSync(generatedFile, targetPath);
fs.renameSync(path.join("dist", generatedFile), targetPath);
console.log("SDK moved to", targetPath);
}

process.chdir(DIR); // go back to integration_test
}

function createPackageJson(testRunId: string, nodeVersion: string, firebaseAdmin: string) {
function createRequirementsTxt(testRunId: string, firebaseAdmin: string) {
console.log("Creating package.json...");
const packageJsonTemplatePath = `${DIR}/package.json.template`;
const packageJsonPath = `${DIR}/functions/package.json`;
const requirementsTemplatePath = `${DIR}/requirements.txt.template`;
const requirementsPath = `${DIR}/functions/requirements.txt`;

fs.copyFileSync(packageJsonTemplatePath, packageJsonPath);
fs.copyFileSync(requirementsTemplatePath, requirementsPath);

let packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
packageJsonContent = packageJsonContent.replace(
/__SDK_TARBALL__/g,
`firebase-functions-${testRunId}.tgz`
let requirementsContent = fs.readFileSync(requirementsPath, "utf8");
requirementsContent = requirementsContent.replace(
/__LOCAL_FIREBASE_FUNCTIONS__/g,
`firebase_functions_${testRunId}.tar.gz`
);
requirementsContent = requirementsContent.replace(
/__FIREBASE_ADMIN__/g,
firebaseAdmin
);
packageJsonContent = packageJsonContent.replace(/__NODE_VERSION__/g, nodeVersion);
packageJsonContent = packageJsonContent.replace(/__FIREBASE_ADMIN__/g, firebaseAdmin);

fs.writeFileSync(packageJsonPath, packageJsonContent);
fs.writeFileSync(requirementsPath, requirementsContent);
}

function installDependencies() {
console.log("Installing dependencies...");
const functionsDir = "functions";
process.chdir(functionsDir); // go to functions

const modulePath = path.join("node_modules", "firebase-functions");
if (fs.existsSync(modulePath)) {
execSync(`rm -rf ${modulePath}`, { stdio: "inherit" });
const venvPath = path.join("venv");
if (fs.existsSync(venvPath)) {
execSync(`rm -rf ${venvPath}`, { stdio: "inherit" });
}

execSync("npm install", { stdio: "inherit" });
execSync("python3 -m venv venv", { stdio: "inherit" });
execSync("source venv/bin/activate", { stdio: "inherit" });
execSync("python3 -m pip install -r requirements.txt", { stdio: "inherit" });
process.chdir("../"); // go back to integration_test
}

function buildFunctions() {
console.log("Building functions...");
process.chdir(path.join(DIR, "functions")); // go to functions

execSync("npm run build", { stdio: "inherit" });
process.chdir(DIR); // go back to integration_test
}

0 comments on commit 1dba352

Please sign in to comment.