Skip to content

Commit

Permalink
Merge pull request #1349 from dhermes/move-system-test-runner-to-python
Browse files Browse the repository at this point in the history
Move system test runner from bash to Python
  • Loading branch information
dhermes committed Jan 7, 2016
2 parents 972e875 + d6c3b87 commit b5946a1
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 132 deletions.
113 changes: 113 additions & 0 deletions scripts/attempt_system_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Attempt to run system tests.
If the system tests are being run on Travis, no test need be run if
the build is for a PR and not a merged commit.
If being run as part of a Travis build for a merged commit, the
encrypted `key.json` file need be decrypted before running tests.
"""


import os
import subprocess
import sys

from system_tests.run_system_test import run_module_tests


MODULES = (
'datastore',
'storage',
'pubsub',
'bigquery',
)
SCRIPTS_DIR = os.path.dirname(__file__)
ENCRYPTED_KEYFILE = os.path.abspath(
os.path.join(SCRIPTS_DIR, '..', 'system_tests', 'key.json.enc'))


def check_environment():
"""Check what environment this is running in.
In particular, if the environment is Travis.
:rtype: tuple
:returns: A pair of booleans. The first indicates if the test
is running in Travis and the second indicates if
the current build is a non-PR for a merge to master.
"""
if os.getenv('TRAVIS') == 'true':
is_travis = True
non_pr = (os.getenv('TRAVIS_PULL_REQUEST') == 'false' and
os.getenv('TRAVIS_BRANCH') == 'master')
else:
is_travis = non_pr = False

return is_travis, non_pr


def decrypt_keyfile():
"""Decrypt a keyfile."""
print('Running in Travis during merge, decrypting stored '
'key file.')

encrypted_key = os.getenv('encrypted_a1b222e8c14d_key')
encrypted_iv = os.getenv('encrypted_a1b222e8c14d_iv')
out_file = os.getenv('GOOGLE_APPLICATION_CREDENTIALS')
# Convert encrypted key file into decrypted file to be used.
subprocess.call([
'openssl', 'aes-256-cbc',
'-K', encrypted_key,
'-iv', encrypted_iv,
'-in', ENCRYPTED_KEYFILE,
'-out', out_file, '-d'
])


def prepare_to_run():
"""Prepare to run system tests.
If on Travis during a PR, exit the entire program; there is
no need to run the system tests.
If on Travis during a build for a non-PR merge to master,
decrypts stored keyfile.
"""
is_travis, non_pr = check_environment()
# Nothing to prepare outside of Travis. Proceed to tests.
if not is_travis:
return

# On a Travis PR, exit the program.
if not non_pr:
print('Running in Travis during non-merge to master, '
'doing nothing.')
sys.exit(0)

# On a Travis build for a merge commit to master, decrypt.
decrypt_keyfile()


def main():
"""Run all the system tests if necessary."""
prepare_to_run()
for module in MODULES:
run_module_tests(module)


if __name__ == '__main__':
main()
41 changes: 0 additions & 41 deletions scripts/run_system_tests.sh

This file was deleted.

46 changes: 29 additions & 17 deletions system_tests/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,23 @@
from gcloud import bigquery


_helpers.PROJECT = TESTS_PROJECT
CLIENT = bigquery.Client()
DATASET_NAME = 'system_tests_%012d' % (1000 * time.time(),)


class Config(object):
"""Run-time configuration to be modified at set-up.
This is a mutable stand-in to allow test set-up to modify
global state.
"""
CLIENT = None


def setUpModule():
_helpers.PROJECT = TESTS_PROJECT
Config.CLIENT = bigquery.Client()


class TestBigQuery(unittest2.TestCase):

def setUp(self):
Expand All @@ -37,26 +49,26 @@ def tearDown(self):
doomed.delete()

def test_create_dataset(self):
dataset = CLIENT.dataset(DATASET_NAME)
dataset = Config.CLIENT.dataset(DATASET_NAME)
self.assertFalse(dataset.exists())
dataset.create()
self.to_delete.append(dataset)
self.assertTrue(dataset.exists())
self.assertEqual(dataset.name, DATASET_NAME)

def test_reload_dataset(self):
dataset = CLIENT.dataset(DATASET_NAME)
dataset = Config.CLIENT.dataset(DATASET_NAME)
dataset.friendly_name = 'Friendly'
dataset.description = 'Description'
dataset.create()
self.to_delete.append(dataset)
other = CLIENT.dataset(DATASET_NAME)
other = Config.CLIENT.dataset(DATASET_NAME)
other.reload()
self.assertEqual(other.friendly_name, 'Friendly')
self.assertEqual(other.description, 'Description')

def test_patch_dataset(self):
dataset = CLIENT.dataset(DATASET_NAME)
dataset = Config.CLIENT.dataset(DATASET_NAME)
self.assertFalse(dataset.exists())
dataset.create()
self.to_delete.append(dataset)
Expand All @@ -68,7 +80,7 @@ def test_patch_dataset(self):
self.assertEqual(dataset.description, 'Description')

def test_update_dataset(self):
dataset = CLIENT.dataset(DATASET_NAME)
dataset = Config.CLIENT.dataset(DATASET_NAME)
self.assertFalse(dataset.exists())
dataset.create()
self.to_delete.append(dataset)
Expand All @@ -90,20 +102,20 @@ def test_list_datasets(self):
'newest%d' % (1000 * time.time(),),
]
for dataset_name in datasets_to_create:
dataset = CLIENT.dataset(dataset_name)
dataset = Config.CLIENT.dataset(dataset_name)
dataset.create()
self.to_delete.append(dataset)

# Retrieve the datasets.
all_datasets, token = CLIENT.list_datasets()
all_datasets, token = Config.CLIENT.list_datasets()
self.assertTrue(token is None)
created = [dataset for dataset in all_datasets
if dataset.name in datasets_to_create and
dataset.project == CLIENT.project]
dataset.project == Config.CLIENT.project]
self.assertEqual(len(created), len(datasets_to_create))

def test_create_table(self):
dataset = CLIENT.dataset(DATASET_NAME)
dataset = Config.CLIENT.dataset(DATASET_NAME)
self.assertFalse(dataset.exists())
dataset.create()
self.to_delete.append(dataset)
Expand All @@ -119,7 +131,7 @@ def test_create_table(self):
self.assertEqual(table.name, TABLE_NAME)

def test_list_tables(self):
dataset = CLIENT.dataset(DATASET_NAME)
dataset = Config.CLIENT.dataset(DATASET_NAME)
self.assertFalse(dataset.exists())
dataset.create()
self.to_delete.append(dataset)
Expand All @@ -145,7 +157,7 @@ def test_list_tables(self):
self.assertEqual(len(created), len(tables_to_create))

def test_patch_table(self):
dataset = CLIENT.dataset(DATASET_NAME)
dataset = Config.CLIENT.dataset(DATASET_NAME)
self.assertFalse(dataset.exists())
dataset.create()
self.to_delete.append(dataset)
Expand All @@ -165,7 +177,7 @@ def test_patch_table(self):
self.assertEqual(table.description, 'Description')

def test_update_table(self):
dataset = CLIENT.dataset(DATASET_NAME)
dataset = Config.CLIENT.dataset(DATASET_NAME)
self.assertFalse(dataset.exists())
dataset.create()
self.to_delete.append(dataset)
Expand Down Expand Up @@ -203,7 +215,7 @@ def test_load_table_then_dump_table(self):
('Bhettye Rhubble', 27, None),
]
ROW_IDS = range(len(ROWS))
dataset = CLIENT.dataset(DATASET_NAME)
dataset = Config.CLIENT.dataset(DATASET_NAME)
self.assertFalse(dataset.exists())
dataset.create()
self.to_delete.append(dataset)
Expand Down Expand Up @@ -270,7 +282,7 @@ def test_load_table_from_storage_then_dump_table(self):

self.to_delete.insert(0, blob)

dataset = CLIENT.dataset(DATASET_NAME)
dataset = Config.CLIENT.dataset(DATASET_NAME)
dataset.create()
self.to_delete.append(dataset)

Expand All @@ -281,7 +293,7 @@ def test_load_table_from_storage_then_dump_table(self):
table.create()
self.to_delete.insert(0, table)

job = CLIENT.load_table_from_storage(
job = Config.CLIENT.load_table_from_storage(
'bq_load_storage_test_%d' % (TIMESTAMP,), table, GS_URL)
job.create_disposition = 'CREATE_NEVER'
job.skip_leading_rows = 1
Expand Down
Loading

0 comments on commit b5946a1

Please sign in to comment.