File: test_client.py

package info (click to toggle)
networking-mlnx 1%3A13.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 704 kB
  • sloc: python: 4,863; sh: 180; makefile: 36
file content (157 lines) | stat: -rw-r--r-- 6,701 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright 2014 Mellanox Technologies, Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import mock

from oslo_config import cfg
from oslo_config import fixture as fixture_config

from networking_mlnx.plugins.ml2.drivers.sdn import client
from networking_mlnx.plugins.ml2.drivers.sdn import config
from networking_mlnx.plugins.ml2.drivers.sdn import constants as sdn_const
from networking_mlnx.tests import base


class TestClient(base.TestCase):

    def setUp(self):
        super(TestClient, self).setUp()
        self.conf_fixture = self.useFixture(fixture_config.Config())
        self.conf = self.conf_fixture.conf
        self._set_args()
        self.client = client.SdnRestClient.create_client()
        self.url = 'some_url'
        self.data = {'some': 'data'}

    def _set_args(self):
        self.conf.register_opts(config.sdn_opts, sdn_const.GROUP_OPT)
        for arg in client.SdnRestClient.MANDATORY_ARGS:
            self.conf.set_override(name=arg,
                                   override="http://some_val",
                                   group=sdn_const.GROUP_OPT)

    def test_mandatory_args(self):
        mandatory_arg_objects = filter(
                lambda obj: obj.name in client.SdnRestClient.MANDATORY_ARGS,
                config.sdn_opts)
        for arg in mandatory_arg_objects:
            self._set_args()
            self.conf.unregister_opt(opt=arg,
                                     group=sdn_const.GROUP_OPT)
            self.assertRaises(cfg.NoSuchOptError,
                              client.SdnRestClient.create_client)

    @mock.patch('networking_mlnx.plugins.ml2.drivers.'
                'sdn.client.SdnRestClient.request')
    def test_get(self, mocked_request):
        self.client.get(self.url, self.data)
        expected_url = '/'.join((self.conf.sdn.url, self.url))
        mocked_request.assert_called_once_with(sdn_const.GET,
                                               expected_url,
                                               self.data)

        mocked_request.reset_mock()
        self.client.get(self.url)
        mocked_request.assert_called_once_with(sdn_const.GET,
                                               expected_url,
                                               None)

        mocked_request.reset_mock()
        self.client.get()
        mocked_request.assert_called_once_with(sdn_const.GET,
                                               self.conf.sdn.url,
                                               None)

    @mock.patch('networking_mlnx.plugins.ml2.drivers.'
                'sdn.client.SdnRestClient.request')
    def test_put(self, mocked_request):
        self.client.put(self.url, self.data)
        expected_url = '/'.join((self.conf.sdn.url,
                                 self.conf.sdn.domain,
                                 self.url))
        mocked_request.assert_called_once_with(sdn_const.PUT,
                                               expected_url,
                                               self.data)

        mocked_request.reset_mock()
        self.client.put(self.url)
        mocked_request.assert_called_once_with(sdn_const.PUT,
                                               expected_url,
                                               None)

        mocked_request.reset_mock()
        self.client.put()
        expected_url = '/'.join((self.conf.sdn.url, self.conf.sdn.domain))
        mocked_request.assert_called_once_with(sdn_const.PUT,
                                               expected_url,
                                               None)

    @mock.patch('networking_mlnx.plugins.ml2.drivers.'
                'sdn.client.SdnRestClient.request')
    def test_post(self, mocked_request):
        self.client.post(self.url, self.data)
        expected_url = '/'.join((self.conf.sdn.url,
                                 self.conf.sdn.domain,
                                 self.url))
        mocked_request.assert_called_once_with(sdn_const.POST,
                                               expected_url,
                                               self.data)

        mocked_request.reset_mock()
        self.client.post(self.url)
        mocked_request.assert_called_once_with(sdn_const.POST,
                                               expected_url,
                                               None)

        mocked_request.reset_mock()
        self.client.post()
        expected_url = '/'.join((self.conf.sdn.url, self.conf.sdn.domain))
        mocked_request.assert_called_once_with(sdn_const.POST,
                                               expected_url,
                                               None)

    @mock.patch('networking_mlnx.plugins.ml2.drivers.'
                'sdn.client.SdnRestClient.request')
    def test_delete(self, mocked_request):
        self.client.delete(self.url, self.data)
        expected_url = '/'.join((self.conf.sdn.url,
                                 self.conf.sdn.domain,
                                 self.url))
        mocked_request.assert_called_once_with(sdn_const.DELETE,
                                               expected_url,
                                               self.data)

        mocked_request.reset_mock()
        self.client.delete(self.url)
        mocked_request.assert_called_once_with(sdn_const.DELETE,
                                               expected_url,
                                               None)

        mocked_request.reset_mock()
        self.client.delete()
        expected_url = '/'.join((self.conf.sdn.url, self.conf.sdn.domain))
        mocked_request.assert_called_once_with(sdn_const.DELETE,
                                               expected_url,
                                               None)

    @mock.patch('networking_mlnx.plugins.ml2.drivers.'
                'sdn.client.SdnRestClient._get_session',
                return_value=mock.Mock())
    def test_request_bad_data(self, mocked_get_session):
        # non serialized json data
        data = self
        self.assertRaises(ValueError,
                          self.client.request,
                          sdn_const.DELETE, '', data)