cinder/cinder/tests/unit/scheduler/test_stochastic_weight_handler.py
Gorka Eguileor ef44589948 Simplify calls to mock_object
mock_object method in TestCase class now accepts keyword arguments that
will be properly handled when no new object is provided, so we can now
simplify calls in our tests.

This patch changes calls like
    self.mock_object(os.path, 'exists', mock.Mock(return_value=True))
into
    self.mock_object(os.path, 'exists', return_value=True)

Change-Id: I904ab4956a081c0292e2c982320e5005b055d3d5
2016-12-13 18:37:26 +01:00

64 lines
1.9 KiB
Python

#
# 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.
"""
Tests for stochastic weight handler
"""
import ddt
import random
from cinder.scheduler import base_weight
from cinder.scheduler.weights.stochastic import StochasticHostWeightHandler
from cinder import test
@ddt.ddt
class StochasticWeightHandlerTestCase(test.TestCase):
"""Test case for StochasticHostWeightHandler."""
@ddt.data(
(0.0, 'A'),
(0.1, 'A'),
(0.2, 'B'),
(0.3, 'B'),
(0.4, 'B'),
(0.5, 'B'),
(0.6, 'B'),
(0.7, 'C'),
(0.8, 'C'),
(0.9, 'C'),
)
@ddt.unpack
def test_get_weighed_objects_correct(self, rand_value, expected_obj):
self.mock_object(random,
'random',
return_value=rand_value)
class MapWeigher(base_weight.BaseWeigher):
minval = 0
maxval = 100
def _weigh_object(self, obj, weight_map):
return weight_map[obj]
weight_map = {'A': 1, 'B': 3, 'C': 2}
objs = sorted(weight_map.keys())
weigher_classes = [MapWeigher]
handler = StochasticHostWeightHandler('fake_namespace')
weighted_objs = handler.get_weighed_objects(weigher_classes,
objs,
weight_map)
winner = weighted_objs[0].obj
self.assertEqual(expected_obj, winner)