QA: Add tests for instance start/stop via RAPI
[ganeti-local] / test / ganeti.utils.nodesetup_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2006, 2007, 2010, 2011 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 testSettingOrdering(self):
80     utils.SetEtcHostsEntry(self.tmpname, "127.0.0.1", "localhost.localdomain",
81                            ["localhost"])
82
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)
88
89   def testRemovingExistingHost(self):
90     utils.RemoveEtcHostsEntry(self.tmpname, "router")
91
92     self.assertFileContent(self.tmpname,
93       "# This is a test file for /etc/hosts\n"
94       "127.0.0.1\tlocalhost\n"
95       "192.0.2.1 gw\n")
96     self.assertFileMode(self.tmpname, 0644)
97
98   def testRemovingSingleExistingHost(self):
99     utils.RemoveEtcHostsEntry(self.tmpname, "localhost")
100
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)
105
106   def testRemovingNonExistingHost(self):
107     utils.RemoveEtcHostsEntry(self.tmpname, "myhost")
108
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)
114
115   def testRemovingAlias(self):
116     utils.RemoveEtcHostsEntry(self.tmpname, "gw")
117
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)
123
124
125 if __name__ == "__main__":
126   testutils.GanetiTestProgram()