Skip to content

Commit

Permalink
fixes tests and linter, removes travis and adds Kokoro
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Nov 27, 2019
1 parent 78abdb5 commit 9485a86
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 124 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ htmlcov/
nosetests.xml
coverage.xml
*,cover
sponge_log.xml

*.log

Expand Down
8 changes: 6 additions & 2 deletions .kokoro/common.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# Download trampoline resources. These will be in ${KOKORO_GFILE_DIR}
gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/trampoline"

# Download secrets from Cloud Storage.
gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/getting-started-python"

# All builds use the trampoline script to run in docker.
build_file: "getting-started-python/.kokoro/trampoline.sh"

Expand All @@ -12,7 +15,8 @@ env_vars: {
value: "gcr.io/cloud-devrel-kokoro-resources/python@sha256:4b6ba8c199e96248980db4538065cddeea594138b9b9fb2d0388603922087747"
}

# Tell the trampoline which build file to use.
env_vars: {
key: "FIRESTORE_PROJECT_ID"
value: "getting-started-python-tests"
key: "TRAMPOLINE_BUILD_FILE"
value: "github/getting-started-python/.kokoro/system_tests.sh"
}
Empty file added .kokoro/presubmit.cfg
Empty file.
10 changes: 0 additions & 10 deletions .kokoro/system_tests.cfg
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@
# Format: //devtools/kokoro/config/proto/build.proto

# Download secrets from Cloud Storage.
gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/getting-started-python"

# Tell the trampoline which build file to use.
env_vars: {
key: "TRAMPOLINE_BUILD_FILE"
value: "github/getting-started-python/.kokoro/system_tests.sh"
}
1 change: 1 addition & 0 deletions .kokoro/system_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ SECRETS_PASSWORD=$(cat "${KOKORO_GFILE_DIR}/secrets-password.txt")
export GOOGLE_APPLICATION_CREDENTIALS="$(pwd)/service-account.json"

# Run tests
nox -s lint
nox -s run_tests
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Getting started with Python on Google Cloud Platform

