Example #1
0
 def test_localhost(self):
     """
     Simply login to the local machine and exit.
     """
     results_list = list(sshm('localhost', 'exit'))
     result = results_list[0]
     self.assertEqual(0, result['return_code'])
Example #2
0
 def test_localhost(self):
     """
     Simply login to the local machine and exit.
     """
     results_list = list(sshm('localhost', 'exit'))
     result = results_list[0]
     self.assertEqual(0, result['return_code'])
Example #3
0
    def test_stdin(self):
        """
        Test the STDIN sending process between sshm and ssh.
        """
        sub, proc = fake_subprocess('', '', 0)
        self.addCleanup(setattr, lib, 'popen', lib.popen)
        lib.popen = sub.popen

        # Fake the process's poll to be None once
        c = [1, None]
        def none_once():
            return c.pop()
        proc.poll = none_once

        from io import BytesIO
        stdin_contents = b'foobar'
        stdin = BytesIO(stdin_contents)

        result_list = list(lib.sshm('example.com', 'exit', stdin=stdin))
        self.assertEqual(1, len(result_list))
        self.assertEqual(result_list[0],
                {
                    'stdout': '',
                    'uri': 'example.com',
                    'cmd': ['ssh', 'example.com', 'exit'],
                    'return_code': 0,
                    'stderr': '',
                    'thread_num':0,
                    }
                )
Example #4
0
 def test_localhost_nonzero(self):
     """
     Simply login to the local machine and exit with a non-zero.
     """
     results_list = list(sshm('localhost', 'exit 1'))
     result = results_list[0]
     self.assertEqual(1, result['return_code'])
Example #5
0
 def test_localhost_nonzero(self):
     """
     Simply login to the local machine and exit with a non-zero.
     """
     results_list = list(sshm('localhost', 'exit 1'))
     result = results_list[0]
     self.assertEqual(1, result['return_code'])
Example #6
0
    def test_stdin(self):
        """
        Test the STDIN sending process between sshm and ssh.
        """
        sub, proc = fake_subprocess('', '', 0)
        self.addCleanup(setattr, lib, 'popen', lib.popen)
        lib.popen = sub.popen

        # Fake the process's poll to be None once
        c = [1, None]

        def none_once():
            return c.pop()

        proc.poll = none_once

        from io import BytesIO
        stdin_contents = b'foobar'
        stdin = BytesIO(stdin_contents)

        result_list = list(lib.sshm('example.com', 'exit', stdin=stdin))
        self.assertEqual(1, len(result_list))
        self.assertEqual(
            result_list[0], {
                'stdout': '',
                'uri': 'example.com',
                'cmd': ['ssh', 'example.com', 'exit'],
                'return_code': 0,
                'stderr': '',
                'thread_num': 0,
            })
Example #7
0
    def test_sshm_ssh(self):
        """
        Test how sshm uses the 'ssh' function.
        """
        def side_effect(thread_num, context, *a, **kw):
            """
            Send empty results.
            """
            sink = context.socket(zmq.PUSH)
            sink.connect(lib.SINK_URL)
            sink.send_pyobj({
                'thread_num': thread_num,
            })

        self.addCleanup(setattr, lib, 'ssh', lib.ssh)
        lib.ssh = MagicMock(side_effect=side_effect)

        from io import BytesIO
        stdin_contents = b'stdin contents'
        stdin = BytesIO(stdin_contents)

        extra_arguments = [
            '-o=Something yes',
        ]
        result_list = list(
            lib.sshm('example[01-03].com',
                     'foo',
                     extra_arguments=extra_arguments,
                     stdin=stdin))

        for result in result_list:
            self.assertIn('thread_num', result)

        expected_uris = [
            'example01.com',
            'example02.com',
            'example03.com',
        ]

        self.assertTrue(lib.ssh.called)
        # Verify each ssh function call
        self.assertEqual(len(lib.ssh.call_args_list), len(expected_uris))
        for args_list, expected_uri in zip(lib.ssh.call_args_list,
                                           expected_uris):
            args, kwargs = args_list

            self.assertEqual(kwargs, {})

            thread_num, context, uri, command, extra_arguments, stdin = args
            self.assertEqual(int, type(thread_num))
            self.assertEqual(zmq.Context, type(context))
            self.assertEqual(expected_uri, uri)
            self.assertEqual('foo', command)
            self.assertEqual(extra_arguments, extra_arguments)
            self.assertTrue(stdin)
