From 748fa52cca843bdee6468ebb14b62f6020b23a9b Mon Sep 17 00:00:00 2001 From: Andres Riancho Date: Thu, 26 Dec 2013 13:12:50 -0300 Subject: [PATCH 1/4] Fix for https://github.com/spulec/moto/issues/72 Need to run the whole test suite to make sure no regressions were added --- moto/core/models.py | 15 ++++++++++++--- tests/test_core/test_nested.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 tests/test_core/test_nested.py diff --git a/moto/core/models.py b/moto/core/models.py index 17238fcb0..dd1f37947 100644 --- a/moto/core/models.py +++ b/moto/core/models.py @@ -7,9 +7,13 @@ from .utils import convert_regex_to_flask_path class MockAWS(object): + nested_count = 0 + def __init__(self, backend): self.backend = backend - HTTPretty.reset() + + if self.__class__.nested_count == 0: + HTTPretty.reset() def __call__(self, func): return self.decorate_callable(func) @@ -22,7 +26,9 @@ class MockAWS(object): def start(self): self.backend.reset() - HTTPretty.enable() + + if not HTTPretty.is_enabled(): + HTTPretty.enable() for method in HTTPretty.METHODS: for key, value in self.backend.urls.iteritems(): @@ -40,7 +46,10 @@ class MockAWS(object): ) def stop(self): - HTTPretty.disable() + self.__class__.nested_count -= 1 + + if self.__class__.nested_count == 0: + HTTPretty.disable() def decorate_callable(self, func): def wrapper(*args, **kwargs): diff --git a/tests/test_core/test_nested.py b/tests/test_core/test_nested.py new file mode 100644 index 000000000..9ae366a50 --- /dev/null +++ b/tests/test_core/test_nested.py @@ -0,0 +1,28 @@ +import unittest + +from boto.sqs.connection import SQSConnection +from boto.sqs.message import Message +from boto.ec2 import EC2Connection + +from moto import mock_sqs, mock_ec2 + + +class TestNestedDecorators(unittest.TestCase): + + @mock_sqs + def setup_sqs_queue(self): + conn = SQSConnection() + q = conn.create_queue('some-queue') + + m = Message() + m.set_body('This is my first message.') + q.write(m) + + self.assertEqual(q.count(), 1) + + @mock_ec2 + def test_nested(self): + self.setup_sqs_queue() + + conn = EC2Connection() + conn.run_instances('ami-123456') \ No newline at end of file From b331e8ee8e0f4fbf8b76677b9630ee3b11e36aab Mon Sep 17 00:00:00 2001 From: Andres Riancho Date: Thu, 26 Dec 2013 13:42:41 -0300 Subject: [PATCH 2/4] Ignore pycharm metadata --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e17800a77..7d3471a59 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ dist/* *.pyc .noseids build/ +.idea/ From f843a1bba9f0016331c7ca0994d51fc04effe1d7 Mon Sep 17 00:00:00 2001 From: Andres Riancho Date: Thu, 26 Dec 2013 13:43:18 -0300 Subject: [PATCH 3/4] Ignore backup files --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 7d3471a59..923d72edd 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ dist/* .tox .coverage *.pyc +*~ .noseids build/ .idea/ + From 186b40de54e3b294b2ee81c87c44acf5c25b1c17 Mon Sep 17 00:00:00 2001 From: Andres Riancho Date: Thu, 26 Dec 2013 13:52:03 -0300 Subject: [PATCH 4/4] All tests pass! Fixes https://github.com/spulec/moto/issues/72 --- moto/core/models.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/moto/core/models.py b/moto/core/models.py index dd1f37947..e3777741f 100644 --- a/moto/core/models.py +++ b/moto/core/models.py @@ -25,6 +25,7 @@ class MockAWS(object): self.stop() def start(self): + self.__class__.nested_count += 1 self.backend.reset() if not HTTPretty.is_enabled(): @@ -48,6 +49,9 @@ class MockAWS(object): def stop(self): self.__class__.nested_count -= 1 + if self.__class__.nested_count < 0: + raise RuntimeError('Called stop() before start().') + if self.__class__.nested_count == 0: HTTPretty.disable()