netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / tests / MockXenAPI.py
1 # Copyright (c) 2011, 2012 Nicira, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import re
16
17
18 def xapi_local():
19     return Session()
20
21
22 class Session(object):
23     def __init__(self):
24         self.xenapi = XenAPI()
25
26
27 class Failure(Exception):
28     pass
29
30
31 class XenAPI(object):
32     def __init__(self):
33         self.network = Network()
34         self.pool = Pool()
35         self.VIF = VIF()
36         self.VM = VM()
37
38     def login_with_password(self, unused_username, unused_password):
39         pass
40
41
42 class RecordRef(object):
43     def __init__(self, attrs):
44         self.attrs = attrs
45
46
47 class Table(object):
48     def __init__(self, records):
49         self.records = records
50
51     def get_all(self):
52         return [RecordRef(rec) for rec in self.records]
53
54     def get_all_records_where(self, condition):
55         k, v = re.match(r'field "([^"]*)"="([^"]*)"$', condition).groups()
56         d = {}
57
58         # I'm sure that the keys used in the dictionary below are wrong
59         # but I can't find any documentation on get_all_records_where
60         # and this satisfies the current test case.
61         i = 0
62         for rec in self.records:
63             if rec[k] == v:
64                 d[i] = rec
65                 i += 1
66         return d
67
68     def get_by_uuid(self, uuid):
69         recs = [rec for rec in self.records if rec["uuid"] == uuid]
70         if len(recs) != 1:
71             raise Failure("No record with UUID %s" % uuid)
72         return RecordRef(recs[0])
73
74     def get_record(self, record_ref):
75         return record_ref.attrs
76
77
78 class Network(Table):
79     __records = ({"uuid": "9b66c68b-a74e-4d34-89a5-20a8ab352d1e",
80                   "bridge": "xenbr0",
81                   "other_config":
82                       {"vswitch-controller-fail-mode": "secure",
83                        "nicira-bridge-id": "custom bridge ID"}},
84                  {"uuid": "e1c9019d-375b-45ac-a441-0255dd2247de",
85                   "bridge": "xenbr1",
86                   "other_config":
87                       {"vswitch-disable-in-band": "true"}})
88
89     def __init__(self):
90         Table.__init__(self, Network.__records)
91
92
93 class Pool(Table):
94     __records = ({"uuid": "7a793edf-e5f4-4994-a0f9-cee784c0cda3",
95                   "other_config":
96                       {"vswitch-controller-fail-mode": "secure"}},)
97
98     def __init__(self):
99         Table.__init__(self, Pool.__records)
100
101
102 class VIF(Table):
103     __records = ({"uuid": "6ab1b260-398e-49ba-827b-c7696108964c",
104                   "other_config":
105                       {"nicira-iface-id": "custom iface ID"}},)
106
107     def __init__(self):
108         Table.__init__(self, VIF.__records)
109
110
111 class VM(Table):
112     __records = ({"uuid": "fcb8a3f6-dc04-41d2-8b8a-55afd2b755b8",
113                   "other_config":
114                       {"nicira-vm-id": "custom vm ID"}},)
115
116     def __init__(self):
117         Table.__init__(self, VM.__records)