Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / astakos / test.py @ 736072e2

History | View | Annotate | Download (9 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 91478081 Stavros Sachtouris
from logging import getLogger
36 c1004a00 Stavros Sachtouris
from unittest import TestCase
37 3c50df2e Stavros Sachtouris
38 91478081 Stavros Sachtouris
from kamaki.clients import ClientError
39 274bcf7a Stavros Sachtouris
40 274bcf7a Stavros Sachtouris
41 274bcf7a Stavros Sachtouris
example = dict(
42 508570ae Stavros Sachtouris
    access=dict(
43 b44a5a37 Stavros Sachtouris
         token=dict(
44 b44a5a37 Stavros Sachtouris
            expires="2013-07-14T10:07:42.481134+00:00",
45 b44a5a37 Stavros Sachtouris
            id="ast@k0sT0k3n==",
46 b44a5a37 Stavros Sachtouris
            tenant=dict(
47 b44a5a37 Stavros Sachtouris
                id="42",
48 b44a5a37 Stavros Sachtouris
                name="Simple Name 0")
49 b44a5a37 Stavros Sachtouris
        ),
50 508570ae Stavros Sachtouris
        serviceCatalog=[
51 508570ae Stavros Sachtouris
            dict(name='service name 1', type='compute', endpoints=[
52 508570ae Stavros Sachtouris
                dict(versionId='v1', publicUrl='http://1.1.1.1/v1'),
53 508570ae Stavros Sachtouris
                dict(versionId='v2', publicUrl='http://1.1.1.1/v2')]),
54 508570ae Stavros Sachtouris
            dict(name='service name 3', type='object-storage', endpoints=[
55 508570ae Stavros Sachtouris
                dict(versionId='v2', publicUrl='http://1.1.1.1/v2'),
56 508570ae Stavros Sachtouris
                dict(versionId='v2.1', publicUrl='http://1.1.1.1/v2/xtra')])
57 508570ae Stavros Sachtouris
            ],
58 508570ae Stavros Sachtouris
        user=dict(
59 508570ae Stavros Sachtouris
            name='Simple Name 0',
60 508570ae Stavros Sachtouris
            username='User Full Name 0',
61 508570ae Stavros Sachtouris
            auth_token_expires='1362585796000',
62 508570ae Stavros Sachtouris
            auth_token_created='1359931796000',
63 508570ae Stavros Sachtouris
            email=['user0@example.gr'],
64 508570ae Stavros Sachtouris
            id=42,
65 508570ae Stavros Sachtouris
            uuid='aus3r-uu1d-507-73s71ng-as7ak0s')
66 508570ae Stavros Sachtouris
        )
67 528550d9 Stavros Sachtouris
    )
68 c1004a00 Stavros Sachtouris
69 c1004a00 Stavros Sachtouris
70 975270f2 Stavros Sachtouris
class FR(object):
71 975270f2 Stavros Sachtouris
    json = example
72 975270f2 Stavros Sachtouris
    headers = {}
73 975270f2 Stavros Sachtouris
    content = json
74 975270f2 Stavros Sachtouris
    status = None
75 975270f2 Stavros Sachtouris
    status_code = 200
76 975270f2 Stavros Sachtouris
77 91478081 Stavros Sachtouris
astakos_pkg = 'kamaki.clients.astakos'
78 c1004a00 Stavros Sachtouris
79 c1004a00 Stavros Sachtouris
80 3c50df2e Stavros Sachtouris
class AstakosClient(TestCase):
81 c1004a00 Stavros Sachtouris
82 274bcf7a Stavros Sachtouris
    cached = False
83 274bcf7a Stavros Sachtouris
84 528550d9 Stavros Sachtouris
    def assert_dicts_are_equal(self, d1, d2):
85 528550d9 Stavros Sachtouris
        for k, v in d1.items():
86 528550d9 Stavros Sachtouris
            self.assertTrue(k in d2)
87 528550d9 Stavros Sachtouris
            if isinstance(v, dict):
88 528550d9 Stavros Sachtouris
                self.assert_dicts_are_equal(v, d2[k])
89 528550d9 Stavros Sachtouris
            else:
90 528550d9 Stavros Sachtouris
                self.assertEqual(unicode(v), unicode(d2[k]))
91 528550d9 Stavros Sachtouris
92 c1004a00 Stavros Sachtouris
    def setUp(self):
93 c1004a00 Stavros Sachtouris
        self.url = 'https://astakos.example.com'
94 c1004a00 Stavros Sachtouris
        self.token = 'ast@k0sT0k3n=='
95 91478081 Stavros Sachtouris
        from kamaki.clients.astakos import AstakosClient as AC
96 91478081 Stavros Sachtouris
        self.client = AC(self.url, self.token)
97 c1004a00 Stavros Sachtouris
98 975270f2 Stavros Sachtouris
    def tearDown(self):
99 975270f2 Stavros Sachtouris
        FR.json = example
100 975270f2 Stavros Sachtouris
101 cd42f8d8 Stavros Sachtouris
    @patch('%s.LoggedAstakosClient.__init__' % astakos_pkg, return_value=None)
102 91478081 Stavros Sachtouris
    @patch(
103 cd42f8d8 Stavros Sachtouris
        '%s.LoggedAstakosClient.get_endpoints' % astakos_pkg,
104 91478081 Stavros Sachtouris
        return_value=example)
105 91478081 Stavros Sachtouris
    def _authenticate(self, get_endpoints, sac):
106 975270f2 Stavros Sachtouris
        r = self.client.authenticate()
107 91478081 Stavros Sachtouris
        self.assertEqual(
108 91478081 Stavros Sachtouris
            sac.mock_calls[-1], call(self.token, self.url,
109 91478081 Stavros Sachtouris
                logger=getLogger('astakosclient')))
110 91478081 Stavros Sachtouris
        self.assertEqual(get_endpoints.mock_calls[-1], call())
111 274bcf7a Stavros Sachtouris
        return r
112 c1004a00 Stavros Sachtouris
113 274bcf7a Stavros Sachtouris
    def test_authenticate(self):
114 274bcf7a Stavros Sachtouris
        r = self._authenticate()
115 528550d9 Stavros Sachtouris
        self.assert_dicts_are_equal(r, example)
116 91478081 Stavros Sachtouris
        uuid = example['access']['user']['id']
117 91478081 Stavros Sachtouris
        self.assert_dicts_are_equal(self.client._uuids, {self.token: uuid})
118 91478081 Stavros Sachtouris
        self.assert_dicts_are_equal(self.client._cache, {uuid: r})
119 91478081 Stavros Sachtouris
        from astakosclient import AstakosClient as SAC
120 91478081 Stavros Sachtouris
        self.assertTrue(isinstance(self.client._astakos[uuid], SAC))
121 91478081 Stavros Sachtouris
        self.assert_dicts_are_equal(self.client._uuids2usernames, {uuid: {}})
122 91478081 Stavros Sachtouris
        self.assert_dicts_are_equal(self.client._usernames2uuids, {uuid: {}})
123 91478081 Stavros Sachtouris
124 172ee8f9 Stavros Sachtouris
    def test_get_client(self):
125 172ee8f9 Stavros Sachtouris
        if not self.cached:
126 172ee8f9 Stavros Sachtouris
            self._authenticate()
127 172ee8f9 Stavros Sachtouris
        from astakosclient import AstakosClient as SNFAC
128 172ee8f9 Stavros Sachtouris
        self.assertTrue(self.client.get_client(), SNFAC)
129 172ee8f9 Stavros Sachtouris
130 91478081 Stavros Sachtouris
    def test_get_token(self):
131 91478081 Stavros Sachtouris
        self._authenticate()
132 91478081 Stavros Sachtouris
        uuid = self.client._uuids.values()[0]
133 91478081 Stavros Sachtouris
        self.assertEqual(self.client.get_token(uuid), self.token)
134 528550d9 Stavros Sachtouris
135 528550d9 Stavros Sachtouris
    def test_get_services(self):
136 528550d9 Stavros Sachtouris
        if not self.cached:
137 528550d9 Stavros Sachtouris
            self._authenticate()
138 528550d9 Stavros Sachtouris
        slist = self.client.get_services()
139 508570ae Stavros Sachtouris
        self.assertEqual(slist, example['access']['serviceCatalog'])
140 c1004a00 Stavros Sachtouris
141 528550d9 Stavros Sachtouris
    def test_get_service_details(self):
142 528550d9 Stavros Sachtouris
        if not self.cached:
143 528550d9 Stavros Sachtouris
            self._authenticate()
144 528550d9 Stavros Sachtouris
        stype = '#FAIL'
145 528550d9 Stavros Sachtouris
        self.assertRaises(ClientError, self.client.get_service_details, stype)
146 528550d9 Stavros Sachtouris
        stype = 'compute'
147 508570ae Stavros Sachtouris
        expected = [s for s in example['access']['serviceCatalog'] if (
148 508570ae Stavros Sachtouris
            s['type'] == stype)]
149 528550d9 Stavros Sachtouris
        self.assert_dicts_are_equal(
150 528550d9 Stavros Sachtouris
            self.client.get_service_details(stype), expected[0])
151 528550d9 Stavros Sachtouris
152 528550d9 Stavros Sachtouris
    def test_get_service_endpoints(self):
153 528550d9 Stavros Sachtouris
        if not self.cached:
154 528550d9 Stavros Sachtouris
            self._authenticate()
155 528550d9 Stavros Sachtouris
        stype, version = 'compute', 'V2'
156 528550d9 Stavros Sachtouris
        self.assertRaises(
157 528550d9 Stavros Sachtouris
            ClientError, self.client.get_service_endpoints, stype)
158 508570ae Stavros Sachtouris
        expected = [s for s in example['access']['serviceCatalog'] if (
159 508570ae Stavros Sachtouris
            s['type'] == stype)]
160 528550d9 Stavros Sachtouris
        expected = [e for e in expected[0]['endpoints'] if (
161 508570ae Stavros Sachtouris
            e['versionId'] == version.lower())]
162 528550d9 Stavros Sachtouris
        self.assert_dicts_are_equal(
163 528550d9 Stavros Sachtouris
            self.client.get_service_endpoints(stype, version), expected[0])
164 528550d9 Stavros Sachtouris
165 528550d9 Stavros Sachtouris
    def test_user_info(self):
166 274bcf7a Stavros Sachtouris
        if not self.cached:
167 274bcf7a Stavros Sachtouris
            self._authenticate()
168 508570ae Stavros Sachtouris
        self.assertTrue(set(example['access']['user'].keys()).issubset(
169 508570ae Stavros Sachtouris
            self.client.user_info().keys()))
170 c1004a00 Stavros Sachtouris
171 528550d9 Stavros Sachtouris
    def test_item(self):
172 274bcf7a Stavros Sachtouris
        if not self.cached:
173 274bcf7a Stavros Sachtouris
            self._authenticate()
174 508570ae Stavros Sachtouris
        for term, val in example['access']['user'].items():
175 274bcf7a Stavros Sachtouris
            self.assertEqual(self.client.term(term, self.token), val)
176 528550d9 Stavros Sachtouris
        self.assertTrue(
177 508570ae Stavros Sachtouris
            example['access']['user']['email'][0] in self.client.term('email'))
178 c1004a00 Stavros Sachtouris
179 528550d9 Stavros Sachtouris
    def test_list_users(self):
180 274bcf7a Stavros Sachtouris
        if not self.cached:
181 b44a5a37 Stavros Sachtouris
            self._authenticate()
182 91478081 Stavros Sachtouris
        FR.json = example
183 975270f2 Stavros Sachtouris
        self._authenticate()
184 528550d9 Stavros Sachtouris
        r = self.client.list_users()
185 274bcf7a Stavros Sachtouris
        self.assertTrue(len(r) == 1)
186 274bcf7a Stavros Sachtouris
        self.assertEqual(r[0]['auth_token'], self.token)
187 85898ca4 Stavros Sachtouris
188 91478081 Stavros Sachtouris
    @patch(
189 cd42f8d8 Stavros Sachtouris
        '%s.LoggedAstakosClient.get_usernames' % astakos_pkg,
190 91478081 Stavros Sachtouris
        return_value={42: 'username42', 43: 'username43'})
191 91478081 Stavros Sachtouris
    def test_uuids2usernames(self, get_usernames):
192 91478081 Stavros Sachtouris
        from astakosclient import AstakosClientException
193 91478081 Stavros Sachtouris
        self.assertRaises(
194 91478081 Stavros Sachtouris
            AstakosClientException, self.client.uuids2usernames, [42, 43])
195 91478081 Stavros Sachtouris
        with patch(
196 cd42f8d8 Stavros Sachtouris
                '%s.LoggedAstakosClient.__init__' % astakos_pkg,
197 91478081 Stavros Sachtouris
                return_value=None) as sac:
198 91478081 Stavros Sachtouris
            with patch(
199 cd42f8d8 Stavros Sachtouris
                    '%s.LoggedAstakosClient.get_endpoints' % astakos_pkg,
200 91478081 Stavros Sachtouris
                    return_value=example) as get_endpoints:
201 91478081 Stavros Sachtouris
                r = self.client.uuids2usernames([42, 43])
202 91478081 Stavros Sachtouris
                self.assert_dicts_are_equal(
203 91478081 Stavros Sachtouris
                    r, {42: 'username42', 43: 'username43'})
204 91478081 Stavros Sachtouris
                self.assertEqual(sac.mock_calls[-1], call(
205 91478081 Stavros Sachtouris
                    self.token, self.url, logger=getLogger('astakosclient')))
206 91478081 Stavros Sachtouris
                self.assertEqual(get_endpoints.mock_calls[-1], call())
207 91478081 Stavros Sachtouris
                self.assertEqual(get_usernames.mock_calls[-1], call([42, 43]))
208 91478081 Stavros Sachtouris
209 91478081 Stavros Sachtouris
    @patch(
210 cd42f8d8 Stavros Sachtouris
        '%s.LoggedAstakosClient.get_uuids' % astakos_pkg,
211 91478081 Stavros Sachtouris
        return_value={'username42': 42, 'username43': 43})
212 91478081 Stavros Sachtouris
    def test_usernames2uuids(self, get_uuids):
213 91478081 Stavros Sachtouris
        from astakosclient import AstakosClientException
214 91478081 Stavros Sachtouris
        self.assertRaises(
215 91478081 Stavros Sachtouris
            AstakosClientException, self.client.usernames2uuids, ['u1', 'u2'])
216 91478081 Stavros Sachtouris
        with patch(
217 cd42f8d8 Stavros Sachtouris
                '%s.LoggedAstakosClient.__init__' % astakos_pkg,
218 91478081 Stavros Sachtouris
                return_value=None) as sac:
219 91478081 Stavros Sachtouris
            with patch(
220 cd42f8d8 Stavros Sachtouris
                    '%s.LoggedAstakosClient.get_endpoints' % astakos_pkg,
221 91478081 Stavros Sachtouris
                    return_value=example) as get_endpoints:
222 91478081 Stavros Sachtouris
                r = self.client.usernames2uuids(['u1', 'u2'])
223 91478081 Stavros Sachtouris
                self.assert_dicts_are_equal(
224 91478081 Stavros Sachtouris
                    r, {'username42': 42, 'username43': 43})
225 91478081 Stavros Sachtouris
                self.assertEqual(sac.mock_calls[-1], call(
226 91478081 Stavros Sachtouris
                    self.token, self.url, logger=getLogger('astakosclient')))
227 91478081 Stavros Sachtouris
                self.assertEqual(get_endpoints.mock_calls[-1], call())
228 91478081 Stavros Sachtouris
                self.assertEqual(get_uuids.mock_calls[-1], call(['u1', 'u2']))
229 91478081 Stavros Sachtouris
230 91478081 Stavros Sachtouris
231 85898ca4 Stavros Sachtouris
if __name__ == '__main__':
232 85898ca4 Stavros Sachtouris
    from sys import argv
233 85898ca4 Stavros Sachtouris
    from kamaki.clients.test import runTestCase
234 3c50df2e Stavros Sachtouris
    runTestCase(AstakosClient, 'AstakosClient', argv[1:])