4 # Copyright (C) 2006, 2007, 2010, 2011 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")
47 os.chmod(self.tmpname, 0644)
49 def testSettingNewIp(self):
50 utils.SetEtcHostsEntry(self.tmpname, "198.51.100.4", "myhost.example.com",
53 self.assertFileContent(self.tmpname,
54 "# This is a test file for /etc/hosts\n"
55 "127.0.0.1\tlocalhost\n"
56 "192.0.2.1 router gw\n"
57 "198.51.100.4\tmyhost.example.com myhost\n")
58 self.assertFileMode(self.tmpname, 0644)
60 def testSettingExistingIp(self):
61 utils.SetEtcHostsEntry(self.tmpname, "192.0.2.1", "myhost.example.com",
64 self.assertFileContent(self.tmpname,
65 "# This is a test file for /etc/hosts\n"
66 "127.0.0.1\tlocalhost\n"
67 "192.0.2.1\tmyhost.example.com myhost\n")
68 self.assertFileMode(self.tmpname, 0644)
70 def testSettingDuplicateName(self):
71 utils.SetEtcHostsEntry(self.tmpname, "198.51.100.4", "myhost", ["myhost"])
73 self.assertFileContent(self.tmpname,
74 "# This is a test file for /etc/hosts\n"
75 "127.0.0.1\tlocalhost\n"
76 "192.0.2.1 router gw\n"
77 "198.51.100.4\tmyhost\n")
78 self.assertFileMode(self.tmpname, 0644)
80 def testSettingOrdering(self):
81 utils.SetEtcHostsEntry(self.tmpname, "127.0.0.1", "localhost.localdomain",
84 self.assertFileContent(self.tmpname,
85 "# This is a test file for /etc/hosts\n"
86 "127.0.0.1\tlocalhost.localdomain localhost\n"
87 "192.0.2.1 router gw\n")
88 self.assertFileMode(self.tmpname, 0644)
90 def testRemovingExistingHost(self):
91 utils.RemoveEtcHostsEntry(self.tmpname, "router")
93 self.assertFileContent(self.tmpname,
94 "# This is a test file for /etc/hosts\n"
95 "127.0.0.1\tlocalhost\n"
97 self.assertFileMode(self.tmpname, 0644)
99 def testRemovingSingleExistingHost(self):
100 utils.RemoveEtcHostsEntry(self.tmpname, "localhost")
102 self.assertFileContent(self.tmpname,
103 "# This is a test file for /etc/hosts\n"
104 "192.0.2.1 router gw\n")
105 self.assertFileMode(self.tmpname, 0644)
107 def testRemovingNonExistingHost(self):
108 utils.RemoveEtcHostsEntry(self.tmpname, "myhost")
110 self.assertFileContent(self.tmpname,
111 "# This is a test file for /etc/hosts\n"
112 "127.0.0.1\tlocalhost\n"
113 "192.0.2.1 router gw\n")
114 self.assertFileMode(self.tmpname, 0644)
116 def testRemovingAlias(self):
117 utils.RemoveEtcHostsEntry(self.tmpname, "gw")
119 self.assertFileContent(self.tmpname,
120 "# This is a test file for /etc/hosts\n"
121 "127.0.0.1\tlocalhost\n"
122 "192.0.2.1 router\n")
123 self.assertFileMode(self.tmpname, 0644)
126 if __name__ == "__main__":
127 testutils.GanetiTestProgram()