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

feat: Integration tests #215

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions .gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
venv
build
!integration_test/.env
23 changes: 21 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ on:
- main

jobs:
integration-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.12
uses: actions/setup-python@v4
with:
python-version: 3.12
- 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 Expand Up @@ -51,11 +70,11 @@ jobs:
- name: Lint with pylint
run: |
source venv/bin/activate
python3.10 -m pylint $(git ls-files '*.py')
python3.10 -m pylint src tests
- name: Lint with mypy
run: |
source venv/bin/activate
python3.10 -m mypy .
python3.10 -m mypy src tests

docs:
runs-on: ubuntu-latest
Expand Down
22 changes: 22 additions & 0 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
steps:
- name: "gcr.io/cloud-builders/git"
id: "Checkout tests"
dir: "integration_test"
entrypoint: "bash"
args:
- "./checkout.sh"
- name: "node:18"
entrypoint: "npm"
dir: "integration_test"
args: ["install"]
- name: "node:18"
entrypoint: "npx"
dir: "integration_test"
args: ["firebase", "use", "cf3-integration-tests-d7be6"]
- name: "gcr.io/cf3-integration-tests-d7be6/node_python:0.0.2"
entrypoint: "npm"
dir: "integration_test"
args: ["start"]

options:
defaultLogsBucketBehavior: REGIONAL_USER_OWNED_BUCKET
11 changes: 11 additions & 0 deletions integration_test/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
TEST_RUNTIME=python
REGION=us-central1
PROJECT_ID=
DATABASE_URL=
STORAGE_BUCKET=
FIREBASE_ADMIN=6.5.0
FIREBASE_APP_ID=
FIREBASE_MEASUREMENT_ID=
FIREBASE_AUTH_DOMAIN=
FIREBASE_API_KEY=
GOOGLE_ANALYTICS_API_SECRET=
10 changes: 10 additions & 0 deletions integration_test/builders/python-node/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Use a combined base image with Python 3.11 and Node 18
FROM python:3.11

RUN python -m pip install virtualenv

# Install Node.js 18 using the Nodesource repository
RUN apt-get update && \
curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
apt-get install -y nodejs && \
rm -rf /var/lib/apt/lists/*
45 changes: 45 additions & 0 deletions integration_test/checkout.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env sh

set -ex

cleanup() {
current_dir_name=$(basename "$PWD")

if [ "$current_dir_name" = ".tmp" ]; then
cd ..
fi

if [ -z "$DEBUG" ]; then
rm -rf .tmp
fi
}

trap cleanup EXIT

if [ -d ".tmp" ]; then
rm -rf .tmp
fi

mkdir .tmp
cd .tmp

git init
git remote add origin https://github.com/firebase/firebase-functions.git
git config core.sparseCheckout true
mkdir -p .git/info
echo 'integration_test/**/*' > .git/info/sparse-checkout
git fetch origin v2-integration-tests
git pull origin v2-integration-tests
git checkout v2-integration-tests

rm -rf integration_test/functions
rm -f integration_test/.env.example