Example #8
0
    def test_localhost_multi(self):
        """
        Simply login to the local machine three times and verify there is
        output.
        """
        results_list = list(
            sshm('localhost,localhost,localhost', 'echo testing'))

        # Verify all instances are unique
        self.assertEqual(3, len(results_list))

        for result in results_list:
            self.assertEqual(0, result['return_code'])
            self.assertEqual('testing\n', result['stdout'])
Example #9
0
    def test_localhost_multi(self):
        """
        Simply login to the local machine three times and verify there is
        output.
        """
        results_list = list(sshm('localhost,localhost,localhost', 'echo testing'))

        # Verify all instances are unique
        self.assertEqual(3,
                len(results_list))

        for result in results_list:
            self.assertEqual(0, result['return_code'])
            self.assertEqual('testing\n', result['stdout'])
Example #10
0
    def test_localhost_stdin(self):
        """
        Login to the local machine and pass a file object through stdin.
        """
        contents = b'hello'

        with _get_temp_file(contents) as file_handle:
            results_list = list(sshm('localhost', 'cat', stdin=file_handle))
            result = results_list[0]
            self.assertEqual(result['return_code'], 0)
            self.assertEqual('hello', result['stdout'])
            # We expect a unicode string.  Python3.x's strings are unicode.
            try:
                self.assertIsInstance(result['stdout'], unicode)
            except NameError:
                self.assertIsInstance(result['stdout'], str)
Example #11
0
    def test_triple(self):
        """
        You can SSH into three servers at once.
        """
        sub, proc = fake_subprocess('', '', 0)
        self.addCleanup(setattr, lib, 'popen', lib.popen)
        lib.popen = sub.popen

        results_list = list(
            lib.sshm(['example01.com', 'example[02-03].com'], 'exit'))
        self.assertEqual(3, len(results_list))
        self.assertEqual(3, len(set([r['uri'] for r in results_list])))
        for result in results_list:
            self.assertNotIn('traceback', result)
            self.assertIn(result['uri'],
                          ['example01.com', 'example02.com', 'example03.com'])
Example #12
0
    def test_localhost_stdin(self):
        """
        Login to the local machine and pass a file object through stdin.
        """
        contents = b'hello'

        with _get_temp_file(contents) as file_handle:
            results_list = list(sshm('localhost', 'cat', stdin=file_handle))
            result = results_list[0]
            self.assertEqual(result['return_code'], 0)
            self.assertEqual('hello', result['stdout'])
            # We expect a unicode string.  Python3.x's strings are unicode.
            try:
                self.assertIsInstance(result['stdout'], unicode)
            except NameError:
                self.assertIsInstance(result['stdout'], str)
Example #13
0
    def test_sshm_ssh(self):
        """
        Test how sshm uses the 'ssh' function.
        """
        def side_effect(thread_num, context, *a, **kw):
            """
            Send empty results.
            """
            sink = context.socket(zmq.PUSH)
            sink.connect(lib.SINK_URL)
            sink.send_pyobj({'thread_num':thread_num,})
        self.addCleanup(setattr, lib, 'ssh', lib.ssh)
        lib.ssh = MagicMock(side_effect=side_effect)

        from io import BytesIO
        stdin_contents = b'stdin contents'
        stdin = BytesIO(stdin_contents)

        extra_arguments = ['-o=Something yes',]
        result_list = list(lib.sshm('example[01-03].com', 'foo',
            extra_arguments=extra_arguments, stdin=stdin))

        for result in result_list:
            self.assertIn('thread_num', result)

        expected_uris = [
                'example01.com',
                'example02.com',
                'example03.com',
                ]

        self.assertTrue(lib.ssh.called)
        # Verify each ssh function call
        self.assertEqual(len(lib.ssh.call_args_list), len(expected_uris))
        for args_list, expected_uri in zip(lib.ssh.call_args_list, expected_uris):
            args, kwargs = args_list

            self.assertEqual(kwargs, {})

            thread_num, context, uri, command, extra_arguments, stdin = args
            self.assertEqual(int, type(thread_num))
            self.assertEqual(zmq.Context, type(context))
            self.assertEqual(expected_uri, uri)
            self.assertEqual('foo', command)
            self.assertEqual(extra_arguments, extra_arguments)
            self.assertTrue(stdin)
