]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
Rewrite unit tests for orchestra.remote
authorZack Cerza <zack@redhat.com>
Wed, 18 May 2016 16:33:03 +0000 (10:33 -0600)
committerZack Cerza <zack@redhat.com>
Wed, 18 May 2016 17:19:02 +0000 (11:19 -0600)
They were using Fudge. Rather than write more Fudge tests, I decided to
rewrite the module to use Mock like almost everything else in teuthology
does.

Signed-off-by: Zack Cerza <zack@redhat.com>
teuthology/orchestra/test/test_remote.py

index 526b0df514a50c6644c049c0dc38f28cf83eb83c..714f8b100b73831a5fe4c5abb81bdd3cb33e18c8 100644 (file)
@@ -1,8 +1,6 @@
-import fudge
-import fudge.inspector
-from pytest import skip
+from mock import patch, Mock, MagicMock
 
-from cStringIO import StringIO, OutputType
+from cStringIO import StringIO
 
 from .. import remote
 from .. import opsys
@@ -10,11 +8,29 @@ from ..run import RemoteProcess
 
 
 class TestRemote(object):
+
+    def setup(self):
+        self.start_patchers()
+
+    def teardown(self):
+        self.stop_patchers()
+
+    def start_patchers(self):
+        self.m_ssh = MagicMock()
+        self.patcher_ssh = patch(
+            'teuthology.orchestra.connection.paramiko.SSHClient',
+            self.m_ssh,
+        )
+        self.patcher_ssh.start()
+
+    def stop_patchers(self):
+        self.patcher_ssh.stop()
+
     def test_shortname(self):
         r = remote.Remote(
             name='jdoe@xyzzy.example.com',
             shortname='xyz',
-            ssh=fudge.Fake('SSHConnection'),
+            ssh=self.m_ssh,
             )
         assert r.shortname == 'xyz'
         assert str(r) == 'jdoe@xyzzy.example.com'
@@ -22,122 +38,100 @@ class TestRemote(object):
     def test_shortname_default(self):
         r = remote.Remote(
             name='jdoe@xyzzy.example.com',
-            ssh=fudge.Fake('SSHConnection'),
+            ssh=self.m_ssh,
             )
         assert r.shortname == 'xyzzy'
         assert str(r) == 'jdoe@xyzzy.example.com'
 
-    @fudge.with_fakes
     def test_run(self):
-        fudge.clear_expectations()
-        ssh = fudge.Fake('SSHConnection')
-        ssh.expects('get_transport').returns_fake().expects('getpeername')\
-            .returns(('name', 22))
-        run = fudge.Fake('run')
+        m_transport = MagicMock()
+        m_transport.getpeername.return_value = ('name', 22)
+        self.m_ssh.get_transport.return_value = m_transport
+        m_run = MagicMock()
         args = [
             'something',
             'more',
             ]
-        foo = object()
-        ret = RemoteProcess(
-            client=ssh,
-            args='fakey',
-            )
-        r = remote.Remote(name='jdoe@xyzzy.example.com', ssh=ssh)
-        run.expects_call().with_args(
-            client=fudge.inspector.arg.passes_test(lambda v: v is ssh),
-            args=fudge.inspector.arg.passes_test(lambda v: v is args),
-            foo=fudge.inspector.arg.passes_test(lambda v: v is foo),
-            name=r.shortname,
-            ).returns(ret)
-        # monkey patch ook ook
-        r._runner = run
-        got = r.run(
+        proc = RemoteProcess(
+            client=self.m_ssh,
             args=args,
-            foo=foo,
             )
-        assert got is ret
-        assert got.remote is r
+        m_run.return_value = proc
+        rem = remote.Remote(name='jdoe@xyzzy.example.com', ssh=self.m_ssh)
+        rem._runner = m_run
+        result = rem.run(args=args)
+        assert m_transport.getpeername.called_once_with()
+        assert m_run.called_once_with(args=args)
+        assert result is proc
+        assert result.remote is rem
 
-    @fudge.with_fakes
     def test_hostname(self):
-        skip("skipping hostname test while the workaround is in place")
-        fudge.clear_expectations()
-        ssh = fudge.Fake('SSHConnection')
-        ssh.expects('get_transport').returns_fake().expects('getpeername')\
-            .returns(('name', 22))
-        run = fudge.Fake('run')
+        m_transport = MagicMock()
+        m_transport.getpeername.return_value = ('name', 22)
+        self.m_ssh.get_transport.return_value = m_transport
+        m_run = MagicMock()
         args = [
             'hostname',
             '--fqdn',
             ]
         stdout = StringIO('test_hostname')
         stdout.seek(0)
-        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,
+        proc = RemoteProcess(
+            client=self.m_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
+            )
+        proc.stdout = stdout
+        proc._stdout_buf = Mock()
+        proc._stdout_buf.channel.recv_exit_status.return_value = 0
+        r = remote.Remote(name='xyzzy.example.com', ssh=self.m_ssh)
+        m_run.return_value = proc
+        r._runner = m_run
         assert r.hostname == 'test_hostname'
 
-    @fudge.with_fakes
     def test_arch(self):
-        fudge.clear_expectations()
-        ssh = fudge.Fake('SSHConnection')
-        ssh.expects('get_transport').returns_fake().expects('getpeername')\
-            .returns(('name', 22))
-        run = fudge.Fake('run')
+        m_transport = MagicMock()
+        m_transport.getpeername.return_value = ('name', 22)
+        self.m_ssh.get_transport.return_value = m_transport
+        m_run = MagicMock()
         args = [
             'uname',
             '-m',
             ]
         stdout = StringIO('test_arch')
         stdout.seek(0)
-        ret = RemoteProcess(
-            client=ssh,
+        proc = RemoteProcess(
+            client=self.m_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,
+        proc._stdout_buf = Mock()
+        proc._stdout_buf.channel = Mock()
+        proc._stdout_buf.channel.recv_exit_status.return_value = 0
+        proc._stdout_buf.channel.expects('recv_exit_status').returns(0)
+        proc.stdout = stdout
+        m_run.return_value = proc
+        r = remote.Remote(name='jdoe@xyzzy.example.com', ssh=self.m_ssh)
+        r._runner = m_run
+        assert m_transport.getpeername.called_once_with()
+        assert proc._stdout_buf.channel.recv_exit_status.called_once_with()
+        assert m_run.called_once_with(
+            client=self.m_ssh,
             args=args,
-            stdout=fudge.inspector.arg.passes_test(
-                lambda v: isinstance(v, OutputType)),
+            stdout=StringIO(),
             name=r.shortname,
-            ).returns(ret)
-        # monkey patch ook ook
-        r._runner = run
+        )
         assert r.arch == 'test_arch'
 
-    @fudge.with_fakes
     def test_host_key(self):
-        fudge.clear_expectations()
-        ssh = fudge.Fake('SSHConnection')
-        key = ssh.expects('get_transport').returns_fake().expects(
-            'get_remote_server_key').returns_fake()
-        key.expects('get_name').returns('key_type')
-        key.expects('get_base64').returns('test ssh key')
-        r = remote.Remote(name='jdoe@xyzzy.example.com', ssh=ssh)
+        m_key = MagicMock()
+        m_key.get_name.return_value = 'key_type'
+        m_key.get_base64.return_value = 'test ssh key'
+        m_transport = MagicMock()
+        m_transport.get_remote_server_key.return_value = m_key
+        self.m_ssh.get_transport.return_value = m_transport
+        r = remote.Remote(name='jdoe@xyzzy.example.com', ssh=self.m_ssh)
         assert r.host_key == 'key_type test ssh key'
+        self.m_ssh.get_transport.assert_called_once_with()
+        m_transport.get_remote_server_key.assert_called_once_with()
 
     def test_inventory_info(self):
         r = remote.Remote('user@host', host_key='host_key')