~thedac/ubuntu/vivid/neutron-lbaas/2015.1.1

« back to all changes in this revision

Viewing changes to neutron_lbaas/tests/unit/services/loadbalancer/drivers/netscaler/test_ncc_client.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2015-01-14 11:31:23 UTC
  • Revision ID: package-import@ubuntu.com-20150114113123-t0ymuw5vm45jn8gu
Tags: upstream-2015.1~b1
ImportĀ upstreamĀ versionĀ 2015.1~b1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014 Citrix Systems
 
2
#
 
3
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
4
#    not use this file except in compliance with the License. You may obtain
 
5
#    a copy of the License at
 
6
#
 
7
#         http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
#    Unless required by applicable law or agreed to in writing, software
 
10
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
11
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
12
#    License for the specific language governing permissions and limitations
 
13
#    under the License.
 
14
 
 
15
import mock
 
16
from neutron.tests.unit import testlib_api
 
17
import requests
 
18
 
 
19
from neutron_lbaas.services.loadbalancer.drivers.netscaler import ncc_client
 
20
from neutron_lbaas.services.loadbalancer.drivers.netscaler \
 
21
    import netscaler_driver
 
22
 
 
23
NCC_CLIENT_CLASS = ('neutron_lbaas.services.loadbalancer.drivers'
 
24
                    '.netscaler.ncc_client.NSClient')
 
25
 
 
26
TESTURI_SCHEME = 'http'
 
27
TESTURI_HOSTNAME = '1.1.1.1'
 
28
TESTURI_PORT = 4433
 
29
TESTURI_PATH = '/ncc_service/1.0'
 
30
TESTURI = '%s://%s:%s%s' % (TESTURI_SCHEME, TESTURI_HOSTNAME,
 
31
                            TESTURI_PORT, TESTURI_PATH)
 
32
TEST_USERNAME = 'user211'
 
33
TEST_PASSWORD = '@30xHl5cT'
 
34
TEST_TENANT_ID = '9c5245a2-0432-9d4c-4829-9bd7028603a1'
 
35
TESTVIP_ID = '52ab5d71-6bb2-457f-8414-22a4ba55efec'
 
36
 
 
37
 
 
38
class TestNSClient(testlib_api.WebTestCase):
 
39
 
 
40
    """A Unit test for the NetScaler NCC client module."""
 
41
 
 
42
    def setUp(self):
 
43
        self.log = mock.patch.object(ncc_client, 'LOG').start()
 
44
        super(TestNSClient, self).setUp()
 
45
        # mock the requests.request function call
 
46
        self.request_method_mock = mock.Mock()
 
47
        requests.request = self.request_method_mock
 
48
        self.testclient = self._get_nsclient()
 
49
 
 
50
    def test_instantiate_nsclient_with_empty_uri(self):
 
51
        """Asserts that a call with empty URI will raise an exception."""
 
52
        self.assertRaises(ncc_client.NCCException, ncc_client.NSClient,
 
53
                          '', TEST_USERNAME, TEST_PASSWORD)
 
54
 
 
55
    def test_create_resource_with_no_connection(self):
 
56
        """Asserts that a call with no connection will raise an exception."""
 
57
        # mock a connection object that fails to establish a connection
 
58
        self.request_method_mock.side_effect = (
 
59
            requests.exceptions.ConnectionError())
 
60
        resource_path = netscaler_driver.VIPS_RESOURCE
 
61
        resource_name = netscaler_driver.VIP_RESOURCE
 
62
        resource_body = self._get_testvip_httpbody_for_create()
 
63
        # call method under test: create_resource() and assert that
 
64
        # it raises an exception
 
65
        self.assertRaises(ncc_client.NCCException,
 
66
                          self.testclient.create_resource,
 
67
                          TEST_TENANT_ID, resource_path,
 
68
                          resource_name, resource_body)
 
69
 
 
70
    def test_create_resource_with_error(self):
 
71
        """Asserts that a failed create call raises an exception."""
 
72
        # create a mock object to represent a valid http response
 
73
        # with a failure status code.
 
74
        fake_response = requests.Response()
 
75
        fake_response.status_code = requests.codes.unauthorized
 
76
        fake_response.headers = []
 
77
        requests.request.return_value = fake_response
 
78
        resource_path = netscaler_driver.VIPS_RESOURCE
 
79
        resource_name = netscaler_driver.VIP_RESOURCE
 
80
        resource_body = self._get_testvip_httpbody_for_create()
 
81
        # call method under test: create_resource
 
82
        # and assert that it raises the expected exception.
 
83
        self.assertRaises(ncc_client.NCCException,
 
84
                          self.testclient.create_resource,
 
85
                          TEST_TENANT_ID, resource_path,
 
86
                          resource_name, resource_body)
 
87
 
 
88
    def test_create_resource(self):
 
89
        """Asserts that a correct call will succeed."""
 
90
        # obtain the mock object that corresponds to the call of request()
 
91
        fake_response = requests.Response()
 
92
        fake_response.status_code = requests.codes.created
 
93
        fake_response.headers = []
 
94
        self.request_method_mock.return_value = fake_response
 
95
        resource_path = netscaler_driver.VIPS_RESOURCE
 
96
        resource_name = netscaler_driver.VIP_RESOURCE
 
97
        resource_body = self._get_testvip_httpbody_for_create()
 
98
        # call method under test: create_resource()
 
