Commit b774bb10 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

utils.ReadFile: Add parameter to read only one line



Reading only one line is useful for reading passwords from files. This
also adds unittests for utils.ReadFile.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 95f84636
......@@ -1383,21 +1383,28 @@ def EnsureDirs(dirs):
raise errors.GenericError("%s is not a directory" % dir_name)
def ReadFile(file_name, size=-1):
def ReadFile(file_name, size=-1, oneline=False):
"""Reads a file.
@type size: int
@param size: Read at most size bytes (if negative, entire file)
@type oneline: bool
@param oneline: Whether to read only one line (newline char is not included)
@rtype: str
@return: the (possibly partial) content of the file
"""
f = open(file_name, "r")
try:
return f.read(size)
if oneline:
data = f.readline(size).rstrip("\r\n")
else:
data = f.read(size)
finally:
f.close()
return data
def WriteFile(file_name, fn=None, data=None,
mode=None, uid=-1, gid=-1,
......
......@@ -38,6 +38,7 @@ import OpenSSL
import warnings
import distutils.version
import glob
import md5
import ganeti
import testutils
......@@ -516,6 +517,82 @@ class TestMatchNameComponent(unittest.TestCase):
None)
class TestReadFile(testutils.GanetiTestCase):
def setUp(self):
testutils.GanetiTestCase.setUp(self)
self.tmpdir = tempfile.mkdtemp()
self.fname = utils.PathJoin(self.tmpdir, "data1")
def tearDown(self):
testutils.GanetiTestCase.tearDown(self)
shutil.rmtree(self.tmpdir)
def testReadAll(self):
data = utils.ReadFile(self._TestDataFilename("cert1.pem"))
self.assertEqual(len(data), 814)
h = md5.new()
h.update(data)
self.assertEqual(h.hexdigest(), "a491efb3efe56a0535f924d5f8680fd4")
def testReadSize(self):
data = utils.ReadFile(self._TestDataFilename("cert1.pem"),
size=100)
self.assertEqual(len(data), 100)
h = md5.new()
h.update(data)
self.assertEqual(h.hexdigest(), "893772354e4e690b9efd073eed433ce7")
def testReadOneline(self):
data = utils.ReadFile(self._TestDataFilename("cert1.pem"),
oneline=True)
self.assertEqual(len(data), 27)
self.assertEqual(data, "-----BEGIN CERTIFICATE-----")
def testReadOnelineSize(self):
dummydata = (1024 * "Hello World! ")
self.assertFalse(set("\r\n") & set(dummydata))
utils.WriteFile(self.fname, data=dummydata)
data = utils.ReadFile(self.fname, oneline=True, size=555)
self.assertEqual(len(data), 555)
self.assertEqual(data, dummydata[:555])
self.assertFalse(set("\r\n") & set(data))
def testReadOnelineSize2(self):
for end in ["\n", "\r\n"]:
dummydata = (1024 * ("Hello World%s" % end))
self.assert_(set("\r\n") & set(dummydata))
utils.WriteFile(self.fname, data=dummydata)
data = utils.ReadFile(self.fname, oneline=True, size=555)
self.assertEqual(len(data), len("Hello World"))
self.assertEqual(data, dummydata[:11])
self.assertFalse(set("\r\n") & set(data))
def testReadOnelineWhitespace(self):
for ws in [" ", "\t", "\t\t \t", "\t "]:
dummydata = (1024 * ("Foo bar baz %s\n" % ws))
self.assert_(set("\r\n") & set(dummydata))
utils.WriteFile(self.fname, data=dummydata)
data = utils.ReadFile(self.fname, oneline=True, size=555)
explen = len("Foo bar baz ") + len(ws)
self.assertEqual(len(data), explen)
self.assertEqual(data, dummydata[:explen])
self.assertFalse(set("\r\n") & set(data))
def testError(self):
self.assertRaises(EnvironmentError, utils.ReadFile,
utils.PathJoin(self.tmpdir, "does-not-exist"))
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