Repository navigation
opentelemetry-test-utils: expose Weaver reports and diagnostics #5739
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
Open
lmolkova
wants to merge
9
commits into
open-telemetry:main
Choose a base branch
from
lmolkova:feat/weaver-report-diagnostics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a671078
opentelemetry-test-utils: expose Weaver reports and diagnostics
lmolkova 8f0275e
Name changelog fragment for PR 5739
lmolkova 69bb776
Preserve Weaver diagnostics after kill timeout and fix test portability
lmolkova 3ee3a29
Merge remote-tracking branch 'origin/main' into feat/weaver-report-di…
lmolkova 1113cae
Make local --advice-data paths absolute
lmolkova 05c4652
Keep startup_timeout position and fix Windows test
lmolkova d9b78ac
Specify encoding in advice data test
lmolkova 3c7b697
Merge branch 'main' into feat/weaver-report-diagnostics
lmolkova 20d8d0a
Merge branch 'main' into feat/weaver-report-diagnostics
lmolkova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| `opentelemetry-test-utils`: expose live-check reports and retained stdout/stderr, | ||
| and add named Weaver configuration and advice-data options. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
tests/opentelemetry-test-utils/tests/test_weaver_live_check_access.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # pylint: disable=protected-access | ||
|
|
||
| import json | ||
| import os | ||
| import subprocess | ||
| import tempfile | ||
| import unittest | ||
| from contextlib import ExitStack | ||
| from pathlib import Path | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| from opentelemetry.test.weaver_live_check import LiveCheckError, LiveCheckReport, WeaverLiveCheck | ||
|
|
||
|
|
||
| class TestReportAccess(unittest.TestCase): | ||
| def test_report_can_be_serialized_without_losing_fields(self): | ||
| raw = {"samples": [{"span": {"name": "test"}}], "statistics": {"count": 1}, "extra": [1]} | ||
| report = LiveCheckReport(raw) | ||
| self.assertEqual(json.loads(json.dumps(report.to_dict())), raw) | ||
| self.assertEqual(report.samples, raw["samples"]) | ||
| self.assertEqual(report.statistics, raw["statistics"]) | ||
| report.to_dict()["extra"].append(2) | ||
| report.samples[0]["span"]["name"] = "changed" | ||
| report.statistics["count"] = 2 | ||
| self.assertEqual(report["extra"], [1]) | ||
| self.assertEqual(report.samples[0]["span"]["name"], "test") | ||
| self.assertEqual(report.statistics["count"], 1) | ||
|
|
||
| def test_missing_report_sections(self): | ||
| report = LiveCheckReport({}) | ||
| self.assertEqual(report.samples, []) | ||
| self.assertEqual(report.statistics, {}) | ||
|
|
||
|
|
||
| class TestWeaverOptions(unittest.TestCase): | ||
| @patch("opentelemetry.test.weaver_live_check.shutil.which", return_value="weaver") | ||
| def test_named_options(self, _which): | ||
| for data in ("data/*.json", "data with spaces", "https://example.com/data.tar.gz[policies]"): | ||
| with self.subTest(advice_data=data): | ||
| weaver = WeaverLiveCheck( | ||
| registry="registry", | ||
| otlp_port=4317, | ||
| admin_port=4320, | ||
| config="config with spaces.toml", | ||
| advice_data=data, | ||
| extra_args=["--quiet"], | ||
| ) | ||
| command = weaver._command | ||
| self.assertEqual(command[command.index("--config") + 1], os.path.abspath("config with spaces.toml")) | ||
| self.assertEqual(command[command.index("--advice-data") + 1], data) | ||
| self.assertEqual(command[-1], "--quiet") | ||
|
|
||
| @patch("opentelemetry.test.weaver_live_check.shutil.which", return_value="weaver") | ||
| def test_local_advice_data_is_absolute(self, _which): | ||
| with tempfile.TemporaryDirectory() as directory: | ||
| Path(directory, "data.json").write_text("{}", encoding="utf-8") | ||
| cwd = os.getcwd() | ||
| os.chdir(directory) | ||
| try: | ||
| weaver = WeaverLiveCheck(registry="registry", otlp_port=4317, admin_port=4320, advice_data="data.json") | ||
| expected = os.path.abspath("data.json") | ||
| finally: | ||
| os.chdir(cwd) | ||
| command = weaver._command | ||
| self.assertEqual(command[command.index("--advice-data") + 1], expected) | ||
|
|
||
| @patch("opentelemetry.test.weaver_live_check.shutil.which", return_value="weaver") | ||
| def test_optional_flags_are_omitted(self, _which): | ||
| weaver = WeaverLiveCheck(registry="registry", otlp_port=4317, admin_port=4320) | ||
| self.assertNotIn("--config", weaver._command) | ||
| self.assertNotIn("--advice-data", weaver._command) | ||
|
|
||
|
|
||
| class TestWeaverOutput(unittest.TestCase): | ||
| def setUp(self): | ||
| stack = ExitStack() | ||
| self.addCleanup(stack.close) | ||
| stack.enter_context(patch("opentelemetry.test.weaver_live_check.shutil.which", return_value="weaver")) | ||
| directory = Path(stack.enter_context(tempfile.TemporaryDirectory())) | ||
| self.stdout_path = directory / "stdout.log" | ||
| self.stderr_path = directory / "stderr.log" | ||
| self.stdout_path.write_bytes(b"output\xff\n") | ||
| self.stderr_path.write_bytes(b"diagnostic\n") | ||
| self.weaver = WeaverLiveCheck(registry="registry", otlp_port=4317, admin_port=4320) | ||
| self.weaver._stdout_path = str(self.stdout_path) | ||
| self.weaver._stderr_path = str(self.stderr_path) | ||
| self.process = Mock() | ||
| self.process.poll.return_value = None | ||
| self.weaver._process = self.process | ||
| self.addCleanup(self.weaver.close) | ||
|
|
||
| def test_reading_output_does_not_stop_process(self): | ||
| self.assertEqual(self.weaver.stdout, "output\ufffd\n") | ||
| self.assertEqual(self.weaver.stderr, "diagnostic\n") | ||
| self.process.assert_not_called() | ||
| self.assertEqual(self.process.mock_calls, []) | ||
| self.stderr_path.write_bytes(b"more diagnostics\n") | ||
| self.assertEqual(self.weaver.stderr, "more diagnostics\n") | ||
|
|
||
| def test_close_retains_output_and_deletes_files(self): | ||
| self.weaver.close() | ||
| self.weaver.close() | ||
| self.assertFalse(self.stdout_path.exists()) | ||
| self.assertFalse(self.stderr_path.exists()) | ||
| self.assertEqual(self.weaver.stdout, "output\ufffd\n") | ||
| self.assertEqual(self.weaver.stderr, "diagnostic\n") | ||
|
|
||
| def test_forced_exit_retains_output(self): | ||
| self.process.wait.side_effect = [subprocess.TimeoutExpired("weaver", 5), 0] | ||
| self.process.poll.side_effect = [None, 0] | ||
| self.weaver.close() | ||
| self.process.kill.assert_called_once_with() | ||
| self.assertEqual(self.process.wait.call_count, 2) | ||
| self.assertEqual(self.weaver.stderr, "diagnostic\n") | ||
|
|
||
| def test_output_preserves_windows_line_endings(self): | ||
| self.stderr_path.write_bytes(b"diagnostic\r\n") | ||
| self.weaver.close() | ||
| self.assertEqual(self.weaver.stderr, "diagnostic\r\n") | ||
|
|
||
| def test_post_kill_timeout_preserves_original_error_and_output(self): | ||
| self.process.wait.side_effect = subprocess.TimeoutExpired("weaver", 5) | ||
| self.process.poll.side_effect = [None, 0] | ||
| with patch.object(self.weaver, "start", return_value=self.weaver): | ||
| with self.assertRaisesRegex(RuntimeError, "original failure"): | ||
| with self.weaver: | ||
| raise RuntimeError("original failure") | ||
| self.process.kill.assert_called_once_with() | ||
| self.assertEqual(self.process.wait.call_count, 2) | ||
| self.assertFalse(self.stdout_path.exists()) | ||
| self.assertFalse(self.stderr_path.exists()) | ||
| self.assertEqual(self.weaver.stdout, "output\ufffd\n") | ||
| self.assertEqual(self.weaver.stderr, "diagnostic\n") | ||
|
|
||
| def test_error_keeps_output_after_close(self): | ||
| report = LiveCheckReport({}) | ||
| with patch.object(self.weaver, "_do_stop", return_value=(report, 1)): | ||
| with self.assertRaises(LiveCheckError) as caught: | ||
| self.weaver.end_and_check() | ||
| self.weaver.close() | ||
| self.assertEqual(caught.exception.stdout, "output\ufffd\n") | ||
| self.assertEqual(caught.exception.stderr, "diagnostic\n") | ||
|
|
||
| def test_missing_output_file_preserves_last_read(self): | ||
| self.assertEqual(self.weaver.stderr, "diagnostic\n") | ||
| self.stderr_path.unlink() | ||
| self.assertEqual(self.weaver.stderr, "diagnostic\n") | ||
|
|
||
| def test_error_constructor_remains_compatible(self): | ||
| error = LiveCheckError("violation", LiveCheckReport({})) | ||
| self.assertEqual(error.stdout, "") | ||
| self.assertEqual(error.stderr, "") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.