|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | +from unittest import mock |
| 17 | + |
| 18 | +from opentelemetry.sdk.resources import Resource, ResourceDetector |
| 19 | + |
| 20 | +from loongsuite.distro.resource import ( |
| 21 | + GEN_AI_INSTRUMENTATION_SDK_NAME, |
| 22 | + HOST_IP, |
| 23 | + LoongSuiteResourceDetector, |
| 24 | + _get_host_ip, |
| 25 | + _get_host_ip_with_pid, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +class TestLoongSuiteResourceDetector(unittest.TestCase): |
| 30 | + def test_is_resource_detector(self): |
| 31 | + self.assertIsInstance( |
| 32 | + LoongSuiteResourceDetector(), ResourceDetector |
| 33 | + ) |
| 34 | + |
| 35 | + def test_detect_returns_resource(self): |
| 36 | + resource = LoongSuiteResourceDetector().detect() |
| 37 | + self.assertIsInstance(resource, Resource) |
| 38 | + |
| 39 | + def test_detect_contains_expected_keys(self): |
| 40 | + attributes = LoongSuiteResourceDetector().detect().attributes |
| 41 | + self.assertIn(HOST_IP, attributes) |
| 42 | + self.assertIn(GEN_AI_INSTRUMENTATION_SDK_NAME, attributes) |
| 43 | + |
| 44 | + def test_gen_ai_instrumentation_sdk_name_value(self): |
| 45 | + attributes = LoongSuiteResourceDetector().detect().attributes |
| 46 | + self.assertEqual( |
| 47 | + attributes[GEN_AI_INSTRUMENTATION_SDK_NAME], |
| 48 | + "loongsuite-genai-utils", |
| 49 | + ) |
| 50 | + |
| 51 | + @mock.patch("loongsuite.distro.resource.os.getpid", return_value=1) |
| 52 | + @mock.patch( |
| 53 | + "loongsuite.distro.resource._get_host_ip", return_value="127.0.0.1" |
| 54 | + ) |
| 55 | + def test_host_ip_format_is_ip_dash_pid(self, _mock_ip, _mock_pid): |
| 56 | + attributes = LoongSuiteResourceDetector().detect().attributes |
| 57 | + self.assertEqual(attributes[HOST_IP], "127.0.0.1-1") |
| 58 | + |
| 59 | + @mock.patch("loongsuite.distro.resource.os.getpid", return_value=42) |
| 60 | + @mock.patch( |
| 61 | + "loongsuite.distro.resource._get_host_ip", return_value="10.0.0.5" |
| 62 | + ) |
| 63 | + def test_get_host_ip_with_pid(self, _mock_ip, _mock_pid): |
| 64 | + self.assertEqual(_get_host_ip_with_pid(), "10.0.0.5-42") |
| 65 | + |
| 66 | + |
| 67 | +class TestGetHostIp(unittest.TestCase): |
| 68 | + def test_returns_detected_ip(self): |
| 69 | + mock_sock = mock.MagicMock() |
| 70 | + mock_sock.getsockname.return_value = ("192.168.1.100", 12345) |
| 71 | + with mock.patch( |
| 72 | + "loongsuite.distro.resource.socket.socket", |
| 73 | + return_value=mock_sock, |
| 74 | + ): |
| 75 | + self.assertEqual(_get_host_ip(), "192.168.1.100") |
| 76 | + mock_sock.connect.assert_called_once() |
| 77 | + mock_sock.close.assert_called_once() |
| 78 | + |
| 79 | + def test_falls_back_to_loopback_on_error(self): |
| 80 | + mock_sock = mock.MagicMock() |
| 81 | + mock_sock.connect.side_effect = OSError("no network") |
| 82 | + with mock.patch( |
| 83 | + "loongsuite.distro.resource.socket.socket", |
| 84 | + return_value=mock_sock, |
| 85 | + ): |
| 86 | + self.assertEqual(_get_host_ip(), "127.0.0.1") |
| 87 | + # The socket must still be closed even when detection fails. |
| 88 | + mock_sock.close.assert_called_once() |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + unittest.main() |
0 commit comments