Skip to content

Commit

Permalink
feat: Add Integration tests CI
Browse files Browse the repository at this point in the history
  • Loading branch information
exaby73 committed Jul 30, 2024
1 parent 1ed8fee commit 436c724
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 1,419 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@ on:
- main

jobs:
integration-tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python: ["3.10", "3.12"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
- name: Setup Node 18
uses: actions/setup-node@v3
with:
node-version: 18
- name: Install Node dependencies
run: |
cd integration_tests && npm install
- name: Run Integration Tests
run: |
cd integration_tests && npm start
test:
runs-on: ubuntu-latest
strategy:
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,3 @@ doc/dist
.idea
.vscode/*
!.vscode/settings.json
integration_tests/serviceAccount.json
integration_tests/functions/firebase_functions_*.tar.gz
integration_tests/functions/requirements.txt
3 changes: 1 addition & 2 deletions integration_tests/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ REGION=us-central1
PROJECT_ID=
DATABASE_URL=
STORAGE_BUCKET=
NODE_VERSION=18
FIREBASE_ADMIN=^10.0.0
FIREBASE_ADMIN=6.5.0
FIREBASE_APP_ID=
FIREBASE_MEASUREMENT_ID=
FIREBASE_AUTH_DOMAIN=
Expand Down
6 changes: 5 additions & 1 deletion integration_tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ node_modules/
.yarn-integrity

# dotenv environment variables file
.env
.env
/functions/functions.yaml
/serviceAccount.json
/functions/requirements.txt
/functions/firebase_functions.tar.gz
5 changes: 1 addition & 4 deletions integration_tests/functions/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# Welcome to Cloud Functions for Firebase for Python!
# To get started, simply uncomment the below code or create your own.
# Deploy with `firebase deploy`

from firebase_admin import initialize_app
from v2.firestore_tests import *

initialize_app()
86 changes: 49 additions & 37 deletions integration_tests/functions/v2/firestore_tests.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,67 @@
from firebase_admin import firestore
from firebase_functions import firestore_fn, logger
from integration_tests.functions.region import REGION
from 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)
@firestore_fn.on_document_created(document='tests/{documentId}',
region=REGION,
timeout_sec=540)
def firestoreOnDocumentCreatedTests(
event: firestore_fn.Event[firestore_fn.DocumentSnapshot]):
documentId = event.params['documentId']

firestore.client().collection('firestoreOnDocumentCreatedTests').document(documentId).set({
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})
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)
@firestore_fn.on_document_deleted(document='tests/{documentId}',
region=REGION,
timeout_sec=540)
def firestoreOnDocumentDeletedTests(
event: firestore_fn.Event[firestore_fn.DocumentSnapshot]):
documentId = event.params['documentId']

firestore.client().collection('firestoreOnDocumentDeletedTests').document(documentId).set({
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})
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)
@firestore_fn.on_document_updated(document='tests/{documentId}',
region=REGION,
timeout_sec=540)
def firestoreOnDocumentUpdatedTests(
event: firestore_fn.Event[firestore_fn.DocumentSnapshot]):
documentId = event.params['documentId']

firestore.client().collection('firestoreOnDocumentUpdatedTests').document(documentId).set({
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})
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)
@firestore_fn.on_document_written(document='tests/{documentId}',
region=REGION,
timeout_sec=540)
def firestoreOnDocumentWrittenTests(
event: firestore_fn.Event[firestore_fn.DocumentSnapshot]):
documentId = event.params['documentId']

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions integration_tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
"scripts": {
"copyfiles": "cp ./serviceAccount.json ./dist/serviceAccount.json",
"build": "tsc && npm run copyfiles",
"test": "jest --detectOpenHandles",
"test": "jest --detectOpenHandles tests/v2/firestore.test.ts",
"start": "npm run build && node dist/run.js"
},
"devDependencies": {
"@types/firebase": "^3.2.1",
"@types/jest": "^29.5.11",
"@types/jest": "^29.5.12",
"@types/js-yaml": "^4.0.9",
"@types/node-fetch": "2",
"jest": "^29.7.0",
"prettier": "^3.3.3",
"ts-jest": "^29.1.1"
}
}
29 changes: 23 additions & 6 deletions integration_tests/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ async function discoverAndModifyEndpoints() {
const killServer = await delegate.serveAdmin(port.toString(), {}, env);

console.log("Started on port", port);
const originalYaml = await detectFromPort(port, config.projectId, config.runtime, 10000);
const originalYaml = await detectFromPort(
port,
config.projectId,
config.runtime,
10000
);

modifiedYaml = {
...originalYaml,
Expand Down Expand Up @@ -206,7 +211,11 @@ function cleanFiles(): void {
process.chdir("../"); // go back to integration_test
}

const spawnAsync = (command: string, args: string[], options: any): Promise<string> => {
const spawnAsync = (
command: string,
args: string[],
options: any
): Promise<string> => {
return new Promise((resolve, reject) => {
const child = spawn(command, args, options);

Expand All @@ -231,19 +240,23 @@ const spawnAsync = (command: string, args: string[], options: any): Promise<stri

async function runTests(): Promise<void> {
try {
console.log("Starting Node.js Tests...");
console.log("Starting Python Tests...");
const output = await spawnAsync("npm", ["test"], {
env: {
...process.env,
GOOGLE_APPLICATION_CREDENTIALS: path.join(__dirname, "serviceAccount.json"),
GOOGLE_APPLICATION_CREDENTIALS: path.join(
__dirname,
"serviceAccount.json"
),
TEST_RUN_ID,
},
stdio: "inherit",
});
console.log(output);
console.log("Node.js Tests Completed.");
console.log("Python Tests Completed.");
} catch (error) {
console.error("Error during testing:", error);
throw error;
}
}

Expand Down Expand Up @@ -272,6 +285,7 @@ async function runIntegrationTests(): Promise<void> {
await runTests();
} catch (err) {
console.error("Error occurred during integration tests", err);
throw new Error("Integration tests failed");
} finally {
await handleCleanUp();
}
Expand All @@ -282,4 +296,7 @@ runIntegrationTests()
console.log("Integration tests completed");
process.exit(0);
})
.catch((error) => console.error("An error occurred during integration tests", error));
.catch((error) => {
console.error("An error occurred during integration tests", error);
process.exit(1);
});
25 changes: 17 additions & 8 deletions integration_tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@ function buildSdk(testRunId: string) {
process.chdir(path.join(DIR, "..")); // go up to root

// remove existing build
fs.rmdirSync("dist", { recursive: true });
fs.rmSync("dist", { recursive: true, force: true });
// remove existing venv
fs.rmSync("venv", { recursive: true, force: true });

// make virtual environment for building
execSync("python3 -m venv venv", { stdio: "inherit" });

// build the package
execSync("python -m pip install --upgrade build", { stdio: "inherit" });
execSync("python -m build -s", { stdio: "inherit" });
execSync(
"source venv/bin/activate && python -m pip install --upgrade build",
{ stdio: "inherit" },
);
execSync("source venv/bin/activate && python -m build -s", {
stdio: "inherit",
});

// move the generated tarball package to functions
const generatedFile = fs
Expand All @@ -33,7 +43,7 @@ function buildSdk(testRunId: string) {
const targetPath = path.join(
"integration_tests",
"functions",
`firebase_functions_${testRunId}.tar.gz`
`firebase_functions.tar.gz`,
);
fs.renameSync(path.join("dist", generatedFile), targetPath);
console.log("SDK moved to", targetPath);
Expand All @@ -52,11 +62,11 @@ function createRequirementsTxt(testRunId: string, firebaseAdmin: string) {
let requirementsContent = fs.readFileSync(requirementsPath, "utf8");
requirementsContent = requirementsContent.replace(
/__LOCAL_FIREBASE_FUNCTIONS__/g,
`firebase_functions_${testRunId}.tar.gz`
`firebase_functions.tar.gz`,
);
requirementsContent = requirementsContent.replace(
/__FIREBASE_ADMIN__/g,
firebaseAdmin
firebaseAdmin,
);

fs.writeFileSync(requirementsPath, requirementsContent);
Expand All @@ -73,7 +83,6 @@ function installDependencies() {
}

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

0 comments on commit 436c724

Please sign in to comment.