root / kamaki / clients / astakos / test.py @ 8cec3671
History | View | Annotate | Download (6.1 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from mock import patch, call |
35 |
from unittest import TestCase |
36 |
|
37 |
from kamaki.clients import astakos, ClientError |
38 |
|
39 |
|
40 |
example = dict(
|
41 |
serviceCatalog=[ |
42 |
dict(name='service name 1', type='compute', endpoints=[ |
43 |
dict(version_id='v1', publicUrl='http://1.1.1.1/v1'), |
44 |
dict(version_id='v2', publicUrl='http://1.1.1.1/v2')]), |
45 |
dict(name='service name 2', type='image', endpoints=[ |
46 |
dict(version_id='v2', publicUrl='http://1.1.1.1/v2'), |
47 |
dict(version_id='v2.1', publicUrl='http://1.1.1.1/v2/xtra')]) |
48 |
], |
49 |
user=dict(
|
50 |
name='Simple Name',
|
51 |
username='User Full Name',
|
52 |
auth_token_expires='1362583796000',
|
53 |
auth_token_created='1359991796000',
|
54 |
email=['user@example.gr'],
|
55 |
id=42,
|
56 |
uuid='aus3r-uu1d-f0r-73s71ng-as7ak0s')
|
57 |
) |
58 |
|
59 |
example0 = dict(
|
60 |
serviceCatalog=[ |
61 |
dict(name='service name 1', type='compute', endpoints=[ |
62 |
dict(version_id='v1', publicUrl='http://1.1.1.1/v1'), |
63 |
dict(version_id='v2', publicUrl='http://1.1.1.1/v2')]), |
64 |
dict(name='service name 3', type='object-storage', endpoints=[ |
65 |
dict(version_id='v2', publicUrl='http://1.1.1.1/v2'), |
66 |
dict(version_id='v2.1', publicUrl='http://1.1.1.1/v2/xtra')]) |
67 |
], |
68 |
user=dict(
|
69 |
name='Simple Name 0',
|
70 |
username='User Full Name 0',
|
71 |
auth_token_expires='1362585796000',
|
72 |
auth_token_created='1359931796000',
|
73 |
email=['user0@example.gr'],
|
74 |
id=42,
|
75 |
uuid='aus3r-uu1d-507-73s71ng-as7ak0s')
|
76 |
) |
77 |
|
78 |
|
79 |
class FR(object): |
80 |
json = example |
81 |
headers = {} |
82 |
content = json |
83 |
status = None
|
84 |
status_code = 200
|
85 |
|
86 |
astakos_pkg = 'kamaki.clients.astakos.AstakosClient'
|
87 |
|
88 |
|
89 |
class AstakosClient(TestCase): |
90 |
|
91 |
cached = False
|
92 |
|
93 |
def assert_dicts_are_equal(self, d1, d2): |
94 |
for k, v in d1.items(): |
95 |
self.assertTrue(k in d2) |
96 |
if isinstance(v, dict): |
97 |
self.assert_dicts_are_equal(v, d2[k])
|
98 |
else:
|
99 |
self.assertEqual(unicode(v), unicode(d2[k])) |
100 |
|
101 |
def setUp(self): |
102 |
self.url = 'https://astakos.example.com' |
103 |
self.token = 'ast@k0sT0k3n==' |
104 |
self.client = astakos.AstakosClient(self.url, self.token) |
105 |
|
106 |
def tearDown(self): |
107 |
FR.json = example |
108 |
|
109 |
@patch('%s.get' % astakos_pkg, return_value=FR()) |
110 |
def _authenticate(self, get): |
111 |
r = self.client.authenticate()
|
112 |
self.assertEqual(get.mock_calls[-1], call('/tokens')) |
113 |
self.cached = True |
114 |
return r
|
115 |
|
116 |
def test_authenticate(self): |
117 |
r = self._authenticate()
|
118 |
self.assert_dicts_are_equal(r, example)
|
119 |
|
120 |
def test_get_services(self): |
121 |
if not self.cached: |
122 |
self._authenticate()
|
123 |
slist = self.client.get_services()
|
124 |
self.assertEqual(slist, example['serviceCatalog']) |
125 |
|
126 |
def test_get_service_details(self): |
127 |
if not self.cached: |
128 |
self._authenticate()
|
129 |
stype = '#FAIL'
|
130 |
self.assertRaises(ClientError, self.client.get_service_details, stype) |
131 |
stype = 'compute'
|
132 |
expected = [s for s in example['serviceCatalog'] if s['type'] == stype] |
133 |
self.assert_dicts_are_equal(
|
134 |
self.client.get_service_details(stype), expected[0]) |
135 |
|
136 |
def test_get_service_endpoints(self): |
137 |
if not self.cached: |
138 |
self._authenticate()
|
139 |
stype, version = 'compute', 'V2' |
140 |
self.assertRaises(
|
141 |
ClientError, self.client.get_service_endpoints, stype)
|
142 |
expected = [s for s in example['serviceCatalog'] if s['type'] == stype] |
143 |
expected = [e for e in expected[0]['endpoints'] if ( |
144 |
e['version_id'] == version.lower())]
|
145 |
self.assert_dicts_are_equal(
|
146 |
self.client.get_service_endpoints(stype, version), expected[0]) |
147 |
|
148 |
def test_user_info(self): |
149 |
if not self.cached: |
150 |
self._authenticate()
|
151 |
self.assertTrue(set( |
152 |
example['user'].keys()).issubset(self.client.user_info().keys())) |
153 |
|
154 |
def test_item(self): |
155 |
if not self.cached: |
156 |
self._authenticate()
|
157 |
for term, val in example['user'].items(): |
158 |
self.assertEqual(self.client.term(term, self.token), val) |
159 |
self.assertTrue(
|
160 |
example['user']['email'][0] in self.client.term('email')) |
161 |
|
162 |
def test_list_users(self): |
163 |
if not self.cached: |
164 |
self._authenticate
|
165 |
FR.json = example0 |
166 |
self._authenticate()
|
167 |
r = self.client.list_users()
|
168 |
self.assertTrue(len(r) == 1) |
169 |
self.assertEqual(r[0]['auth_token'], self.token) |
170 |
|
171 |
if __name__ == '__main__': |
172 |
from sys import argv |
173 |
from kamaki.clients.test import runTestCase |
174 |
runTestCase(AstakosClient, 'AstakosClient', argv[1:]) |