|
@@ -19,21 +19,27 @@ limitations under the License.
|
|
|
'''
|
|
|
|
|
|
from unittest import TestCase
|
|
|
+import unittest
|
|
|
import ambari_agent.hostname as hostname
|
|
|
import ambari_agent.AmbariConfig as AmbariConfig
|
|
|
import socket
|
|
|
import tempfile
|
|
|
import shutil
|
|
|
import os, pprint, json,stat
|
|
|
+from mock.mock import patch
|
|
|
|
|
|
class TestHostname(TestCase):
|
|
|
|
|
|
def test_hostname(self):
|
|
|
+ hostname.cached_hostname = None
|
|
|
+ hostname.cached_public_hostname = None
|
|
|
self.assertEquals(hostname.hostname(), socket.getfqdn(),
|
|
|
"hostname should equal the socket-based hostname")
|
|
|
pass
|
|
|
|
|
|
def test_hostname_override(self):
|
|
|
+ hostname.cached_hostname = None
|
|
|
+ hostname.cached_public_hostname = None
|
|
|
fd = tempfile.mkstemp(text=True)
|
|
|
tmpname = fd[1]
|
|
|
os.close(fd[0])
|
|
@@ -51,10 +57,11 @@ class TestHostname(TestCase):
|
|
|
finally:
|
|
|
os.remove(tmpname)
|
|
|
config.remove_option('agent', 'hostname_script')
|
|
|
-
|
|
|
pass
|
|
|
|
|
|
def test_public_hostname_override(self):
|
|
|
+ hostname.cached_hostname = None
|
|
|
+ hostname.cached_public_hostname = None
|
|
|
fd = tempfile.mkstemp(text=True)
|
|
|
tmpname = fd[1]
|
|
|
os.close(fd[0])
|
|
@@ -74,9 +81,21 @@ class TestHostname(TestCase):
|
|
|
finally:
|
|
|
os.remove(tmpname)
|
|
|
config.remove_option('agent', 'public_hostname_script')
|
|
|
+ pass
|
|
|
|
|
|
+ @patch.object(socket, "getfqdn")
|
|
|
+ def test_caching(self, getfqdn_mock):
|
|
|
+ hostname.cached_hostname = None
|
|
|
+ hostname.cached_public_hostname = None
|
|
|
+ getfqdn_mock.side_effect = ["test.example.com", "test2.example.com'"]
|
|
|
+ self.assertEquals(hostname.hostname(), "test.example.com")
|
|
|
+ self.assertEquals(hostname.hostname(), "test.example.com")
|
|
|
+ self.assertEqual(getfqdn_mock.call_count, 1)
|
|
|
pass
|
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
+ unittest.main(verbosity=2)
|
|
|
+
|
|
|
|
|
|
|
|
|
|