Skip to content

Commit 8cbd9d4

Browse files
author
Andrew Xue
authored
Update resources tests (open-telemetry#893)
1 parent 09df35c commit 8cbd9d4

File tree

3 files changed

+61
-32
lines changed

3 files changed

+61
-32
lines changed

ext/opentelemetry-exporter-cloud-trace/tests/test_cloud_trace_exporter.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,10 @@ def test_extract_links(self):
305305
),
306306
)
307307

308-
def test_extract_resources(self):
308+
def test_extract_empty_resources(self):
309309
self.assertEqual(_extract_resources(Resource.create_empty()), {})
310+
311+
def test_extract_well_formed_resources(self):
310312
resource = Resource(
311313
labels={
312314
"cloud.account.id": 123,
@@ -325,19 +327,19 @@ def test_extract_resources(self):
325327
}
326328
self.assertEqual(_extract_resources(resource), expected_extract)
327329

330+
def test_extract_malformed_resources(self):
331+
# This resource doesn't have all the fields required for a gce_instance
332+
# Specifically its missing "host.id", "cloud.zone", "cloud.account.id"
328333
resource = Resource(
329334
labels={
330-
"cloud.account.id": "123",
331-
"host.id": "host",
332-
"extra_info": "extra",
333-
"not_gcp_resource": "value",
334335
"gcp.resource_type": "gce_instance",
335336
"cloud.provider": "gcp",
336337
}
337338
)
338339
# Should throw when passed a malformed GCP resource dict
339340
self.assertRaises(KeyError, _extract_resources, resource)
340341

342+
def test_extract_unsupported_gcp_resources(self):
341343
resource = Resource(
342344
labels={
343345
"cloud.account.id": "123",
@@ -350,6 +352,8 @@ def test_extract_resources(self):
350352
)
351353
self.assertEqual(_extract_resources(resource), {})
352354

355+
def test_extract_unsupported_provider_resources(self):
356+
# Resources with currently unsupported providers will be ignored
353357
resource = Resource(
354358
labels={
355359
"cloud.account.id": "123",

ext/opentelemetry-exporter-cloud-trace/tests/test_gcp_resource_detector.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def test_finding_resources(self, getter):
6767
)
6868
self.assertEqual(getter.call_count, 1)
6969

70+
# Found resources should be cached and not require another network call
7071
found_resources = resource_finder.detect()
7172
self.assertEqual(getter.call_count, 1)
7273
self.assertEqual(

opentelemetry-sdk/tests/resources/test_resources.py

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,13 @@ def test_immutability(self):
8686
labels["cost"] = 999.91
8787
self.assertEqual(resource.labels, labels_copy)
8888

89-
def test_otel_resource_detector(self):
90-
detector = resources.OTELResourceDetector()
91-
self.assertEqual(detector.detect(), resources.Resource.create_empty())
92-
os.environ["OTEL_RESOURCE"] = "k=v"
93-
self.assertEqual(detector.detect(), resources.Resource({"k": "v"}))
94-
os.environ["OTEL_RESOURCE"] = " k = v "
95-
self.assertEqual(detector.detect(), resources.Resource({"k": "v"}))
96-
os.environ["OTEL_RESOURCE"] = "k=v,k2=v2"
97-
self.assertEqual(
98-
detector.detect(), resources.Resource({"k": "v", "k2": "v2"})
99-
)
100-
os.environ["OTEL_RESOURCE"] = " k = v , k2 = v2 "
101-
self.assertEqual(
102-
detector.detect(), resources.Resource({"k": "v", "k2": "v2"})
103-
)
104-
105-
def test_aggregated_resources(self):
89+
def test_aggregated_resources_no_detectors(self):
10690
aggregated_resources = resources.get_aggregated_resources([])
10791
self.assertEqual(
10892
aggregated_resources, resources.Resource.create_empty()
10993
)
11094

95+
def test_aggregated_resources_with_static_resource(self):
11196
static_resource = resources.Resource({"static_key": "static_value"})
11297

11398
self.assertEqual(
@@ -117,9 +102,10 @@ def test_aggregated_resources(self):
117102
static_resource,
118103
)
119104

105+
# Static resource values should never be overwritten
120106
resource_detector = mock.Mock(spec=resources.ResourceDetector)
121107
resource_detector.detect.return_value = resources.Resource(
122-
{"static_key": "overwrite_existing_value", "key": "value"}
108+
{"static_key": "try_to_overwrite_existing_value", "key": "value"}
123109
)
124110
self.assertEqual(
125111
resources.get_aggregated_resources(
@@ -128,6 +114,7 @@ def test_aggregated_resources(self):
128114
resources.Resource({"static_key": "static_value", "key": "value"}),
129115
)
130116

117+
def test_aggregated_resources_multiple_detectors(self):
131118
resource_detector1 = mock.Mock(spec=resources.ResourceDetector)
132119
resource_detector1.detect.return_value = resources.Resource(
133120
{"key1": "value1"}
@@ -139,11 +126,13 @@ def test_aggregated_resources(self):
139126
resource_detector3 = mock.Mock(spec=resources.ResourceDetector)
140127
resource_detector3.detect.return_value = resources.Resource(
141128
{
142-
"key2": "overwrite_existing_value",
143-
"key3": "overwrite_existing_value",
129+
"key2": "try_to_overwrite_existing_value",
130+
"key3": "try_to_overwrite_existing_value",
144131
"key4": "value4",
145132
}
146133
)
134+
135+
# New values should not overwrite existing values
147136
self.assertEqual(
148137
resources.get_aggregated_resources(
149138
[resource_detector1, resource_detector2, resource_detector3]
@@ -158,21 +147,56 @@ def test_aggregated_resources(self):
158147
),
159148
)
160149

150+
def test_resource_detector_ignore_error(self):
161151
resource_detector = mock.Mock(spec=resources.ResourceDetector)
162152
resource_detector.detect.side_effect = Exception()
163153
resource_detector.raise_on_error = False
164154
self.assertEqual(
165-
resources.get_aggregated_resources(
166-
[resource_detector, resource_detector1]
167-
),
168-
resources.Resource({"key1": "value1"}),
155+
resources.get_aggregated_resources([resource_detector]),
156+
resources.Resource.create_empty(),
169157
)
170158

159+
def test_resource_detector_raise_error(self):
171160
resource_detector = mock.Mock(spec=resources.ResourceDetector)
172161
resource_detector.detect.side_effect = Exception()
173162
resource_detector.raise_on_error = True
174163
self.assertRaises(
175-
Exception,
176-
resources.get_aggregated_resources,
177-
[resource_detector, resource_detector1],
164+
Exception, resources.get_aggregated_resources, [resource_detector],
165+
)
166+
167+
168+
class TestOTELResourceDetector(unittest.TestCase):
169+
def setUp(self) -> None:
170+
os.environ["OTEL_RESOURCE"] = ""
171+
172+
def tearDown(self) -> None:
173+
os.environ.pop("OTEL_RESOURCE")
174+
175+
def test_empty(self):
176+
detector = resources.OTELResourceDetector()
177+
os.environ["OTEL_RESOURCE"] = ""
178+
self.assertEqual(detector.detect(), resources.Resource.create_empty())
179+
180+
def test_one(self):
181+
detector = resources.OTELResourceDetector()
182+
os.environ["OTEL_RESOURCE"] = "k=v"
183+
self.assertEqual(detector.detect(), resources.Resource({"k": "v"}))
184+
185+
def test_one_with_whitespace(self):
186+
detector = resources.OTELResourceDetector()
187+
os.environ["OTEL_RESOURCE"] = " k = v "
188+
self.assertEqual(detector.detect(), resources.Resource({"k": "v"}))
189+
190+
def test_multiple(self):
191+
detector = resources.OTELResourceDetector()
192+
os.environ["OTEL_RESOURCE"] = "k=v,k2=v2"
193+
self.assertEqual(
194+
detector.detect(), resources.Resource({"k": "v", "k2": "v2"})
195+
)
196+
197+
def test_multiple_with_whitespace(self):
198+
detector = resources.OTELResourceDetector()
199+
os.environ["OTEL_RESOURCE"] = " k = v , k2 = v2 "
200+
self.assertEqual(
201+
detector.detect(), resources.Resource({"k": "v", "k2": "v2"})
178202
)

0 commit comments

Comments
 (0)