99
        self.testclient.create_resource(TEST_TENANT_ID, resource_path,
 
100
                                        resource_name, resource_body)
 
101
        # assert that request() was called
 
102
        # with the expected params.
 
103
        resource_url = "%s/%s" % (self.testclient.service_uri, resource_path)
 
104
        self.request_method_mock.assert_called_once_with(
 
105
            'POST',
 
106
            url=resource_url,
 
107
            headers=mock.ANY,
 
108
            data=mock.ANY)
 
109
 
 
110
    def test_update_resource_with_error(self):
 
111
        """Asserts that a failed update call raises an exception."""
 
112
        # create a valid http response with a failure status code.
 
113
        fake_response = requests.Response()
 
114
        fake_response.status_code = requests.codes.unauthorized
 
115
        fake_response.headers = []
 
116
        # obtain the mock object that corresponds to the call of request()
 
117
        self.request_method_mock.return_value = fake_response
 
118
        resource_path = "%s/%s" % (netscaler_driver.VIPS_RESOURCE,
 
119
                                   TESTVIP_ID)
 
120
        resource_name = netscaler_driver.VIP_RESOURCE
 
121
        resource_body = self._get_testvip_httpbody_for_update()
 
122
        # call method under test: update_resource() and
 
123
        # assert that it raises the expected exception.
 
124
        self.assertRaises(ncc_client.NCCException,
 
125
                          self.testclient.update_resource,
 
126
                          TEST_TENANT_ID, resource_path,
 
127
                          resource_name, resource_body)
 
128
 
 
129
    def test_update_resource(self):
 
130
        """Asserts that a correct update call will succeed."""
 
131
        # create a valid http response with a successful status code.
 
132
        fake_response = requests.Response()
 
133
        fake_response.status_code = requests.codes.ok
 
134
        fake_response.headers = []
 
135
        # obtain the mock object that corresponds to the call of request()
 
136
        self.request_method_mock.return_value = fake_response
 
137
        resource_path = "%s/%s" % (netscaler_driver.VIPS_RESOURCE,
 
138
                                   TESTVIP_ID)
 
139
        resource_name = netscaler_driver.VIP_RESOURCE
 
140
        resource_body = self._get_testvip_httpbody_for_update()
 
141
        # call method under test: update_resource.
 
142
        self.testclient.update_resource(TEST_TENANT_ID, resource_path,
 
143
                                        resource_name, resource_body)
 
144
        resource_url = "%s/%s" % (self.testclient.service_uri, resource_path)
 
145
        # assert that requests.request() was called with the
 
146
        # expected params.
 
147
        self.request_method_mock.assert_called_once_with(
 
148
            'PUT',
 
149
            url=resource_url,
 
150
            headers=mock.ANY,
 
151
            data=mock.ANY)
 
152
 
 
153
    def test_delete_resource_with_error(self):
 
154
        """Asserts that a failed delete call raises an exception."""
 
155
        # create a valid http response with a failure status code.
 
156
        fake_response = requests.Response()
 
157
        fake_response.status_code = requests.codes.unauthorized
 
158
        fake_response.headers = []
 
159
        resource_path = "%s/%s" % (netscaler_driver.VIPS_RESOURCE,
 
160
                                   TESTVIP_ID)
 
161
        # call method under test: create_resource
 
162
        self.assertRaises(ncc_client.NCCException,
 
163
                          self.testclient.remove_resource,
 
164
                          TEST_TENANT_ID, resource_path)
 
165
 
 
166
    def test_delete_resource(self):
 
167
        """Asserts that a correct delete call will succeed."""
 
168
        # create a valid http response with a failure status code.
 
169
        fake_response = requests.Response()
 
170
        fake_response.status_code = requests.codes.ok
 
171
        fake_response.headers = []
 
172
        # obtain the mock object that corresponds to the call of request()
 
173
        self.request_method_mock.return_value = fake_response
 
174
        resource_path = "%s/%s" % (netscaler_driver.VIPS_RESOURCE,
 
175
                                   TESTVIP_ID)
 
176
        resource_url = "%s/%s" % (self.testclient.service_uri, resource_path)
 
177
        # call method under test: create_resource
 
178
        self.testclient.remove_resource(TEST_TENANT_ID, resource_path)
 
179
        # assert that httplib.HTTPConnection request() was called with the
 
180
        # expected params
 
181
        self.request_method_mock.assert_called_once_with(
 
182
            'DELETE',
 
183
            url=resource_url,
 
184
            headers=mock.ANY,
 
185
            data=mock.ANY)
 
186
 
 
187
    def _get_nsclient(self):
 
188
        return ncc_client.NSClient(TESTURI, TEST_USERNAME, TEST_PASSWORD)
 
189
 
 
190
    def _get_testvip_httpbody_for_create(self):
 
191
        body = {
 
192
            'name': 'vip1',
 
193
            'address': '10.0.0.3',
 
194
            'pool_id': 'da477c13-24cd-4c9f-8c19-757a61ef3b9d',
 
195
            'protocol': 'HTTP',
 
196
            'protocol_port': 80,
 
197
            'admin_state_up': True,
 
198
        }
 
199
        return body
 
200
 
 
201
    def _get_testvip_httpbody_for_update(self):
 
202
        body = {}
 
203
        body['name'] = 'updated vip1'
 
204
        body['admin_state_up'] = False
 
205
        return body