8000 Moving compute engine samples to py.test · rodmur/python-docs-samples@d756f9f · GitHub
[go: up one dir, main page]

Skip to content

Commit d756f9f

Browse files
author
Jon Wayne Parrott
committed
Moving compute engine samples to py.test
1 parent 64812dd commit d756f9f

File tree

4 files changed

+44
-47
lines changed

4 files changed

+44
-47
lines changed

compute/api/create_instance_test.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,23 @@
1414
import re
1515

1616
from create_instance import main
17-
import pytest
18-
import testing
17+
from testing import mark_flaky
1918

2019

21-
@pytest.mark.slow
22-
class TestComputeGettingStarted(testing.CloudTest):
20+
@mark_flaky
21+
def test_main(cloud_config, capsys):
22+
main(
23+
cloud_config.GCLOUD_PROJECT,
24+
cloud_config.CLOUD_STORAGE_BUCKET,
25+
'us-central1-f',
26+
'test-instance',
27+
wait=False)
2328

24-
def test_main(self):
25-
with testing.capture_stdout() as mock_stdout:
26-
main(
27-
self.config.GCLOUD_PROJECT,
28-
self.config.CLOUD_STORAGE_BUCKET,
29-
'us-central1-f',
30-
'test-instance',
31-
wait=False)
29+
out, _ = capsys.readouterr()
3230

33-
stdout = mock_stdout.getvalue()
31+
expected_output = re.compile(
32+
(r'Instances in project .* and zone us-central1-.* - test-instance'
33+
r'.*Deleting instance.*done..$'),
34+
re.DOTALL)
3435

35-
expected_output = re.compile(
36-
(r'Instances in project %s and zone us-central1-.* - test-instance'
37-
r'.*Deleting instance.*done..$') % self.config.GCLOUD_PROJECT,
38-
re.DOTALL)
39-
self.assertRegexpMatches(
40-
stdout,
41-
expected_output)
36+
assert re.search(expected_output, out)

compute/autoscaler/demo/frontend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get_user_cputime(self):
4646
return os.times()[0]
4747

4848
def busy_wait(self):
49-
for _ in xrange(100000):
49+
for _ in range(100000):
5050
pass
5151

5252
def burn_cpu(self):

compute/autoscaler/demo/frontend_test.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import unittest
16-
1715
import frontend
16+
import pytest
1817

1918

2019
class FakeTime(object):
@@ -40,30 +39,33 @@ def busy_wait(self):
4039
self.cpu_time += self.cpu_time_step
4140

4241

43-
class TestHandlers(unittest.TestCase):
44-
def setUp(self):
45-
self.fake_time = FakeTime()
46-
self.cpu_burner = frontend.CpuBurner()
47-
self.cpu_burner.get_user_cputime = self.fake_time.get_user_cputime
48-
self.cpu_burner.get_walltime = self.fake_time.get_walltime
49-
self.cpu_burner.busy_wait = self.fake_time.busy_wait
42+
@pytest.fixture
43+
def faketime():
44+
return FakeTime()
45+
46+
47+
@pytest.fixture
48+
def cpuburner(faketime):
49+
cpuburner = frontend.CpuBurner()
50+
cpuburner.get_user_cputime = faketime.get_user_cputime
51+
cpuburner.get_walltime = faketime.get_walltime
52+
cpuburner.busy_wait = faketime.busy_wait
53+
return cpuburner
5054

51-
# In this test scenario CPU time advances at 25% of the wall time speed.
52-
# Given the request requires 1 CPU core second, we expect it to finish
53-
# within the timeout (5 seconds) and return success.
54-
def test_ok_response(self):
55-
self.fake_time.cpu_time_step = 0.25
56-
(code, _) = self.cpu_burner.handle_http_request()
57-
self.assertEqual(200, code)
5855

59-
# In this test scenario CPU time advances at 15% of the wall time speed.
60-
# Given the request requires 1 CPU core second, we expect it to timeout
61-
# after 5 simulated wall time seconds and return error 500.
62-
def test_timeout(self):
63-
self.fake_time.cpu_time_step = 0.15
64-
(code, _) = self.cpu_burner.handle_http_request()
65-
self.assertEqual(500, code)
56+
# In this test scenario CPU time advances at 25% of the wall time speed.
57+
# Given the request requires 1 CPU core second, we expect it to finish
58+
# within the timeout (5 seconds) and return success.
59+
def test_ok_response(faketime, cpuburner):
60+
faketime.cpu_time_step = 0.25
61+
(code, _) = cpuburner.handle_http_request()
62+
assert code == 200
6663

6764

68-
if __name__ == '__main__':
69-
unittest.main()
65+
# In this test scenario CPU time advances at 15% of the wall time speed.
66+
# Given the request requires 1 CPU core second, we expect it to timeout
67+
# after 5 simulated wall time seconds and return error 500.
68+
def test_timeout(faketime, cpuburner):
69+
faketime.cpu_time_step = 0.15
70+
(code, _) = cpuburner.handle_http_request()
71+
assert code == 500

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ examples =
1212
blog/introduction_to_data_models_in_cloud_datastore
1313
cloud_logging/api
1414
compute/api
15-
compute/autoscaler
15+
compute/autoscaler/demo
1616
datastore/api
1717
managed_vms/cloudsql
1818
managed_vms/datastore

0 commit comments

Comments
 (0)
0