|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +# pylint: disable=protected-access |
| 5 | + |
| 6 | +import json |
| 7 | +import os |
| 8 | +import subprocess |
| 9 | +import tempfile |
| 10 | +import unittest |
| 11 | +from contextlib import ExitStack |
| 12 | +from pathlib import Path |
| 13 | +from unittest.mock import Mock, patch |
| 14 | + |
| 15 | +from opentelemetry.test.weaver_live_check import LiveCheckError, LiveCheckReport, WeaverLiveCheck |
| 16 | + |
| 17 | + |
| 18 | +class TestReportAccess(unittest.TestCase): |
| 19 | + def test_report_can_be_serialized_without_losing_fields(self): |
| 20 | + raw = {"samples": [{"span": {"name": "test"}}], "statistics": {"count": 1}, "extra": [1]} |
| 21 | + report = LiveCheckReport(raw) |
| 22 | + self.assertEqual(json.loads(json.dumps(report.to_dict())), raw) |
| 23 | + self.assertEqual(report.samples, raw["samples"]) |
| 24 | + self.assertEqual(report.statistics, raw["statistics"]) |
| 25 | + report.to_dict()["extra"].append(2) |
| 26 | + report.samples[0]["span"]["name"] = "changed" |
| 27 | + report.statistics["count"] = 2 |
| 28 | + self.assertEqual(report["extra"], [1]) |
| 29 | + self.assertEqual(report.samples[0]["span"]["name"], "test") |
| 30 | + self.assertEqual(report.statistics["count"], 1) |
| 31 | + |
| 32 | + def test_missing_report_sections(self): |
| 33 | + report = LiveCheckReport({}) |
| 34 | + self.assertEqual(report.samples, []) |
| 35 | + self.assertEqual(report.statistics, {}) |
| 36 | + |
| 37 | + |
| 38 | +class TestWeaverOptions(unittest.TestCase): |
| 39 | + @patch("opentelemetry.test.weaver_live_check.shutil.which", return_value="weaver") |
| 40 | + def test_named_options(self, _which): |
| 41 | + for data in ("data/*.json", "data with spaces", "https://example.com/data.tar.gz[policies]"): |
| 42 | + with self.subTest(advice_data=data): |
| 43 | + weaver = WeaverLiveCheck( |
| 44 | + registry="registry", |
| 45 | + otlp_port=4317, |
| 46 | + admin_port=4320, |
| 47 | + config="config with spaces.toml", |
| 48 | + advice_data=data, |
| 49 | + extra_args=["--quiet"], |
| 50 | + ) |
| 51 | + command = weaver._command |
| 52 | + self.assertEqual(command[command.index("--config") + 1], os.path.abspath("config with spaces.toml")) |
| 53 | + self.assertEqual(command[command.index("--advice-data") + 1], data) |
| 54 | + self.assertEqual(command[-1], "--quiet") |
| 55 | + |
| 56 | + @patch("opentelemetry.test.weaver_live_check.shutil.which", return_value="weaver") |
| 57 | + def test_local_advice_data_is_absolute(self, _which): |
| 58 | + with tempfile.TemporaryDirectory() as directory: |
| 59 | + Path(directory, "data.json").write_text("{}", encoding="utf-8") |
| 60 | + cwd = os.getcwd() |
| 61 | + os.chdir(directory) |
| 62 | + try: |
| 63 | + weaver = WeaverLiveCheck(registry="registry", otlp_port=4317, admin_port=4320, advice_data="data.json") |
| 64 | + expected = os.path.abspath("data.json") |
| 65 | + finally: |
| 66 | + os.chdir(cwd) |
| 67 | + command = weaver._command |
| 68 | + self.assertEqual(command[command.index("--advice-data") + 1], expected) |
| 69 | + |
| 70 | + @patch("opentelemetry.test.weaver_live_check.shutil.which", return_value="weaver") |
| 71 | + def test_optional_flags_are_omitted(self, _which): |
| 72 | + weaver = WeaverLiveCheck(registry="registry", otlp_port=4317, admin_port=4320) |
| 73 | + self.assertNotIn("--config", weaver._command) |
| 74 | + self.assertNotIn("--advice-data", weaver._command) |
| 75 | + |
| 76 | + |
| 77 | +class TestWeaverOutput(unittest.TestCase): |
| 78 | + def setUp(self): |
| 79 | + stack = ExitStack() |
| 80 | + self.addCleanup(stack.close) |
| 81 | + stack.enter_context(patch("opentelemetry.test.weaver_live_check.shutil.which", return_value="weaver")) |
| 82 | + directory = Path(stack.enter_context(tempfile.TemporaryDirectory())) |
| 83 | + self.stdout_path = directory / "stdout.log" |
| 84 | + self.stderr_path = directory / "stderr.log" |
| 85 | + self.stdout_path.write_bytes(b"output\xff\n") |
| 86 | + self.stderr_path.write_bytes(b"diagnostic\n") |
| 87 | + self.weaver = WeaverLiveCheck(registry="registry", otlp_port=4317, admin_port=4320) |
| 88 | + self.weaver._stdout_path = str(self.stdout_path) |
| 89 | + self.weaver._stderr_path = str(self.stderr_path) |
| 90 | + self.process = Mock() |
| 91 | + self.process.poll.return_value = None |
| 92 | + self.weaver._process = self.process |
| 93 | + self.addCleanup(self.weaver.close) |
| 94 | + |
| 95 | + def test_reading_output_does_not_stop_process(self): |
| 96 | + self.assertEqual(self.weaver.stdout, "output\ufffd\n") |
| 97 | + self.assertEqual(self.weaver.stderr, "diagnostic\n") |
| 98 | + self.process.assert_not_called() |
| 99 | + self.assertEqual(self.process.mock_calls, []) |
| 100 | + self.stderr_path.write_bytes(b"more diagnostics\n") |
| 101 | + self.assertEqual(self.weaver.stderr, "more diagnostics\n") |
| 102 | + |
| 103 | + def test_close_retains_output_and_deletes_files(self): |
| 104 | + self.weaver.close() |
| 105 | + self.weaver.close() |
| 106 | + self.assertFalse(self.stdout_path.exists()) |
| 107 | + self.assertFalse(self.stderr_path.exists()) |
| 108 | + self.assertEqual(self.weaver.stdout, "output\ufffd\n") |
| 109 | + self.assertEqual(self.weaver.stderr, "diagnostic\n") |
| 110 | + |
| 111 | + def test_forced_exit_retains_output(self): |
| 112 | + self.process.wait.side_effect = [subprocess.TimeoutExpired("weaver", 5), 0] |
| 113 | + self.process.poll.side_effect = [None, 0] |
| 114 | + self.weaver.close() |
| 115 | + self.process.kill.assert_called_once_with() |
| 116 | + self.assertEqual(self.process.wait.call_count, 2) |
| 117 | + self.assertEqual(self.weaver.stderr, "diagnostic\n") |
| 118 | + |
| 119 | + def test_output_preserves_windows_line_endings(self): |
| 120 | + self.stderr_path.write_bytes(b"diagnostic\r\n") |
| 121 | + self.weaver.close() |
| 122 | + self.assertEqual(self.weaver.stderr, "diagnostic\r\n") |
| 123 | + |
| 124 | + def test_post_kill_timeout_preserves_original_error_and_output(self): |
| 125 | + self.process.wait.side_effect = subprocess.TimeoutExpired("weaver", 5) |
| 126 | + self.process.poll.side_effect = [None, 0] |
| 127 | + with patch.object(self.weaver, "start", return_value=self.weaver): |
| 128 | + with self.assertRaisesRegex(RuntimeError, "original failure"): |
| 129 | + with self.weaver: |
| 130 | + raise RuntimeError("original failure") |
| 131 | + self.process.kill.assert_called_once_with() |
| 132 | + self.assertEqual(self.process.wait.call_count, 2) |
| 133 | + self.assertFalse(self.stdout_path.exists()) |
| 134 | + self.assertFalse(self.stderr_path.exists()) |
| 135 | + self.assertEqual(self.weaver.stdout, "output\ufffd\n") |
| 136 | + self.assertEqual(self.weaver.stderr, "diagnostic\n") |
| 137 | + |
| 138 | + def test_error_keeps_output_after_close(self): |
| 139 | + report = LiveCheckReport({}) |
| 140 | + with patch.object(self.weaver, "_do_stop", return_value=(report, 1)): |
| 141 | + with self.assertRaises(LiveCheckError) as caught: |
| 142 | + self.weaver.end_and_check() |
| 143 | + self.weaver.close() |
| 144 | + self.assertEqual(caught.exception.stdout, "output\ufffd\n") |
| 145 | + self.assertEqual(caught.exception.stderr, "diagnostic\n") |
| 146 | + |
| 147 | + def test_missing_output_file_preserves_last_read(self): |
| 148 | + self.assertEqual(self.weaver.stderr, "diagnostic\n") |
| 149 | + self.stderr_path.unlink() |
| 150 | + self.assertEqual(self.weaver.stderr, "diagnostic\n") |
| 151 | + |
| 152 | + def test_error_constructor_remains_compatible(self): |
| 153 | + error = LiveCheckError("violation", LiveCheckReport({})) |
| 154 | + self.assertEqual(error.stdout, "") |
| 155 | + self.assertEqual(error.stderr, "") |
0 commit comments