Coverage for manila/tests/scheduler/weighers/test_host_affinity.py: 100%
58 statements
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
1# Copyright 2020 NetApp, Inc.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
16"""
17Tests for Host Affinity Weigher.
18"""
20from unittest import mock
22from manila.common import constants
23from manila.db import api as db_api
24from manila.scheduler.weighers import host_affinity
25from manila import test
26from manila.tests import db_utils
27from manila.tests.scheduler import fakes
30class HostAffinityWeigherTestCase(test.TestCase):
31 def setUp(self):
32 super(HostAffinityWeigherTestCase, self).setUp()
33 self.weigher = host_affinity.HostAffinityWeigher()
35 @staticmethod
36 def _create_weight_properties(snapshot_id=None,
37 snapshot_host=None,
38 availability_zone_id=None):
39 return {
40 'request_spec': {
41 'snapshot_id': snapshot_id,
42 'snapshot_host': snapshot_host,
43 },
44 'availability_zone_id': availability_zone_id,
45 }
47 def test_without_snapshot_id(self):
48 host_state = fakes.FakeHostState('host1', {
49 'host': 'host1@AAA#pool2',
50 })
51 weight_properties = self._create_weight_properties(
52 snapshot_host='fake_snapshot_host')
54 weight = self.weigher._weigh_object(host_state, weight_properties)
55 self.assertEqual(0, weight)
57 def test_without_snapshot_host(self):
58 host_state = fakes.FakeHostState('host1', {
59 'host': 'host1@AAA#pool2',
60 })
61 weight_properties = self._create_weight_properties(
62 snapshot_id='fake_snapshot_id')
64 weight = self.weigher._weigh_object(host_state, weight_properties)
65 self.assertEqual(0, weight)
67 def test_same_backend_and_pool(self):
68 share = db_utils.create_share(host="host1@AAA#pool1",
69 status=constants.STATUS_AVAILABLE)
70 snapshot = db_utils.create_snapshot(share_id=share['id'])
71 self.mock_object(db_api, 'share_snapshot_get',
72 mock.Mock(return_value=snapshot))
74 host_state = fakes.FakeHostState('host1@AAA#pool1', {})
75 weight_properties = self._create_weight_properties(
76 snapshot_id=snapshot['id'], snapshot_host=share['host'])
78 weight = self.weigher._weigh_object(host_state, weight_properties)
79 self.assertEqual(100, weight)
81 def test_same_backend_different_pool(self):
82 share = db_utils.create_share(host="host1@AAA#pool1",
83 status=constants.STATUS_AVAILABLE)
84 snapshot = db_utils.create_snapshot(share_id=share['id'])
85 self.mock_object(db_api, 'share_snapshot_get',
86 mock.Mock(return_value=snapshot))
88 host_state = fakes.FakeHostState('host1@AAA#pool2', {})
89 weight_properties = self._create_weight_properties(
90 snapshot_id=snapshot['id'], snapshot_host=share['host'])
92 weight = self.weigher._weigh_object(host_state, weight_properties)
93 self.assertEqual(75, weight)
95 def test_different_backend_same_availability_zone(self):
96 share = db_utils.create_share(
97 host="host1@AAA#pool1", status=constants.STATUS_AVAILABLE,
98 availability_zone=fakes.FAKE_AZ_1['name'])
99 snapshot = db_utils.create_snapshot(share_id=share['id'])
101 self.mock_object(db_api, 'share_snapshot_get',
102 mock.Mock(return_value=snapshot))
103 self.mock_object(db_api, 'availability_zone_get',
104 mock.Mock(return_value=type(
105 'FakeAZ', (object, ), {
106 'id': fakes.FAKE_AZ_1['id'],
107 'name': fakes.FAKE_AZ_1['name'],
108 })))
109 host_state = fakes.FakeHostState('host2@BBB#pool1', {})
110 weight_properties = self._create_weight_properties(
111 snapshot_id=snapshot['id'], snapshot_host=share['host'],
112 availability_zone_id='zone1')
114 weight = self.weigher._weigh_object(host_state, weight_properties)
115 self.assertEqual(50, weight)
117 def test_different_backend_and_availability_zone(self):
118 share = db_utils.create_share(
119 host="host1@AAA#pool1", status=constants.STATUS_AVAILABLE,
120 availability_zone=fakes.FAKE_AZ_1['name'])
121 snapshot = db_utils.create_snapshot(share_id=share['id'])
123 self.mock_object(db_api, 'share_snapshot_get',
124 mock.Mock(return_value=snapshot))
125 self.mock_object(db_api, 'availability_zone_get',
126 mock.Mock(return_value=type(
127 'FakeAZ', (object,), {
128 'id': fakes.FAKE_AZ_2['id'],
129 'name': fakes.FAKE_AZ_2['name'],
130 })))
131 host_state = fakes.FakeHostState('host2@BBB#pool1', {})
132 weight_properties = self._create_weight_properties(
133 snapshot_id=snapshot['id'], snapshot_host=share['host'],
134 availability_zone_id='zone1'
135 )
137 weight = self.weigher._weigh_object(host_state, weight_properties)
138 self.assertEqual(25, weight)