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, TestSuite, makeSuite, TextTestRunner
|
35 |
|
from mock import Mock, patch
|
36 |
|
|
37 |
|
|
38 |
|
class _fakeResponse(object):
|
39 |
|
sample = 'sample string'
|
40 |
|
getheaders = Mock(return_value={})
|
41 |
|
content = Mock(return_value=sample)
|
42 |
|
read = Mock(return_value=sample)
|
43 |
|
status = Mock(return_value=None)
|
44 |
|
status_code = 200
|
45 |
|
reason = Mock(return_value=None)
|
46 |
|
release = Mock()
|
47 |
|
headers = {}
|
48 |
|
|
49 |
|
|
50 |
|
class Client(TestCase):
|
51 |
|
|
52 |
|
def setUp(self):
|
53 |
|
from kamaki.clients import Client
|
54 |
|
from kamaki.clients.connection.kamakicon import (
|
55 |
|
KamakiHTTPConnection)
|
56 |
|
self.token = 'F@k3T0k3n'
|
57 |
|
self.base_url = 'http://www.example.com'
|
58 |
|
self.KC = KamakiHTTPConnection
|
59 |
|
self.KR = _fakeResponse
|
60 |
|
self.c = Client(self.base_url, self.token, self.KC())
|
61 |
|
|
62 |
|
def test_request(self):
|
63 |
|
req = self.c.request
|
64 |
|
method = 'GET'
|
65 |
|
path = '/online/path'
|
66 |
|
with patch.object(self.KC, 'perform_request', return_value=self.KR()):
|
67 |
|
r = req(method, path)
|
68 |
|
self.assertTrue(isinstance(r, self.KR))
|
69 |
|
# async_headers/params do not persist
|
70 |
|
# TODO: Use a real but mocked KamakiConnection instance
|
71 |
|
tmp_headers = dict(h1='v1', h2='v2')
|
72 |
|
tmp_params = dict(p1='v1', p2=None)
|
73 |
|
r = req(method, path, async_headers=tmp_headers)
|
74 |
|
self.assertFalse(self.c.headers)
|
75 |
|
r = req(method, path, async_params=tmp_params)
|
76 |
|
|
77 |
|
|
78 |
|
def get_test_classes(module=__import__(__name__), name=''):
|
79 |
|
from inspect import getmembers, isclass
|
80 |
|
for objname, obj in getmembers(module):
|
81 |
|
if (objname == name or not name) and isclass(obj) and (
|
82 |
|
issubclass(obj, TestCase)):
|
83 |
|
yield (obj, objname)
|
84 |
|
|
85 |
|
|
86 |
|
def main(argv):
|
87 |
|
for cls, name in get_test_classes(name=argv[1] if len(argv) > 1 else ''):
|
88 |
|
args = argv[2:]
|
89 |
|
suite = TestSuite()
|
90 |
|
if args:
|
91 |
|
suite.addTest(cls('_'.join(['test'] + args)))
|
92 |
|
else:
|
93 |
|
suite.addTest(makeSuite(cls))
|
94 |
|
print('Test %s' % name)
|
95 |
|
TextTestRunner(verbosity=2).run(suite)
|
96 |
|
|
97 |
|
|
98 |
|
if __name__ == '__main__':
|
99 |
|
from sys import argv
|
100 |
|
main(argv)
|