Rename OpGetTags and LUGetTags
[ganeti-local] / test / ganeti.utils.nodesetup_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2006, 2007, 2010 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 """Script for testing ganeti.utils.nodesetup"""
23
24 import os
25 import tempfile
26 import unittest
27
28 from ganeti import constants
29 from ganeti import utils
30
31 import testutils
32
33
34 class TestEtcHosts(testutils.GanetiTestCase):
35   """Test functions modifying /etc/hosts"""
36
37   def setUp(self):
38     testutils.GanetiTestCase.setUp(self)
39     self.tmpname = self._CreateTempFile()
40     handle = open(self.tmpname, "w")
41     try:
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")
45     finally:
46       handle.close()
47
48   def testSettingNewIp(self):
49     utils.SetEtcHostsEntry(self.tmpname, "198.51.100.4", "myhost.example.com",
50                            ["myhost"])
51
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)
58
59   def testSettingExistingIp(self):
60     utils.SetEtcHostsEntry(self.tmpname, "192.0.2.1", "myhost.example.com",
61                            ["myhost"])
62
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)
68
69   def testSettingDuplicateName(self):
70     utils.SetEtcHostsEntry(self.tmpname, "198.51.100.4", "myhost", ["myhost"])
71
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)
78
79   def testRemovingExistingHost(self):
80     utils.RemoveEtcHostsEntry(self.tmpname, "router")
81
82     self.assertFileContent(self.tmpname,
83       "# This is a test file for /etc/hosts\n"
84       "127.0.0.1\tlocalhost\n"
85       "192.0.2.1 gw\n")
86     self.assertFileMode(self.tmpname, 0644)
87
88   def testRemovingSingleExistingHost(self):
89     utils.RemoveEtcHostsEntry(self.tmpname, "localhost")
90
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)
95
96   def testRemovingNonExistingHost(self):
97     utils.RemoveEtcHostsEntry(self.tmpname, "myhost")
98
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)
104
105   def testRemovingAlias(self):
106     utils.RemoveEtcHostsEntry(self.tmpname, "gw")
107
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)
113
114
115 if __name__ == "__main__":
116   testutils.GanetiTestProgram()