Debian Codegraph @

test/hs/Test/Ganeti/Network.hs

raw
1 {-# LANGUAGE TemplateHaskell, TypeSynonymInstances, FlexibleInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3
4 module Test.Ganeti.Network
5 ( testNetwork
6 , genBitStringMaxLen
7 ) where
8
9 import Data.Maybe (fromMaybe)
10
11 import Test.QuickCheck
12
13 import Ganeti.Network as Network
14 import Ganeti.Objects as Objects
15 import Ganeti.Objects.BitArray as BA
16
17 import Test.Ganeti.Objects ( genBitStringMaxLen )
18 import Test.Ganeti.TestCommon
19 import Test.Ganeti.TestHelper
20
21 -- * Test cases
22
23 -- | Check that the address pool's properties are calculated correctly.
24 prop_addressPoolProperties :: Network -> Property
25 prop_addressPoolProperties a =
26 conjoin
27 [ counterexample
28 ("Not all reservations are included in 'allReservations' of " ++
29 "address pool:" ++ show a) (allReservationsSubsumesInternal a)
30 , counterexample
31 ("Not all external reservations are covered by 'allReservations' " ++
32 "of address pool: " ++ show a)
33 (allReservationsSubsumesExternal a)
34 , counterexample
35 ("The counts of free and reserved addresses do not add up for " ++
36 "address pool: " ++ show a)
37 (checkCounts a)
38 , counterexample
39 ("'isFull' wrongly classified the status of the address pool: " ++
40 show a) (checkIsFull a)
41 , counterexample
42 ("Network map is inconsistent with reservations of address pool: " ++
43 show a) (checkGetMap a)
44 ]
45
46 -- | Checks for the subset relation on 'Maybe' values.
47 subsetMaybe :: Maybe BitArray -> Maybe BitArray -> Bool
48 subsetMaybe (Just x) (Just y) = subset x y
49 subsetMaybe x y = x == y -- only if they're both Nothing
50
51 -- | Check that all internally reserved ips are included in 'allReservations'.
52 allReservationsSubsumesInternal :: Network -> Bool
53 allReservationsSubsumesInternal a =
54 reservations a `subsetMaybe` allReservations a
55
56 -- | Check that all externally reserved ips are included in 'allReservations'.
57 allReservationsSubsumesExternal :: Network -> Bool
58 allReservationsSubsumesExternal a =
59 extReservations a `subsetMaybe` allReservations a
60
61 -- | Check that the counts of free and reserved ips add up.
62 checkCounts :: Network -> Property
63 checkCounts a =
64 netIpv4NumHosts a ==? toInteger (getFreeCount a + getReservedCount a)
65
66 -- | Check that the detection of a full network works correctly.
67 checkIsFull :: Network -> Property
68 checkIsFull a =
69 isFull a ==? maybe True (and . toList) (allReservations a)
70
71 -- | Check that the map representation of the network corresponds to the
72 -- network's reservations.
73 checkGetMap :: Network -> Property
74 checkGetMap a =
75 fromMaybe BA.empty (allReservations a)
76 ==? fromList (Prelude.map (== 'X') (getMap a))
77
78 testSuite "Network"
79 [ 'prop_addressPoolProperties
80 ]