forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
344 lines (294 loc) · 11.9 KB
/
test_client.py
File metadata and controls
344 lines (294 loc) · 11.9 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# Copyright 2015 Google LLC
#
# 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 unittest
import mock
def _make_credentials():
import google.auth.credentials
return mock.Mock(spec=google.auth.credentials.Credentials)
class TestClient(unittest.TestCase):
PROJECT = "PROJECT"
ZONE_NAME = "zone-name"
@staticmethod
def _get_target_class():
from google.cloud.dns.client import Client
return Client
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)
def test_ctor_defaults(self):
from google.api_core.client_info import ClientInfo
from google.cloud.dns._http import Connection
creds = _make_credentials()
http = object()
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http)
self.assertIsInstance(client._connection, Connection)
self.assertIs(client._connection.credentials, creds)
self.assertIs(client._connection.http, http)
self.assertIsInstance(client._connection._client_info, ClientInfo)
self.assertEqual(
client._connection.API_BASE_URL, client._connection.DEFAULT_API_ENDPOINT
)
def test_ctor_w_client_info(self):
from google.api_core.client_info import ClientInfo
from google.cloud.dns._http import Connection
client_info = ClientInfo()
creds = _make_credentials()
http = object()
client = self._make_one(
project=self.PROJECT, credentials=creds, _http=http, client_info=client_info
)
self.assertIsInstance(client._connection, Connection)
self.assertIs(client._connection.credentials, creds)
self.assertIs(client._connection.http, http)
self.assertIs(client._connection._client_info, client_info)
def test_ctor_w_empty_client_options_object(self):
from google.api_core.client_info import ClientInfo
from google.api_core.client_options import ClientOptions
from google.cloud.dns._http import Connection
creds = _make_credentials()
http = object()
client = self._make_one(
project=self.PROJECT,
credentials=creds,
_http=http,
client_options=ClientOptions(),
)
self.assertIsInstance(client._connection, Connection)
self.assertIs(client._connection.credentials, creds)
self.assertIs(client._connection.http, http)
self.assertIsInstance(client._connection._client_info, ClientInfo)
self.assertEqual(
client._connection.API_BASE_URL, client._connection.DEFAULT_API_ENDPOINT
)
def test_ctor_w_client_options_object(self):
from google.api_core.client_options import ClientOptions
api_endpoint = "https://foo-dns.googleapis.com"
creds = _make_credentials()
http = object()
client_options = ClientOptions(api_endpoint=api_endpoint)
client = self._make_one(
project=self.PROJECT,
credentials=creds,
_http=http,
client_options=client_options,
)
self.assertEqual(client._connection.API_BASE_URL, api_endpoint)
def test_ctor_w_client_options_dict(self):
api_endpoint = "https://foo-dns.googleapis.com"
creds = _make_credentials()
http = object()
client_options = {"api_endpoint": api_endpoint}
client = self._make_one(
project=self.PROJECT,
credentials=creds,
_http=http,
client_options=client_options,
)
self.assertEqual(client._connection.API_BASE_URL, api_endpoint)
def test_quotas_defaults(self):
PATH = "projects/%s" % (self.PROJECT,)
MANAGED_ZONES = 1234
RRS_PER_RRSET = 23
RRSETS_PER_ZONE = 345
RRSET_ADDITIONS = 456
RRSET_DELETIONS = 567
TOTAL_SIZE = 67890
DATA = {
"quota": {
"managedZones": MANAGED_ZONES,
"resourceRecordsPerRrset": RRS_PER_RRSET,
"rrsetsPerManagedZone": RRSETS_PER_ZONE,
"rrsetAdditionsPerChange": RRSET_ADDITIONS,
"rrsetDeletionsPerChange": RRSET_DELETIONS,
"totalRrdataSizePerChange": TOTAL_SIZE,
}
}
CONVERTED = {key: int(value) for key, value in DATA["quota"].items()}
creds = _make_credentials()
client = self._make_one(self.PROJECT, creds)
conn = client._connection = _Connection(DATA)
quotas = client.quotas()
self.assertEqual(quotas, CONVERTED)
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req["method"], "GET")
self.assertEqual(req["path"], "/%s" % PATH)
def test_quotas_w_kind_key(self):
PATH = "projects/%s" % (self.PROJECT,)
MANAGED_ZONES = 1234
RRS_PER_RRSET = 23
RRSETS_PER_ZONE = 345
RRSET_ADDITIONS = 456
RRSET_DELETIONS = 567
TOTAL_SIZE = 67890
DATA = {
"quota": {
"managedZones": MANAGED_ZONES,
"resourceRecordsPerRrset": RRS_PER_RRSET,
"rrsetsPerManagedZone": RRSETS_PER_ZONE,
"rrsetAdditionsPerChange": RRSET_ADDITIONS,
"rrsetDeletionsPerChange": RRSET_DELETIONS,
"totalRrdataSizePerChange": TOTAL_SIZE,
"whitelistedKeySpecs": [
{
"keyType": "keySigning",
"algorithm": "rsasha512",
"keyLength": 2048,
}
],
}
}
CONVERTED = DATA["quota"]
WITH_KIND = {"quota": DATA["quota"].copy()}
WITH_KIND["quota"]["kind"] = "dns#quota"
WITH_KIND["quota"]["whitelistedKeySpecs"][0]["kind"] = "dns#dnsKeySpec"
creds = _make_credentials()
client = self._make_one(self.PROJECT, creds)
conn = client._connection = _Connection(WITH_KIND)
quotas = client.quotas()
self.assertEqual(quotas, CONVERTED)
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req["method"], "GET")
self.assertEqual(req["path"], "/%s" % PATH)
def test_list_zones_defaults(self):
from google.cloud.dns.zone import ManagedZone
ID_1 = "123"
ZONE_1 = "zone_one"
DNS_1 = "one.example.com"
ID_2 = "234"
ZONE_2 = "zone_two"
DNS_2 = "two.example.com"
PATH = "projects/%s/managedZones" % (self.PROJECT,)
TOKEN = "TOKEN"
DATA = {
"nextPageToken": TOKEN,
"managedZones": [
{
"kind": "dns#managedZone",
"id": ID_1,
"name": ZONE_1,
"dnsName": DNS_1,
},
{
"kind": "dns#managedZone",
"id": ID_2,
"name": ZONE_2,
"dnsName": DNS_2,
},
],
}
creds = _make_credentials()
client = self._make_one(self.PROJECT, creds)
conn = client._connection = _Connection(DATA)
iterator = client.list_zones()
page = next(iterator.pages)
zones = list(page)
token = iterator.next_page_token
self.assertEqual(len(zones), len(DATA["managedZones"]))
for found, expected in zip(zones, DATA["managedZones"]):
self.assertIsInstance(found, ManagedZone)
self.assertEqual(found.zone_id, expected["id"])
self.assertEqual(found.name, expected["name"])
self.assertEqual(found.dns_name, expected["dnsName"])
self.assertEqual(token, TOKEN)
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req["method"], "GET")
self.assertEqual(req["path"], "/%s" % PATH)
def test_list_zones_explicit(self):
from google.cloud.dns.zone import ManagedZone
ID_1 = "123"
ZONE_1 = "zone_one"
DNS_1 = "one.example.com"
ID_2 = "234"
ZONE_2 = "zone_two"
DNS_2 = "two.example.com"
PATH = "projects/%s/managedZones" % (self.PROJECT,)
TOKEN = "TOKEN"
DATA = {
"managedZones": [
{
"kind": "dns#managedZone",
"id": ID_1,
"name": ZONE_1,
"dnsName": DNS_1,
},
{
"kind": "dns#managedZone",
"id": ID_2,
"name": ZONE_2,
"dnsName": DNS_2,
},
]
}
creds = _make_credentials()
client = self._make_one(self.PROJECT, creds)
conn = client._connection = _Connection(DATA)
iterator = client.list_zones(max_results=3, page_token=TOKEN)
page = next(iterator.pages)
zones = list(page)
token = iterator.next_page_token
self.assertEqual(len(zones), len(DATA["managedZones"]))
for found, expected in zip(zones, DATA["managedZones"]):
self.assertIsInstance(found, ManagedZone)
self.assertEqual(found.zone_id, expected["id"])
self.assertEqual(found.name, expected["name"])
self.assertEqual(found.dns_name, expected["dnsName"])
self.assertIsNone(token)
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req["method"], "GET")
self.assertEqual(req["path"], "/%s" % PATH)
self.assertEqual(req["query_params"], {"maxResults": 3, "pageToken": TOKEN})
def test_zone_explicit(self):
from google.cloud.dns.zone import ManagedZone
DESCRIPTION = "DESCRIPTION"
DNS_NAME = "test.example.com"
creds = _make_credentials()
client = self._make_one(self.PROJECT, creds)
zone = client.zone(self.ZONE_NAME, DNS_NAME, DESCRIPTION)
self.assertIsInstance(zone, ManagedZone)
self.assertEqual(zone.name, self.ZONE_NAME)
self.assertEqual(zone.dns_name, DNS_NAME)
self.assertEqual(zone.description, DESCRIPTION)
self.assertIs(zone._client, client)
def test_zone_w_dns_name_wo_description(self):
from google.cloud.dns.zone import ManagedZone
DNS_NAME = "test.example.com"
creds = _make_credentials()
client = self._make_one(self.PROJECT, creds)
zone = client.zone(self.ZONE_NAME, DNS_NAME)
self.assertIsInstance(zone, ManagedZone)
self.assertEqual(zone.name, self.ZONE_NAME)
self.assertEqual(zone.dns_name, DNS_NAME)
self.assertEqual(zone.description, DNS_NAME)
self.assertIs(zone._client, client)
def test_zone_wo_dns_name(self):
from google.cloud.dns.zone import ManagedZone
creds = _make_credentials()
client = self._make_one(self.PROJECT, creds)
zone = client.zone(self.ZONE_NAME)
self.assertIsInstance(zone, ManagedZone)
self.assertEqual(zone.name, self.ZONE_NAME)
self.assertIsNone(zone.dns_name)
self.assertIsNone(zone.description)
self.assertIs(zone._client, client)
class _Connection(object):
def __init__(self, *responses):
self._responses = responses
self._requested = []
def api_request(self, **kw):
self._requested.append(kw)
response, self._responses = self._responses[0], self._responses[1:]
return response