Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update resources tests #893

Merged
merged 3 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ext/opentelemetry-exporter-cloud-trace/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Add support for resources and resource detector
([#853](https://github.com/open-telemetry/opentelemetry-python/pull/853))
- Improve resource and resource detector tests
AndrewAXue marked this conversation as resolved.
Show resolved Hide resolved
([#893](https://github.com/open-telemetry/opentelemetry-python/pull/893))

## Version 0.10b0

Expand Down
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,18 @@ 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
AndrewAXue marked this conversation as resolved.
Show resolved Hide resolved
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 +351,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
2 changes: 2 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Add support for resources and resource detector
([#853](https://github.com/open-telemetry/opentelemetry-python/pull/853))
- Improve resource and resource detector tests
([#893](https://github.com/open-telemetry/opentelemetry-python/pull/893))

## Version 0.10b0

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_otel_resource_detector_empty(self):
AndrewAXue marked this conversation as resolved.
Show resolved Hide resolved
detector = resources.OTELResourceDetector()
os.environ["OTEL_RESOURCE"] = ""
self.assertEqual(detector.detect(), resources.Resource.create_empty())

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

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

def test_otel_resource_detector_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_otel_resource_detector_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"})
)