summaryrefslogtreecommitdiff
path: root/tests/test_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_utils.py')
-rw-r--r--tests/test_utils.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 00feb6c..e4c1c45 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -14,6 +14,8 @@
#
import unittest
+import tempfile
+import os
from env import waagent
sample_mount_list = """\
@@ -43,5 +45,39 @@ class TestWAAgentUtils(unittest.TestCase):
mp = waagent.GetMountPoint(malformed, device_name)
self.assertEqual(mp, None)
+ def test_replace_in_file_found(self):
+ tmpfilename = tempfile.mkstemp('', 'tmp', None, True)[1]
+ try:
+ tmpfile = open(tmpfilename, 'w')
+ tmpfile.write('Replace Me')
+ tmpfile.close()
+
+ result = waagent.ReplaceStringInFile(tmpfilename, r'c. ', 'ced ')
+
+ tmpfile = open(tmpfilename, 'r')
+ newcontents = tmpfile.read();
+ tmpfile.close()
+
+ self.assertEqual('Replaced Me', str(newcontents))
+ finally:
+ os.remove(tmpfilename)
+
+ def test_replace_in_file_not_found(self):
+ tmpfilename = tempfile.mkstemp('', 'tmp', None, True)[1]
+ try:
+ tmpfile = open(tmpfilename, 'w')
+ tmpfile.write('Replace Me')
+ tmpfile.close()
+
+ result = waagent.ReplaceStringInFile(tmpfilename, r'not here ', 'ced ')
+
+ tmpfile = open(tmpfilename, 'r')
+ newcontents = tmpfile.read();
+ tmpfile.close()
+
+ self.assertEqual('Replace Me', str(newcontents))
+ finally:
+ os.remove(tmpfilename)
+
if __name__ == '__main__':
unittest.main()