Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / tests / rapi_pool_tests.py @ 1464a17b

History | View | Annotate | Download (3.7 kB)

1
# Copyright 2011-2012 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 django.test import TestCase
35

    
36
from synnefo.logic import rapi_pool
37

    
38
from mock import patch
39

    
40

    
41
@patch('synnefo.logic.rapi_pool.GanetiRapiClient', spec=True)
42
class GanetiRapiPoolTest(TestCase):
43
    def test_new_client(self, rclient):
44
        cl = rapi_pool.get_rapi_client(1, 'amxixa', 'cluster0', '5080', 'user',
45
                                       'pass')
46
        rclient.assert_called_once_with("cluster0", "5080", "user", "pass")
47
        self.assertTrue('amxixa' in rapi_pool._pools)
48
        self.assertTrue(cl._pool is rapi_pool._pools[rapi_pool._hashes[1]])
49

    
50
    def test_invalid_get(self, rclient):
51
        self.assertRaises(ValueError, rapi_pool.get_rapi_client, 1, 'amxixa',
52
                          None, '5080', 'user', 'pass')
53
        self.assertRaises(ValueError, rapi_pool.get_rapi_client, 1, 'amxixa',
54
                          'Foo', None, 'user', 'pass')
55

    
56
    def test_get_from_pool(self, rclient):
57
        cl = rapi_pool.get_rapi_client(1, 'dummyhash', 'cluster1', '5080',
58
                                       'user', 'pass')
59
        rclient.assert_called_once_with("cluster1", "5080", "user", "pass")
60
        rapi_pool.put_rapi_client(cl)
61
        rclient.reset_mock()
62
        cl2 = rapi_pool.get_rapi_client(1, 'dummyhash', 'cluster1', '5080',
63
                                        'user', 'pass')
64
        self.assertTrue(cl is cl2)
65
        self.assertFalse(rclient.mock_calls)
66

    
67
    def test_changed_credentials(self, rclient):
68
        cl = rapi_pool.get_rapi_client(1, 'dummyhash2', 'cluster2', '5080',
69
                                       'user', 'pass')
70
        rclient.assert_called_once_with("cluster2", "5080", "user", "pass")
71
        rapi_pool.put_rapi_client(cl)
72
        rclient.reset_mock()
73
        rapi_pool.get_rapi_client(1, 'dummyhash3', 'cluster2', '5080',
74
                                  'user', 'new_pass')
75
        rclient.assert_called_once_with("cluster2", "5080", "user", "new_pass")
76
        self.assertFalse('dummyhash2' in rapi_pool._pools)
77

    
78
    def test_no_pool(self, rclient):
79
        cl = rapi_pool.get_rapi_client(1, 'dummyhash2', 'cluster2', '5080',
80
                                       'user', 'pass')
81
        cl._pool = None
82
        rapi_pool.put_rapi_client(cl)
83
        self.assertTrue(cl not in rapi_pool._pools.values())