This file is indexed.

/usr/lib/python3/dist-packages/vmware_nsxlib/tests/unit/v3/test_ns_group_manager.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
# Copyright (c) 2015 VMware, Inc.
# All Rights Reserved.
#
#    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 vmware_nsxlib.tests.unit.v3 import nsxlib_testcase
from vmware_nsxlib.v3 import exceptions as nsxlib_exc
from vmware_nsxlib.v3 import ns_group_manager
from vmware_nsxlib.v3 import nsx_constants as consts


# Pool of fake ns-groups uuids
NSG_IDS = ['11111111-1111-1111-1111-111111111111',
           '22222222-2222-2222-2222-222222222222',
           '33333333-3333-3333-3333-333333333333',
           '44444444-4444-4444-4444-444444444444',
           '55555555-5555-5555-5555-555555555555']


def _mock_create_and_list_nsgroups(test_method):
    nsgroups = []

    def _create_nsgroup_mock(name, desc, tags, membership_criteria=None):
        nsgroup = {'id': NSG_IDS[len(nsgroups)],
                   'display_name': name,
                   'description': desc,
                   'tags': tags}
        nsgroups.append(nsgroup)
        return nsgroup

    def wrap(*args, **kwargs):
        with mock.patch(
            'vmware_nsxlib.v3.security.NsxLibNsGroup.create'
        ) as create_nsgroup_mock:
            create_nsgroup_mock.side_effect = _create_nsgroup_mock
            with mock.patch(
                "vmware_nsxlib.v3.security.NsxLibNsGroup.list"
            ) as list_nsgroups_mock:
                list_nsgroups_mock.side_effect = lambda: nsgroups
                test_method(*args, **kwargs)
    return wrap


