From 7339d4d78e1b8dd4dfad158aff4a72000f812428 Mon Sep 17 00:00:00 2001 From: Ali-Akber Saifee Date: Tue, 19 May 2020 11:03:52 -0700 Subject: [PATCH] Use docker for setting up backends for tests --- .github/workflows/main.yml | 11 ++--------- docker-compose.yml | 11 +++++++++++ tests/__init__.py | 2 +- tests/test_flask_ext.py | 26 +++++++++++++------------- tests/test_regressions.py | 8 ++++---- 5 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 docker-compose.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d8c4bb17..0b3a35db 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,17 +12,10 @@ jobs: strategy: matrix: python-version: [2.7, pypy2, 3.5, 3.7, 3.8, pypy3] - services: - redis: - image: redis - ports: - - 6379:6379 - memcached: - image: memcached - ports: - - 11211:11211 steps: - uses: actions/checkout@v2 + - name: Start backend services + run: docker-compose up -d - name: Cache dependencies uses: actions/cache@v1 with: diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..ec835dbd --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3.2' + +services: + memcached: + image: memcached + ports: + - 31211:11211 + redis: + image: redis + ports: + - 36379:6379 diff --git a/tests/__init__.py b/tests/__init__.py index 5f5d42ea..ec52a370 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -11,7 +11,7 @@ class FlaskLimiterTestCase(unittest.TestCase): def setUp(self): - redis.Redis().flushall() + redis.from_url("redis://localhost:36379").flushall() def build_app(self, config={}, **limiter_args): app = Flask(__name__) diff --git a/tests/test_flask_ext.py b/tests/test_flask_ext.py index cf7504d5..cf52f5cf 100644 --- a/tests/test_flask_ext.py +++ b/tests/test_flask_ext.py @@ -44,10 +44,10 @@ def test_constructor_arguments_over_config(self): strategy='moving-window', key_func=get_remote_address ) limiter.init_app(app) - app.config.setdefault(C.STORAGE_URL, "redis://localhost:6379") + app.config.setdefault(C.STORAGE_URL, "redis://localhost:36379") self.assertEqual(type(limiter._limiter), MovingWindowRateLimiter) limiter = Limiter( - storage_uri='memcached://localhost:11211', + storage_uri='memcached://localhost:31211', key_func=get_remote_address ) limiter.init_app(app) @@ -191,7 +191,7 @@ def test_fallback_to_memory_config(self): _, limiter = self.build_app( config={C.ENABLED: True}, default_limits=["5/minute"], - storage_uri="redis://localhost:6379", + storage_uri="redis://localhost:36379", in_memory_fallback=["1/minute"] ) self.assertEqual(len(limiter._in_memory_fallback), 1) @@ -201,7 +201,7 @@ def test_fallback_to_memory_config(self): config={C.ENABLED: True, C.IN_MEMORY_FALLBACK: "1/minute"}, default_limits=["5/minute"], - storage_uri="redis://localhost:6379", + storage_uri="redis://localhost:36379", ) self.assertEqual(len(limiter._in_memory_fallback), 1) self.assertEqual(limiter._in_memory_fallback_enabled, True) @@ -209,14 +209,14 @@ def test_fallback_to_memory_config(self): _, limiter = self.build_app( config={C.ENABLED: True, C.IN_MEMORY_FALLBACK_ENABLED: True}, global_limits=["5/minute"], - storage_uri="redis://localhost:6379", + storage_uri="redis://localhost:36379", ) self.assertEqual(limiter._in_memory_fallback_enabled, True) _, limiter = self.build_app( config={C.ENABLED: True}, global_limits=["5/minute"], - storage_uri="redis://localhost:6379", + storage_uri="redis://localhost:36379", in_memory_fallback_enabled=True ) @@ -224,7 +224,7 @@ def test_fallback_to_memory_backoff_check(self): app, limiter = self.build_app( config={C.ENABLED: True}, default_limits=["5/minute"], - storage_uri="redis://localhost:6379", + storage_uri="redis://localhost:36379", in_memory_fallback=["1/minute"] ) @@ -276,7 +276,7 @@ def test_fallback_to_memory_with_global_override(self): app, limiter = self.build_app( config={C.ENABLED: True}, default_limits=["5/minute"], - storage_uri="redis://localhost:6379", + storage_uri="redis://localhost:36379", in_memory_fallback=["1/minute"] ) @@ -327,7 +327,7 @@ def test_fallback_to_memory(self): app, limiter = self.build_app( config={C.ENABLED: True}, global_limits=["2/minute"], - storage_uri="redis://localhost:6379", + storage_uri="redis://localhost:36379", in_memory_fallback_enabled=True, headers_enabled=True ) @@ -1337,7 +1337,7 @@ def null(): def test_reset_unsupported(self): app, limiter = self.build_app({ C.GLOBAL_LIMITS: "1 per day", - C.STORAGE_URL: 'memcached://localhost:11211' + C.STORAGE_URL: 'memcached://localhost:31211' }) @app.route("/") @@ -1846,13 +1846,13 @@ def _(): def test_custom_key_prefix(self): app1, limiter1 = self.build_app( - key_prefix="moo", storage_uri="redis://localhost:6379" + key_prefix="moo", storage_uri="redis://localhost:36379" ) app2, limiter2 = self.build_app( {C.KEY_PREFIX: "cow"}, - storage_uri="redis://localhost:6379" + storage_uri="redis://localhost:36379" ) - app3, limiter3 = self.build_app(storage_uri="redis://localhost:6379") + app3, limiter3 = self.build_app(storage_uri="redis://localhost:36379") @app1.route("/test") @limiter1.limit("1/day") diff --git a/tests/test_regressions.py b/tests/test_regressions.py index e4fdfd71..a628d024 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -14,7 +14,7 @@ def test_redis_request_slower_than_fixed_window(self): app, limiter = self.build_app( { C.GLOBAL_LIMITS: "5 per second", - C.STORAGE_URL: "redis://localhost:6379", + C.STORAGE_URL: "redis://localhost:36379", C.STRATEGY: "fixed-window", C.HEADERS_ENABLED: True } @@ -33,7 +33,7 @@ def test_redis_request_slower_than_moving_window(self): app, limiter = self.build_app( { C.GLOBAL_LIMITS: "5 per second", - C.STORAGE_URL: "redis://localhost:6379", + C.STORAGE_URL: "redis://localhost:36379", C.STRATEGY: "moving-window", C.HEADERS_ENABLED: True } @@ -95,12 +95,12 @@ def t1(): def test_custom_key_prefix_with_headers(self): app1, limiter1 = self.build_app( key_prefix="moo", - storage_uri="redis://localhost:6379", + storage_uri="redis://localhost:36379", headers_enabled=True ) app2, limiter2 = self.build_app( key_prefix="cow", - storage_uri="redis://localhost:6379", + storage_uri="redis://localhost:36379", headers_enabled=True )