summaryrefslogtreecommitdiff
path: root/nova/tests/unit/scheduler/filters/test_pci_passthrough_filters.py
blob: 27d80b884e7e774984ce618ed650c76111aa6c94 (plain)
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
#    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.

from unittest import mock

from oslo_utils.fixture import uuidsentinel as uuids

from nova import objects
from nova.pci import stats
from nova.scheduler.filters import pci_passthrough_filter
from nova import test
from nova.tests.unit.scheduler import fakes


class TestPCIPassthroughFilter(test.NoDBTestCase):

    def setUp(self):
        super(TestPCIPassthroughFilter, self).setUp()
        self.filt_cls = pci_passthrough_filter.PciPassthroughFilter()

    def test_pci_passthrough_pass(self):
        pci_stats_mock = mock.MagicMock()
        pci_stats_mock.support_requests.return_value = True
        request = objects.InstancePCIRequest(count=1,
            spec=[{'vendor_id': '8086'}])
        requests = objects.InstancePCIRequests(requests=[request])
        spec_obj = objects.RequestSpec(pci_requests=requests)
        host = fakes.FakeHostState(
            "host1",
            "node1",
            attribute_dict={
                "pci_stats": pci_stats_mock,
                "allocation_candidates": [{"mappings": {}}],
            },
        )
        self.assertTrue(self.filt_cls.host_passes(host, spec_obj))
        pci_stats_mock.support_requests.assert_called_once_with(
            requests.requests, provider_mapping={})

    def test_pci_passthrough_fail(self):
        pci_stats_mock = mock.MagicMock()
        pci_stats_mock.support_requests.return_value = False
        request = objects.InstancePCIRequest(count=1,
            spec=[{'vendor_id': '8086'}])
        requests = objects.InstancePCIRequests(requests=[request])
        spec_obj = objects.RequestSpec(pci_requests=requests)
        host = fakes.FakeHostState(
            "host1",
            "node1",
            attribute_dict={
                "pci_stats": pci_stats_mock,
                "allocation_candidates": [{"mappings": {}}],
            },
        )
        self.assertFalse(self.filt_cls.host_passes(host, spec_obj))
        pci_stats_mock.support_requests.assert_called_once_with(
            requests.requests, provider_mapping={})

    def test_pci_passthrough_no_pci_request(self):
        spec_obj = objects.RequestSpec(pci_requests=None)
        host = fakes.FakeHostState('h1', 'n1', {})
        self.assertTrue(self.filt_cls.host_passes(host, spec_obj))

    def test_pci_passthrough_empty_pci_request_obj(self):
        requests = objects.InstancePCIRequests(requests=[])
        spec_obj = objects.RequestSpec(pci_requests=requests)
        host = fakes.FakeHostState('h1', 'n1', {})
        self.assertTrue(self.filt_cls.host_passes(host, spec_obj))

    def test_pci_passthrough_no_pci_stats(self):
        request = objects.InstancePCIRequest(count=1,
            spec=[{'vendor_id': '8086'}])
        requests = objects.InstancePCIRequests(requests=[request])
        spec_obj = objects.RequestSpec(pci_requests=requests)
        host = fakes.FakeHostState('host1', 'node1',
            attribute_dict={
                'pci_stats': stats.PciDeviceStats(objects.NUMATopology())})
        self.assertFalse(self.filt_cls.host_passes(host, spec_obj))

    def test_pci_passthrough_with_pci_stats_none(self):
        request = objects.InstancePCIRequest(count=1,
            spec=[{'vendor_id': '8086'}])
        requests = objects.InstancePCIRequests(requests=[request])
        spec_obj = objects.RequestSpec(pci_requests=requests)
        host = fakes.FakeHostState('host1', 'node1',
            attribute_dict={'pci_stats': None})
        self.assertFalse(self.filt_cls.host_passes(host, spec_obj))

    def test_filters_candidates(self):
        pci_stats_mock = mock.MagicMock()
        # simulate that only the second allocation candidate fits
        pci_stats_mock.support_requests.side_effect = [False, True, False]
        request = objects.InstancePCIRequest(
            count=1,
            spec=[{"vendor_id": "8086"}],
            request_id=uuids.req1,
        )
        requests = objects.InstancePCIRequests(requests=[request])
        spec_obj = objects.RequestSpec(pci_requests=requests)
        host = fakes.FakeHostState(
            "host1",
            "node1",
            attribute_dict={
                "pci_stats": pci_stats_mock,
                # simulate the placement returned 3 possible candidates
                "allocation_candidates": [
                    {"mappings": {f"{uuids.req1}-0": ["candidate_rp_1"]}},
                    {"mappings": {f"{uuids.req1}-0": ["candidate_rp_2"]}},
                    {"mappings": {f"{uuids.req1}-0": ["candidate_rp_3"]}},
                ],
            },
        )

        # run the filter and expect that it passes the host as there is at
        # least one viable candidate
        self.assertTrue(self.filt_cls.host_passes(host, spec_obj))

        # also assert that the filter checked all three candidates
        pci_stats_mock.support_requests.assert_has_calls(
            [
                mock.call(
                    requests.requests,
                    provider_mapping={f"{uuids.req1}-0": ["candidate_rp_1"]},
                ),
                mock.call(
                    requests.requests,
                    provider_mapping={f"{uuids.req1}-0": ["candidate_rp_2"]},
                ),
                mock.call(
                    requests.requests,
                    provider_mapping={f"{uuids.req1}-0": ["candidate_rp_3"]},
                ),
            ]
        )
        # and also it reduced the candidates in the host state to the only
        # matching one
        self.assertEqual(1, len(host.allocation_candidates))
        self.assertEqual(
            {"mappings": {f"{uuids.req1}-0": ["candidate_rp_2"]}},
            host.allocation_candidates[0],
        )

    def test_filter_fails_if_no_matching_candidate_left(self):
        pci_stats_mock = mock.MagicMock()
        # simulate that the only candidate we have does not match
        pci_stats_mock.support_requests.side_effect = [False]
        request = objects.InstancePCIRequest(
            count=1,
            spec=[{"vendor_id": "8086"}],
            request_id=uuids.req1,
        )
        requests = objects.InstancePCIRequests(requests=[request])
        spec_obj = objects.RequestSpec(pci_requests=requests)
        host = fakes.FakeHostState(
            "host1",
            "node1",
            attribute_dict={
                "pci_stats": pci_stats_mock,
                # simulate the placement returned 3 possible candidates
                "allocation_candidates": [
                    {"mappings": {f"{uuids.req1}-0": ["candidate_rp_1"]}},
                ],
            },
        )

        # run the filter and expect that it fails the host as there is no
        # viable candidate left
        self.assertFalse(self.filt_cls.host_passes(host, spec_obj))

        # also assert that the filter checked our candidate
        pci_stats_mock.support_requests.assert_called_once_with(
            requests.requests,
            provider_mapping={f"{uuids.req1}-0": ["candidate_rp_1"]},
        )
        # and also it made the candidates list empty in the host state
        self.assertEqual(0, len(host.allocation_candidates))