string: strerror_r() for windows.
[sliver-openvswitch.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 def xapi_local():
18     return Session()
19
20
21 class Session(object):
22     def __init__(self):
23         self.xenapi = XenAPI()
24
25
26 class Failure(Exception):
27     pass
28
29
30 class XenAPI(object):
31     def __init__(self):
32         self.network = Network()
33         self.pool = Pool()
34         self.VIF = VIF()
35         self.VM = VM()
36
37     def login_with_password(self, unused_username, unused_password):
38         pass
39
40
41 class RecordRef(object):
42     def __init__(self, attrs):
43         self.attrs = attrs
44
45
46 class Table(object):
47     def __init__(self, records):
48         self.records = records
49
50     def get_all(self):
51         return [RecordRef(rec) for rec in self.records]
52
53     def get_all_records_where(self, condition):
54         k, v = re.match(r'field "([^"]*)"="([^"]*)"$', condition).groups()
55         d = {}
56
57         # I'm sure that the keys used in the dictionary below are wrong
58         # but I can't find any documentation on get_all_records_where
59         # and this satisfies the current test case.
60         i = 0
61         for rec in self.records:
62             if rec[k] == v:
63                 d[i] = rec
64                 i += 1
65         return d
66
67     def get_by_uuid(self, uuid):
68         recs = [rec for rec in self.records if rec["uuid"] == uuid]
69         if len(recs) != 1:
70             raise Failure("No record with UUID %s" % uuid)
71         return RecordRef(recs[0])
72
73     def get_record(self, record_ref):
74         return record_ref.attrs
75
76
77 class Network(Table):
78     __records = ({"uuid": "9b66c68b-a74e-4d34-89a5-20a8ab352d1e",
79                   "bridge": "xenbr0",
80                   "other_config":
81                       {"vswitch-controller-fail-mode": "secure",
82                        "nicira-bridge-id": "custom bridge ID"}},
83                  {"uuid": "e1c9019d-375b-45ac-a441-0255dd2247de",
84                   "bridge": "xenbr1",
85                   "other_config":
86                       {"vswitch-disable-in-band": "true"}})
87
88     def __init__(self):
89         Table.__init__(self, Network.__records)
90
91
92 class Pool(Table):
93     __records = ({"uuid": "7a793edf-e5f4-4994-a0f9-cee784c0cda3",
94                   "other_config":
95                       {"vswitch-controller-fail-mode": "secure"}},)
96
97     def __init__(self):
98         Table.__init__(self, Pool.__records)
99
100 class VIF(Table):
101     __records = ({"uuid": "6ab1b260-398e-49ba-827b-c7696108964c",
102                   "other_config":
103                       {"nicira-iface-id": "custom iface ID"}},)
104
105     def __init__(self):
106         Table.__init__(self, VIF.__records)
107
108 class VM(Table):
109     __records = ({"uuid": "fcb8a3f6-dc04-41d2-8b8a-55afd2b755b8",
110                   "other_config":
111                       {"nicira-vm-id": "custom vm ID"}},)
112
113     def __init__(self):
114         Table.__init__(self, VM.__records)