summaryrefslogtreecommitdiff
path: root/designate/tests/unit/backend/test_bind9.py
blob: 6934bec11de914f86d2886e9b04f2db83e381d4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Copyright 2015 FUJITSU LIMITED
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock

import designate.tests
from designate import exceptions
from designate import objects
from designate import utils
from designate.backend import impl_bind9
from designate.tests import fixtures


class Bind9BackendTestCase(designate.tests.TestCase):
    def setUp(self):
        super(Bind9BackendTestCase, self).setUp()
        self.zone = objects.Zone(
            id='cca7908b-dad4-4c50-adba-fb67d4c556e8',
            name='example.com.',
            email='example@example.com'
        )

        self.target = {
            'id': '4588652b-50e7-46b9-b688-a9bad40a873e',
            'type': 'bind9',
            'masters': [
                {'host': '192.168.1.1', 'port': 53},
                {'host': '192.168.1.2', 'port': 35}
            ],
            'options': [
                {'key': 'host', 'value': '192.168.2.3'},
                {'key': 'port', 'value': '53'},
                {'key': 'rndc_host', 'value': '192.168.2.4'},
                {'key': 'rndc_port', 'value': '953'},
                {'key': 'rndc_bin_path', 'value': '/usr/sbin/rndc'},
                {'key': 'rndc_config_file', 'value': '/etc/rndc.conf'},
                {'key': 'rndc_key_file', 'value': '/etc/rndc.key'},
                {'key': 'clean_zonefile', 'value': 'true'}
            ],
        }

        self.backend = impl_bind9.Bind9Backend(
            objects.PoolTarget.from_dict(self.target)
        )

    @mock.patch.object(impl_bind9.Bind9Backend, '_execute_rndc')
    def test_create_zone(self, mock_execute):
        with fixtures.random_seed(0):
            self.backend.create_zone(self.admin_context, self.zone)

        mock_execute.assert_called_with(
            [
                'addzone',
                'example.com  { type slave; masters { 192.168.1.1 port 53; 192.168.1.2 port 35;}; file "slave.example.com.cca7908b-dad4-4c50-adba-fb67d4c556e8"; };'  # noqa
            ]
        )

    @mock.patch.object(impl_bind9.Bind9Backend, '_execute_rndc')
    def test_create_zone_with_view(self, mock_execute):
        self.target['options'].append(
            {'key': 'view', 'value': 'guest'},
        )

        backend = impl_bind9.Bind9Backend(
            objects.PoolTarget.from_dict(self.target)
        )

        with fixtures.random_seed(1):
            backend.create_zone(self.admin_context, self.zone)

        mock_execute.assert_called_with(
            [
                'addzone',
                'example.com in guest { type slave; masters { 192.168.1.2 port 35; 192.168.1.1 port 53;}; file "slave.example.com.cca7908b-dad4-4c50-adba-fb67d4c556e8"; };'  # noqa
            ]
        )

    @mock.patch.object(impl_bind9.Bind9Backend, '_execute_rndc')
    def test_create_zone_raises_on_exception(self, mock_execute):
        mock_execute.side_effect = exceptions.Backend('badop')
        self.assertRaises(
            exceptions.Backend,
            self.backend.create_zone, self.admin_context, self.zone
        )

    @mock.patch.object(impl_bind9.Bind9Backend, '_execute_rndc')
    def test_create_zone_already_exists(self, mock_execute):
        mock_execute.side_effect = exceptions.Backend('already exists')

        self.backend.create_zone(self.admin_context, self.zone)

    @mock.patch.object(impl_bind9.Bind9Backend, '_execute_rndc')
    def test_delete_zone(self, mock_execute):
        self.backend.delete_zone(self.admin_context, self.zone)

        mock_execute.assert_called_with(
            ['delzone', '-clean', 'example.com ']
        )

    @mock.patch.object(impl_bind9.Bind9Backend, '_execute_rndc')
    def test_delete_zone_with_view(self, mock_execute):
        self.target['options'].append(
            {'key': 'view', 'value': 'guest'},
        )

        backend = impl_bind9.Bind9Backend(
            objects.PoolTarget.from_dict(self.target)
        )

        backend.delete_zone(self.admin_context, self.zone)

        mock_execute.assert_called_with(
            ['delzone', '-clean', 'example.com in guest']
        )

    @mock.patch.object(impl_bind9.Bind9Backend, '_execute_rndc')
    def test_delete_zone_without_clean_zonefile(self, mock_execute):
        self.target['options'] = [
            {'key': 'clean_zonefile', 'value': 'false'}
        ]

        backend = impl_bind9.Bind9Backend(
            objects.PoolTarget.from_dict(self.target)
        )

        backend.delete_zone(self.admin_context, self.zone)

        mock_execute.assert_called_with(
            ['delzone', 'example.com ']
        )

    @mock.patch.object(impl_bind9.Bind9Backend, '_execute_rndc')
    def test_delete_zone_raises_on_exception(self, mock_execute):
        mock_execute.side_effect = exceptions.Backend('badop')
        self.assertRaises(
            exceptions.Backend,
            self.backend.delete_zone, self.admin_context, self.zone
        )

    @mock.patch.object(impl_bind9.Bind9Backend, '_execute_rndc')
    def test_delete_zone_already_deleted(self, mock_execute):
        mock_execute.side_effect = exceptions.Backend('not found')

        self.backend.delete_zone(self.admin_context, self.zone)

    def test_generate_rndc_base_call(self):
        self.assertEqual(
            [
                '/usr/sbin/rndc', '-s', '192.168.2.4', '-p', '953',
                '-c', '/etc/rndc.conf', '-k', '/etc/rndc.key'
            ],
            self.backend._generate_rndc_base_call()
        )

    def test_generate_rndc_base_call_without_config_file(self):
        self.target['options'] = [
            {'key': 'rndc_host', 'value': '192.168.4.4'},
            {'key': 'rndc_port', 'value': '953'},
            {'key': 'rndc_bin_path', 'value': '/usr/sbin/rndc'},
            {'key': 'rndc_key_file', 'value': '/etc/rndc.key'},
        ]

        backend = impl_bind9.Bind9Backend(
            objects.PoolTarget.from_dict(self.target)
        )

        self.assertEqual(
            [
                '/usr/sbin/rndc', '-s', '192.168.4.4', '-p', '953',
                '-k', '/etc/rndc.key'
            ],
            backend._generate_rndc_base_call()
        )

    def test_generate_rndc_base_call_without_key_file(self):
        self.target['options'] = [
            {'key': 'rndc_host', 'value': '192.168.3.4'},
            {'key': 'rndc_port', 'value': '953'},
            {'key': 'rndc_bin_path', 'value': '/usr/sbin/rndc'},
            {'key': 'rndc_config_file', 'value': '/etc/rndc.conf'},
        ]

        backend = impl_bind9.Bind9Backend(
            objects.PoolTarget.from_dict(self.target)
        )

        self.assertEqual(
            [
                '/usr/sbin/rndc', '-s', '192.168.3.4', '-p', '953',
                '-c', '/etc/rndc.conf'
            ],
            backend._generate_rndc_base_call()
        )

    @mock.patch('designate.utils.execute')
    def test_execute_rndc(self, mock_execute):
        rndc_op = ['delzone', 'example.com ']

        self.backend._execute_rndc(rndc_op)

        mock_execute.assert_called_with(
            '/usr/sbin/rndc', '-s', '192.168.2.4', '-p', '953',
            '-c', '/etc/rndc.conf', '-k', '/etc/rndc.key',
            'delzone', 'example.com '
        )

    @mock.patch('designate.utils.execute')
    def test_execute_rndc_raises_on_exception(self, mock_execute):
        mock_execute.side_effect = utils.processutils.ProcessExecutionError()
        rndc_op = ['badop', 'example.com ']

        self.assertRaises(
            exceptions.Backend,
            self.backend._execute_rndc, rndc_op
        )