summaryrefslogtreecommitdiffstats
path: root/cyborg_enhancement/mitaka_version/cyborg/cyborg/tests/unit/fake_physical_function.py
blob: 4a2bbb7c5beef6ab9eec428fb9bc4663b990a5ee (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
# Copyright 2018 Huawei Technologies Co.,LTD.
#
#    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.

import datetime

from oslo_serialization import jsonutils
from oslo_utils import uuidutils

from cyborg import objects
from cyborg.objects import fields
from cyborg.objects import physical_function


def fake_db_physical_function(**updates):
    root_uuid = uuidutils.generate_uuid()
    db_physical_function = {
        'id': 1,
        'deleted': False,
        'uuid': root_uuid,
        'name': 'dp_name',
        'parent_uuid': None,
        'root_uuid': root_uuid,
        'pcie_address': '00:7f:0b.2',
        'host': 'host_name',
        'board': 'KU115',
        'vendor': 'Xilinx',
        'version': '1.0',
        'type': 'pf',
        'assignable': True,
        'instance_uuid': None,
        'availability': 'Available',
        'accelerator_id': 1
        }

    for name, field in physical_function.PhysicalFunction.fields.items():
        if name in db_physical_function:
            continue
        if field.nullable:
            db_physical_function[name] = None
        elif field.default != fields.UnspecifiedDefault:
            db_physical_function[name] = field.default
        else:
            raise Exception('fake_db_physical_function needs help with %s'
                            % name)

    if updates:
        db_physical_function.update(updates)

    return db_physical_function


def fake_physical_function_obj(context, obj_pf_class=None, **updates):
    if obj_pf_class is None:
        obj_pf_class = objects.VirtualFunction
    expected_attrs = updates.pop('expected_attrs', None)
    pf = obj_pf_class._from_db_object(context,
                                      obj_pf_class(),
                                      fake_db_physical_function(**updates),
                                      expected_attrs=expected_attrs)
    pf.obj_reset_changes()
    return pf