Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / test / pithos.py @ 756e9de9

History | View | Annotate | Download (5.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 unittest import TestCase
35
from mock import patch, call, Mock
36

    
37
from kamaki.clients import ClientError
38
from kamaki.clients.pithos import PithosClient as PC
39
from kamaki.clients.astakos import AstakosClient
40
from kamaki.clients.connection.kamakicon import KamakiHTTPConnection as C
41

    
42
user_id = 'ac0un7-1d-5tr1ng'
43

    
44
account_info = {
45
    'content-language': 'en-us',
46
    'content-type': 'text/html; charset=utf-8',
47
    'date': 'Wed, 06 Mar 2013 13:25:51 GMT',
48
    'last-modified': 'Mon, 04 Mar 2013 18:22:31 GMT',
49
    'server': 'gunicorn/0.14.5',
50
    'vary': 'Accept-Language',
51
    'x-account-bytes-used': '751615526',
52
    'x-account-container-count': 7,
53
    'x-account-policy-quota': 53687091200,
54
    'x-account-policy-versioning': 'auto'}
55

    
56

    
57
class Pithos(TestCase):
58

    
59
    class FR(object):
60
        """FR stands for Fake Response"""
61
        json = dict()
62
        headers = {}
63
        content = json
64
        status = None
65
        status_code = 200
66
        headers = dict()
67

    
68
        def release(self):
69
            pass
70

    
71
    files = []
72

    
73
    def assert_dicts_are_equal(self, d1, d2):
74
        for k, v in d1.items():
75
            self.assertTrue(k in d2)
76
            if isinstance(v, dict):
77
                self.assert_dicts_are_equal(v, d2[k])
78
            else:
79
                self.assertEqual(unicode(v), unicode(d2[k]))
80

    
81
    def setUp(self):
82
        self.url = 'https://www.example.com/pithos'
83
        self.token = 'p17h0570k3n'
84
        self.client = PC(self.url, self.token)
85
        self.client.account = user_id
86
        self.client.container = 'c0nt@1n3r_i'
87

    
88
    def tearDown(self):
89
        self.FR.headers = dict()
90
        self.FR.status_code = 200
91

    
92
    def test_get_account_info(self):
93
        self.FR.headers = account_info
94
        self.FR.status_code = 204
95
        with patch.object(C, 'perform_request', return_value=self.FR()):
96
            r = self.client.get_account_info()
97
            self.assertEqual(self.client.http_client.url, self.url)
98
            self.assertEqual(self.client.http_client.path, '/%s' % user_id)
99
            self.assert_dicts_are_equal(r, account_info)
100
            PC.set_param = Mock()
101
            untils = ['date 1', 'date 2', 'date 3']
102
            for unt in untils:
103
                r = self.client.get_account_info(until=unt)
104
                self.assert_dicts_are_equal(r, account_info)
105
            for i in range(len(untils)):
106
                self.assertEqual(
107
                    PC.set_param.mock_calls[i],
108
                    call('until', untils[i], iff=untils[i]))
109
            self.FR.status_code = 401
110
            self.assertRaises(ClientError, self.client.get_account_info)
111

    
112
    def test_replace_account_meta(self):
113
        self.FR.status_code = 202
114
        metas = dict(k1='v1', k2='v2', k3='v3')
115
        PC.set_header = Mock()
116
        with patch.object(C, 'perform_request', return_value=self.FR()):
117
            self.client.replace_account_meta(metas)
118
            self.assertEqual(self.client.http_client.url, self.url)
119
            self.assertEqual(self.client.http_client.path, '/%s' % user_id)
120
            prfx = 'X-Account-Meta-'
121
            expected = [call('%s%s' % (prfx, k), v) for k, v in metas.items()]
122
            self.assertEqual(PC.set_header.mock_calls, expected)
123

    
124
    def test_del_account_meta(self):
125
        keys = ['k1', 'k2', 'k3']
126
        with patch.object(PC, 'account_post', return_value=self.FR()) as ap:
127
            expected = []
128
            for key in keys:
129
                self.client.del_account_meta(key)
130
                expected.append(call(update=True, metadata={key: ''}))
131
            self.assertEqual(ap.mock_calls, expected)
132

    
133
    def test_create_container(self):
134
        self.FR.status_code = 201
135
        with patch.object(PC, 'put', return_value=self.FR()) as put:
136
            cont = 's0m3c0n731n3r'
137
            self.client.create_container(cont)
138
            expected = [call('/%s/%s' % (user_id, cont), success=(201, 202))]
139
            self.assertEqual(put.mock_calls, expected)
140
            self.FR.status_code = 202
141
            self.assertRaises(ClientError, self.client.create_container, cont)