Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.utils.nodesetup_unittest.py @ c1912a48

History | View | Annotate | Download (4 kB)

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
    os.chmod(self.tmpname, 0644)
48

    
49
  def testSettingNewIp(self):
50
    utils.SetEtcHostsEntry(self.tmpname, "198.51.100.4", "myhost.example.com",
51
                           ["myhost"])
52

    
53
    self.assertFileContent(self.tmpname,
54
      "# This is a test file for /etc/hosts\n"
55
      "127.0.0.1\tlocalhost\n"
56
      "192.0.2.1 router gw\n"
57
      "198.51.100.4\tmyhost.example.com myhost\n")
58
    self.assertFileMode(self.tmpname, 0644)
59

    
60
  def testSettingExistingIp(self):
61
    utils.SetEtcHostsEntry(self.tmpname, "192.0.2.1", "myhost.example.com",
62
                           ["myhost"])
63

    
64
    self.assertFileContent(self.tmpname,
65
      "# This is a test file for /etc/hosts\n"
66
      "127.0.0.1\tlocalhost\n"
67
      "192.0.2.1\tmyhost.example.com myhost\n")
68
    self.assertFileMode(self.tmpname, 0644)
69

    
70
  def testSettingDuplicateName(self):
71
    utils.SetEtcHostsEntry(self.tmpname, "198.51.100.4", "myhost", ["myhost"])
72

    
73
    self.assertFileContent(self.tmpname,
74
      "# This is a test file for /etc/hosts\n"
75
      "127.0.0.1\tlocalhost\n"
76
      "192.0.2.1 router gw\n"
77
      "198.51.100.4\tmyhost\n")
78
    self.assertFileMode(self.tmpname, 0644)
79

    
80
  def testSettingOrdering(self):
81
    utils.SetEtcHostsEntry(self.tmpname, "127.0.0.1", "localhost.localdomain",
82
                           ["localhost"])
83

    
84
    self.assertFileContent(self.tmpname,
85
      "# This is a test file for /etc/hosts\n"
86
      "127.0.0.1\tlocalhost.localdomain localhost\n"
87
      "192.0.2.1 router gw\n")
88
    self.assertFileMode(self.tmpname, 0644)
89

    
90
  def testRemovingExistingHost(self):
91
    utils.RemoveEtcHostsEntry(self.tmpname, "router")
92

    
93
    self.assertFileContent(self.tmpname,
94
      "# This is a test file for /etc/hosts\n"
95
      "127.0.0.1\tlocalhost\n"
96
      "192.0.2.1 gw\n")
97
    self.assertFileMode(self.tmpname, 0644)
98

    
99
  def testRemovingSingleExistingHost(self):
100
    utils.RemoveEtcHostsEntry(self.tmpname, "localhost")
101

    
102
    self.assertFileContent(self.tmpname,
103
      "# This is a test file for /etc/hosts\n"
104
      "192.0.2.1 router gw\n")
105
    self.assertFileMode(self.tmpname, 0644)
106

    
107
  def testRemovingNonExistingHost(self):
108
    utils.RemoveEtcHostsEntry(self.tmpname, "myhost")
109

    
110
    self.assertFileContent(self.tmpname,
111
      "# This is a test file for /etc/hosts\n"
112
      "127.0.0.1\tlocalhost\n"
113
      "192.0.2.1 router gw\n")
114
    self.assertFileMode(self.tmpname, 0644)
115

    
116
  def testRemovingAlias(self):
117
    utils.RemoveEtcHostsEntry(self.tmpname, "gw")
118

    
119
    self.assertFileContent(self.tmpname,
120
      "# This is a test file for /etc/hosts\n"
121
      "127.0.0.1\tlocalhost\n"
122
      "192.0.2.1 router\n")
123
    self.assertFileMode(self.tmpname, 0644)
124

    
125

    
126
if __name__ == "__main__":
127
  testutils.GanetiTestProgram()