[![Build Status](https://travis-ci.org/GoogleCloudPlatform/getting-started-python.svg)](https://travis-ci.org/GoogleCloudPlatform/getting-started-python)

This repository is the complete sample code for the [Python Getting Started on Google Cloud Platform](http://cloud.google.com/python) tutorials. Please refer to the tutorials for instructions on configuring, running, and deploying these samples.

The code for the samples is contained in individual folders in this repository.
Expand Down
2 changes: 1 addition & 1 deletion authenticating-users/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_metadata(item_name):
path += item_name
response = requests.get(
'{}{}'.format(endpoint, path),
headers = {'Metadata-Flavor': 'Google'}
headers={'Metadata-Flavor': 'Google'}
)
metadata = response.text
return metadata
Expand Down
3 changes: 2 additions & 1 deletion background/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
import json
import os

from flask import Flask, redirect, render_template, request
from google.cloud import firestore
from google.cloud import pubsub

from flask import Flask, render_template, redirect, request

app = Flask(__name__)

# Get client objects to reuse over multiple invocations
Expand Down
33 changes: 13 additions & 20 deletions background/app/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,23 @@
# limitations under the License.

import os
import pytest
import uuid

import google.auth
from google.cloud import firestore
from google.cloud import pubsub
import main
import pytest

os.environ['GOOGLE_CLOUD_PROJECT'] = os.environ['FIRESTORE_PROJECT_ID']
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.realpath(
os.path.join(
os.path.dirname(__file__),
'..',
'..',
'firestore-service-account.json'
)
)

credentials, project_id = google.auth.default()
os.environ['GOOGLE_CLOUD_PROJECT'] = project_id
SUBSCRIPTION_NAME = 'projects/{}/subscriptions/{}'.format(
os.getenv('FIRESTORE_PROJECT_ID'), 'test-' + str(uuid.uuid4())
project_id, 'test-' + str(uuid.uuid4())
)
TOPIC_NAME = 'projects/{}/topics/{}'.format(
project_id, 'translate'
)

import main


@pytest.yield_fixture
Expand Down Expand Up @@ -68,15 +64,12 @@ def publisher():
@pytest.yield_fixture
def subscriber():
subscriber = pubsub.SubscriberClient()
topic_name = 'projects/{}/topics/{}'.format(
os.getenv('FIRESTORE_PROJECT_ID'), 'translate'
)
subscription = subscriber.create_subscription(
SUBSCRIPTION_NAME, topic_name
subscriber.create_subscription(
SUBSCRIPTION_NAME, TOPIC_NAME
)
yield subscriber
subscriber.delete_subscription(SUBSCRIPTION_NAME)


def test_index(db, publisher):
main.app.testing = True
Expand Down Expand Up @@ -104,7 +97,7 @@ def test_translate(db, publisher, subscriber):

assert r.status_code < 400

response = subscriber.pull(SUBSCRIPTION_NAME, 1, timeout=2.0)
response = subscriber.pull(SUBSCRIPTION_NAME, 1, timeout=10.0)
assert len(response.received_messages) == 1
assert b'This is a test' in response.received_messages[0].message.data
assert b'fr' in response.received_messages[0].message.data
4 changes: 2 additions & 2 deletions background/function/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import base64
import hashlib
import json
import os

from google.cloud import firestore
from google.cloud import translate
Expand Down Expand Up @@ -72,6 +71,7 @@ def document_name(message):

# Note that document IDs should not contain the '/' character
name = base64.b64encode(hashed, altchars=b'+-').decode('utf-8')
return name


@firestore.transactional
Expand All @@ -81,7 +81,7 @@ def update_database(transaction, message):

try:
doc_ref.get(transaction=transaction)
except NotFound:
except firestore.NotFound:
return # Don't replace an existing translation

transaction.set(doc_ref, message)
Expand Down
15 changes: 1 addition & 14 deletions background/function/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,8 @@

import base64
import json
import os
import pytest

from google.cloud import firestore

os.environ['GOOGLE_CLOUD_PROJECT'] = os.environ['FIRESTORE_PROJECT_ID']
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.realpath(
os.path.join(
os.path.dirname(__file__),
'..',
'..',
'firestore-service-account.json'
)
)

import main


Expand All @@ -41,7 +28,7 @@ def clear_collection(collection):


def test_invocations():
db = firestore.Client(project=os.environ['FIRESTORE_PROJECT_ID'])
db = firestore.Client()
main.db = db

translations = db.collection('translations')
Expand Down
36 changes: 0 additions & 36 deletions conftest.py

This file was deleted.

4 changes: 1 addition & 3 deletions encrypt-secrets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
read -s -p "Enter password for encryption: " password
echo

tar cvf secrets.tar {service-account.json,firestore-service-account.json,config.py}
tar cvf secrets.tar service-account.json
openssl aes-256-cbc -k "$password" -in secrets.tar -out secrets.tar.enc
rm secrets.tar

travis encrypt "secrets_password=$password" --add
12 changes: 2 additions & 10 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
DIRS = [
# Hello world doesn't have system tests, just a lint test which will be
# covered by the global lint here.
'authenticating-users',
'background/app',
'background/function',
'sessions',
Expand Down Expand Up @@ -49,7 +50,7 @@ def run_test(session, dir):
'pytest',
*(PYTEST_COMMON_ARGS + session.posargs),
# Pytest will return 5 when no tests are collected. This can happen
# on travis where slow and flaky tests are excluded.
# when slow and flaky tests are excluded.
# See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
success_codes=[0, 5])

Expand All @@ -59,12 +60,3 @@ def run_test(session, dir):
def run_tests(session, dir=None):
"""Run all tests for all directories (slow!)"""
run_test(session, dir)


@nox.session
@nox.parametrize('dir', DIRS)
def travis(session, dir=None):
"""On travis, only run the py3.4 and cloudsql tests."""
run_tests(
session,
dir=dir)
Binary file modified secrets.tar.enc
Binary file not shown.
4 changes: 2 additions & 2 deletions sessions/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import random
from uuid import uuid4

from google.cloud import firestore
from flask import Flask, make_response, request
from google.cloud import firestore


app = Flask(__name__)
Expand Down Expand Up @@ -66,7 +66,7 @@ def home():
session = get_session_data(transaction, request.cookies.get('session_id'))

resp = make_response(template.format(
session['views'],
session['views'],
session['greeting']
)
)
Expand Down
12 changes: 1 addition & 11 deletions sessions/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import pytest
import re
import uuid

os.environ['GOOGLE_CLOUD_PROJECT'] = os.environ['FIRESTORE_PROJECT_ID']
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.realpath(
os.path.join(
os.path.dirname(__file__),
'..',
'firestore-service-account.json'
)
)

import main
import pytest


@pytest.fixture
Expand Down

0 comments on commit 9485a86

Please sign in to comment.