This file is indexed.

/usr/lib/python3/dist-packages/vmware_nsxlib/tests/unit/v3/test_qos_switching_profile.py is in python3-vmware-nsxlib 12.0.1-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Copyright (c) 2015 VMware, Inc.
#
# 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 copy

import mock
from oslo_log import log

from vmware_nsxlib.tests.unit.v3 import nsxlib_testcase
from vmware_nsxlib.tests.unit.v3 import test_constants
from vmware_nsxlib.v3 import nsx_constants

LOG = log.getLogger(__name__)


class NsxLibQosTestCase(nsxlib_testcase.NsxClientTestCase):

    def _body(self, qos_marking=None, dscp=None,
              description=test_constants.FAKE_NAME):
        body = {
            "resource_type": "QosSwitchingProfile",
            "tags": []
        }
        if qos_marking:
            body = self.nsxlib.qos_switching_profile._update_dscp_in_args(
                body, qos_marking, dscp)

        body["display_name"] = test_constants.FAKE_NAME
        body["description"] = description

        return body

    def _body_with_shaping(self, shaping_enabled=False,
                           burst_size=None,
                           peak_bandwidth=None,
                           average_bandwidth=None,
                           description=test_constants.FAKE_NAME,
                           qos_marking=None,
                           dscp=0, direction=nsx_constants.EGRESS,
                           body=None):
        if body is None:
            body = copy.deepcopy(test_constants.FAKE_QOS_PROFILE)
        body["display_name"] = test_constants.FAKE_NAME
        body["description"] = description

        resource_type = (nsx_constants.EGRESS_SHAPING
                         if direction == nsx_constants.EGRESS
                         else nsx_constants.INGRESS_SHAPING)
        for shaper in body["shaper_configuration"]:
            if shaper["resource_type"] == resource_type:
                shaper["enabled"] = shaping_enabled
                if burst_size:
                    shaper["burst_size_bytes"] = burst_size
                if peak_bandwidth:
                    shaper["peak_bandwidth_mbps"] = peak_bandwidth
                if average_bandwidth:
                    shaper["average_bandwidth_mbps"] = average_bandwidth
                break

        if qos_marking:
            body = self.nsxlib.qos_switching_profile._update_dscp_in_args(
                body, qos_marking, dscp)

        return body

    def test_create_qos_switching_profile(self):
        """Test creating a qos-switching profile

        returns the correct response
        """
        with mock.patch.object(self.nsxlib.client, 'create') as create:
            self.nsxlib.qos_switching_profile.create(
                tags=[],
                name=test_constants.FAKE_NAME,
                description=test_constants.FAKE_NAME)
            create.assert_called_with(
                'switching-profiles', self._body())

    def test_update_qos_switching_profile(self):
        """Test updating a qos-switching profile

        returns the correct response
        """
        original_profile = self._body()
        new_description = "Test"
        with mock.patch.object(self.nsxlib.client, 'get',
                               return_value=original_profile):
            with mock.patch.object(self.nsxlib.client, 'update') as update:

                # update the description of the profile
                self.nsxlib.qos_switching_profile.update(
                    test_constants.FAKE_QOS_PROFILE['id'],
                    tags=[],
                    description=new_description)
                update.assert_called_with(
                    'switching-profiles/%s'
                    % test_constants.FAKE_QOS_PROFILE['id'],
                    self._body(description=new_description),
                    headers=None)

    def _enable_qos_switching_profile_shaping(
        self, direction=nsx_constants.EGRESS, new_burst_size=100):
        """Test updating a qos-switching profile

        returns the correct response
        """
        original_burst = 10
        original_profile = self._body_with_shaping(direction=direction,
                                                   burst_size=original_burst)
        peak_bandwidth = 200
        average_bandwidth = 300
        qos_marking = "untrusted"
        dscp = 10

        with mock.patch.object(self.nsxlib.client, 'get',
                               return_value=original_profile):
            with mock.patch.object(self.nsxlib.client, 'update') as update:
                # update the bw shaping of the profile
                self.nsxlib.qos_switching_profile.update_shaping(
                    test_constants.FAKE_QOS_PROFILE['id'],
                    shaping_enabled=True,
                    burst_size=new_burst_size,
                    peak_bandwidth=peak_bandwidth,
                    average_bandwidth=average_bandwidth,
                    qos_marking=qos_marking,
                    dscp=dscp, direction=direction)

                actual_body = copy.deepcopy(update.call_args[0][1])
                actual_path = update.call_args[0][0]
                expected_path = ('switching-profiles/%s' %
                                 test_constants.FAKE_QOS_PROFILE['id'])
                expected_burst = (new_burst_size if new_burst_size is not None
                                  else original_burst)
                expected_body = self._body_with_shaping(
                    shaping_enabled=True,
                    burst_size=expected_burst,
                    peak_bandwidth=peak_bandwidth,
                    average_bandwidth=average_bandwidth,
                    qos_marking="untrusted", dscp=10,
                    direction=direction)
                self.assertEqual(expected_path, actual_path)
                self.assertEqual(expected_body, actual_body)

    def test_enable_qos_switching_profile_egress_shaping(self):
        self._enable_qos_switching_profile_shaping(
            direction=nsx_constants.EGRESS)

    def test_enable_qos_switching_profile_ingress_shaping(self):
        self._enable_qos_switching_profile_shaping(
            direction=nsx_constants.INGRESS)

    def test_update_qos_switching_profile_with_burst_size(self):
        self._enable_qos_switching_profile_shaping(
            direction=nsx_constants.EGRESS, new_burst_size=101)

    def test_update_qos_switching_profile_without_burst_size(self):
        self._enable_qos_switching_profile_shaping(
            direction=nsx_constants.EGRESS, new_burst_size=None)

    def test_update_qos_switching_profile_zero_burst_size(self):
        self._enable_qos_switching_profile_shaping(
            direction=nsx_constants.EGRESS, new_burst_size=0)

    def _disable_qos_switching_profile_shaping(
        self, direction=nsx_constants.EGRESS):
        """Test updating a qos-switching profile.

        Returns the correct response
        """
        burst_size = 100
        peak_bandwidth = 200
        average_bandwidth = 300
        original_profile = self._body_with_shaping(
            shaping_enabled=True,
            burst_size=burst_size,
            peak_bandwidth=peak_bandwidth,
            average_bandwidth=average_bandwidth,
            qos_marking="untrusted",
            dscp=10, direction=direction)

        with mock.patch.object(self.nsxlib.client, 'get',
                               return_value=original_profile):
            with mock.patch.object(self.nsxlib.client, 'update') as update:
                # update the bw shaping of the profile
                self.nsxlib.qos_switching_profile.update_shaping(
                    test_constants.FAKE_QOS_PROFILE['id'],
                    shaping_enabled=False, qos_marking="trusted",
                    direction=direction)

                actual_body = copy.deepcopy(update.call_args[0][1])
                actual_path = update.call_args[0][0]
                expected_path = ('switching-profiles/%s' %
                                 test_constants.FAKE_QOS_PROFILE['id'])
                expected_body = self._body_with_shaping(qos_marking="trusted",
                                                        direction=direction)
                self.assertEqual(expected_path, actual_path)
                self.assertEqual(expected_body, actual_body)

    def test_disable_qos_switching_profile_egress_shaping(self):
        self._disable_qos_switching_profile_shaping(
            direction=nsx_constants.EGRESS)

    def test_disable_qos_switching_profile_ingress_shaping(self):
        self._disable_qos_switching_profile_shaping(
            direction=nsx_constants.INGRESS)

    def test_delete_qos_switching_profile(self):
        """Test deleting qos-switching-profile"""
        with mock.patch.object(self.nsxlib.client, 'delete') as delete:
            self.nsxlib.qos_switching_profile.delete(
                test_constants.FAKE_QOS_PROFILE['id'])
            delete.assert_called_with(
                'switching-profiles/%s'
                % test_constants.FAKE_QOS_PROFILE['id'])

    def test_qos_switching_profile_set_shaping(self):
        """Test updating a qos-switching profile

        returns the correct response
        """
        egress_peak_bandwidth = 200
        egress_average_bandwidth = 300
        egress_burst_size = 500
        ingress_peak_bandwidth = 100
        ingress_average_bandwidth = 400
        ingress_burst_size = 600
        qos_marking = "untrusted"
        dscp = 10

        original_profile = self._body_with_shaping()
        with mock.patch.object(self.nsxlib.client, 'get',
                               return_value=original_profile):
            with mock.patch.object(self.nsxlib.client, 'update') as update:
                # update the bw shaping of the profile
                self.nsxlib.qos_switching_profile.set_profile_shaping(
                    test_constants.FAKE_QOS_PROFILE['id'],
                    ingress_bw_enabled=True,
                    ingress_burst_size=ingress_burst_size,
                    ingress_peak_bandwidth=ingress_peak_bandwidth,
                    ingress_average_bandwidth=ingress_average_bandwidth,
                    egress_bw_enabled=True,
                    egress_burst_size=egress_burst_size,
                    egress_peak_bandwidth=egress_peak_bandwidth,
                    egress_average_bandwidth=egress_average_bandwidth,
                    qos_marking=qos_marking,
                    dscp=dscp)

                actual_body = copy.deepcopy(update.call_args[0][1])
                actual_path = update.call_args[0][0]
                expected_path = ('switching-profiles/%s' %
                                 test_constants.FAKE_QOS_PROFILE['id'])
                expected_body = self._body_with_shaping(
                    shaping_enabled=True,
                    burst_size=egress_burst_size,
                    peak_bandwidth=egress_peak_bandwidth,
                    average_bandwidth=egress_average_bandwidth,
                    qos_marking="untrusted", dscp=10,
                    direction=nsx_constants.EGRESS)
                # Add the other direction to the body
                expected_body = self._body_with_shaping(
                    shaping_enabled=True,
                    burst_size=ingress_burst_size,
                    peak_bandwidth=ingress_peak_bandwidth,
                    average_bandwidth=ingress_average_bandwidth,
                    direction=nsx_constants.INGRESS,
                    body=expected_body)

                self.assertEqual(expected_path, actual_path)
                self.assertEqual(expected_body, actual_body)