Revision 858905fb test/ganeti.utils_unittest.py

b/test/ganeti.utils_unittest.py
38 38
import unittest
39 39
import warnings
40 40
import OpenSSL
41
from cStringIO import StringIO
41 42

  
42 43
import testutils
43 44
from ganeti import constants
......
2242 2243
    self.assertFalse(utils.IgnoreProcessNotFound(os.kill, pid, 0))
2243 2244

  
2244 2245

  
2246
class TestShellWriter(unittest.TestCase):
2247
  def test(self):
2248
    buf = StringIO()
2249
    sw = utils.ShellWriter(buf)
2250
    sw.Write("#!/bin/bash")
2251
    sw.Write("if true; then")
2252
    sw.IncIndent()
2253
    try:
2254
      sw.Write("echo true")
2255

  
2256
      sw.Write("for i in 1 2 3")
2257
      sw.Write("do")
2258
      sw.IncIndent()
2259
      try:
2260
        self.assertEqual(sw._indent, 2)
2261
        sw.Write("date")
2262
      finally:
2263
        sw.DecIndent()
2264
      sw.Write("done")
2265
    finally:
2266
      sw.DecIndent()
2267
    sw.Write("echo %s", utils.ShellQuote("Hello World"))
2268
    sw.Write("exit 0")
2269

  
2270
    self.assertEqual(sw._indent, 0)
2271

  
2272
    output = buf.getvalue()
2273

  
2274
    self.assert_(output.endswith("\n"))
2275

  
2276
    lines = output.splitlines()
2277
    self.assertEqual(len(lines), 9)
2278
    self.assertEqual(lines[0], "#!/bin/bash")
2279
    self.assert_(re.match(r"^\s+date$", lines[5]))
2280
    self.assertEqual(lines[7], "echo 'Hello World'")
2281

  
2282
  def testEmpty(self):
2283
    buf = StringIO()
2284
    sw = utils.ShellWriter(buf)
2285
    sw = None
2286
    self.assertEqual(buf.getvalue(), "")
2287

  
2288

  
2245 2289
if __name__ == '__main__':
2246 2290
  testutils.GanetiTestProgram()

Also available in: Unified diff