Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-03-28 07:48:52

0001 import unittest
0002 from unittest.mock import patch
0003 import os
0004 from pyrobird.utils import is_running_in_container
0005 
0006 class TestUtils(unittest.TestCase):
0007     
0008     @patch('os.path.exists')
0009     @patch.dict(os.environ, {}, clear=True)
0010     def test_is_running_in_container_docker(self, mock_exists):
0011         # Mock /.dockerenv exists
0012         mock_exists.side_effect = lambda p: p == '/.dockerenv'
0013         self.assertTrue(is_running_in_container())
0014         
0015     @patch('os.path.exists')
0016     @patch.dict(os.environ, {'KUBERNETES_SERVICE_HOST': '10.0.0.1'}, clear=True)
0017     def test_is_running_in_container_k8s(self, mock_exists):
0018         # Mock /.dockerenv does not exist
0019         mock_exists.return_value = False
0020         self.assertTrue(is_running_in_container())
0021         
0022     @patch('os.path.exists')
0023     @patch.dict(os.environ, {'SINGULARITY_NAME': 'my_container'}, clear=True)
0024     def test_is_running_in_container_singularity(self, mock_exists):
0025         # Mock /.dockerenv does not exist
0026         mock_exists.return_value = False
0027         self.assertTrue(is_running_in_container())
0028         
0029     @patch('os.path.exists')
0030     @patch.dict(os.environ, {'APPTAINER_CONTAINER': 'my_container'}, clear=True)
0031     def test_is_running_in_container_apptainer(self, mock_exists):
0032         # Mock /.dockerenv does not exist
0033         mock_exists.return_value = False
0034         self.assertTrue(is_running_in_container())
0035         
0036     @patch('os.path.exists')
0037     @patch.dict(os.environ, {}, clear=True)
0038     def test_is_running_in_container_bare_metal(self, mock_exists):
0039         # Mock /.dockerenv does not exist
0040         mock_exists.return_value = False
0041         self.assertFalse(is_running_in_container())
0042 
0043 if __name__ == '__main__':
0044     unittest.main()