Revision 582ed043 test/ganeti.utils_unittest.py

b/test/ganeti.utils_unittest.py
518 518

  
519 519

  
520 520
class TestReadFile(testutils.GanetiTestCase):
521
  def setUp(self):
522
    testutils.GanetiTestCase.setUp(self)
523

  
524
    self.tmpdir = tempfile.mkdtemp()
525
    self.fname = utils.PathJoin(self.tmpdir, "data1")
526

  
527
  def tearDown(self):
528
    testutils.GanetiTestCase.tearDown(self)
529

  
530
    shutil.rmtree(self.tmpdir)
531 521

  
532 522
  def testReadAll(self):
533 523
    data = utils.ReadFile(self._TestDataFilename("cert1.pem"))
......
546 536
    h.update(data)
547 537
    self.assertEqual(h.hexdigest(), "893772354e4e690b9efd073eed433ce7")
548 538

  
549
  def testReadOneline(self):
550
    data = utils.ReadFile(self._TestDataFilename("cert1.pem"),
551
                          oneline=True)
552
    self.assertEqual(len(data), 27)
553
    self.assertEqual(data, "-----BEGIN CERTIFICATE-----")
554

  
555
  def testReadOnelineSize(self):
556
    dummydata = (1024 * "Hello World! ")
557
    self.assertFalse(set("\r\n") & set(dummydata))
558

  
559
    utils.WriteFile(self.fname, data=dummydata)
560

  
561
    data = utils.ReadFile(self.fname, oneline=True, size=555)
562
    self.assertEqual(len(data), 555)
563
    self.assertEqual(data, dummydata[:555])
564
    self.assertFalse(set("\r\n") & set(data))
565

  
566
  def testReadOnelineSize2(self):
567
    for end in ["\n", "\r\n"]:
568
      dummydata = (1024 * ("Hello World%s" % end))
569
      self.assert_(set("\r\n") & set(dummydata))
570

  
571
      utils.WriteFile(self.fname, data=dummydata)
572

  
573
      data = utils.ReadFile(self.fname, oneline=True, size=555)
574
      self.assertEqual(len(data), len("Hello World"))
575
      self.assertEqual(data, dummydata[:11])
576
      self.assertFalse(set("\r\n") & set(data))
577

  
578
  def testReadOnelineWhitespace(self):
579
    for ws in [" ", "\t", "\t\t  \t", "\t "]:
580
      dummydata = (1024 * ("Foo bar baz %s\n" % ws))
581
      self.assert_(set("\r\n") & set(dummydata))
582

  
583
      utils.WriteFile(self.fname, data=dummydata)
584

  
585
      data = utils.ReadFile(self.fname, oneline=True, size=555)
586
      explen = len("Foo bar baz ") + len(ws)
587
      self.assertEqual(len(data), explen)
588
      self.assertEqual(data, dummydata[:explen])
589
      self.assertFalse(set("\r\n") & set(data))
590

  
591 539
  def testError(self):
592 540
    self.assertRaises(EnvironmentError, utils.ReadFile,
593
                      utils.PathJoin(self.tmpdir, "does-not-exist"))
541
                      "/dev/null/does-not-exist")
594 542

  
595 543

  
596 544
class TestTimestampForFilename(unittest.TestCase):

Also available in: Unified diff