Commit e587b46a authored by Guido Trotter's avatar Guido Trotter
Browse files

utils.ReadOneLineFile()

Read the first non-empty file line. When strict is set, abort if more
than one line is non-empty. Some unittests inspired by the reverted ones
from commit b774bb10

, and some new ones.
Signed-off-by: default avatarGuido Trotter <ultrotter@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 582ed043
......@@ -1498,6 +1498,24 @@ def WriteFile(file_name, fn=None, data=None,
return result
def ReadOneLineFile(file_name, strict=False):
"""Return the first non-empty line from a file.
@type strict: boolean
@param strict: if True, abort if the file has more than one
non-empty line
"""
file_lines = ReadFile(file_name).splitlines()
full_lines = filter(bool, file_lines)
if not file_lines or not full_lines:
raise errors.GenericError("No data in one-liner file %s" % file_name)
elif strict and len(full_lines) > 1:
raise errors.GenericError("Too many lines in one-liner file %s" %
file_name)
return full_lines[0]
def FirstFree(seq, base=0):
"""Returns the first non-existing integer from seq.
......
......@@ -52,7 +52,7 @@ from ganeti.utils import IsProcessAlive, RunCmd, \
ShellQuote, ShellQuoteArgs, TcpPing, ListVisibleFiles, \
SetEtcHostsEntry, RemoveEtcHostsEntry, FirstFree, OwnIpAddress, \
TailFile, ForceDictType, SafeEncode, IsNormAbsPath, FormatTime, \
UnescapeAndSplit, RunParts, PathJoin, HostInfo
UnescapeAndSplit, RunParts, PathJoin, HostInfo, ReadOneLineFile
from ganeti.errors import LockError, UnitParseError, GenericError, \
ProgrammerError, OpPrereqError
......@@ -541,6 +541,83 @@ class TestReadFile(testutils.GanetiTestCase):
"/dev/null/does-not-exist")
class TestReadOneLineFile(testutils.GanetiTestCase):
def setUp(self):
testutils.GanetiTestCase.setUp(self)
def testDefault(self):
data = ReadOneLineFile(self._TestDataFilename("cert1.pem"))
self.assertEqual(len(data), 27)
self.assertEqual(data, "-----BEGIN CERTIFICATE-----")
def testNotStrict(self):
data = ReadOneLineFile(self._TestDataFilename("cert1.pem"), strict=False)
self.assertEqual(len(data), 27)
self.assertEqual(data, "-----BEGIN CERTIFICATE-----")
def testStrictFailure(self):
self.assertRaises(errors.GenericError, ReadOneLineFile,
self._TestDataFilename("cert1.pem"), strict=True)
def testLongLine(self):
dummydata = (1024 * "Hello World! ")
myfile = self._CreateTempFile()
utils.WriteFile(myfile, data=dummydata)
datastrict = ReadOneLineFile(myfile, strict=True)
datalax = ReadOneLineFile(myfile, strict=False)
self.assertEqual(dummydata, datastrict)
self.assertEqual(dummydata, datalax)
def testNewline(self):
myfile = self._CreateTempFile()
myline = "myline"
for nl in ["", "\n", "\r\n"]:
dummydata = "%s%s" % (myline, nl)
utils.WriteFile(myfile, data=dummydata)
datalax = ReadOneLineFile(myfile, strict=False)
self.assertEqual(myline, datalax)
datastrict = ReadOneLineFile(myfile, strict=True)
self.assertEqual(myline, datastrict)
def testWhitespaceAndMultipleLines(self):
myfile = self._CreateTempFile()
for nl in ["", "\n", "\r\n"]:
for ws in [" ", "\t", "\t\t \t", "\t "]:
dummydata = (1024 * ("Foo bar baz %s%s" % (ws, nl)))
utils.WriteFile(myfile, data=dummydata)
datalax = ReadOneLineFile(myfile, strict=False)
if nl:
self.assert_(set("\r\n") & set(dummydata))
self.assertRaises(errors.GenericError, ReadOneLineFile,
myfile, strict=True)
explen = len("Foo bar baz ") + len(ws)
self.assertEqual(len(datalax), explen)
self.assertEqual(datalax, dummydata[:explen])
self.assertFalse(set("\r\n") & set(datalax))
else:
datastrict = ReadOneLineFile(myfile, strict=True)
self.assertEqual(dummydata, datastrict)
self.assertEqual(dummydata, datalax)
def testEmptylines(self):
myfile = self._CreateTempFile()
myline = "myline"
for nl in ["\n", "\r\n"]:
for ol in ["", "otherline"]:
dummydata = "%s%s%s%s%s%s" % (nl, nl, myline, nl, ol, nl)
utils.WriteFile(myfile, data=dummydata)
self.assert_(set("\r\n") & set(dummydata))
datalax = ReadOneLineFile(myfile, strict=False)
self.assertEqual(myline, datalax)
if ol:
self.assertRaises(errors.GenericError, ReadOneLineFile,
myfile, strict=True)
else:
datastrict = ReadOneLineFile(myfile, strict=True)
self.assertEqual(myline, datastrict)
class TestTimestampForFilename(unittest.TestCase):
def test(self):
self.assert_("." not in utils.TimestampForFilename())
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment