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