Revision 5b48df93
b/htest/Test/Ganeti/Objects.hs | ||
---|---|---|
126 | 126 |
-- validation rules. |
127 | 127 |
$(genArbitrary ''PartialIPolicy) |
128 | 128 |
|
129 |
-- | FIXME: This generates completely random data, without normal |
|
130 |
-- validation rules. |
|
131 |
instance Arbitrary NodeGroup where |
|
132 |
arbitrary = NodeGroup <$> genFQDN <*> pure [] <*> arbitrary <*> arbitrary |
|
133 |
<*> arbitrary <*> pure (GenericContainer Map.empty) |
|
134 |
<*> arbitrary |
|
135 |
-- ts |
|
136 |
<*> arbitrary <*> arbitrary |
|
137 |
-- uuid |
|
138 |
<*> arbitrary |
|
139 |
-- serial |
|
140 |
<*> arbitrary |
|
141 |
-- tags |
|
142 |
<*> (Set.fromList <$> genTags) |
|
143 |
|
|
144 | 129 |
$(genArbitrary ''FilledISpecParams) |
145 | 130 |
$(genArbitrary ''FilledIPolicy) |
146 | 131 |
$(genArbitrary ''IpFamily) |
... | ... | |
332 | 317 |
(Just pool) -> (getFreeCount pool, getReservedCount pool, net) |
333 | 318 |
Nothing -> (-1, -1, net) |
334 | 319 |
|
320 |
-- | Tests the compatibility between Haskell-serialized node groups and their |
|
321 |
-- python-decoded and encoded version. |
|
322 |
case_py_compat_nodegroups :: HUnit.Assertion |
|
323 |
case_py_compat_nodegroups = do |
|
324 |
let num_groups = 500::Int |
|
325 |
sample_groups <- sample' (vectorOf num_groups genNodeGroup) |
|
326 |
let groups = head sample_groups |
|
327 |
serialized = J.encode groups |
|
328 |
-- check for non-ASCII fields, usually due to 'arbitrary :: String' |
|
329 |
mapM_ (\group -> when (any (not . isAscii) (J.encode group)) . |
|
330 |
HUnit.assertFailure $ |
|
331 |
"Node group has non-ASCII fields: " ++ show group |
|
332 |
) groups |
|
333 |
py_stdout <- |
|
334 |
runPython "from ganeti import objects\n\ |
|
335 |
\from ganeti import serializer\n\ |
|
336 |
\import sys\n\ |
|
337 |
\group_data = serializer.Load(sys.stdin.read())\n\ |
|
338 |
\decoded = [objects.NodeGroup.FromDict(g) for g in group_data]\n\ |
|
339 |
\encoded = [g.ToDict() for g in decoded]\n\ |
|
340 |
\print serializer.Dump(encoded)" serialized |
|
341 |
>>= checkPythonResult |
|
342 |
let deserialised = J.decode py_stdout::J.Result [NodeGroup] |
|
343 |
decoded <- case deserialised of |
|
344 |
J.Ok ops -> return ops |
|
345 |
J.Error msg -> |
|
346 |
HUnit.assertFailure ("Unable to decode node groups: " ++ msg) |
|
347 |
-- this already raised an expection, but we need it |
|
348 |
-- for proper types |
|
349 |
>> fail "Unable to decode node groups" |
|
350 |
HUnit.assertEqual "Mismatch in number of returned node groups" |
|
351 |
(length decoded) (length groups) |
|
352 |
mapM_ (uncurry (HUnit.assertEqual "Different result after encoding/decoding") |
|
353 |
) $ zip decoded groups |
|
354 |
|
|
355 |
-- | Generates a node group with up to 3 networks. |
|
356 |
-- | FIXME: This generates still somewhat completely random data, without normal |
|
357 |
-- validation rules. |
|
358 |
genNodeGroup :: Gen NodeGroup |
|
359 |
genNodeGroup = do |
|
360 |
name <- genFQDN |
|
361 |
members <- pure [] |
|
362 |
ndparams <- arbitrary |
|
363 |
alloc_policy <- arbitrary |
|
364 |
ipolicy <- arbitrary |
|
365 |
diskparams <- pure (GenericContainer Map.empty) |
|
366 |
num_networks <- choose (0, 3) |
|
367 |
networks <- vectorOf num_networks genValidNetwork |
|
368 |
-- timestamp fields |
|
369 |
ctime <- arbitrary |
|
370 |
mtime <- arbitrary |
|
371 |
uuid <- arbitrary |
|
372 |
serial <- arbitrary |
|
373 |
tags <- Set.fromList <$> genTags |
|
374 |
let group = NodeGroup name members ndparams alloc_policy ipolicy diskparams |
|
375 |
networks ctime mtime uuid serial tags |
|
376 |
return group |
|
377 |
|
|
378 |
instance Arbitrary NodeGroup where |
|
379 |
arbitrary = genNodeGroup |
|
380 |
|
|
335 | 381 |
testSuite "Objects" |
336 | 382 |
[ 'prop_fillDict |
337 | 383 |
, 'prop_Disk_serialisation |
... | ... | |
340 | 386 |
, 'prop_Node_serialisation |
341 | 387 |
, 'prop_Config_serialisation |
342 | 388 |
, 'case_py_compat_networks |
389 |
, 'case_py_compat_nodegroups |
|
343 | 390 |
] |
Also available in: Unified diff