class TestNSGroupManager(nsxlib_testcase.NsxLibTestCase):
    """Tests for vmware_nsxlib.v3.ns_group_manager.NSGroupManager."""

    @_mock_create_and_list_nsgroups
    def test_first_initialization(self):
        size = 5
        cont_manager = ns_group_manager.NSGroupManager(self.nsxlib, size)
        nested_groups = cont_manager.nested_groups
        self.assertEqual({i: NSG_IDS[i] for i in range(size)},
                         nested_groups)

    @_mock_create_and_list_nsgroups
    def test_reconfigure_number_of_nested_groups(self):
        # We need to test that when changing the number of nested groups then
        # the NSGroupManager picks the ones which were previously created
        # and create the ones which are missing, which also verifies that it
        # also recognizes existing nested groups.

        size = 2
        # Creates 2 nested groups.
        ns_group_manager.NSGroupManager(self.nsxlib, size)

        size = 5
        # Creates another 3 nested groups.
        nested_groups = ns_group_manager.NSGroupManager(
            self.nsxlib, size).nested_groups
        self.assertEqual({i: NSG_IDS[i] for i in range(size)},
                         nested_groups)

    @_mock_create_and_list_nsgroups
    @mock.patch('vmware_nsxlib.v3.security.NsxLibNsGroup.remove_member')
    @mock.patch('vmware_nsxlib.v3.security.NsxLibNsGroup.add_members')
    def test_add_and_remove_nsgroups(self,
                                     add_member_mock,
                                     remove_member_mock):
        # We verify that when adding a new nsgroup the properly placed
        # according to its id and the number of nested groups.

        size = 5
        cont_manager = ns_group_manager.NSGroupManager(self.nsxlib, size)
        nsgroup_id = 'nsgroup_id'

        with mock.patch.object(cont_manager, '_hash_uuid', return_value=7):
            cont_manager.add_nsgroup(nsgroup_id)
            cont_manager.remove_nsgroup(nsgroup_id)

        # There are 5 nested groups, the hash function will return 7, therefore
        # we expect that the nsgroup will be placed in the 3rd group.
        add_member_mock.assert_called_once_with(
            NSG_IDS[2], consts.NSGROUP, [nsgroup_id])
        remove_member_mock.assert_called_once_with(
            NSG_IDS[2], consts.NSGROUP, nsgroup_id,
            verify=True)

    @_mock_create_and_list_nsgroups
    @mock.patch('vmware_nsxlib.v3.security.NsxLibNsGroup.remove_member')
    @mock.patch('vmware_nsxlib.v3.security.NsxLibNsGroup.add_members')
    def test_when_nested_group_is_full(self,
                                       add_member_mock,
                                       remove_member_mock):

        def _add_member_mock(nsgroup, target_type, target_id):
            if nsgroup == NSG_IDS[2]:
                raise nsxlib_exc.NSGroupIsFull(nsgroup_id=nsgroup)

        def _remove_member_mock(nsgroup, target_type, target_id, verify=False):
            if nsgroup == NSG_IDS[2]:
                raise nsxlib_exc.NSGroupMemberNotFound(nsgroup_id=nsgroup,
                                                       member_id=target_id)

        add_member_mock.side_effect = _add_member_mock
        remove_member_mock.side_effect = _remove_member_mock

        size = 5
        cont_manager = ns_group_manager.NSGroupManager(self.nsxlib, size)
        nsgroup_id = 'nsgroup_id'

        with mock.patch.object(cont_manager, '_hash_uuid', return_value=7):
            cont_manager.add_nsgroup(nsgroup_id)
            cont_manager.remove_nsgroup(nsgroup_id)

        # Trying to add nsgroup to the nested group at index 2 will raise
        # NSGroupIsFull exception, we expect that the nsgroup will be added to
        # the nested group at index 3.
        calls = [mock.call(NSG_IDS[2],
                           consts.NSGROUP, [nsgroup_id]),
                 mock.call(NSG_IDS[3],
                           consts.NSGROUP, [nsgroup_id])]
        add_member_mock.assert_has_calls(calls)

        # Since the nsgroup was added to the nested group at index 3, it will
        # fail to remove it from the group at index 2, and then will try to
        # remove it from the group at index 3.
        calls = [
            mock.call(
                NSG_IDS[2], consts.NSGROUP,
                nsgroup_id, verify=True),
            mock.call(
                NSG_IDS[3], consts.NSGROUP,
                nsgroup_id, verify=True)]
        remove_member_mock.assert_has_calls(calls)

    @_mock_create_and_list_nsgroups
    @mock.patch('vmware_nsxlib.v3.security.NsxLibNsGroup.remove_member')
    @mock.patch('vmware_nsxlib.v3.security.NsxLibNsGroup.add_members')
    def test_initialize_with_absent_nested_groups(self,
                                                  add_member_mock,
                                                  remove_member_mock):
        size = 3
        cont_manager = ns_group_manager.NSGroupManager(self.nsxlib, size)
        # list_nsgroups will return nested group 1 and 3, but not group 2.
        nsgroups = cont_manager.nsxlib_nsgroup.list()
        with mock.patch("vmware_nsxlib.v3.security.NsxLibNsGroup.list",
                        side_effect=lambda: nsgroups[::2]):
            # invoking the initialization process again, it should process
            # groups 1 and 3 and create group 2.
            cont_manager = ns_group_manager.NSGroupManager(self.nsxlib, size)
            self.assertEqual({0: NSG_IDS[0],
                              1: NSG_IDS[3],
                              2: NSG_IDS[2]},
                             cont_manager.nested_groups)

    @_mock_create_and_list_nsgroups
    def test_suggest_nested_group(self):
        size = 5
        cont_manager = ns_group_manager.NSGroupManager(self.nsxlib, size)
        # We expect that the first suggested index is 2
        expected_suggested_groups = NSG_IDS[2:5] + NSG_IDS[:2]
        suggest_group = lambda: cont_manager._suggest_nested_group('fake-id')
        with mock.patch.object(cont_manager, '_hash_uuid', return_value=7):
            for i, suggested in enumerate(suggest_group()):
                self.assertEqual(expected_suggested_groups[i], suggested)