Revision 528550d9 kamaki/clients/astakos/test.py
b/kamaki/clients/astakos/test.py | ||
---|---|---|
34 | 34 |
from mock import patch, call |
35 | 35 |
from unittest import TestCase |
36 | 36 |
|
37 |
from kamaki.clients import astakos |
|
37 |
from kamaki.clients import astakos, ClientError
|
|
38 | 38 |
|
39 | 39 |
|
40 | 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( |
|
41 | 50 |
name='Simple Name', |
42 | 51 |
username='User Full Name', |
43 | 52 |
auth_token_expires='1362583796000', |
... | ... | |
45 | 54 |
email=['user@example.gr'], |
46 | 55 |
id=42, |
47 | 56 |
uuid='aus3r-uu1d-f0r-73s71ng-as7ak0s') |
57 |
) |
|
48 | 58 |
|
49 | 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( |
|
50 | 69 |
name='Simple Name 0', |
51 | 70 |
username='User Full Name 0', |
52 |
auth_token_expires='1362583796001',
|
|
53 |
auth_token_created='1359991796001',
|
|
71 |
auth_token_expires='1362585796000',
|
|
72 |
auth_token_created='1359931796000',
|
|
54 | 73 |
email=['user0@example.gr'], |
55 |
id=32, |
|
56 |
uuid='an07h2r-us3r-uu1d-f0r-as7ak0s') |
|
74 |
id=42, |
|
75 |
uuid='aus3r-uu1d-507-73s71ng-as7ak0s') |
|
76 |
) |
|
57 | 77 |
|
58 | 78 |
|
59 | 79 |
class FR(object): |
... | ... | |
70 | 90 |
|
71 | 91 |
cached = False |
72 | 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 |
|
|
73 | 101 |
def setUp(self): |
74 | 102 |
self.url = 'https://astakos.example.com' |
75 | 103 |
self.token = 'ast@k0sT0k3n==' |
... | ... | |
81 | 109 |
@patch('%s.get' % astakos_pkg, return_value=FR()) |
82 | 110 |
def _authenticate(self, get): |
83 | 111 |
r = self.client.authenticate() |
84 |
self.assertEqual(get.mock_calls[-1], call('/astakos/api/authenticate'))
|
|
112 |
self.assertEqual(get.mock_calls[-1], call('/tokens'))
|
|
85 | 113 |
self.cached = True |
86 | 114 |
return r |
87 | 115 |
|
88 | 116 |
def test_authenticate(self): |
89 | 117 |
r = self._authenticate() |
90 |
for term, val in example.items(): |
|
91 |
self.assertTrue(term in r) |
|
92 |
self.assertEqual(val, r[term]) |
|
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']) |
|
93 | 125 |
|
94 |
def test_info(self): |
|
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): |
|
95 | 149 |
if not self.cached: |
96 | 150 |
self._authenticate() |
97 |
return self.test_info() |
|
98 | 151 |
self.assertTrue(set( |
99 |
example.keys()).issubset(self.client.info().keys()))
|
|
152 |
example['user'].keys()).issubset(self.client.user_info().keys()))
|
|
100 | 153 |
|
101 |
def test_get(self):
|
|
154 |
def test_item(self):
|
|
102 | 155 |
if not self.cached: |
103 | 156 |
self._authenticate() |
104 |
return self.test_get() |
|
105 |
for term, val in example.items(): |
|
157 |
for term, val in example['user'].items(): |
|
106 | 158 |
self.assertEqual(self.client.term(term, self.token), val) |
107 |
self.assertTrue(example['email'][0] in self.client.term('email')) |
|
159 |
self.assertTrue( |
|
160 |
example['user']['email'][0] in self.client.term('email')) |
|
108 | 161 |
|
109 |
def test_list(self): |
|
162 |
def test_list_users(self):
|
|
110 | 163 |
if not self.cached: |
111 | 164 |
self._authenticate |
112 | 165 |
FR.json = example0 |
113 | 166 |
self._authenticate() |
114 |
r = self.client.list() |
|
167 |
r = self.client.list_users()
|
|
115 | 168 |
self.assertTrue(len(r) == 1) |
116 | 169 |
self.assertEqual(r[0]['auth_token'], self.token) |
117 | 170 |
|
Also available in: Unified diff