Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / astakos / test.py @ 508570ae

History | View | Annotate | Download (6.5 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
    access=dict(
42
        serviceCatalog=[
43
            dict(name='service name 1', type='compute', endpoints=[
44
                dict(versionId='v1', publicUrl='http://1.1.1.1/v1'),
45
                dict(versionId='v2', publicUrl='http://1.1.1.1/v2')]),
46
            dict(name='service name 2', type='image', endpoints=[
47
                dict(versionId='v2', publicUrl='http://1.1.1.1/v2'),
48
                dict(versionId='v2.1', publicUrl='http://1.1.1.1/v2/xtra')])
49
            ],
50
        user=dict(
51
            name='Simple Name',
52
            username='User Full Name',
53
            auth_token_expires='1362583796000',
54
            auth_token_created='1359991796000',
55
            email=['user@example.gr'],
56
            id=42,
57
            uuid='aus3r-uu1d-f0r-73s71ng-as7ak0s')
58
        )
59
    )
60

    
61
example0 = dict(
62
    access=dict(
63
        serviceCatalog=[
64
            dict(name='service name 1', type='compute', endpoints=[
65
                dict(versionId='v1', publicUrl='http://1.1.1.1/v1'),
66
                dict(versionId='v2', publicUrl='http://1.1.1.1/v2')]),
67
            dict(name='service name 3', type='object-storage', endpoints=[
68
                dict(versionId='v2', publicUrl='http://1.1.1.1/v2'),
69
                dict(versionId='v2.1', publicUrl='http://1.1.1.1/v2/xtra')])
70
            ],
71
        user=dict(
72
            name='Simple Name 0',
73
            username='User Full Name 0',
74
            auth_token_expires='1362585796000',
75
            auth_token_created='1359931796000',
76
            email=['user0@example.gr'],
77
            id=42,
78
            uuid='aus3r-uu1d-507-73s71ng-as7ak0s')
79
        )
80
    )
81

    
82

    
83
class FR(object):
84
    json = example
85
    headers = {}
86
    content = json
87
    status = None
88
    status_code = 200
89

    
90
astakos_pkg = 'kamaki.clients.astakos.AstakosClient'
91

    
92

    
93
class AstakosClient(TestCase):
94

    
95
    cached = False
96

    
97
    def assert_dicts_are_equal(self, d1, d2):
98
        for k, v in d1.items():
99
            self.assertTrue(k in d2)
100
            if isinstance(v, dict):
101
                self.assert_dicts_are_equal(v, d2[k])
102
            else:
103
                self.assertEqual(unicode(v), unicode(d2[k]))
104

    
105
    def setUp(self):
106
        self.url = 'https://astakos.example.com'
107
        self.token = 'ast@k0sT0k3n=='
108
        self.client = astakos.AstakosClient(self.url, self.token)
109

    
110
    def tearDown(self):
111
        FR.json = example
112

    
113
    @patch('%s.post' % astakos_pkg, return_value=FR())
114
    def _authenticate(self, post):
115
        r = self.client.authenticate()
116
        send_body = dict(auth=dict(token=dict(id=self.token)))
117
        self.assertEqual(post.mock_calls[-1], call('/tokens', json=send_body))
118
        self.cached = True
119
        return r
120

    
121
    def test_authenticate(self):
122
        r = self._authenticate()
123
        self.assert_dicts_are_equal(r, example)
124

    
125
    def test_get_services(self):
126
        if not self.cached:
127
            self._authenticate()
128
        slist = self.client.get_services()
129
        self.assertEqual(slist, example['access']['serviceCatalog'])
130

    
131
    def test_get_service_details(self):
132
        if not self.cached:
133
            self._authenticate()
134
        stype = '#FAIL'
135
        self.assertRaises(ClientError, self.client.get_service_details, stype)
136
        stype = 'compute'
137
        expected = [s for s in example['access']['serviceCatalog'] if (
138
            s['type'] == stype)]
139
        self.assert_dicts_are_equal(
140
            self.client.get_service_details(stype), expected[0])
141

    
142
    def test_get_service_endpoints(self):
143
        if not self.cached:
144
            self._authenticate()
145
        stype, version = 'compute', 'V2'
146
        self.assertRaises(
147
            ClientError, self.client.get_service_endpoints, stype)
148
        expected = [s for s in example['access']['serviceCatalog'] if (
149
            s['type'] == stype)]
150
        expected = [e for e in expected[0]['endpoints'] if (
151
            e['versionId'] == version.lower())]
152
        self.assert_dicts_are_equal(
153
            self.client.get_service_endpoints(stype, version), expected[0])
154

    
155
    def test_user_info(self):
156
        if not self.cached:
157
            self._authenticate()
158
        self.assertTrue(set(example['access']['user'].keys()).issubset(
159
            self.client.user_info().keys()))
160

    
161
    def test_item(self):
162
        if not self.cached:
163
            self._authenticate()
164
        for term, val in example['access']['user'].items():
165
            self.assertEqual(self.client.term(term, self.token), val)
166
        self.assertTrue(
167
            example['access']['user']['email'][0] in self.client.term('email'))
168

    
169
    def test_list_users(self):
170
        if not self.cached:
171
            self._authenticate
172
        FR.json = example0
173
        self._authenticate()
174
        r = self.client.list_users()
175
        self.assertTrue(len(r) == 1)
176
        self.assertEqual(r[0]['auth_token'], self.token)
177

    
178
if __name__ == '__main__':
179
    from sys import argv
180
    from kamaki.clients.test import runTestCase
181
    runTestCase(AstakosClient, 'AstakosClient', argv[1:])