mv integration_test/* ../
for file in integration_test/.[!.]* integration_test/..?*; do
[ -e "$file" ] && mv "$file" ../
done

cd ..
rm -rf tests/v1
rm -rf .tmp
2 changes: 2 additions & 0 deletions integration_test/functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.local
venv
14 changes: 14 additions & 0 deletions integration_test/functions/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from firebase_admin import initialize_app
from v2.database_tests import *
from v2.eventarc_tests import *
from v2.firestore_tests import *
from v2.https_tests import *
from v2.identity_tests import *
from v2.pubsub_tests import *
from v2.remote_config_tests import *
from v2.scheduler_tests import *
from v2.storage_tests import *
from v2.tasks_tests import *
from v2.test_lab_tests import *

initialize_app()
1 change: 1 addition & 0 deletions integration_test/functions/region.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REGION = "us-central1"
76 changes: 76 additions & 0 deletions integration_test/functions/v2/database_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import json

from firebase_admin import firestore
from firebase_functions import logger
from firebase_functions.db_fn import (on_value_created, Event, on_value_deleted,
on_value_updated, on_value_written,
Change)

from region import REGION


@on_value_created(reference="databaseCreatedTests/{testId}/start",
region=REGION)
def databaseCreatedTests(event: Event[object]):
test_id = event.params['testId']

firestore.client().collection("databaseCreatedTests").document(test_id).set(
{
"testId": test_id,
"type": event.type,
"id": event.id,
"time": event.time,
"url": event.reference,
})


@on_value_deleted(reference="databaseDeletedTests/{testId}/start",
region=REGION)
def databaseDeletedTests(event: Event[object]):
test_id = event.params['testId']

firestore.client().collection("databaseDeletedTests").document(test_id).set(
{
"testId": test_id,
"type": event.type,
"id": event.id,
"time": event.time,
"url": event.reference,
})


@on_value_updated(reference="databaseUpdatedTests/{testId}/start",
region=REGION)
def databaseUpdatedTests(event: Event[Change[object]]):
test_id = event.params['testId']
data = event.data.after

firestore.client().collection("databaseUpdatedTests").document(test_id).set(
{
"testId": test_id,
"type": event.type,
"id": event.id,
"time": event.time,
"url": event.reference,
"data": json.dumps(data if data is not None else {})
})


@on_value_written(reference="databaseWrittenTests/{testId}/start",
region=REGION)
def databaseWrittenTests(event: Event[Change[object]]):
test_id = event.params['testId']
if event.data.after is None:
logger.info(
f"Event for {test_id} is None; presuming data cleanup, so skipping."
)
return

firestore.client().collection("databaseWrittenTests").document(test_id).set(
{
"testId": test_id,
"type": event.type,
"id": event.id,
"time": event.time,
"url": event.reference,
})
19 changes: 19 additions & 0 deletions integration_test/functions/v2/eventarc_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import json

from firebase_admin import firestore
from firebase_functions.eventarc_fn import (on_custom_event_published,
CloudEvent)


@on_custom_event_published(event_type="achieved-leaderboard")
def eventarcOnCustomEventPublishedTests(event: CloudEvent):
test_id = event.data["testId"]

firestore.client().collection(
"eventarcOnCustomEventPublishedTests").document(test_id).set({
"id": event.id,
"type": event.type,
"time": event.time,
"source": event.source,
"data": json.dumps(event.data),
})
67 changes: 67 additions & 0 deletions integration_test/functions/v2/firestore_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from firebase_admin import firestore
from firebase_functions.firestore_fn import (on_document_created, Event,
DocumentSnapshot,
on_document_deleted,
on_document_updated,
on_document_written)
from region import REGION


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

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


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

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


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

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


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

firestore.client().collection('firestoreOnDocumentWrittenTests').document(
document_id).set({
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})
19 changes: 19 additions & 0 deletions integration_test/functions/v2/https_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from firebase_admin import firestore
from firebase_functions.https_fn import (on_call, CallableRequest, on_request,
Request)

from region import REGION


@on_call(invoker="private", region=REGION)
def httpsOnCallV2Tests(request: CallableRequest):
data = request.data
firestore.client().collection("httpsOnCallV2Tests").document(
data["testId"]).set(data)


@on_request(invoker="private", region=REGION)
def httpsOnRequestV2Tests(request: Request):
data = request.json
firestore.client().collection("httpsOnRequestV2Tests").document(
data["testId"]).set(data)
32 changes: 32 additions & 0 deletions integration_test/functions/v2/identity_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from firebase_admin import firestore
from firebase_functions.identity_fn import (before_user_created,
BeforeCreateResponse,
before_user_signed_in,
BeforeSignInResponse,
AuthBlockingEvent)


@before_user_created()
def identityBeforeUserCreatedTests(
event: AuthBlockingEvent) -> BeforeCreateResponse:
uid = event.data.uid

firestore.client().collection("identityBeforeUserCreatedTests").document(
uid).set({
"eventId": event.event_id,
"timestamp": event.timestamp,
})

return BeforeCreateResponse(**event.data.__dict__)


@before_user_signed_in()
def identityBeforeUserSignedInTests(
event: AuthBlockingEvent) -> BeforeSignInResponse:
uid = event.data.uid

firestore.client().collection("identityBeforeUserSignedInTests").document(
uid).set({
"eventId": event.event_id,
"timestamp": event.timestamp,
})
Loading
Loading