root / test / ganeti.utils.nodesetup_unittest.py @ 44fbd23b
History | View | Annotate | Download (3.6 kB)
1 | 17b97ab3 | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | 17b97ab3 | Michael Hanselmann | #
|
3 | 17b97ab3 | Michael Hanselmann | |
4 | 17b97ab3 | Michael Hanselmann | # Copyright (C) 2006, 2007, 2010 Google Inc.
|
5 | 17b97ab3 | Michael Hanselmann | #
|
6 | 17b97ab3 | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | 17b97ab3 | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | 17b97ab3 | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | 17b97ab3 | Michael Hanselmann | # (at your option) any later version.
|
10 | 17b97ab3 | Michael Hanselmann | #
|
11 | 17b97ab3 | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | 17b97ab3 | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 17b97ab3 | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 17b97ab3 | Michael Hanselmann | # General Public License for more details.
|
15 | 17b97ab3 | Michael Hanselmann | #
|
16 | 17b97ab3 | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | 17b97ab3 | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | 17b97ab3 | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 17b97ab3 | Michael Hanselmann | # 02110-1301, USA.
|
20 | 17b97ab3 | Michael Hanselmann | |
21 | 17b97ab3 | Michael Hanselmann | |
22 | 17b97ab3 | Michael Hanselmann | """Script for testing ganeti.utils.nodesetup"""
|
23 | 17b97ab3 | Michael Hanselmann | |
24 | 17b97ab3 | Michael Hanselmann | import os |
25 | 17b97ab3 | Michael Hanselmann | import tempfile |
26 | 17b97ab3 | Michael Hanselmann | import unittest |
27 | 17b97ab3 | Michael Hanselmann | |
28 | 17b97ab3 | Michael Hanselmann | from ganeti import constants |
29 | 17b97ab3 | Michael Hanselmann | from ganeti import utils |
30 | 17b97ab3 | Michael Hanselmann | |
31 | 17b97ab3 | Michael Hanselmann | import testutils |
32 | 17b97ab3 | Michael Hanselmann | |
33 | 17b97ab3 | Michael Hanselmann | |
34 | 17b97ab3 | Michael Hanselmann | class TestEtcHosts(testutils.GanetiTestCase): |
35 | 17b97ab3 | Michael Hanselmann | """Test functions modifying /etc/hosts"""
|
36 | 17b97ab3 | Michael Hanselmann | |
37 | 17b97ab3 | Michael Hanselmann | def setUp(self): |
38 | 17b97ab3 | Michael Hanselmann | testutils.GanetiTestCase.setUp(self)
|
39 | 17b97ab3 | Michael Hanselmann | self.tmpname = self._CreateTempFile() |
40 | 17b97ab3 | Michael Hanselmann | handle = open(self.tmpname, "w") |
41 | 17b97ab3 | Michael Hanselmann | try:
|
42 | 17b97ab3 | Michael Hanselmann | handle.write("# This is a test file for /etc/hosts\n")
|
43 | 17b97ab3 | Michael Hanselmann | handle.write("127.0.0.1\tlocalhost\n")
|
44 | 17b97ab3 | Michael Hanselmann | handle.write("192.0.2.1 router gw\n")
|
45 | 17b97ab3 | Michael Hanselmann | finally:
|
46 | 17b97ab3 | Michael Hanselmann | handle.close() |
47 | 17b97ab3 | Michael Hanselmann | |
48 | 17b97ab3 | Michael Hanselmann | def testSettingNewIp(self): |
49 | 17b97ab3 | Michael Hanselmann | utils.SetEtcHostsEntry(self.tmpname, "198.51.100.4", "myhost.example.com", |
50 | 17b97ab3 | Michael Hanselmann | ["myhost"])
|
51 | 17b97ab3 | Michael Hanselmann | |
52 | 17b97ab3 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
53 | 17b97ab3 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
54 | 17b97ab3 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
55 | 17b97ab3 | Michael Hanselmann | "192.0.2.1 router gw\n"
|
56 | 17b97ab3 | Michael Hanselmann | "198.51.100.4\tmyhost.example.com myhost\n")
|
57 | 17b97ab3 | Michael Hanselmann | self.assertFileMode(self.tmpname, 0644) |
58 | 17b97ab3 | Michael Hanselmann | |
59 | 17b97ab3 | Michael Hanselmann | def testSettingExistingIp(self): |
60 | 17b97ab3 | Michael Hanselmann | utils.SetEtcHostsEntry(self.tmpname, "192.0.2.1", "myhost.example.com", |
61 | 17b97ab3 | Michael Hanselmann | ["myhost"])
|
62 | 17b97ab3 | Michael Hanselmann | |
63 | 17b97ab3 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
64 | 17b97ab3 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
65 | 17b97ab3 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
66 | 17b97ab3 | Michael Hanselmann | "192.0.2.1\tmyhost.example.com myhost\n")
|
67 | 17b97ab3 | Michael Hanselmann | self.assertFileMode(self.tmpname, 0644) |
68 | 17b97ab3 | Michael Hanselmann | |
69 | 17b97ab3 | Michael Hanselmann | def testSettingDuplicateName(self): |
70 | 17b97ab3 | Michael Hanselmann | utils.SetEtcHostsEntry(self.tmpname, "198.51.100.4", "myhost", ["myhost"]) |
71 | 17b97ab3 | Michael Hanselmann | |
72 | 17b97ab3 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
73 | 17b97ab3 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
74 | 17b97ab3 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
75 | 17b97ab3 | Michael Hanselmann | "192.0.2.1 router gw\n"
|
76 | 17b97ab3 | Michael Hanselmann | "198.51.100.4\tmyhost\n")
|
77 | 17b97ab3 | Michael Hanselmann | self.assertFileMode(self.tmpname, 0644) |
78 | 17b97ab3 | Michael Hanselmann | |
79 | 17b97ab3 | Michael Hanselmann | def testRemovingExistingHost(self): |
80 | 17b97ab3 | Michael Hanselmann | utils.RemoveEtcHostsEntry(self.tmpname, "router") |
81 | 17b97ab3 | Michael Hanselmann | |
82 | 17b97ab3 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
83 | 17b97ab3 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
84 | 17b97ab3 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
85 | 17b97ab3 | Michael Hanselmann | "192.0.2.1 gw\n")
|
86 | 17b97ab3 | Michael Hanselmann | self.assertFileMode(self.tmpname, 0644) |
87 | 17b97ab3 | Michael Hanselmann | |
88 | 17b97ab3 | Michael Hanselmann | def testRemovingSingleExistingHost(self): |
89 | 17b97ab3 | Michael Hanselmann | utils.RemoveEtcHostsEntry(self.tmpname, "localhost") |
90 | 17b97ab3 | Michael Hanselmann | |
91 | 17b97ab3 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
92 | 17b97ab3 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
93 | 17b97ab3 | Michael Hanselmann | "192.0.2.1 router gw\n")
|
94 | 17b97ab3 | Michael Hanselmann | self.assertFileMode(self.tmpname, 0644) |
95 | 17b97ab3 | Michael Hanselmann | |
96 | 17b97ab3 | Michael Hanselmann | def testRemovingNonExistingHost(self): |
97 | 17b97ab3 | Michael Hanselmann | utils.RemoveEtcHostsEntry(self.tmpname, "myhost") |
98 | 17b97ab3 | Michael Hanselmann | |
99 | 17b97ab3 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
100 | 17b97ab3 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
101 | 17b97ab3 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
102 | 17b97ab3 | Michael Hanselmann | "192.0.2.1 router gw\n")
|
103 | 17b97ab3 | Michael Hanselmann | self.assertFileMode(self.tmpname, 0644) |
104 | 17b97ab3 | Michael Hanselmann | |
105 | 17b97ab3 | Michael Hanselmann | def testRemovingAlias(self): |
106 | 17b97ab3 | Michael Hanselmann | utils.RemoveEtcHostsEntry(self.tmpname, "gw") |
107 | 17b97ab3 | Michael Hanselmann | |
108 | 17b97ab3 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
109 | 17b97ab3 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
110 | 17b97ab3 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
111 | 17b97ab3 | Michael Hanselmann | "192.0.2.1 router\n")
|
112 | 17b97ab3 | Michael Hanselmann | self.assertFileMode(self.tmpname, 0644) |
113 | 17b97ab3 | Michael Hanselmann | |
114 | 17b97ab3 | Michael Hanselmann | |
115 | 17b97ab3 | Michael Hanselmann | if __name__ == "__main__": |
116 | 17b97ab3 | Michael Hanselmann | testutils.GanetiTestProgram() |