4 # Copyright (C) 2010 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Script for unittesting the RAPI rlib2 module
30 from ganeti import constants
31 from ganeti import opcodes
32 from ganeti import compat
33 from ganeti import http
35 from ganeti.rapi import rlib2
40 class TestParseInstanceCreateRequestVersion1(testutils.GanetiTestCase):
42 testutils.GanetiTestCase.setUp(self)
44 self.Parse = rlib2._ParseInstanceCreateRequestVersion1
52 [{"size": 5, }, {"size": 100, }],
55 [{"size": 123, "mode": constants.DISK_RDWR, }],
57 # With unknown setting
58 [{"size": 123, "unknown": 999 }],
70 { "ip": "192.0.2.6", "mode": constants.NIC_MODE_ROUTED,
71 "mac": "01:23:45:67:68:9A",
73 { "mode": constants.NIC_MODE_BRIDGED, "link": "n0", "bridge": "br1", },
77 [{ "unknown": 999, }, { "foobar": "Hello World", }],
83 { constants.BE_VCPUS: 2, },
84 { constants.BE_MEMORY: 123, },
85 { constants.BE_VCPUS: 2,
86 constants.BE_MEMORY: 1024,
87 constants.BE_AUTO_BALANCE: True, }
92 { constants.HV_BOOT_ORDER: "anc", },
93 { constants.HV_KERNEL_PATH: "/boot/fookernel",
94 constants.HV_ROOT_PATH: "/dev/hda1", },
97 for mode in [constants.INSTANCE_CREATE, constants.INSTANCE_IMPORT]:
98 for nics in nic_variants:
99 for disk_template in constants.DISK_TEMPLATES:
100 for disks in disk_variants:
101 for beparams in beparam_variants:
102 for hvparams in hvparam_variants:
104 "name": "inst1.example.com",
105 "hypervisor": constants.HT_FAKE,
109 "disk_template": disk_template,
112 if beparams is not None:
113 data["beparams"] = beparams
115 if hvparams is not None:
116 data["hvparams"] = hvparams
118 for dry_run in [False, True]:
119 op = self.Parse(data, dry_run)
120 self.assert_(isinstance(op, opcodes.OpCreateInstance))
121 self.assertEqual(op.mode, mode)
122 self.assertEqual(op.disk_template, disk_template)
123 self.assertEqual(op.dry_run, dry_run)
124 self.assertEqual(len(op.disks), len(disks))
125 self.assertEqual(len(op.nics), len(nics))
127 for opdisk, disk in zip(op.disks, disks):
128 for key in constants.IDISK_PARAMS:
129 self.assertEqual(opdisk.get(key), disk.get(key))
130 self.assertFalse("unknown" in opdisk)
132 for opnic, nic in zip(op.nics, nics):
133 for key in constants.INIC_PARAMS:
134 self.assertEqual(opnic.get(key), nic.get(key))
135 self.assertFalse("unknown" in opnic)
136 self.assertFalse("foobar" in opnic)
139 self.assertEqualValues(op.beparams, {})
141 self.assertEqualValues(op.beparams, beparams)
144 self.assertEqualValues(op.hvparams, {})
146 self.assertEqualValues(op.hvparams, hvparams)
148 def testErrors(self):
149 # Test all required fields
151 "name": "inst1.example.com",
154 "mode": constants.INSTANCE_CREATE,
155 "disk_template": constants.DT_PLAIN
158 for name in reqfields.keys():
159 self.assertRaises(http.HttpBadRequest, self.Parse,
160 dict(i for i in reqfields.iteritems() if i[0] != name),
163 # Invalid disks and nics
164 for field in ["disks", "nics"]:
165 invalid_values = [None, 1, "", {}, [1, 2, 3], ["hda1", "hda2"]]
168 invalid_values.append([
171 { "mode": constants.DISK_RDWR, },
174 for invvalue in invalid_values:
175 data = reqfields.copy()
176 data[field] = invvalue
177 self.assertRaises(http.HttpBadRequest, self.Parse, data, False)
180 class TestParseExportInstanceRequest(testutils.GanetiTestCase):
182 testutils.GanetiTestCase.setUp(self)
184 self.Parse = rlib2._ParseExportInstanceRequest
189 "mode": constants.EXPORT_MODE_REMOTE,
190 "destination": [(1, 2, 3), (99, 99, 99)],
192 "remove_instance": True,
193 "x509_key_name": ("name", "hash"),
194 "destination_x509_ca": ("x", "y", "z"),
196 op = self.Parse(name, data)
197 self.assert_(isinstance(op, opcodes.OpExportInstance))
198 self.assertEqual(op.instance_name, name)
199 self.assertEqual(op.mode, constants.EXPORT_MODE_REMOTE)
200 self.assertEqual(op.shutdown, True)
201 self.assertEqual(op.remove_instance, True)
202 self.assertEqualValues(op.x509_key_name, ("name", "hash"))
203 self.assertEqualValues(op.destination_x509_ca, ("x", "y", "z"))
205 def testDefaults(self):
208 "destination": "node2",
211 op = self.Parse(name, data)
212 self.assert_(isinstance(op, opcodes.OpExportInstance))
213 self.assertEqual(op.instance_name, name)
214 self.assertEqual(op.mode, constants.EXPORT_MODE_LOCAL)
215 self.assertEqual(op.remove_instance, False)
217 def testErrors(self):
218 self.assertRaises(http.HttpBadRequest, self.Parse, "err1",
219 { "remove_instance": "True", })
220 self.assertRaises(http.HttpBadRequest, self.Parse, "err1",
221 { "remove_instance": "False", })
224 if __name__ == '__main__':
225 testutils.GanetiTestProgram()