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")
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 testSettingOrdering(self):
80 utils.SetEtcHostsEntry(self.tmpname, "127.0.0.1", "localhost.localdomain",
83 self.assertFileContent(self.tmpname,
84 "# This is a test file for /etc/hosts\n"
85 "127.0.0.1\tlocalhost.localdomain localhost\n"
86 "192.0.2.1 router gw\n")
87 self.assertFileMode(self.tmpname, 0644)
89 def testRemovingExistingHost(self):
90 utils.RemoveEtcHostsEntry(self.tmpname, "router")
92 self.assertFileContent(self.tmpname,
93 "# This is a test file for /etc/hosts\n"
94 "127.0.0.1\tlocalhost\n"
96 self.assertFileMode(self.tmpname, 0644)
98 def testRemovingSingleExistingHost(self):
99 utils.RemoveEtcHostsEntry(self.tmpname, "localhost")
101 self.assertFileContent(self.tmpname,
102 "# This is a test file for /etc/hosts\n"
103 "192.0.2.1 router gw\n")
104 self.assertFileMode(self.tmpname, 0644)
106 def testRemovingNonExistingHost(self):
107 utils.RemoveEtcHostsEntry(self.tmpname, "myhost")
109 self.assertFileContent(self.tmpname,
110 "# This is a test file for /etc/hosts\n"
111 "127.0.0.1\tlocalhost\n"
112 "192.0.2.1 router gw\n")
113 self.assertFileMode(self.tmpname, 0644)
115 def testRemovingAlias(self):
116 utils.RemoveEtcHostsEntry(self.tmpname, "gw")
118 self.assertFileContent(self.tmpname,
119 "# This is a test file for /etc/hosts\n"
120 "127.0.0.1\tlocalhost\n"
121 "192.0.2.1 router\n")
122 self.assertFileMode(self.tmpname, 0644)
125 if __name__ == "__main__":
126 testutils.GanetiTestProgram()