File: test_zone.py

package info (click to toggle)
python-shade 1.30.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,196 kB
  • sloc: python: 33,354; sh: 111; makefile: 15
file content (156 lines) | stat: -rw-r--r-- 5,188 bytes parent folder | download | duplicates (2)
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
# 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 copy
import testtools

import shade
from shade.tests.unit import base


zone_dict = {
    'name': 'example.net.',
    'type': 'PRIMARY',
    'email': 'test@example.net',
    'description': 'Example zone',
    'ttl': 3600,
}

new_zone_dict = copy.copy(zone_dict)
new_zone_dict['id'] = '1'


class TestZone(base.RequestsMockTestCase):

    def setUp(self):
        super(TestZone, self).setUp()
        self.use_designate()

    def test_create_zone(self):
        self.register_uris([
            dict(method='POST',
                 uri=self.get_mock_url(
                     'dns', 'public', append=['v2', 'zones']),
                 json=new_zone_dict,
                 validate=dict(
                     json=zone_dict))
        ])
        z = self.cloud.create_zone(
            name=zone_dict['name'],
            zone_type=zone_dict['type'],
            email=zone_dict['email'],
            description=zone_dict['description'],
            ttl=zone_dict['ttl'],
            masters=None)
        self.assertEqual(new_zone_dict, z)
        self.assert_calls()

    def test_create_zone_exception(self):
        self.register_uris([
            dict(method='POST',
                 uri=self.get_mock_url(
                     'dns', 'public', append=['v2', 'zones']),
                 status_code=500)
        ])
        with testtools.ExpectedException(
            shade.exc.OpenStackCloudHTTPError,
            "Unable to create zone example.net."
        ):
            self.cloud.create_zone('example.net.')
        self.assert_calls()

    def test_update_zone(self):
        new_ttl = 7200
        updated_zone = copy.copy(new_zone_dict)
        updated_zone['ttl'] = new_ttl
        self.register_uris([
            dict(method='GET',
                 uri=self.get_mock_url(
                     'dns', 'public', append=['v2', 'zones']),
                 json={
                     "zones": [new_zone_dict],
                     "links": {},
                     "metadata": {
                         'total_count': 1}}),
            dict(method='PATCH',
                 uri=self.get_mock_url(
                     'dns', 'public', append=['v2', 'zones', '1']),
                 json=updated_zone,
                 validate=dict(
                     json={"ttl": new_ttl}))
        ])
        z = self.cloud.update_zone('1', ttl=new_ttl)
        self.assertEqual(updated_zone, z)
        self.assert_calls()

    def test_delete_zone(self):
        self.register_uris([
            dict(method='GET',
                 uri=self.get_mock_url(
                     'dns', 'public', append=['v2', 'zones']),
                 json={
                     "zones": [new_zone_dict],
                     "links": {},
                     "metadata": {
                         'total_count': 1}}),
            dict(method='DELETE',
                 uri=self.get_mock_url(
                     'dns', 'public', append=['v2', 'zones', '1']),
                 json=new_zone_dict)
        ])
        self.assertTrue(self.cloud.delete_zone('1'))
        self.assert_calls()

    def test_get_zone_by_id(self):
        self.register_uris([
            dict(method='GET',
                 uri=self.get_mock_url(
                     'dns', 'public', append=['v2', 'zones']),
                 json={
                     "zones": [new_zone_dict],
                     "links": {},
                     "metadata": {
                         'total_count': 1}})
        ])
        zone = self.cloud.get_zone('1')
        self.assertEqual(zone['id'], '1')
        self.assert_calls()

    def test_get_zone_by_name(self):
        self.register_uris([
            dict(method='GET',
                 uri=self.get_mock_url(
                     'dns', 'public', append=['v2', 'zones']),
                 json={
                     "zones": [new_zone_dict],
                     "links": {},
                     "metadata": {
                         'total_count': 1}})
        ])
        zone = self.cloud.get_zone('example.net.')
        self.assertEqual(zone['name'], 'example.net.')
        self.assert_calls()

    def test_get_zone_not_found_returns_false(self):
        self.register_uris([
            dict(method='GET',
                 uri=self.get_mock_url(
                     'dns', 'public', append=['v2', 'zones']),
                 json={
                     "zones": [],
                     "links": {},
                     "metadata": {
                         'total_count': 1}})
        ])
        zone = self.cloud.get_zone('nonexistingzone.net.')
        self.assertFalse(zone)
        self.assert_calls()