def testPrefixIdempotencey(self): original = ( "/var/log/libvirt/libvirtd.log {\n" " weekly\n" "}\n" ) self._writeConf(original) with ConfigFile(self.tname, version='3.4.4', sectionStart="# start conf", sectionEnd="# end conf", prefix="# comment ") as conf: conf.prefixLines() with open(self.tname, 'r') as f: self.assertEqual(f.read(), "# comment /var/log/libvirt/libvirtd.log {\n" "# comment weekly\n" "# comment }\n") with ConfigFile(self.tname, version='3.4.4', sectionStart="# start conf", sectionEnd="# end conf", prefix="# comment ") as conff: conff.unprefixLines() with open(self.tname, 'r') as f: self.assertEqual(f.read(), original)
def testOutOfContext(self): conff = ConfigFile(self.tname, version='3.4.4', sectionStart="# start conf", sectionEnd="# end conf") self.assertRaises(RuntimeError, conff.prefixLines) self.assertRaises(RuntimeError, conff.removeConf)
def testHasConf(self): self._writeConf("key=val\n" "kay=val\n" "# start conf-3.4.4\n" "all you sections are belong to us\n" "# end conf-3.4.4\n") self.assertTrue(ConfigFile(self.tname, version='3.4.4', sectionStart="# start conf", sectionEnd="# end conf").hasConf())
def testRemoveEntireLinePrefix(self): self._writeConf("# comment\n") with ConfigFile(self.tname, version='3.4.4', sectionStart="# start conf", sectionEnd="# end conf", prefix="# comment") as conf: conf.unprefixLines() with open(self.tname, 'r') as f: self.assertEqual(f.read(), "\n")
def testRemoveConfSection(self): self._writeConf("key=val\n" "remove me!(see 'Backward compatibility')# by vdsm\n" "key=val\n" "# start conf-text-here don't matter\n" "all you sections are belong to us\n" "# end conf-text-here don't matter\n" "# comment line\n") with ConfigFile(self.tname, version='3.4.4', sectionStart="# start conf", sectionEnd="# end conf", prefix="# comment") as conf: conf.removeConf() with open(self.tname, 'r') as f: self.assertEqual(f.read(), "key=val\n" "key=val\n" "# comment line\n")
def testSort(self): self._writeConf("") with ConfigFile(self.tname, version='3.4.4', sectionStart="# start conf", sectionEnd="# end conf") as conf: conf.addEntry("key3", "val") conf.addEntry("key2", "val") conf.addEntry("key1", "val") conf.addEntry("key4", "val") with open(self.tname, 'r') as f: self.assertEqual(f.read(), "# start conf-3.4.4\n" "key1=val\n" "key2=val\n" "key3=val\n" "key4=val\n" "# end conf-3.4.4\n")
def testAddExistingConf(self): self._writeConf("key1=val1\n" " key2 =val2\n" "#key3=val4") with ConfigFile(self.tname, version='3.4.4', sectionStart="# start conf", sectionEnd="# end conf") as conf: conf.addEntry("key3", "val3") conf.addEntry("key2", "val3") with open(self.tname, 'r') as f: self.assertEqual(f.read(), "key1=val1\n" " key2 =val2\n" "#key3=val4" "# start conf-3.4.4\n" "key3=val3\n" "# end conf-3.4.4\n")
def testPrefixAndPrepend(self): self._writeConf("/var/log/libvirt/libvirtd.log {\n" " weekly\n" "}\n") with ConfigFile(self.tname, version='3.4.4', sectionStart="# start conf", sectionEnd="# end conf", prefix="# comment ") as conf: conf.prefixLines() conf.prependSection("Some text to\n" "add at the top\n") with open(self.tname, 'r') as f: self.assertEqual(f.read(), "# start conf-3.4.4\n" "Some text to\n" "add at the top\n" "# end conf-3.4.4\n" "# comment /var/log/libvirt/libvirtd.log {\n" "# comment weekly\n" "# comment }\n")