root / test / ganeti.utils.nodesetup_unittest.py @ 9888b9e6
History | View | Annotate | Download (3.9 kB)
1 | 17b97ab3 | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | 17b97ab3 | Michael Hanselmann | #
|
3 | 17b97ab3 | Michael Hanselmann | |
4 | 8f9a87c5 | Iustin Pop | # Copyright (C) 2006, 2007, 2010, 2011 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 | 8f9a87c5 | Iustin Pop | def testSettingOrdering(self): |
80 | 8f9a87c5 | Iustin Pop | utils.SetEtcHostsEntry(self.tmpname, "127.0.0.1", "localhost.localdomain", |
81 | 8f9a87c5 | Iustin Pop | ["localhost"])
|
82 | 8f9a87c5 | Iustin Pop | |
83 | 8f9a87c5 | Iustin Pop | self.assertFileContent(self.tmpname, |
84 | 8f9a87c5 | Iustin Pop | "# This is a test file for /etc/hosts\n"
|
85 | 8f9a87c5 | Iustin Pop | "127.0.0.1\tlocalhost.localdomain localhost\n"
|
86 | 8f9a87c5 | Iustin Pop | "192.0.2.1 router gw\n")
|
87 | 8f9a87c5 | Iustin Pop | self.assertFileMode(self.tmpname, 0644) |
88 | 8f9a87c5 | Iustin Pop | |
89 | 17b97ab3 | Michael Hanselmann | def testRemovingExistingHost(self): |
90 | 17b97ab3 | Michael Hanselmann | utils.RemoveEtcHostsEntry(self.tmpname, "router") |
91 | 17b97ab3 | Michael Hanselmann | |
92 | 17b97ab3 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
93 | 17b97ab3 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
94 | 17b97ab3 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
95 | 17b97ab3 | Michael Hanselmann | "192.0.2.1 gw\n")
|
96 | 17b97ab3 | Michael Hanselmann | self.assertFileMode(self.tmpname, 0644) |
97 | 17b97ab3 | Michael Hanselmann | |
98 | 17b97ab3 | Michael Hanselmann | def testRemovingSingleExistingHost(self): |
99 | 17b97ab3 | Michael Hanselmann | utils.RemoveEtcHostsEntry(self.tmpname, "localhost") |
100 | 17b97ab3 | Michael Hanselmann | |
101 | 17b97ab3 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
102 | 17b97ab3 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
103 | 17b97ab3 | Michael Hanselmann | "192.0.2.1 router gw\n")
|
104 | 17b97ab3 | Michael Hanselmann | self.assertFileMode(self.tmpname, 0644) |
105 | 17b97ab3 | Michael Hanselmann | |
106 | 17b97ab3 | Michael Hanselmann | def testRemovingNonExistingHost(self): |
107 | 17b97ab3 | Michael Hanselmann | utils.RemoveEtcHostsEntry(self.tmpname, "myhost") |
108 | 17b97ab3 | Michael Hanselmann | |
109 | 17b97ab3 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
110 | 17b97ab3 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
111 | 17b97ab3 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
112 | 17b97ab3 | Michael Hanselmann | "192.0.2.1 router gw\n")
|
113 | 17b97ab3 | Michael Hanselmann | self.assertFileMode(self.tmpname, 0644) |
114 | 17b97ab3 | Michael Hanselmann | |
115 | 17b97ab3 | Michael Hanselmann | def testRemovingAlias(self): |
116 | 17b97ab3 | Michael Hanselmann | utils.RemoveEtcHostsEntry(self.tmpname, "gw") |
117 | 17b97ab3 | Michael Hanselmann | |
118 | 17b97ab3 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
119 | 17b97ab3 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
120 | 17b97ab3 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
121 | 17b97ab3 | Michael Hanselmann | "192.0.2.1 router\n")
|
122 | 17b97ab3 | Michael Hanselmann | self.assertFileMode(self.tmpname, 0644) |
123 | 17b97ab3 | Michael Hanselmann | |
124 | 17b97ab3 | Michael Hanselmann | |
125 | 17b97ab3 | Michael Hanselmann | if __name__ == "__main__": |
126 | 17b97ab3 | Michael Hanselmann | testutils.GanetiTestProgram() |