]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
Set Remote.hostname by asking the host itself
authorZack Cerza <zack.cerza@inktank.com>
Mon, 8 Sep 2014 15:36:00 +0000 (09:36 -0600)
committerZack Cerza <zack.cerza@inktank.com>
Thu, 11 Sep 2014 21:12:52 +0000 (15:12 -0600)
Signed-off-by: Zack Cerza <zack.cerza@inktank.com>
teuthology/orchestra/remote.py
teuthology/orchestra/test/test_remote.py

index e7f7a61ac34ff51bcad786f69eb96ce386d346b7..a8363e031dd85c12a0d5c6bcd4daadb2fa6f8bc9 100644 (file)
@@ -37,13 +37,13 @@ class Remote(object):
                  host_key=None, keep_alive=True):
         self.name = name
         if '@' in name:
-            (self.user, self.hostname) = name.split('@')
+            (self.user, hostname) = name.split('@')
         else:
             # os.getlogin() doesn't work on non-login shells. The following
             # should work on any unix system
             self.user = pwd.getpwuid(os.getuid()).pw_name
-            self.hostname = name
-        self._shortname = shortname
+            hostname = name
+        self._shortname = shortname or hostname.split('.')[0]
         self.host_key = host_key
         self.keep_alive = keep_alive
         self.console = console
@@ -69,12 +69,19 @@ class Remote(object):
             log.debug(e)
             return False
 
+    @property
+    def hostname(self):
+        if not hasattr(self, '_hostname'):
+            proc = self.run(args=['hostname', '--fqdn'], stdout=StringIO())
+            proc.wait()
+            self._hostname = proc.stdout.getvalue().strip()
+        return self._hostname
+
     @property
     def shortname(self):
-        name = self._shortname
-        if name is None:
-            name = self.hostname.split('.')[0]
-        return name
+        if self._shortname is None:
+            self._shortname = self.hostname.split('.')[0]
+        return self._shortname
 
     @property
     def is_online(self):
index 8dd00191d6604481e7550fafb6e045e8fb28fa46..d0dcf27c661feebad70f83dd7426c3526c09d1be 100644 (file)
@@ -58,6 +58,42 @@ class TestRemote(object):
         assert got is ret
         assert got.remote is r
 
+    @fudge.with_fakes
+    def test_hostname(self):
+        fudge.clear_expectations()
+        ssh = fudge.Fake('SSHConnection')
+        ssh.expects('get_transport').returns_fake().expects('getpeername')\
+            .returns(('name', 22))
+        run = fudge.Fake('run')
+        args = [
+            'hostname',
+            '--fqdn',
+            ]
+        stdout = StringIO('test_hostname')
+        print repr(stdout)
+        stdout.seek(0)
+        print repr(stdout)
+        ret = RemoteProcess(
+            client=ssh,
+            args='fakey',
+            )
+        # status = self._stdout_buf.channel.recv_exit_status()
+        ret._stdout_buf = fudge.Fake()
+        ret._stdout_buf.channel = fudge.Fake()
+        ret._stdout_buf.channel.expects('recv_exit_status').returns(0)
+        ret.stdout = stdout
+        r = remote.Remote(name='jdoe@xyzzy.example.com', ssh=ssh)
+        run.expects_call().with_args(
+            client=ssh,
+            args=args,
+            stdout=fudge.inspector.arg.passes_test(
+                lambda v: isinstance(v, OutputType)),
+            name=r.shortname,
+            ).returns(ret)
+        # monkey patch ook ook
+        r._runner = run
+        assert r.hostname == 'test_hostname'
+
     @fudge.with_fakes
     def test_arch(self):
         fudge.clear_expectations()