From d756f9f219b61b226695e9c66235916cb5709b38 Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Fri, 19 Feb 2016 12:31:19 -0800 Subject: [PATCH] Moving compute engine samples to py.test --- compute/api/create_instance_test.py | 35 +++++++--------- compute/autoscaler/demo/frontend.py | 2 +- compute/autoscaler/demo/frontend_test.py | 52 ++++++++++++------------ tox.ini | 2 +- 4 files changed, 44 insertions(+), 47 deletions(-) diff --git a/compute/api/create_instance_test.py b/compute/api/create_instance_test.py index beb7c93f85b..70de82a33c1 100644 --- a/compute/api/create_instance_test.py +++ b/compute/api/create_instance_test.py @@ -14,28 +14,23 @@ import re from create_instance import main -import pytest -import testing +from testing import mark_flaky -@pytest.mark.slow -class TestComputeGettingStarted(testing.CloudTest): +@mark_flaky +def test_main(cloud_config, capsys): + main( + cloud_config.GCLOUD_PROJECT, + cloud_config.CLOUD_STORAGE_BUCKET, + 'us-central1-f', + 'test-instance', + wait=False) - def test_main(self): - with testing.capture_stdout() as mock_stdout: - main( - self.config.GCLOUD_PROJECT, - self.config.CLOUD_STORAGE_BUCKET, - 'us-central1-f', - 'test-instance', - wait=False) + out, _ = capsys.readouterr() - stdout = mock_stdout.getvalue() + expected_output = re.compile( + (r'Instances in project .* and zone us-central1-.* - test-instance' + r'.*Deleting instance.*done..$'), + re.DOTALL) - expected_output = re.compile( - (r'Instances in project %s and zone us-central1-.* - test-instance' - r'.*Deleting instance.*done..$') % self.config.GCLOUD_PROJECT, - re.DOTALL) - self.assertRegexpMatches( - stdout, - expected_output) + assert re.search(expected_output, out) diff --git a/compute/autoscaler/demo/frontend.py b/compute/autoscaler/demo/frontend.py index c0d1ae60030..294cb2f45d0 100644 --- a/compute/autoscaler/demo/frontend.py +++ b/compute/autoscaler/demo/frontend.py @@ -46,7 +46,7 @@ def get_user_cputime(self): return os.times()[0] def busy_wait(self): - for _ in xrange(100000): + for _ in range(100000): pass def burn_cpu(self): diff --git a/compute/autoscaler/demo/frontend_test.py b/compute/autoscaler/demo/frontend_test.py index dc9deb3bad4..09bae5d8e77 100644 --- a/compute/autoscaler/demo/frontend_test.py +++ b/compute/autoscaler/demo/frontend_test.py @@ -12,9 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import unittest - import frontend +import pytest class FakeTime(object): @@ -40,30 +39,33 @@ def busy_wait(self): self.cpu_time += self.cpu_time_step -class TestHandlers(unittest.TestCase): - def setUp(self): - self.fake_time = FakeTime() - self.cpu_burner = frontend.CpuBurner() - self.cpu_burner.get_user_cputime = self.fake_time.get_user_cputime - self.cpu_burner.get_walltime = self.fake_time.get_walltime - self.cpu_burner.busy_wait = self.fake_time.busy_wait +@pytest.fixture +def faketime(): + return FakeTime() + + +@pytest.fixture +def cpuburner(faketime): + cpuburner = frontend.CpuBurner() + cpuburner.get_user_cputime = faketime.get_user_cputime + cpuburner.get_walltime = faketime.get_walltime + cpuburner.busy_wait = faketime.busy_wait + return cpuburner - # In this test scenario CPU time advances at 25% of the wall time speed. - # Given the request requires 1 CPU core second, we expect it to finish - # within the timeout (5 seconds) and return success. - def test_ok_response(self): - self.fake_time.cpu_time_step = 0.25 - (code, _) = self.cpu_burner.handle_http_request() - self.assertEqual(200, code) - # In this test scenario CPU time advances at 15% of the wall time speed. - # Given the request requires 1 CPU core second, we expect it to timeout - # after 5 simulated wall time seconds and return error 500. - def test_timeout(self): - self.fake_time.cpu_time_step = 0.15 - (code, _) = self.cpu_burner.handle_http_request() - self.assertEqual(500, code) +# In this test scenario CPU time advances at 25% of the wall time speed. +# Given the request requires 1 CPU core second, we expect it to finish +# within the timeout (5 seconds) and return success. +def test_ok_response(faketime, cpuburner): + faketime.cpu_time_step = 0.25 + (code, _) = cpuburner.handle_http_request() + assert code == 200 -if __name__ == '__main__': - unittest.main() +# In this test scenario CPU time advances at 15% of the wall time speed. +# Given the request requires 1 CPU core second, we expect it to timeout +# after 5 simulated wall time seconds and return error 500. +def test_timeout(faketime, cpuburner): + faketime.cpu_time_step = 0.15 + (code, _) = cpuburner.handle_http_request() + assert code == 500 diff --git a/tox.ini b/tox.ini index b9bc6a1d960..4946f9cd4cb 100644 --- a/tox.ini +++ b/tox.ini @@ -12,7 +12,7 @@ examples = blog/introduction_to_data_models_in_cloud_datastore cloud_logging/api compute/api - compute/autoscaler + compute/autoscaler/demo datastore/api managed_vms/cloudsql managed_vms/datastore