7
sys.modules['MySQLdb'] = mock.Mock()
11
class UtilsTests(unittest.TestCase):
13
super(UtilsTests, self).setUp()
15
@mock.patch("rabbit_utils.log")
16
def test_update_empty_hosts_file(self, mock_log):
17
map = {'1.2.3.4': 'my-host'}
18
with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
19
rabbit_utils.HOSTS_FILE = tmpfile.name
20
rabbit_utils.HOSTS_FILE = tmpfile.name
21
rabbit_utils.update_hosts_file(map)
23
with open(tmpfile.name, 'r') as fd:
24
lines = fd.readlines()
26
os.remove(tmpfile.name)
27
self.assertEqual(len(lines), 1)
28
self.assertEqual(lines[0], "%s %s\n" % (map.items()[0]))
30
@mock.patch("rabbit_utils.log")
31
def test_update_hosts_file_w_dup(self, mock_log):
32
map = {'1.2.3.4': 'my-host'}
33
with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
34
rabbit_utils.HOSTS_FILE = tmpfile.name
36
with open(tmpfile.name, 'w') as fd:
37
fd.write("%s %s\n" % (map.items()[0]))
39
rabbit_utils.update_hosts_file(map)
41
with open(tmpfile.name, 'r') as fd:
42
lines = fd.readlines()
44
os.remove(tmpfile.name)
45
self.assertEqual(len(lines), 1)
46
self.assertEqual(lines[0], "%s %s\n" % (map.items()[0]))
48
@mock.patch("rabbit_utils.log")
49
def test_update_hosts_file_entry(self, mock_log):
50
altmap = {'1.1.1.1': 'alt-host'}
51
map = {'1.1.1.1': 'hostA',
55
with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
56
rabbit_utils.HOSTS_FILE = tmpfile.name
58
with open(tmpfile.name, 'w') as fd:
59
fd.write("#somedata\n")
60
fd.write("%s %s\n" % (altmap.items()[0]))
62
rabbit_utils.update_hosts_file(map)
64
with open(rabbit_utils.HOSTS_FILE, 'r') as fd:
65
lines = fd.readlines()
67
os.remove(tmpfile.name)
68
self.assertEqual(len(lines), 5)
69
self.assertEqual(lines[0], "#somedata\n")
70
self.assertEqual(lines[1], "%s %s\n" % (map.items()[0]))
71
self.assertEqual(lines[4], "%s %s\n" % (map.items()[3]))