def test_SSH(self, popenMock): params = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", "1.2.1", "8440", "root") host_log_mock = MagicMock() log = {'text': ""} def write_side_effect(text): log['text'] = log['text'] + text host_log_mock.write.side_effect = write_side_effect ssh = SSH(params.user, params.sshkey_file, "dummy-host", "dummy-command", params.bootdir, host_log_mock) log_sample = "log_sample" error_sample = "error_sample" # Successful run process = MagicMock() popenMock.return_value = process process.communicate.return_value = (log_sample, error_sample) process.returncode = 0 retcode = ssh.run() self.assertTrue(popenMock.called) self.assertTrue(log_sample in log['text']) self.assertTrue(error_sample in log['text']) command_str = str(popenMock.call_args[0][0]) self.assertEquals(command_str, "['ssh', '-o', 'ConnectTimeOut=60', '-o', " "'StrictHostKeyChecking=no', '-o', 'BatchMode=yes', '-tt', '-i', " "'sshkey_file', 'root@dummy-host', 'dummy-command']") self.assertEqual(retcode["exitstatus"], 0) log['text'] = "" #unsuccessfull run process.returncode = 1 retcode = ssh.run() self.assertTrue(log_sample in log['text']) self.assertTrue(error_sample in log['text']) self.assertEqual(retcode["exitstatus"], 1) log['text'] = "" # unsuccessful run with error message process.returncode = 1 dummy_error_message = "dummy_error_message" ssh = SSH(params.user, params.sshkey_file, "dummy-host", "dummy-command", params.bootdir, host_log_mock, errorMessage= dummy_error_message) retcode = ssh.run() self.assertTrue(log_sample in log['text']) self.assertTrue(error_sample in log['text']) self.assertTrue(dummy_error_message in log['text']) self.assertEqual(retcode["exitstatus"], 1)
def test_SSH(self, popenMock): params = SharedState("root", "123", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", "1.2.1", "8440", "root") host_log_mock = MagicMock() log = {'text': ""} def write_side_effect(text): log['text'] = log['text'] + text host_log_mock.write.side_effect = write_side_effect ssh = SSH(params.user, params.sshPort, params.sshkey_file, "dummy-host", "dummy-command", params.bootdir, host_log_mock) log_sample = "log_sample" error_sample = "error_sample" # Successful run process = MagicMock() popenMock.return_value = process process.communicate.return_value = (log_sample, error_sample) process.returncode = 0 retcode = ssh.run() self.assertTrue(popenMock.called) self.assertTrue(log_sample in log['text']) self.assertTrue(error_sample in log['text']) command_str = str(popenMock.call_args[0][0]) self.assertEquals(command_str, "['ssh', '-o', 'ConnectTimeOut=60', '-o', " "'StrictHostKeyChecking=no', '-o', 'BatchMode=yes', '-tt', '-i', " "'sshkey_file', '-p', '123', 'root@dummy-host', 'dummy-command']") self.assertEqual(retcode["exitstatus"], 0) log['text'] = "" #unsuccessfull run process.returncode = 1 retcode = ssh.run() self.assertTrue(log_sample in log['text']) self.assertTrue(error_sample in log['text']) self.assertEqual(retcode["exitstatus"], 1) log['text'] = "" # unsuccessful run with error message process.returncode = 1 dummy_error_message = "dummy_error_message" ssh = SSH(params.user, params.sshPort, params.sshkey_file, "dummy-host", "dummy-command", params.bootdir, host_log_mock, errorMessage= dummy_error_message) retcode = ssh.run() self.assertTrue(log_sample in log['text']) self.assertTrue(error_sample in log['text']) self.assertTrue(dummy_error_message in log['text']) self.assertEqual(retcode["exitstatus"], 1)