~niedbalski/charms/trusty/rabbitmq-server/hosts-ipv4

« back to all changes in this revision

Viewing changes to unit_tests/test_rabbit_utils.py

  • Committer: james.page at ubuntu
  • Date: 2014-11-27 11:03:41 UTC
  • mfrom: (55.4.46 ipv6)
  • Revision ID: james.page@ubuntu.com-20141127110341-hlfvklffs56tyu8c
[dosaboy,r=james-page,t=dosaboy,james-page] Add IPv6 support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import mock
 
2
import os
 
3
import unittest
 
4
import tempfile
 
5
import sys
 
6
 
 
7
sys.modules['MySQLdb'] = mock.Mock()
 
8
import rabbit_utils
 
9
 
 
10
 
 
11
class UtilsTests(unittest.TestCase):
 
12
    def setUp(self):
 
13
        super(UtilsTests, self).setUp()
 
14
 
 
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)
 
22
 
 
23
        with open(tmpfile.name, 'r') as fd:
 
24
            lines = fd.readlines()
 
25
 
 
26
        os.remove(tmpfile.name)
 
27
        self.assertEqual(len(lines), 1)
 
28
        self.assertEqual(lines[0], "%s %s\n" % (map.items()[0]))
 
29
 
 
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
 
35
 
 
36
            with open(tmpfile.name, 'w') as fd:
 
37
                fd.write("%s %s\n" % (map.items()[0]))
 
38
 
 
39
            rabbit_utils.update_hosts_file(map)
 
40
 
 
41
        with open(tmpfile.name, 'r') as fd:
 
42
            lines = fd.readlines()
 
43
 
 
44
        os.remove(tmpfile.name)
 
45
        self.assertEqual(len(lines), 1)
 
46
        self.assertEqual(lines[0], "%s %s\n" % (map.items()[0]))
 
47
 
 
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',
 
52
               '2.2.2.2': 'hostB',
 
53
               '3.3.3.3': 'hostC',
 
54
               '4.4.4.4': 'hostD'}
 
55
        with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
 
56
            rabbit_utils.HOSTS_FILE = tmpfile.name
 
57
 
 
58
            with open(tmpfile.name, 'w') as fd:
 
59
                fd.write("#somedata\n")
 
60
                fd.write("%s %s\n" % (altmap.items()[0]))
 
61
 
 
62
            rabbit_utils.update_hosts_file(map)
 
63
 
 
64
        with open(rabbit_utils.HOSTS_FILE, 'r') as fd:
 
65
            lines = fd.readlines()
 
66
 
 
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]))