def test_set_auth_variable(self):
     ssh_host = HostSSH(
         address='fake_addres',
         username='******',
         password='******',
         private_key=''
     )
     self.assertDictEqual(
         {
             'username': '******',
             'password': '******'
         },
         ssh_host.auth
     )
     ssh_host = HostSSH(
         address='fake_addres',
         username='******',
         password='',
         private_key='fake_pkey'
     )
     self.assertDictEqual(
         {
             'username': '******',
             'pkey': 'fake_pkey'
         },
         ssh_host.auth
     )
 def setUp(self):
     self.host_ssh = HostSSH(address='fake_address',
                             username='******',
                             password='******')
     self.host_ssh.connect = MagicMock()
     self.fake_client = PropertyMock()
     self.fake_client.exec_command.side_effect = (
         make_fake_exec_command_output)
     self.host_ssh.client = self.fake_client
 def test_raise_exception_if_dont_have_pass_and_pkey(self):
     with self.assertRaises(PassAndPkeyEmptyException):
         HostSSH(
             address='fake_addres',
             username='******',
             password='',
             private_key=''
         )
class RunScriptTestCase(TestCase):
    def setUp(self):
        self.host_ssh = HostSSH(address='fake_address',
                                username='******',
                                password='******')
        self.host_ssh.connect = MagicMock()
        self.fake_client = PropertyMock()
        self.fake_client.exec_command.side_effect = (
            make_fake_exec_command_output)
        self.host_ssh.client = self.fake_client

    def test_script_run_with_success(self):
        output = self.host_ssh.run_script('fake command')
        self.assertTrue(self.host_ssh.client.exec_command.called)
        expected_output = {
            'stderr': '',
            'exception': '',
            'stdout': 'fake_stdout',
            'exit_code': 0
        }
        self.assertDictEqual(output, expected_output)

    def test_raise_error_if_status_code_differ_0(self):
        self.fake_client.exec_command.side_effect = (
            make_fake_err_exec_command_output)
        with self.assertRaises(ScriptFailedException):
            self.host_ssh.run_script('fake command')
        self.assertTrue(self.host_ssh.client.exec_command.called)

    def test_return_error_status_without_raise(self):
        self.fake_client.exec_command.side_effect = (
            make_fake_err_exec_command_output)
        output = self.host_ssh.run_script('fake command', raise_if_error=False)
        self.assertTrue(self.host_ssh.client.exec_command.called)
        expected_output = {
            'stderr': 'fake stderr',
            'exception': '',
            'stdout': 'error',
            'exit_code': 88
        }
        self.assertDictEqual(output, expected_output)

    def test_raise_error_on_retry(self):
        self.fake_client.exec_command.side_effect = (
            make_fake_err_exec_command_output)
        with self.assertRaises(ScriptFailedException):
            self.host_ssh.run_script('fake command', retry=True)
        self.assertEqual(self.host_ssh.client.exec_command.call_count, 2)

    def test_script_param(self):
        self.host_ssh.run_script('fake command')
        self.assertTrue(self.host_ssh.client.exec_command.called)
        script_arg = self.host_ssh.client.exec_command.call_args[0][0]
        self.assertEqual(
            script_arg,
            'sudo sh /tmp/{}'.format(self.host_ssh.script_file_name))
 def setUp(self):
     self.host_ssh = HostSSH(address='fake_address',
                             username='******',
                             password='******')
     self.fake_func = MagicMock(
         return_value={
             'stdout': 'fake_stdout',
             'stdin': 'fake_stdin',
             'stderr': 'fake_stderr',
             'exception': '',
         })
     self.decorated = connect_host(self.fake_func)