Revision 1d466a4f

b/lib/backend.py
823 823
  @param instance: the name of the instance being imported/added/etc.
824 824

  
825 825
  """
826
  base = "%s-%s-%s-%d.log" % (kind, os_name, instance, int(time.time()))
826
  base = ("%s-%s-%s-%s.log" %
827
          (kind, os_name, instance, utils.TimestampForFilename()))
827 828
  return utils.PathJoin(constants.LOG_OS_DIR, base)
828 829

  
829 830

  
b/lib/utils.py
1119 1119
  RemoveEtcHostsEntry(constants.ETC_HOSTS, hi.ShortName())
1120 1120

  
1121 1121

  
1122
def TimestampForFilename():
1123
  """Returns the current time formatted for filenames.
1124

  
1125
  The format doesn't contain colons as some shells and applications them as
1126
  separators.
1127

  
1128
  """
1129
  return time.strftime("%Y-%m-%d_%H_%M_%S")
1130

  
1131

  
1122 1132
def CreateBackup(file_name):
1123 1133
  """Creates a backup of a file.
1124 1134

  
......
1133 1143
    raise errors.ProgrammerError("Can't make a backup of a non-file '%s'" %
1134 1144
                                file_name)
1135 1145

  
1136
  prefix = '%s.backup-%d.' % (os.path.basename(file_name), int(time.time()))
1146
  prefix = ("%s.backup-%s." %
1147
            (os.path.basename(file_name), TimestampForFilename()))
1137 1148
  dir_name = os.path.dirname(file_name)
1138 1149

  
1139 1150
  fsrc = open(file_name, 'rb')
......
1141 1152
    (fd, backup_name) = tempfile.mkstemp(prefix=prefix, dir=dir_name)
1142 1153
    fdst = os.fdopen(fd, 'wb')
1143 1154
    try:
1155
      logging.debug("Backing up %s at %s", file_name, backup_name)
1144 1156
      shutil.copyfileobj(fsrc, fdst)
1145 1157
    finally:
1146 1158
      fdst.close()
b/test/ganeti.utils_unittest.py
38 38
import OpenSSL
39 39
import warnings
40 40
import distutils.version
41
import glob
41 42

  
42 43
import ganeti
43 44
import testutils
......
513 514
                     None)
514 515

  
515 516

  
517
class TestTimestampForFilename(unittest.TestCase):
518
  def test(self):
519
    self.assert_("." not in utils.TimestampForFilename())
520
    self.assert_(":" not in utils.TimestampForFilename())
521

  
522

  
523
class TestCreateBackup(testutils.GanetiTestCase):
524
  def setUp(self):
525
    testutils.GanetiTestCase.setUp(self)
526

  
527
    self.tmpdir = tempfile.mkdtemp()
528

  
529
  def tearDown(self):
530
    testutils.GanetiTestCase.tearDown(self)
531

  
532
    shutil.rmtree(self.tmpdir)
533

  
534
  def testEmpty(self):
535
    filename = utils.PathJoin(self.tmpdir, "config.data")
536
    utils.WriteFile(filename, data="")
537
    bname = utils.CreateBackup(filename)
538
    self.assertFileContent(bname, "")
539
    self.assertEqual(len(glob.glob("%s*" % filename)), 2)
540
    utils.CreateBackup(filename)
541
    self.assertEqual(len(glob.glob("%s*" % filename)), 3)
542
    utils.CreateBackup(filename)
543
    self.assertEqual(len(glob.glob("%s*" % filename)), 4)
544

  
545
    fifoname = utils.PathJoin(self.tmpdir, "fifo")
546
    os.mkfifo(fifoname)
547
    self.assertRaises(errors.ProgrammerError, utils.CreateBackup, fifoname)
548

  
549
  def testContent(self):
550
    bkpcount = 0
551
    for data in ["", "X", "Hello World!\n" * 100, "Binary data\0\x01\x02\n"]:
552
      for rep in [1, 2, 10, 127]:
553
        testdata = data * rep
554

  
555
        filename = utils.PathJoin(self.tmpdir, "test.data_")
556
        utils.WriteFile(filename, data=testdata)
557
        self.assertFileContent(filename, testdata)
558

  
559
        for _ in range(3):
560
          bname = utils.CreateBackup(filename)
561
          bkpcount += 1
562
          self.assertFileContent(bname, testdata)
563
          self.assertEqual(len(glob.glob("%s*" % filename)), 1 + bkpcount)
564

  
565

  
516 566
class TestFormatUnit(unittest.TestCase):
517 567
  """Test case for the FormatUnit function"""
518 568

  

Also available in: Unified diff