forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_client.py
More file actions
300 lines (256 loc) · 9.94 KB
/
Copy pathtest_client.py
File metadata and controls
300 lines (256 loc) · 9.94 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
# Copyright 2015 Google Inc. All rights reserved.
#
# 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 unittest2
class TestClient(unittest2.TestCase):
def _getTargetClass(self):
from gcloud.dns.client import Client
return Client
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def test_ctor(self):
from gcloud.dns.connection import Connection
PROJECT = 'PROJECT'
creds = _Credentials()
http = object()
client = self._makeOne(project=PROJECT, credentials=creds, http=http)
self.assertTrue(isinstance(client.connection, Connection))
self.assertTrue(client.connection.credentials is creds)
self.assertTrue(client.connection.http is http)
def test_quotas_defaults(self):
PROJECT = 'PROJECT'
PATH = 'projects/%s' % PROJECT
MANAGED_ZONES = 1234
RRS_PER_RRSET = 23
RRSETS_PER_ZONE = 345
RRSET_ADDITIONS = 456
RRSET_DELETIONS = 567
TOTAL_SIZE = 67890
DATA = {
'quota': {
'managedZones': str(MANAGED_ZONES),
'resourceRecordsPerRrset': str(RRS_PER_RRSET),
'rrsetsPerManagedZone': str(RRSETS_PER_ZONE),
'rrsetAdditionsPerChange': str(RRSET_ADDITIONS),
'rrsetDeletionsPerChange': str(RRSET_DELETIONS),
'totalRrdataSizePerChange': str(TOTAL_SIZE),
}
}
CONVERTED = dict([(key, int(value))
for key, value in DATA['quota'].items()])
creds = _Credentials()
client = self._makeOne(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):
PROJECT = 'PROJECT'
PATH = 'projects/%s' % PROJECT
MANAGED_ZONES = 1234
RRS_PER_RRSET = 23
RRSETS_PER_ZONE = 345
RRSET_ADDITIONS = 456
RRSET_DELETIONS = 567
TOTAL_SIZE = 67890
DATA = {
'quota': {
'managedZones': str(MANAGED_ZONES),
'resourceRecordsPerRrset': str(RRS_PER_RRSET),
'rrsetsPerManagedZone': str(RRSETS_PER_ZONE),
'rrsetAdditionsPerChange': str(RRSET_ADDITIONS),
'rrsetDeletionsPerChange': str(RRSET_DELETIONS),
'totalRrdataSizePerChange': str(TOTAL_SIZE),
}
}
CONVERTED = dict([(key, int(value))
for key, value in DATA['quota'].items()])
WITH_KIND = {'quota': DATA['quota'].copy()}
WITH_KIND['quota']['kind'] = 'dns#quota'
creds = _Credentials()
client = self._makeOne(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 gcloud.dns.zone import ManagedZone
PROJECT = 'PROJECT'
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' % 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 = _Credentials()
client = self._makeOne(PROJECT, creds)
conn = client.connection = _Connection(DATA)
zones, token = client.list_zones()
self.assertEqual(len(zones), len(DATA['managedZones']))
for found, expected in zip(zones, DATA['managedZones']):
self.assertTrue(isinstance(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 gcloud.dns.zone import ManagedZone
PROJECT = 'PROJECT'
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' % 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 = _Credentials()
client = self._makeOne(PROJECT, creds)
conn = client.connection = _Connection(DATA)
zones, token = client.list_zones(max_results=3, page_token=TOKEN)
self.assertEqual(len(zones), len(DATA['managedZones']))
for found, expected in zip(zones, DATA['managedZones']):
self.assertTrue(isinstance(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, None)
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(self):
from gcloud.dns.zone import ManagedZone
PROJECT = 'PROJECT'
ZONE_NAME = 'zone-name'
DNS_NAME = 'test.example.com'
creds = _Credentials()
client = self._makeOne(PROJECT, creds)
zone = client.zone(ZONE_NAME, DNS_NAME)
self.assertTrue(isinstance(zone, ManagedZone))
self.assertEqual(zone.name, ZONE_NAME)
self.assertEqual(zone.dns_name, DNS_NAME)
self.assertTrue(zone._client is client)
def test_get_zone_miss(self):
from gcloud.exceptions import NotFound
PROJECT = 'PROJECT'
CREDENTIALS = _Credentials()
client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)
NONESUCH = 'nonesuch'
URI = '/'.join([
client.connection.API_BASE_URL,
'dns',
client.connection.API_VERSION,
'projects',
PROJECT,
'managedZones',
'nonesuch',
])
http = client.connection._http = _Http(
{'status': '404', 'content-type': 'application/json'},
b'{}',
)
self.assertRaises(NotFound, client.get_zone, NONESUCH)
self.assertEqual(http._called_with['method'], 'GET')
self.assertEqual(http._called_with['uri'], URI)
def test_get_zone_hit(self):
from gcloud.dns.zone import ManagedZone
PROJECT = 'PROJECT'
CREDENTIALS = _Credentials()
client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)
ZONE_NAME = 'zone-name'
ZONE_DNS_NAME = 'example.com.'
URI = '/'.join([
client.connection.API_BASE_URL,
'dns',
client.connection.API_VERSION,
'projects',
PROJECT,
'managedZones',
ZONE_NAME
])
http = client.connection._http = _Http(
{'status': '200', 'content-type': 'application/json'},
'{{"name": "{0}", "dnsName": "{1}"}}'.format(
ZONE_NAME, ZONE_DNS_NAME).encode('utf-8'),
)
zone = client.get_zone(ZONE_NAME)
self.assertTrue(isinstance(zone, ManagedZone))
self.assertEqual(zone.name, ZONE_NAME)
self.assertEqual(zone.dns_name, ZONE_DNS_NAME)
self.assertEqual(http._called_with['method'], 'GET')
self.assertEqual(http._called_with['uri'], URI)
class _Credentials(object):
_scopes = None
@staticmethod
def create_scoped_required():
return True
def create_scoped(self, scope):
self._scopes = scope
return self
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
class _Http(object):
_called_with = None
def __init__(self, headers, content):
from httplib2 import Response
self._response = Response(headers)
self._content = content
def request(self, **kw):
self._called_with = kw
return self._response, self._content