|
| 1 | +""" Tests for the user_agent module. """ |
| 2 | + |
| 3 | +import unittest |
| 4 | +from unittest.mock import patch |
| 5 | +import os |
| 6 | + |
| 7 | +from runpod.user_agent import construct_user_agent |
| 8 | + |
| 9 | + |
| 10 | +class TestConstructUserAgent(unittest.TestCase): |
| 11 | + """Test the construct_user_agent function.""" |
| 12 | + |
| 13 | + @patch('runpod.user_agent.platform.system', return_value='Windows') |
| 14 | + @patch('runpod.user_agent.platform.release', return_value='10') |
| 15 | + @patch('runpod.user_agent.platform.machine', return_value='AMD64') |
| 16 | + @patch('runpod.user_agent.platform.python_version', return_value='3.8.10') |
| 17 | + def test_user_agent_without_integration( |
| 18 | + self, mock_python_version, mock_machine, mock_release, mock_system): |
| 19 | + """Test the User-Agent string without specifying an integration method.""" |
| 20 | + if 'RUNPOD_UA_INTEGRATION' in os.environ: |
| 21 | + del os.environ['RUNPOD_UA_INTEGRATION'] |
| 22 | + |
| 23 | + expected_ua = "RunPod-Python-SDK/0.1.0 (Windows 10; AMD64) Language/Python 3.8.10" |
| 24 | + self.assertEqual(construct_user_agent(), expected_ua) |
| 25 | + |
| 26 | + assert mock_python_version.called |
| 27 | + assert mock_machine.called |
| 28 | + assert mock_release.called |
| 29 | + assert mock_system.called |
| 30 | + |
| 31 | + @patch('runpod.user_agent.platform.system', return_value='Linux') |
| 32 | + @patch('runpod.user_agent.platform.release', return_value='5.4') |
| 33 | + @patch('runpod.user_agent.platform.machine', return_value='x86_64') |
| 34 | + @patch('runpod.user_agent.platform.python_version', return_value='3.9.5') |
| 35 | + @patch.dict(os.environ, {'RUNPOD_UA_INTEGRATION': 'SkyPilot'}) |
| 36 | + def test_user_agent_with_integration( |
| 37 | + self, mock_python_version, mock_machine, mock_release, mock_system): |
| 38 | + """Test the User-Agent string with an integration method specified.""" |
| 39 | + expected_ua = "RunPod-Python-SDK/0.1.0 (Linux 5.4; x86_64) Language/Python 3.9.5 Integration/SkyPilot" # pylint: disable=line-too-long |
| 40 | + self.assertEqual(construct_user_agent(), expected_ua) |
| 41 | + |
| 42 | + assert mock_python_version.called |
| 43 | + assert mock_machine.called |
| 44 | + assert mock_release.called |
| 45 | + assert mock_system.called |
| 46 | + |
| 47 | + |
| 48 | +if __name__ == '__main__': |
| 49 | + unittest.main() |
0 commit comments