def test_get_hostname_gce_nodocker(self): self.mock(platforms, 'is_gce', lambda: True) self.mock(os.path, 'isfile', lambda _: False) self.mock(platforms.gce, 'get_metadata', lambda: {'instance': { 'hostname': 'gcehost' }}) self.assertEqual(os_utilities.get_hostname(), 'gcehost')
def test_get_hostname_gce_nodocker(self): self.mock(platforms, 'is_gce', lambda: True) self.mock(os.path, 'isfile', lambda _: False) manual_mock = not hasattr(platforms, 'gce') if manual_mock: # On macOS. class Mock(object): def get_metadata(self): return None platforms.gce = Mock() try: self.mock(platforms.gce, 'get_metadata', lambda: {'instance': { 'hostname': 'gcehost' }}) self.assertEqual(os_utilities.get_hostname(), 'gcehost') finally: if manual_mock: del platforms.gce
def test_get_hostname_macos(self): self.mock(platforms, 'is_gce', lambda: False) self.mock(os.path, 'isfile', lambda _: False) self.mock(socket, 'getfqdn', lambda: 'somehost.in-addr.arpa') self.mock(socket, 'gethostname', lambda: 'somehost') self.assertEqual(os_utilities.get_hostname(), 'somehost')
def test_get_hostname_gce_docker(self): self.mock(platforms, 'is_gce', lambda: True) self.mock(os.path, 'isfile', lambda _: True) self.mock(socket, 'getfqdn', lambda: 'dockerhost') self.assertEqual(os_utilities.get_hostname(), 'dockerhost')