Coverage for manila/tests/scheduler/filters/test_affinity.py: 97%

56 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2026-02-18 22:19 +0000

1# Copyright (c) 2021 SAP. 

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. 

15 

16import ddt 

17from unittest import mock 

18 

19from manila import exception 

20from manila.scheduler.filters import affinity 

21from manila import test 

22from manila.tests.scheduler import fakes 

23 

24 

25fake_hosts = [ 

26 fakes.FakeHostState('host1', {}), 

27 fakes.FakeHostState('host2', {}), 

28 fakes.FakeHostState('host3', {}), 

29 ] 

30 

31fake_shares_1 = { 

32 'abb6e0ac-7c3e-4ce0-8a69-5a166d246882': { 

33 'instances': [ 

34 {'host': fake_hosts[0].host} 

35 ] 

36 }, 

37 '4de0cc74-450c-4468-8159-52128cf03407': { 

38 'instances': [ 

39 {'host': fake_hosts[0].host} 

40 ] 

41 }, 

42 } 

43 

44fake_shares_2 = { 

45 'c920fb61-e250-4c3c-a25d-1fdd9ca7cbc3': { 

46 'instances': [ 

47 {'host': fake_hosts[1].host} 

48 ] 

49 }, 

50 } 

51 

52fake_shares_3 = { 

53 '3923bebf-9825-4a66-971e-6092a9fe2dbb': { 

54 'instances': [ 

55 {'host': fake_hosts[2].host} 

56 ] 

57 }, 

58 } 

59 

60 

61@ddt.ddt 

62class AffinityFilterTestCase(test.TestCase): 

63 """Test case for AffinityFilter.""" 

64 

65 def setUp(self): 

66 super(AffinityFilterTestCase, self).setUp() 

67 self.filter = affinity.AffinityFilter() 

68 self.anti_filter = affinity.AntiAffinityFilter() 

69 

70 def _make_filter_hints(self, *hints): 

71 return { 

72 'context': None, 

73 'scheduler_hints': {'same_host': ','.join(list(hints))}, 

74 } 

75 

76 def _make_anti_filter_hints(self, *hints): 

77 return { 

78 'context': None, 

79 'scheduler_hints': {'different_host': ','.join(list(hints))}, 

80 } 

81 

82 def _fake_get(self, context, uuid): 

83 if uuid in fake_shares_1.keys(): 

84 return fake_shares_1[uuid] 

85 if uuid in fake_shares_2.keys(): 

86 return fake_shares_2[uuid] 

87 if uuid in fake_shares_3.keys(): 87 ↛ 88line 87 didn't jump to line 88 because the condition on line 87 was never true

88 return fake_shares_3[uuid] 

89 raise exception.ShareNotFound(uuid) 

90 

91 @ddt.data('b5c207da-ac0b-43b0-8691-c6c9e860199d') 

92 @mock.patch('manila.share.api.API.get') 

93 def test_affinity_share_not_found(self, unknown_id, mock_share_get): 

94 mock_share_get.side_effect = self._fake_get 

95 self.assertRaises(exception.ShareNotFound, 

96 self.filter._validate, 

97 self._make_filter_hints(unknown_id)) 

98 

99 @ddt.data( 

100 {'context': None}, 

101 {'context': None, 'scheduler_hints': None}, 

102 {'context': None, 'scheduler_hints': {}}, 

103 ) 

104 def test_affinity_scheduler_hint_not_set(self, hints): 

105 self.assertRaises(affinity.SchedulerHintsNotSet, 

106 self.filter._validate, hints) 

107 

108 @mock.patch('manila.share.api.API.get') 

109 def test_affinity_filter(self, mock_share_get): 

110 mock_share_get.side_effect = self._fake_get 

111 

112 share_ids = fake_shares_1.keys() 

113 hints = self._make_filter_hints(*share_ids) 

114 valid_hosts = self.filter.filter_all(fake_hosts, hints) 

115 valid_hosts = [h.host for h in valid_hosts] 

116 

117 self.assertIn('host1', valid_hosts) 

118 self.assertNotIn('host2', valid_hosts) 

119 self.assertNotIn('host3', valid_hosts) 

120 

121 @mock.patch('manila.share.api.API.get') 

122 def test_anti_affinity_filter(self, mock_share_get): 

123 mock_share_get.side_effect = self._fake_get 

124 

125 share_ids = fake_shares_2.keys() 

126 hints = self._make_anti_filter_hints(*share_ids) 

127 valid_hosts = self.anti_filter.filter_all(fake_hosts, hints) 

128 valid_hosts = [h.host for h in valid_hosts] 

129 

130 self.assertIn('host1', valid_hosts) 

131 self.assertIn('host3', valid_hosts) 

132 self.assertNotIn('host2', valid_hosts)