summaryrefslogtreecommitdiff
path: root/designateclient/functionaltests/v2/fixtures.py
blob: 75f4406ee1fad29e558b5d5a7aec26e55e3815c5 (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
"""
Copyright 2015 Rackspace

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.
"""
from __future__ import absolute_import
from __future__ import print_function
import sys
import tempfile
import traceback

import fixtures
from tempest_lib.exceptions import CommandFailed
from testtools.runtest import MultipleExceptions

from designateclient.functionaltests.client import DesignateCLI


class BaseFixture(fixtures.Fixture):

    def __init__(self, user='default', *args, **kwargs):
        """args/kwargs are forwarded to a create method on DesignateCLI"""
        super(BaseFixture, self).__init__()
        self.args = args
        self.kwargs = kwargs
        self.client = DesignateCLI.as_user(user)

    def setUp(self):
        # Sometimes, exceptions are raised in _setUp methods on fixtures.
        # testtools pushes the exception into a MultipleExceptions object along
        # with an artificial SetupError, which produces bad error messages.
        # This just logs those stack traces to stderr for easier debugging.
        try:
            super(BaseFixture, self).setUp()
        except MultipleExceptions as e:
            for i, exc_info in enumerate(e.args):
                print('--- printing MultipleExceptions traceback {} of {} ---'
                      .format(i + 1, len(e.args)), file=sys.stderr)
                traceback.print_exception(*exc_info)
            raise


class ZoneFixture(BaseFixture):
    """See DesignateCLI.zone_create for __init__ args"""

    def _setUp(self):
        super(ZoneFixture, self)._setUp()
        self.zone = self.client.zone_create(*self.args, **self.kwargs)
        self.addCleanup(self.cleanup_zone, self.client, self.zone.id)

    @classmethod
    def cleanup_zone(cls, client, zone_id):
        try:
            client.zone_delete(zone_id)
        except CommandFailed:
            pass


class TransferRequestFixture(BaseFixture):
    """See DesignateCLI.zone_transfer_request_create for __init__ args"""

    def __init__(self, zone, user='default', target_user='alt', *args,
                 **kwargs):
        super(TransferRequestFixture, self).__init__(user, *args, **kwargs)
        self.zone = zone
        self.target_client = DesignateCLI.as_user(target_user)

        # the client has a bug such that it requires --target-project-id.
        # when this bug is fixed, please remove this
        self.kwargs['target_project_id'] = self.target_client.project_id

    def _setUp(self):
        super(TransferRequestFixture, self)._setUp()
        self.transfer_request = self.client.zone_transfer_request_create(
            zone_id=self.zone.id,
            *self.args, **self.kwargs
        )
        self.addCleanup(self.cleanup_transfer_request, self.client,
                        self.transfer_request.id)
        self.addCleanup(ZoneFixture.cleanup_zone, self.client, self.zone.id)
        self.addCleanup(ZoneFixture.cleanup_zone, self.target_client,
                        self.zone.id)

    @classmethod
    def cleanup_transfer_request(cls, client, transfer_request_id):
        try:
            client.zone_transfer_request_delete(transfer_request_id)
        except CommandFailed:
            pass


class ExportFixture(BaseFixture):
    """See DesignateCLI.zone_export_create for __init__ args"""

    def __init__(self, zone, user='default', *args, **kwargs):
        super(ExportFixture, self).__init__(user, *args, **kwargs)
        self.zone = zone

    def _setUp(self):
        super(ExportFixture, self)._setUp()
        self.zone_export = self.client.zone_export_create(
            zone_id=self.zone.id,
            *self.args, **self.kwargs
        )
        self.addCleanup(self.cleanup_zone_export, self.client,
                        self.zone_export.id)
        self.addCleanup(ZoneFixture.cleanup_zone, self.client, self.zone.id)

    @classmethod
    def cleanup_zone_export(cls, client, zone_export_id):
        try:
            client.zone_export_delete(zone_export_id)
        except CommandFailed:
            pass


class ImportFixture(BaseFixture):
    """See DesignateCLI.zone_import_create for __init__ args"""

    def __init__(self, zone_file_contents, user='default', *args, **kwargs):
        super(ImportFixture, self).__init__(user, *args, **kwargs)
        self.zone_file_contents = zone_file_contents

    def _setUp(self):
        super(ImportFixture, self)._setUp()

        with tempfile.NamedTemporaryFile() as f:
            f.write(self.zone_file_contents)
            f.flush()

            self.zone_import = self.client.zone_import_create(
                zone_file_path=f.name,
                *self.args, **self.kwargs
            )

        self.addCleanup(self.cleanup_zone_import, self.client,
                        self.zone_import.id)
        self.addCleanup(ZoneFixture.cleanup_zone, self.client,
                        self.zone_import.zone_id)

    @classmethod
    def cleanup_zone_import(cls, client, zone_import_id):
        try:
            client.zone_import_delete(zone_import_id)
        except CommandFailed:
            pass


class RecordsetFixture(BaseFixture):
    """See DesignateCLI.recordset_create for __init__ args"""

    def _setUp(self):
        super(RecordsetFixture, self)._setUp()
        self.recordset = self.client.recordset_create(
            *self.args, **self.kwargs)
        self.addCleanup(self.cleanup_recordset, self.client,
                        self.recordset.zone_id, self.recordset.id)

    @classmethod
    def cleanup_recordset(cls, client, zone_id, recordset_id):
        try:
            client.recordset_delete(zone_id, recordset_id)
        except CommandFailed:
            pass


class TLDFixture(BaseFixture):
    """See DesignateCLI.tld_create for __init__ args"""

    def __init__(self, user='admin', *args, **kwargs):
        super(TLDFixture, self).__init__(user=user, *args, **kwargs)

    def _setUp(self):
        super(TLDFixture, self)._setUp()
        self.tld = self.client.tld_create(*self.args, **self.kwargs)
        self.addCleanup(self.cleanup_tld, self.client, self.tld.id)

    @classmethod
    def cleanup_tld(cls, client, tld_id):
        try:
            client.tld_delete(tld_id)
        except CommandFailed:
            pass


class BlacklistFixture(BaseFixture):
    """See DesignateCLI.zone_blacklist_create for __init__ args"""

    def __init__(self, user='admin', *args, **kwargs):
        super(BlacklistFixture, self).__init__(user=user, *args, **kwargs)

    def _setUp(self):
        super(BlacklistFixture, self)._setUp()
        self.blacklist = self.client.zone_blacklist_create(*self.args,
                                                           **self.kwargs)
        self.addCleanup(self.cleanup_blacklist, self.client, self.blacklist.id)

    @classmethod
    def cleanup_blacklist(cls, client, blacklist_id):
        try:
            client.zone_blacklist_delete(blacklist_id)
        except CommandFailed:
            pass