Skip to content

Commit

Permalink
Update resources tests (#893)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Xue authored Jul 10, 2020
1 parent 09df35c commit 8cbd9d4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,10 @@ def test_extract_links(self):
),
)

def test_extract_resources(self):
def test_extract_empty_resources(self):
self.assertEqual(_extract_resources(Resource.create_empty()), {})

def test_extract_well_formed_resources(self):
resource = Resource(
labels={
"cloud.account.id": 123,
Expand All @@ -325,19 +327,19 @@ def test_extract_resources(self):
}
self.assertEqual(_extract_resources(resource), expected_extract)

def test_extract_malformed_resources(self):
# This resource doesn't have all the fields required for a gce_instance
# Specifically its missing "host.id", "cloud.zone", "cloud.account.id"
resource = Resource(
labels={
"cloud.account.id": "123",
"host.id": "host",
"extra_info": "extra",
"not_gcp_resource": "value",
"gcp.resource_type": "gce_instance",
"cloud.provider": "gcp",
}
)
# Should throw when passed a malformed GCP resource dict
self.assertRaises(KeyError, _extract_resources, resource)

def test_extract_unsupported_gcp_resources(self):
resource = Resource(
labels={
"cloud.account.id": "123",
Expand All @@ -350,6 +352,8 @@ def test_extract_resources(self):
)
self.assertEqual(_extract_resources(resource), {})

def test_extract_unsupported_provider_resources(self):
# Resources with currently unsupported providers will be ignored
resource = Resource(
labels={
"cloud.account.id": "123",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def test_finding_resources(self, getter):
)
self.assertEqual(getter.call_count, 1)

# Found resources should be cached and not require another network call
found_resources = resource_finder.detect()
self.assertEqual(getter.call_count, 1)
self.assertEqual(
Expand Down
78 changes: 51 additions & 27 deletions opentelemetry-sdk/tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,13 @@ def test_immutability(self):
labels["cost"] = 999.91
self.assertEqual(resource.labels, labels_copy)

def test_otel_resource_detector(self):
detector = resources.OTELResourceDetector()
self.assertEqual(detector.detect(), resources.Resource.create_empty())
os.environ["OTEL_RESOURCE"] = "k=v"
self.assertEqual(detector.detect(), resources.Resource({"k": "v"}))
os.environ["OTEL_RESOURCE"] = " k = v "
self.assertEqual(detector.detect(), resources.Resource({"k": "v"}))
os.environ["OTEL_RESOURCE"] = "k=v,k2=v2"
self.assertEqual(
detector.detect(), resources.Resource({"k": "v", "k2": "v2"})
)
os.environ["OTEL_RESOURCE"] = " k = v , k2 = v2 "
self.assertEqual(
detector.detect(), resources.Resource({"k": "v", "k2": "v2"})
)

def test_aggregated_resources(self):
def test_aggregated_resources_no_detectors(self):
aggregated_resources = resources.get_aggregated_resources([])
self.assertEqual(
aggregated_resources, resources.Resource.create_empty()
)

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

self.assertEqual(
Expand All @@ -117,9 +102,10 @@ def test_aggregated_resources(self):
static_resource,
)

# Static resource values should never be overwritten
resource_detector = mock.Mock(spec=resources.ResourceDetector)
resource_detector.detect.return_value = resources.Resource(
{"static_key": "overwrite_existing_value", "key": "value"}
{"static_key": "try_to_overwrite_existing_value", "key": "value"}
)
self.assertEqual(
resources.get_aggregated_resources(
Expand All @@ -128,6 +114,7 @@ def test_aggregated_resources(self):
resources.Resource({"static_key": "static_value", "key": "value"}),
)

def test_aggregated_resources_multiple_detectors(self):
resource_detector1 = mock.Mock(spec=resources.ResourceDetector)
resource_detector1.detect.return_value = resources.Resource(
{"key1": "value1"}
Expand All @@ -139,11 +126,13 @@ def test_aggregated_resources(self):
resource_detector3 = mock.Mock(spec=resources.ResourceDetector)
resource_detector3.detect.return_value = resources.Resource(
{
"key2": "overwrite_existing_value",
"key3": "overwrite_existing_value",
"key2": "try_to_overwrite_existing_value",
"key3": "try_to_overwrite_existing_value",
"key4": "value4",
}
)

# New values should not overwrite existing values
self.assertEqual(
resources.get_aggregated_resources(
[resource_detector1, resource_detector2, resource_detector3]
Expand All @@ -158,21 +147,56 @@ def test_aggregated_resources(self):
),
)

def test_resource_detector_ignore_error(self):
resource_detector = mock.Mock(spec=resources.ResourceDetector)
resource_detector.detect.side_effect = Exception()
resource_detector.raise_on_error = False
self.assertEqual(
resources.get_aggregated_resources(
[resource_detector, resource_detector1]
),
resources.Resource({"key1": "value1"}),
resources.get_aggregated_resources([resource_detector]),
resources.Resource.create_empty(),
)

def test_resource_detector_raise_error(self):
resource_detector = mock.Mock(spec=resources.ResourceDetector)
resource_detector.detect.side_effect = Exception()
resource_detector.raise_on_error = True
self.assertRaises(
Exception,
resources.get_aggregated_resources,
[resource_detector, resource_detector1],
Exception, resources.get_aggregated_resources, [resource_detector],
)


class TestOTELResourceDetector(unittest.TestCase):
def setUp(self) -> None:
os.environ["OTEL_RESOURCE"] = ""

def tearDown(self) -> None:
os.environ.pop("OTEL_RESOURCE")

def test_empty(self):
detector = resources.OTELResourceDetector()
os.environ["OTEL_RESOURCE"] = ""
self.assertEqual(detector.detect(), resources.Resource.create_empty())

def test_one(self):
detector = resources.OTELResourceDetector()
os.environ["OTEL_RESOURCE"] = "k=v"
self.assertEqual(detector.detect(), resources.Resource({"k": "v"}))

def test_one_with_whitespace(self):
detector = resources.OTELResourceDetector()
os.environ["OTEL_RESOURCE"] = " k = v "
self.assertEqual(detector.detect(), resources.Resource({"k": "v"}))

def test_multiple(self):
detector = resources.OTELResourceDetector()
os.environ["OTEL_RESOURCE"] = "k=v,k2=v2"
self.assertEqual(
detector.detect(), resources.Resource({"k": "v", "k2": "v2"})
)

def test_multiple_with_whitespace(self):
detector = resources.OTELResourceDetector()
os.environ["OTEL_RESOURCE"] = " k = v , k2 = v2 "
self.assertEqual(
detector.detect(), resources.Resource({"k": "v", "k2": "v2"})
)

0 comments on commit 8cbd9d4

Please sign in to comment.