4 # Copyright (C) 2006, 2007, 2010 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Script for testing ganeti.utils.nodesetup"""
28 from ganeti import constants
29 from ganeti import utils
34 class TestEtcHosts(testutils.GanetiTestCase):
35 """Test functions modifying /etc/hosts"""
38 testutils.GanetiTestCase.setUp(self)
39 self.tmpname = self._CreateTempFile()
40 handle = open(self.tmpname, "w")
42 handle.write("# This is a test file for /etc/hosts\n")
43 handle.write("127.0.0.1\tlocalhost\n")
44 handle.write("192.0.2.1 router gw\n")
48 def testSettingNewIp(self):
49 utils.SetEtcHostsEntry(self.tmpname, "198.51.100.4", "myhost.example.com",
52 self.assertFileContent(self.tmpname,
53 "# This is a test file for /etc/hosts\n"
54 "127.0.0.1\tlocalhost\n"
55 "192.0.2.1 router gw\n"
56 "198.51.100.4\tmyhost.example.com myhost\n")
57 self.assertFileMode(self.tmpname, 0644)
59 def testSettingExistingIp(self):
60 utils.SetEtcHostsEntry(self.tmpname, "192.0.2.1", "myhost.example.com",
63 self.assertFileContent(self.tmpname,
64 "# This is a test file for /etc/hosts\n"
65 "127.0.0.1\tlocalhost\n"
66 "192.0.2.1\tmyhost.example.com myhost\n")
67 self.assertFileMode(self.tmpname, 0644)
69 def testSettingDuplicateName(self):
70 utils.SetEtcHostsEntry(self.tmpname, "198.51.100.4", "myhost", ["myhost"])
72 self.assertFileContent(self.tmpname,
73 "# This is a test file for /etc/hosts\n"
74 "127.0.0.1\tlocalhost\n"
75 "192.0.2.1 router gw\n"
76 "198.51.100.4\tmyhost\n")
77 self.assertFileMode(self.tmpname, 0644)
79 def testRemovingExistingHost(self):
80 utils.RemoveEtcHostsEntry(self.tmpname, "router")
82 self.assertFileContent(self.tmpname,
83 "# This is a test file for /etc/hosts\n"
84 "127.0.0.1\tlocalhost\n"
86 self.assertFileMode(self.tmpname, 0644)
88 def testRemovingSingleExistingHost(self):
89 utils.RemoveEtcHostsEntry(self.tmpname, "localhost")
91 self.assertFileContent(self.tmpname,
92 "# This is a test file for /etc/hosts\n"
93 "192.0.2.1 router gw\n")
94 self.assertFileMode(self.tmpname, 0644)
96 def testRemovingNonExistingHost(self):
97 utils.RemoveEtcHostsEntry(self.tmpname, "myhost")
99 self.assertFileContent(self.tmpname,
100 "# This is a test file for /etc/hosts\n"
101 "127.0.0.1\tlocalhost\n"
102 "192.0.2.1 router gw\n")
103 self.assertFileMode(self.tmpname, 0644)
105 def testRemovingAlias(self):
106 utils.RemoveEtcHostsEntry(self.tmpname, "gw")
108 self.assertFileContent(self.tmpname,
109 "# This is a test file for /etc/hosts\n"
110 "127.0.0.1\tlocalhost\n"
111 "192.0.2.1 router\n")
112 self.assertFileMode(self.tmpname, 0644)
115 if __name__ == "__main__":
116 testutils.GanetiTestProgram()