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

Moving compute engine samples to py.test #193

Merged
merged 1 commit into from
Feb 19, 2016
Merged
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
35 changes: 15 additions & 20 deletions compute/api/create_instance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion compute/autoscaler/demo/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
52 changes: 27 additions & 25 deletions compute/autoscaler/demo/frontend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down