Skip to content

Commit 56dda62

Browse files
committed
moving mock into each individual test
1 parent 7dfa3c2 commit 56dda62

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed
Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import os
2-
3-
import mock
41
import pytest
52

63
from .support import ENDPOINT
@@ -20,17 +17,3 @@ def instance():
2017
return {"openmetrics_endpoint": ENDPOINT, "tags": ["instance"], "tls_verify": "false"}
2118

2219

23-
@pytest.fixture()
24-
def mock_http_response():
25-
f_name = os.path.join(os.path.dirname(__file__), "data", "metrics.txt")
26-
with open(f_name, "r") as f:
27-
text_data = f.read()
28-
with mock.patch(
29-
"requests.get",
30-
return_value=mock.MagicMock(
31-
status_code=200,
32-
iter_lines=lambda **kwargs: text_data.split("\n"),
33-
headers={"Content-Type": "text/plain"},
34-
),
35-
):
36-
yield

redis_enterprise_prometheus/tests/test_redis_enterprise_prometheus.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import ssl
23
from copy import deepcopy
34
from typing import Any, Callable, Dict # noqa: F401
@@ -14,8 +15,16 @@
1415
ssl._create_default_https_context = ssl._create_unverified_context
1516

1617

17-
@pytest.mark.unit
18+
@pytest.mark.unit
1819
def test_instance_additional_check(aggregator, dd_run_check, mock_http_response):
20+
# Load test data
21+
f_name = os.path.join(os.path.dirname(__file__), "data", "metrics.txt")
22+
with open(f_name, "r") as f:
23+
text_data = f.read()
24+
25+
# Mock the HTTP response
26+
mock_http_response(text_data, status_code=200, headers={"Content-Type": "text/plain"})
27+
1928
# add additional metric groups for validation
2029
additional_metric_groups = ["REDIS2.DISK", "REDIS2.REPLICATION", "REDIS2.SEARCH"]
2130
instance = deepcopy(INSTANCE)
@@ -37,6 +46,14 @@ def test_instance_additional_check(aggregator, dd_run_check, mock_http_response)
3746

3847
@pytest.mark.unit
3948
def test_instance_all_additional_check(aggregator, dd_run_check, mock_http_response):
49+
# Load test data
50+
f_name = os.path.join(os.path.dirname(__file__), "data", "metrics.txt")
51+
with open(f_name, "r") as f:
52+
text_data = f.read()
53+
54+
# Mock the HTTP response
55+
mock_http_response(text_data, status_code=200, headers={"Content-Type": "text/plain"})
56+
4057
# add additional metric groups for validation
4158
additional_metric_groups = [
4259
"REDIS2.REPLICATION",
@@ -70,6 +87,14 @@ def test_instance_all_additional_check(aggregator, dd_run_check, mock_http_respo
7087

7188
@pytest.mark.unit
7289
def test_instance_exclude_metrics(aggregator, dd_run_check, mock_http_response):
90+
# Load test data
91+
f_name = os.path.join(os.path.dirname(__file__), "data", "metrics.txt")
92+
with open(f_name, "r") as f:
93+
text_data = f.read()
94+
95+
# Mock the HTTP response
96+
mock_http_response(text_data, status_code=200, headers={"Content-Type": "text/plain"})
97+
7398
# validate exclude_metrics are not present in metrics
7499
exclude_metrics = ["endpoint_client_connections", "redis_server_up"]
75100
instance = deepcopy(INSTANCE)
@@ -90,6 +115,14 @@ def test_end_to_end():
90115

91116
@pytest.mark.unit
92117
def test_instance_invalid_group_check(aggregator, dd_run_check, mock_http_response):
118+
# Load test data
119+
f_name = os.path.join(os.path.dirname(__file__), "data", "metrics.txt")
120+
with open(f_name, "r") as f:
121+
text_data = f.read()
122+
123+
# Mock the HTTP response
124+
mock_http_response(text_data, status_code=200, headers={"Content-Type": "text/plain"})
125+
93126
instance = deepcopy(INSTANCE)
94127
instance["metric_groups"] = ["redis.bogus", "redis.raft"]
95128

@@ -105,6 +138,14 @@ def test_instance_invalid_group_check(aggregator, dd_run_check, mock_http_respon
105138

106139
@pytest.mark.unit
107140
def test_invalid_instance(aggregator, dd_run_check, mock_http_response):
141+
# Load test data
142+
f_name = os.path.join(os.path.dirname(__file__), "data", "metrics.txt")
143+
with open(f_name, "r") as f:
144+
text_data = f.read()
145+
146+
# Mock the HTTP response
147+
mock_http_response(text_data, status_code=200, headers={"Content-Type": "text/plain"})
148+
108149
instance = deepcopy(ERSATZ_INSTANCE)
109150
instance.pop("openmetrics_endpoint")
110151

0 commit comments

Comments
 (0)