"Fossies" - the Fresh Open Source Software Archive

Member "cinder-14.0.2/cinder/tests/unit/scheduler/test_base_filter.py" (4 Oct 2019, 5766 Bytes) of package /linux/misc/openstack/cinder-14.0.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # Copyright (c) 2013 OpenStack Foundation.
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License");
    4 # you may not use this file except in compliance with the License.
    5 # You may obtain 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,
   11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
   12 # implied.
   13 # See the License for the specific language governing permissions and
   14 # limitations under the License.
   15 
   16 import mock
   17 
   18 from cinder.scheduler import base_filter
   19 from cinder import test
   20 
   21 
   22 class TestBaseFilter(test.TestCase):
   23 
   24     def setUp(self):
   25         super(TestBaseFilter, self).setUp()
   26         self.filter = base_filter.BaseFilter()
   27 
   28     def test_filter_one_is_called(self):
   29         filters = [1, 2, 3, 4]
   30         filter_properties = {'x': 'y'}
   31 
   32         self.filter._filter_one = mock.Mock()
   33         self.filter._filter_one.side_effect = [False, True, True, False]
   34         calls = [mock.call(i, filter_properties) for i in filters]
   35 
   36         result = list(self.filter.filter_all(filters, filter_properties))
   37         self.assertEqual([2, 3], result)
   38         self.filter._filter_one.assert_has_calls(calls)
   39 
   40 
   41 class FakeExtension(object):
   42 
   43     def __init__(self, plugin):
   44         self.plugin = plugin
   45 
   46 
   47 class BaseFakeFilter(base_filter.BaseFilter):
   48     pass
   49 
   50 
   51 class FakeFilter1(BaseFakeFilter):
   52     """Derives from BaseFakeFilter and has a fake entry point defined.
   53 
   54     Entry point is returned by fake ExtensionManager.
   55     Should be included in the output of all_classes.
   56     """
   57     pass
   58 
   59 
   60 class FakeFilter2(BaseFakeFilter):
   61     """Derives from BaseFakeFilter but has no entry point.
   62 
   63     Should be not included in all_classes.
   64     """
   65     pass
   66 
   67 
   68 class FakeFilter3(base_filter.BaseFilter):
   69     """Does not derive from BaseFakeFilter.
   70 
   71     Should not be included.
   72     """
   73     pass
   74 
   75 
   76 class FakeFilter4(BaseFakeFilter):
   77     """Derives from BaseFakeFilter and has an entry point.
   78 
   79     Should be included.
   80     """
   81     pass
   82 
   83 
   84 class FakeFilter5(BaseFakeFilter):
   85     """Derives from BaseFakeFilter but has no entry point.
   86 
   87     Should not be included.
   88     """
   89     run_filter_once_per_request = True
   90     pass
   91 
   92 
   93 class FilterA(base_filter.BaseFilter):
   94     def filter_all(self, list_objs, filter_properties):
   95         # return all but the first object
   96         return list_objs[1:]
   97 
   98 
   99 class FilterB(base_filter.BaseFilter):
  100     def filter_all(self, list_objs, filter_properties):
  101         # return an empty list
  102         return None
  103 
  104 
  105 class FakeExtensionManager(list):
  106 
  107     def __init__(self, namespace):
  108         classes = [FakeFilter1, FakeFilter3, FakeFilter4]
  109         exts = map(FakeExtension, classes)
  110         super(FakeExtensionManager, self).__init__(exts)
  111         self.namespace = namespace
  112 
  113 
  114 class TestBaseFilterHandler(test.TestCase):
  115 
  116     def setUp(self):
  117         super(TestBaseFilterHandler, self).setUp()
  118         self.mock_object(base_filter.base_handler.extension,
  119                          'ExtensionManager', FakeExtensionManager)
  120         self.handler = base_filter.BaseFilterHandler(BaseFakeFilter,
  121                                                      'fake_filters')
  122 
  123     def test_get_all_classes(self):
  124         # In order for a FakeFilter to be returned by get_all_classes, it has
  125         # to comply with these rules:
  126         # * It must be derived from BaseFakeFilter
  127         #   AND
  128         # * It must have a python entrypoint assigned (returned by
  129         #   FakeExtensionManager)
  130         expected = [FakeFilter1, FakeFilter4]
  131         result = self.handler.get_all_classes()
  132         self.assertEqual(expected, result)
  133 
  134     def _get_filtered_objects(self, filter_classes, index=0):
  135         filter_objs_initial = [1, 2, 3, 4]
  136         filter_properties = {'x': 'y'}
  137         return self.handler.get_filtered_objects(filter_classes,
  138                                                  filter_objs_initial,
  139                                                  filter_properties,
  140                                                  index)
  141 
  142     @mock.patch.object(FakeFilter4, 'filter_all')
  143     @mock.patch.object(FakeFilter3, 'filter_all', return_value=None)
  144     def test_get_filtered_objects_return_none(self, fake3_filter_all,
  145                                               fake4_filter_all):
  146         filter_classes = [FakeFilter1, FakeFilter2, FakeFilter3, FakeFilter4]
  147         result = self._get_filtered_objects(filter_classes)
  148         self.assertIsNone(result)
  149         self.assertFalse(fake4_filter_all.called)
  150 
  151     def test_get_filtered_objects(self):
  152         filter_objs_expected = [1, 2, 3, 4]
  153         filter_classes = [FakeFilter1, FakeFilter2, FakeFilter3, FakeFilter4]
  154         result = self._get_filtered_objects(filter_classes)
  155         self.assertEqual(filter_objs_expected, result)
  156 
  157     def test_get_filtered_objects_with_filter_run_once(self):
  158         filter_objs_expected = [1, 2, 3, 4]
  159         filter_classes = [FakeFilter5]
  160 
  161         with mock.patch.object(FakeFilter5, 'filter_all',
  162                                return_value=filter_objs_expected
  163                                ) as fake5_filter_all:
  164             result = self._get_filtered_objects(filter_classes)
  165             self.assertEqual(filter_objs_expected, result)
  166             self.assertEqual(1, fake5_filter_all.call_count)
  167 
  168             result = self._get_filtered_objects(filter_classes, index=1)
  169             self.assertEqual(filter_objs_expected, result)
  170             self.assertEqual(1, fake5_filter_all.call_count)
  171 
  172             result = self._get_filtered_objects(filter_classes, index=2)
  173             self.assertEqual(filter_objs_expected, result)
  174             self.assertEqual(1, fake5_filter_all.call_count)