summaryrefslogtreecommitdiff
path: root/designate/tests/unit/backend/test_ns1.py
blob: 9b565bd98a4e534eb3596b7762ec84b30d506d8f (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
# Copyright 2021 NS1 Inc. https://www.ns1.com
#
# Author: Dragan Blagojevic <dblagojevic@daitan.com>
#
# 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 requests_mock

from designate.backend import impl_ns1
from designate import exceptions
from designate import objects
import designate.tests
from designate.tests import fixtures


class NS1BackendTestCase(designate.tests.TestCase):
    def setUp(self):
        super(NS1BackendTestCase, self).setUp()
        self.stdlog = fixtures.StandardLogging()
        self.useFixture(self.stdlog)

        self.api_address = 'https://192.0.2.3/v1/zones/example.com'
        self.context = self.get_context()
        self.zone = objects.Zone(
            id='e2bed4dc-9d01-11e4-89d3-123b93f75cba',
            name='example.com.',
            email='example@example.com',
        )

        self.target = {
            'id': '4588652b-50e7-46b9-b688-a9bad40a873e',
            'type': 'ns1',
            'masters': [
                {'host': '192.0.2.1', 'port': 53},
                {'host': '192.0.2.2', 'port': 35},
            ],
            'options': [
                {'key': 'api_endpoint', 'value': '192.0.2.3'},
                {'key': 'api_token', 'value': 'test_key'},
            ],
        }
        self.target_tsig = {
            'id': '4588652b-50e7-46b9-b688-a9bad40a873e',
            'type': 'ns1',
            'masters': [
                {'host': '192.0.2.1', 'port': 53},
                {'host': '192.0.2.2', 'port': 35},
            ],
            'options': [
                {'key': 'api_endpoint', 'value': '192.0.2.3'},
                {'key': 'api_token', 'value': 'test_key'},
                {'key': 'tsigkey_name', 'value': 'test_key'},
                {'key': 'tsigkey_hash', 'value': 'hmac-sha512'},
                {'key': 'tsigkey_value', 'value': 'aaaabbbbccc'},
            ],
        }
        self.put_request_json = {
            'zone': u'example.com',
            'secondary': {
                'enabled': True,
                'primary_ip': '192.0.2.1',
                'primary_port': 53
            }
        }
        self.put_request_tsig_json = {
            'zone': u'example.com',
            'secondary': {
                'enabled': True,
                'primary_ip': '192.0.2.1',
                'primary_port': 53,
                'tsig': {
                    'enabled': True,
                    'hash': 'hmac-sha512',
                    'name': 'test_key',
                    'key': 'aaaabbbbccc'
                }
            }
        }

        self.backend = impl_ns1.NS1Backend(
            objects.PoolTarget.from_dict(self.target)
        )
        self.backend_tsig = impl_ns1.NS1Backend(
            objects.PoolTarget.from_dict(self.target_tsig)
        )

    @requests_mock.mock()
    def test_create_zone_success(self, req_mock):
        req_mock.put(self.api_address)
        req_mock.get(
            self.api_address,
            status_code=404
        )

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

        self.assertEqual(
            req_mock.last_request.json(),
            self.put_request_json
        )

        self.assertEqual(
            req_mock.last_request.headers.get('X-NSONE-Key'), 'test_key'
        )

    @requests_mock.mock()
    def test_create_zone_with_tsig_success(self, req_mock):
        req_mock.put(self.api_address)
        req_mock.get(
            self.api_address,
            status_code=404
        )

        self.backend_tsig.create_zone(self.context, self.zone)

        self.assertEqual(
            req_mock.last_request.json(),
            self.put_request_tsig_json
        )

        self.assertEqual(
            req_mock.last_request.headers.get('X-NSONE-Key'), 'test_key'
        )

    @requests_mock.mock()
    def test_create_zone_already_exists(self, req_mock):

        req_mock.get(self.api_address, status_code=200)
        req_mock.put(self.api_address)

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

        self.assertIn(
            "Can't create zone example.com. because it already exists",
            self.stdlog.logger.output
        )

        self.assertEqual(
            req_mock.last_request.headers.get('X-NSONE-Key'), 'test_key'
        )

    @requests_mock.mock()
    def test_create_zone_fail(self, req_mock):
        req_mock.put(
            self.api_address,
            status_code=500,
        )
        req_mock.get(
            self.api_address,
            status_code=404,
        )

        self.assertRaisesRegex(
            exceptions.Backend,
            '500 Server Error: None for url: '
            '%s' % self.api_address,
            self.backend.create_zone, self.context, self.zone
        )

        self.assertEqual(
            req_mock.last_request.headers.get('X-NSONE-Key'), 'test_key'
        )

    @requests_mock.mock()
    def test_delete_zone_success(self, req_mock):
        req_mock.delete(self.api_address, status_code=200)
        req_mock.get(self.api_address, status_code=200)

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

        self.assertEqual(
            req_mock.last_request.headers.get('X-NSONE-Key'), 'test_key'
        )

    @requests_mock.mock()
    def test_delete_zone_missing(self, req_mock):
        req_mock.delete(self.api_address, status_code=200)
        req_mock.get(self.api_address, status_code=404)

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

        self.assertIn(
            "Trying to delete zone "
            "<Zone id:'e2bed4dc-9d01-11e4-89d3-123b93f75cba' type:'None' "
            "name:'example.com.' pool_id:'None' serial:'None' action:'None' "
            "status:'None'> "
            "but that zone is not "
            "present in the ns1 backend. Assuming success.",
            self.stdlog.logger.output
        )

        self.assertEqual(
            req_mock.last_request.headers.get('X-NSONE-Key'), 'test_key'
        )

    @requests_mock.mock()
    def test_delete_zone_fail(self, req_mock):
        req_mock.delete(self.api_address, status_code=500)
        req_mock.get(self.api_address, status_code=200)

        self.assertRaisesRegex(
            exceptions.Backend,
            '500 Server Error: None for url: '
            '%s' % self.api_address,
            self.backend.delete_zone, self.context, self.zone
        )
        self.assertEqual(
            req_mock.last_request.headers.get('X-NSONE-Key'), 'test_key'
        )