4 # Copyright (C) 2011 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Script for testing ganeti.client.gnt_instance"""
26 from ganeti import constants
27 from ganeti import utils
28 from ganeti import errors
29 from ganeti import objects
30 from ganeti.client import gnt_instance
35 class TestConsole(unittest.TestCase):
39 self._next_cmd_exitcode = 0
41 def _Test(self, console, show_command, cluster_name):
42 return gnt_instance._DoConsole(console, show_command, cluster_name,
43 feedback_fn=self._Feedback,
44 _runcmd_fn=self._FakeRunCmd)
46 def _Feedback(self, msg, *args):
49 self._output.append(msg)
51 def _FakeRunCmd(self, cmd, interactive=None):
52 self.assertTrue(interactive)
53 self.assertTrue(isinstance(cmd, list))
54 self._cmds.append(cmd)
55 return utils.RunResult(self._next_cmd_exitcode, None, "", "", "cmd",
56 utils.process._TIMEOUT_NONE, 5)
58 def testMessage(self):
59 cons = objects.InstanceConsole(instance="inst98.example.com",
60 kind=constants.CONS_MESSAGE,
61 message="Hello World")
62 self.assertEqual(self._Test(cons, False, "cluster.example.com"),
63 constants.EXIT_SUCCESS)
64 self.assertEqual(len(self._cmds), 0)
65 self.assertEqual(self._output, ["Hello World"])
68 cons = objects.InstanceConsole(instance="inst1.example.com",
69 kind=constants.CONS_VNC,
70 host="node1.example.com",
73 self.assertEqual(self._Test(cons, False, "cluster.example.com"),
74 constants.EXIT_SUCCESS)
75 self.assertEqual(len(self._cmds), 0)
76 self.assertEqual(len(self._output), 1)
77 self.assertTrue(" inst1.example.com " in self._output[0])
78 self.assertTrue(" node1.example.com:5901 " in self._output[0])
79 self.assertTrue("vnc://node1.example.com:5901/" in self._output[0])
81 def testSshShow(self):
82 cons = objects.InstanceConsole(instance="inst31.example.com",
83 kind=constants.CONS_SSH,
84 host="node93.example.com",
86 command="xm console x.y.z")
87 self.assertEqual(self._Test(cons, True, "cluster.example.com"),
88 constants.EXIT_SUCCESS)
89 self.assertEqual(len(self._cmds), 0)
90 self.assertEqual(len(self._output), 1)
91 self.assertTrue(" user_abc@node93.example.com " in self._output[0])
92 self.assertTrue("'xm console x.y.z'" in self._output[0])
95 cons = objects.InstanceConsole(instance="inst31.example.com",
96 kind=constants.CONS_SSH,
97 host="node93.example.com",
99 command=["xm", "console", "x.y.z"])
100 self.assertEqual(self._Test(cons, False, "cluster.example.com"),
101 constants.EXIT_SUCCESS)
102 self.assertEqual(len(self._cmds), 1)
103 self.assertEqual(len(self._output), 0)
105 # This is very important to prevent escapes from the console
106 self.assertTrue("-oEscapeChar=none" in self._cmds[0])
108 def testSshRunFail(self):
109 cons = objects.InstanceConsole(instance="inst31.example.com",
110 kind=constants.CONS_SSH,
111 host="node93.example.com",
113 command=["xm", "console", "x.y.z"])
115 self._next_cmd_exitcode = 100
116 self.assertRaises(errors.OpExecError, self._Test,
117 cons, False, "cluster.example.com")
118 self.assertEqual(len(self._cmds), 1)
119 self.assertEqual(len(self._output), 0)
122 if __name__ == "__main__":
123 testutils.GanetiTestProgram()