summaryrefslogtreecommitdiff
path: root/nova/tests/unit/pci/test_device.py
blob: e0f3d5f46ac14c681402c44d5ff005c1095afa70 (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
# Copyright 2014 Intel Corporation
# All Rights Reserved.
#
#    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 nova import context
from nova import exception
from nova import objects
from nova.pci import device
from nova import test


dev_dict = {
    'created_at': None,
    'updated_at': None,
    'deleted_at': None,
    'deleted': None,
    'id': 1,
    'compute_node_id': 1,
    'address': 'a',
    'vendor_id': 'v',
    'product_id': 'p',
    'numa_node': 1,
    'dev_type': 't',
    'status': 'available',
    'dev_id': 'i',
    'label': 'l',
    'instance_uuid': None,
    'extra_info': '{}',
    'request_id': None,
    }


class PciDeviceTestCase(test.NoDBTestCase):
    def setUp(self):
        super(PciDeviceTestCase, self).setUp()
        self.ctxt = context.get_admin_context()
        self.inst = objects.Instance()
        self.inst.uuid = 'fake-inst-uuid'
        self.inst.pci_devices = objects.PciDeviceList()
        self.devobj = objects.PciDevice._from_db_object(
            self.ctxt,
            objects.PciDevice(),
            dev_dict)

    def test_claim_device(self):
        device.claim(self.devobj, self.inst)
        self.assertEqual(self.devobj.status, 'claimed')
        self.assertEqual(self.devobj.instance_uuid,
                         self.inst.uuid)
        self.assertEqual(len(self.inst.pci_devices), 0)

    def test_claim_device_fail(self):
        self.devobj.status = 'allocated'
        self.assertRaises(exception.PciDeviceInvalidStatus,
                          device.claim, self.devobj, self.inst)

    def test_allocate_device(self):
        device.claim(self.devobj, self.inst)
        device.allocate(self.devobj, self.inst)
        self.assertEqual(self.devobj.status, 'allocated')
        self.assertEqual(self.devobj.instance_uuid, 'fake-inst-uuid')
        self.assertEqual(len(self.inst.pci_devices), 1)
        self.assertEqual(self.inst.pci_devices[0].vendor_id,
                         'v')
        self.assertEqual(self.inst.pci_devices[0].status,
                         'allocated')

    def test_allocacte_device_fail_status(self):
        self.devobj.status = 'removed'
        self.assertRaises(exception.PciDeviceInvalidStatus,
                          device.allocate,
                          self.devobj,
                          self.inst)

    def test_allocacte_device_fail_owner(self):
        inst_2 = objects.Instance()
        inst_2.uuid = 'fake-inst-uuid-2'
        device.claim(self.devobj, self.inst)
        self.assertRaises(exception.PciDeviceInvalidOwner,
                          device.allocate,
                          self.devobj, inst_2)

    def test_free_claimed_device(self):
        device.claim(self.devobj, self.inst)
        device.free(self.devobj, self.inst)
        self.assertEqual(self.devobj.status, 'available')
        self.assertIsNone(self.devobj.instance_uuid)

    def test_free_allocated_device(self):
        device.claim(self.devobj, self.inst)
        device.allocate(self.devobj, self.inst)
        self.assertEqual(len(self.inst.pci_devices), 1)
        device.free(self.devobj, self.inst)
        self.assertEqual(len(self.inst.pci_devices), 0)
        self.assertEqual(self.devobj.status, 'available')
        self.assertIsNone(self.devobj.instance_uuid)

    def test_free_device_fail(self):
        self.devobj.status = 'removed'
        self.assertRaises(exception.PciDeviceInvalidStatus,
                          device.free, self.devobj)

    def test_remove_device(self):
        device.remove(self.devobj)
        self.assertEqual(self.devobj.status, 'removed')
        self.assertIsNone(self.devobj.instance_uuid)

    def test_remove_device_fail(self):
        device.claim(self.devobj, self.inst)
        self.assertRaises(exception.PciDeviceInvalidStatus,
                          device.remove, self.devobj)