Example #14
0
    def test_triple(self):
        """
        You can SSH into three servers at once.
        """
        sub, proc = fake_subprocess('', '', 0)
        self.addCleanup(setattr, lib, 'popen', lib.popen)
        lib.popen = sub.popen

        results_list = list(lib.sshm(['example01.com', 'example[02-03].com'],
            'exit'))
        self.assertEqual(3, len(results_list))
        self.assertEqual(3,
                len(set([r['uri'] for r in results_list]))
                )
        for result in results_list:
            self.assertNotIn('traceback', result)
            self.assertIn(result['uri'],
                    ['example01.com', 'example02.com', 'example03.com']
                    )
Example #15
0
    def test_simple(self):
        """
        Test a simple sshm usage.
        """
        sub, proc = fake_subprocess('', '', 0)
        self.addCleanup(setattr, lib, 'popen', lib.popen)
        lib.popen = sub.popen

        result_list = list(lib.sshm('example.com', 'exit'))
        self.assertEqual(1, len(result_list))
        self.assertEqual(
            result_list[0], {
                'stdout': '',
                'uri': 'example.com',
                'cmd': ['ssh', 'example.com', 'exit'],
                'return_code': 0,
                'stderr': '',
                'thread_num': 0,
            })
Example #16
0
    def test_binary_copy(self):
        """
        Binary files are transfered correctly using STDIN.
        """
        contents = os.urandom(9999999)

        with _get_temp_file(contents) as file_handle, \
        tempfile.NamedTemporaryFile() as tfile_handle:
            results_list = list(sshm('localhost',
                 'cat > %s' % tfile_handle.name, stdin=file_handle)
                 )
            result = results_list[0]
            self.assertEqual(0, result['return_code'])

            self.assertTrue(os.path.isfile(tfile_handle.name))

            # Read the contents of the copied file, make sure they are intact.
            with open(tfile_handle.name, 'rb') as tfile_handle:
                self.assertEqual(tfile_handle.read(), contents)
Example #17
0
    def test_simple(self):
        """
        Test a simple sshm usage.
        """
        sub, proc = fake_subprocess('', '', 0)
        self.addCleanup(setattr, lib, 'popen', lib.popen)
        lib.popen = sub.popen

        result_list = list(lib.sshm('example.com', 'exit'))
        self.assertEqual(1, len(result_list))
        self.assertEqual(result_list[0],
                {
                    'stdout': '',
                    'uri': 'example.com',
                    'cmd': ['ssh', 'example.com', 'exit'],
                    'return_code': 0,
                    'stderr': '',
                    'thread_num':0,
                    }
                )
Example #18
0
    def test_binary_copy(self):
        """
        Binary files are transfered correctly using STDIN.
        """
        contents = os.urandom(9999999)

        with _get_temp_file(contents) as file_handle, \
        tempfile.NamedTemporaryFile() as tfile_handle:
            results_list = list(
                sshm('localhost',
                     'cat > %s' % tfile_handle.name,
                     stdin=file_handle))
            result = results_list[0]
            self.assertEqual(0, result['return_code'])

            self.assertTrue(os.path.isfile(tfile_handle.name))

            # Read the contents of the copied file, make sure they are intact.
            with open(tfile_handle.name, 'rb') as tfile_handle:
                self.assertEqual(tfile_handle.read(), contents)