root / htools / Ganeti / HTools / QC.hs @ fce98abd
History | View | Annotate | Download (70.5 kB)
1 | 23fe06c2 | Iustin Pop | {-# LANGUAGE TemplateHaskell #-} |
---|---|---|---|
2 | fce98abd | Iustin Pop | {-# OPTIONS_GHC -fno-warn-orphans -fno-warn-unused-imports #-} |
3 | fce98abd | Iustin Pop | |
4 | fce98abd | Iustin Pop | -- FIXME: should remove the no-warn-unused-imports option, once we get |
5 | fce98abd | Iustin Pop | -- around to testing function from all modules; until then, we keep |
6 | fce98abd | Iustin Pop | -- the (unused) imports here to generate correct coverage (0 for |
7 | fce98abd | Iustin Pop | -- modules we don't use) |
8 | 23fe06c2 | Iustin Pop | |
9 | 525bfb36 | Iustin Pop | {-| Unittests for ganeti-htools. |
10 | e2fa2baf | Iustin Pop | |
11 | e2fa2baf | Iustin Pop | -} |
12 | e2fa2baf | Iustin Pop | |
13 | e2fa2baf | Iustin Pop | {- |
14 | e2fa2baf | Iustin Pop | |
15 | d6eec019 | Iustin Pop | Copyright (C) 2009, 2010, 2011, 2012 Google Inc. |
16 | e2fa2baf | Iustin Pop | |
17 | e2fa2baf | Iustin Pop | This program is free software; you can redistribute it and/or modify |
18 | e2fa2baf | Iustin Pop | it under the terms of the GNU General Public License as published by |
19 | e2fa2baf | Iustin Pop | the Free Software Foundation; either version 2 of the License, or |
20 | e2fa2baf | Iustin Pop | (at your option) any later version. |
21 | e2fa2baf | Iustin Pop | |
22 | e2fa2baf | Iustin Pop | This program is distributed in the hope that it will be useful, but |
23 | e2fa2baf | Iustin Pop | WITHOUT ANY WARRANTY; without even the implied warranty of |
24 | e2fa2baf | Iustin Pop | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
25 | e2fa2baf | Iustin Pop | General Public License for more details. |
26 | e2fa2baf | Iustin Pop | |
27 | e2fa2baf | Iustin Pop | You should have received a copy of the GNU General Public License |
28 | e2fa2baf | Iustin Pop | along with this program; if not, write to the Free Software |
29 | e2fa2baf | Iustin Pop | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
30 | e2fa2baf | Iustin Pop | 02110-1301, USA. |
31 | e2fa2baf | Iustin Pop | |
32 | e2fa2baf | Iustin Pop | -} |
33 | e2fa2baf | Iustin Pop | |
34 | 15f4c8ca | Iustin Pop | module Ganeti.HTools.QC |
35 | d5dfae0a | Iustin Pop | ( testUtils |
36 | d5dfae0a | Iustin Pop | , testPeerMap |
37 | d5dfae0a | Iustin Pop | , testContainer |
38 | d5dfae0a | Iustin Pop | , testInstance |
39 | d5dfae0a | Iustin Pop | , testNode |
40 | d5dfae0a | Iustin Pop | , testText |
41 | e1dde6ad | Iustin Pop | , testSimu |
42 | d5dfae0a | Iustin Pop | , testOpCodes |
43 | d5dfae0a | Iustin Pop | , testJobs |
44 | d5dfae0a | Iustin Pop | , testCluster |
45 | d5dfae0a | Iustin Pop | , testLoader |
46 | d5dfae0a | Iustin Pop | , testTypes |
47 | 8b5a517a | Iustin Pop | , testCLI |
48 | 3ad57194 | Iustin Pop | , testJSON |
49 | cdd495ae | Iustin Pop | , testLUXI |
50 | c5b4a186 | Iustin Pop | , testSsconf |
51 | d5dfae0a | Iustin Pop | ) where |
52 | 15f4c8ca | Iustin Pop | |
53 | 15f4c8ca | Iustin Pop | import Test.QuickCheck |
54 | e1dde6ad | Iustin Pop | import Text.Printf (printf) |
55 | fce98abd | Iustin Pop | import Data.List (intercalate, nub, isPrefixOf) |
56 | 15f4c8ca | Iustin Pop | import Data.Maybe |
57 | 88f25dd0 | Iustin Pop | import Control.Monad |
58 | 5cefb2b2 | Iustin Pop | import Control.Applicative |
59 | 89298c04 | Iustin Pop | import qualified System.Console.GetOpt as GetOpt |
60 | 88f25dd0 | Iustin Pop | import qualified Text.JSON as J |
61 | 8fcf251f | Iustin Pop | import qualified Data.Map |
62 | 3fea6959 | Iustin Pop | import qualified Data.IntMap as IntMap |
63 | 89298c04 | Iustin Pop | |
64 | 88f25dd0 | Iustin Pop | import qualified Ganeti.OpCodes as OpCodes |
65 | db079755 | Iustin Pop | import qualified Ganeti.Jobs as Jobs |
66 | cdd495ae | Iustin Pop | import qualified Ganeti.Luxi as Luxi |
67 | c5b4a186 | Iustin Pop | import qualified Ganeti.Ssconf as Ssconf |
68 | 15f4c8ca | Iustin Pop | import qualified Ganeti.HTools.CLI as CLI |
69 | 15f4c8ca | Iustin Pop | import qualified Ganeti.HTools.Cluster as Cluster |
70 | 15f4c8ca | Iustin Pop | import qualified Ganeti.HTools.Container as Container |
71 | 223dbe53 | Iustin Pop | import qualified Ganeti.HTools.ExtLoader |
72 | 15f4c8ca | Iustin Pop | import qualified Ganeti.HTools.IAlloc as IAlloc |
73 | 15f4c8ca | Iustin Pop | import qualified Ganeti.HTools.Instance as Instance |
74 | b69be409 | Iustin Pop | import qualified Ganeti.HTools.JSON as JSON |
75 | 15f4c8ca | Iustin Pop | import qualified Ganeti.HTools.Loader as Loader |
76 | cdd495ae | Iustin Pop | import qualified Ganeti.HTools.Luxi as HTools.Luxi |
77 | 15f4c8ca | Iustin Pop | import qualified Ganeti.HTools.Node as Node |
78 | 10ef6b4e | Iustin Pop | import qualified Ganeti.HTools.Group as Group |
79 | 15f4c8ca | Iustin Pop | import qualified Ganeti.HTools.PeerMap as PeerMap |
80 | c478f837 | Iustin Pop | import qualified Ganeti.HTools.Rapi |
81 | e1dde6ad | Iustin Pop | import qualified Ganeti.HTools.Simu as Simu |
82 | 15f4c8ca | Iustin Pop | import qualified Ganeti.HTools.Text as Text |
83 | 15f4c8ca | Iustin Pop | import qualified Ganeti.HTools.Types as Types |
84 | 15f4c8ca | Iustin Pop | import qualified Ganeti.HTools.Utils as Utils |
85 | 223dbe53 | Iustin Pop | import qualified Ganeti.HTools.Version |
86 | e82271f8 | Iustin Pop | import qualified Ganeti.Constants as C |
87 | 15f4c8ca | Iustin Pop | |
88 | a292b4e0 | Iustin Pop | import qualified Ganeti.HTools.Program as Program |
89 | 33b9d92d | Iustin Pop | import qualified Ganeti.HTools.Program.Hail |
90 | 33b9d92d | Iustin Pop | import qualified Ganeti.HTools.Program.Hbal |
91 | 33b9d92d | Iustin Pop | import qualified Ganeti.HTools.Program.Hscan |
92 | 33b9d92d | Iustin Pop | import qualified Ganeti.HTools.Program.Hspace |
93 | 33b9d92d | Iustin Pop | |
94 | 23fe06c2 | Iustin Pop | import Ganeti.HTools.QCHelper (testSuite) |
95 | 8e4f6d56 | Iustin Pop | |
96 | 3fea6959 | Iustin Pop | -- * Constants |
97 | 3fea6959 | Iustin Pop | |
98 | 525bfb36 | Iustin Pop | -- | Maximum memory (1TiB, somewhat random value). |
99 | 8fcf251f | Iustin Pop | maxMem :: Int |
100 | 8fcf251f | Iustin Pop | maxMem = 1024 * 1024 |
101 | 8fcf251f | Iustin Pop | |
102 | 525bfb36 | Iustin Pop | -- | Maximum disk (8TiB, somewhat random value). |
103 | 8fcf251f | Iustin Pop | maxDsk :: Int |
104 | 49f9627a | Iustin Pop | maxDsk = 1024 * 1024 * 8 |
105 | 8fcf251f | Iustin Pop | |
106 | 525bfb36 | Iustin Pop | -- | Max CPUs (1024, somewhat random value). |
107 | 8fcf251f | Iustin Pop | maxCpu :: Int |
108 | 8fcf251f | Iustin Pop | maxCpu = 1024 |
109 | 8fcf251f | Iustin Pop | |
110 | c22d4dd4 | Iustin Pop | -- | Max vcpu ratio (random value). |
111 | c22d4dd4 | Iustin Pop | maxVcpuRatio :: Double |
112 | c22d4dd4 | Iustin Pop | maxVcpuRatio = 1024.0 |
113 | c22d4dd4 | Iustin Pop | |
114 | c22d4dd4 | Iustin Pop | -- | Max spindle ratio (random value). |
115 | c22d4dd4 | Iustin Pop | maxSpindleRatio :: Double |
116 | c22d4dd4 | Iustin Pop | maxSpindleRatio = 1024.0 |
117 | c22d4dd4 | Iustin Pop | |
118 | 5cefb2b2 | Iustin Pop | -- | Max nodes, used just to limit arbitrary instances for smaller |
119 | 5cefb2b2 | Iustin Pop | -- opcode definitions (e.g. list of nodes in OpTestDelay). |
120 | 5cefb2b2 | Iustin Pop | maxNodes :: Int |
121 | 5cefb2b2 | Iustin Pop | maxNodes = 32 |
122 | 5cefb2b2 | Iustin Pop | |
123 | 5cefb2b2 | Iustin Pop | -- | Max opcodes or jobs in a submit job and submit many jobs. |
124 | 5cefb2b2 | Iustin Pop | maxOpCodes :: Int |
125 | 5cefb2b2 | Iustin Pop | maxOpCodes = 16 |
126 | 5cefb2b2 | Iustin Pop | |
127 | 7806125e | Iustin Pop | -- | All disk templates (used later) |
128 | 7806125e | Iustin Pop | allDiskTemplates :: [Types.DiskTemplate] |
129 | 7806125e | Iustin Pop | allDiskTemplates = [minBound..maxBound] |
130 | 7806125e | Iustin Pop | |
131 | 6cff91f5 | Iustin Pop | -- | Null iPolicy, and by null we mean very liberal. |
132 | fce98abd | Iustin Pop | nullIPolicy :: Types.IPolicy |
133 | 6cff91f5 | Iustin Pop | nullIPolicy = Types.IPolicy |
134 | 6cff91f5 | Iustin Pop | { Types.iPolicyMinSpec = Types.ISpec { Types.iSpecMemorySize = 0 |
135 | 6cff91f5 | Iustin Pop | , Types.iSpecCpuCount = 0 |
136 | 6cff91f5 | Iustin Pop | , Types.iSpecDiskSize = 0 |
137 | 6cff91f5 | Iustin Pop | , Types.iSpecDiskCount = 0 |
138 | 6cff91f5 | Iustin Pop | , Types.iSpecNicCount = 0 |
139 | d953a965 | Renรฉ Nussbaumer | , Types.iSpecSpindleUse = 0 |
140 | 6cff91f5 | Iustin Pop | } |
141 | 6cff91f5 | Iustin Pop | , Types.iPolicyMaxSpec = Types.ISpec { Types.iSpecMemorySize = maxBound |
142 | 6cff91f5 | Iustin Pop | , Types.iSpecCpuCount = maxBound |
143 | 6cff91f5 | Iustin Pop | , Types.iSpecDiskSize = maxBound |
144 | 6cff91f5 | Iustin Pop | , Types.iSpecDiskCount = C.maxDisks |
145 | 6cff91f5 | Iustin Pop | , Types.iSpecNicCount = C.maxNics |
146 | d953a965 | Renรฉ Nussbaumer | , Types.iSpecSpindleUse = maxBound |
147 | 6cff91f5 | Iustin Pop | } |
148 | 6cff91f5 | Iustin Pop | , Types.iPolicyStdSpec = Types.ISpec { Types.iSpecMemorySize = Types.unitMem |
149 | 6cff91f5 | Iustin Pop | , Types.iSpecCpuCount = Types.unitCpu |
150 | 6cff91f5 | Iustin Pop | , Types.iSpecDiskSize = Types.unitDsk |
151 | 6cff91f5 | Iustin Pop | , Types.iSpecDiskCount = 1 |
152 | 6cff91f5 | Iustin Pop | , Types.iSpecNicCount = 1 |
153 | d953a965 | Renรฉ Nussbaumer | , Types.iSpecSpindleUse = 1 |
154 | 6cff91f5 | Iustin Pop | } |
155 | 64946775 | Iustin Pop | , Types.iPolicyDiskTemplates = [minBound..maxBound] |
156 | c22d4dd4 | Iustin Pop | , Types.iPolicyVcpuRatio = maxVcpuRatio -- somewhat random value, high |
157 | c22d4dd4 | Iustin Pop | -- enough to not impact us |
158 | c22d4dd4 | Iustin Pop | , Types.iPolicySpindleRatio = maxSpindleRatio |
159 | 6cff91f5 | Iustin Pop | } |
160 | 6cff91f5 | Iustin Pop | |
161 | 6cff91f5 | Iustin Pop | |
162 | 10ef6b4e | Iustin Pop | defGroup :: Group.Group |
163 | 10ef6b4e | Iustin Pop | defGroup = flip Group.setIdx 0 $ |
164 | f3f76ccc | Iustin Pop | Group.create "default" Types.defaultGroupID Types.AllocPreferred |
165 | 6cff91f5 | Iustin Pop | nullIPolicy |
166 | 10ef6b4e | Iustin Pop | |
167 | 10ef6b4e | Iustin Pop | defGroupList :: Group.List |
168 | cb0c77ff | Iustin Pop | defGroupList = Container.fromList [(Group.idx defGroup, defGroup)] |
169 | 10ef6b4e | Iustin Pop | |
170 | 10ef6b4e | Iustin Pop | defGroupAssoc :: Data.Map.Map String Types.Gdx |
171 | 10ef6b4e | Iustin Pop | defGroupAssoc = Data.Map.singleton (Group.uuid defGroup) (Group.idx defGroup) |
172 | 10ef6b4e | Iustin Pop | |
173 | 3fea6959 | Iustin Pop | -- * Helper functions |
174 | 3fea6959 | Iustin Pop | |
175 | 525bfb36 | Iustin Pop | -- | Simple checker for whether OpResult is fail or pass. |
176 | 79a72ce7 | Iustin Pop | isFailure :: Types.OpResult a -> Bool |
177 | 79a72ce7 | Iustin Pop | isFailure (Types.OpFail _) = True |
178 | 79a72ce7 | Iustin Pop | isFailure _ = False |
179 | 79a72ce7 | Iustin Pop | |
180 | 72bb6b4e | Iustin Pop | -- | Checks for equality with proper annotation. |
181 | 72bb6b4e | Iustin Pop | (==?) :: (Show a, Eq a) => a -> a -> Property |
182 | 72bb6b4e | Iustin Pop | (==?) x y = printTestCase |
183 | 72bb6b4e | Iustin Pop | ("Expected equality, but '" ++ |
184 | 72bb6b4e | Iustin Pop | show x ++ "' /= '" ++ show y ++ "'") (x == y) |
185 | 72bb6b4e | Iustin Pop | infix 3 ==? |
186 | 72bb6b4e | Iustin Pop | |
187 | 96bc2003 | Iustin Pop | -- | Show a message and fail the test. |
188 | 96bc2003 | Iustin Pop | failTest :: String -> Property |
189 | 96bc2003 | Iustin Pop | failTest msg = printTestCase msg False |
190 | 96bc2003 | Iustin Pop | |
191 | 525bfb36 | Iustin Pop | -- | Update an instance to be smaller than a node. |
192 | fce98abd | Iustin Pop | setInstanceSmallerThanNode :: Node.Node |
193 | fce98abd | Iustin Pop | -> Instance.Instance -> Instance.Instance |
194 | 3fea6959 | Iustin Pop | setInstanceSmallerThanNode node inst = |
195 | d5dfae0a | Iustin Pop | inst { Instance.mem = Node.availMem node `div` 2 |
196 | d5dfae0a | Iustin Pop | , Instance.dsk = Node.availDisk node `div` 2 |
197 | d5dfae0a | Iustin Pop | , Instance.vcpus = Node.availCpu node `div` 2 |
198 | d5dfae0a | Iustin Pop | } |
199 | 3fea6959 | Iustin Pop | |
200 | 525bfb36 | Iustin Pop | -- | Create an instance given its spec. |
201 | fce98abd | Iustin Pop | createInstance :: Int -> Int -> Int -> Instance.Instance |
202 | 3fea6959 | Iustin Pop | createInstance mem dsk vcpus = |
203 | d5dfae0a | Iustin Pop | Instance.create "inst-unnamed" mem dsk vcpus Types.Running [] True (-1) (-1) |
204 | 981bb5cf | Renรฉ Nussbaumer | Types.DTDrbd8 1 |
205 | 3fea6959 | Iustin Pop | |
206 | 525bfb36 | Iustin Pop | -- | Create a small cluster by repeating a node spec. |
207 | 3fea6959 | Iustin Pop | makeSmallCluster :: Node.Node -> Int -> Node.List |
208 | 3fea6959 | Iustin Pop | makeSmallCluster node count = |
209 | e73c5fe2 | Iustin Pop | let origname = Node.name node |
210 | e73c5fe2 | Iustin Pop | origalias = Node.alias node |
211 | e73c5fe2 | Iustin Pop | nodes = map (\idx -> node { Node.name = origname ++ "-" ++ show idx |
212 | e73c5fe2 | Iustin Pop | , Node.alias = origalias ++ "-" ++ show idx }) |
213 | e73c5fe2 | Iustin Pop | [1..count] |
214 | e73c5fe2 | Iustin Pop | fn = flip Node.buildPeers Container.empty |
215 | e73c5fe2 | Iustin Pop | namelst = map (\n -> (Node.name n, fn n)) nodes |
216 | d5dfae0a | Iustin Pop | (_, nlst) = Loader.assignIndices namelst |
217 | d5dfae0a | Iustin Pop | in nlst |
218 | 3fea6959 | Iustin Pop | |
219 | 3603605a | Iustin Pop | -- | Make a small cluster, both nodes and instances. |
220 | 3603605a | Iustin Pop | makeSmallEmptyCluster :: Node.Node -> Int -> Instance.Instance |
221 | 3603605a | Iustin Pop | -> (Node.List, Instance.List, Instance.Instance) |
222 | 3603605a | Iustin Pop | makeSmallEmptyCluster node count inst = |
223 | 3603605a | Iustin Pop | (makeSmallCluster node count, Container.empty, |
224 | 3603605a | Iustin Pop | setInstanceSmallerThanNode node inst) |
225 | 3603605a | Iustin Pop | |
226 | 525bfb36 | Iustin Pop | -- | Checks if a node is "big" enough. |
227 | d6f9f5bd | Iustin Pop | isNodeBig :: Int -> Node.Node -> Bool |
228 | d6f9f5bd | Iustin Pop | isNodeBig size node = Node.availDisk node > size * Types.unitDsk |
229 | 3fea6959 | Iustin Pop | && Node.availMem node > size * Types.unitMem |
230 | 3fea6959 | Iustin Pop | && Node.availCpu node > size * Types.unitCpu |
231 | 3fea6959 | Iustin Pop | |
232 | e08424a8 | Guido Trotter | canBalance :: Cluster.Table -> Bool -> Bool -> Bool -> Bool |
233 | e08424a8 | Guido Trotter | canBalance tbl dm im evac = isJust $ Cluster.tryBalance tbl dm im evac 0 0 |
234 | 3fea6959 | Iustin Pop | |
235 | f4161783 | Iustin Pop | -- | Assigns a new fresh instance to a cluster; this is not |
236 | 525bfb36 | Iustin Pop | -- allocation, so no resource checks are done. |
237 | f4161783 | Iustin Pop | assignInstance :: Node.List -> Instance.List -> Instance.Instance -> |
238 | f4161783 | Iustin Pop | Types.Idx -> Types.Idx -> |
239 | f4161783 | Iustin Pop | (Node.List, Instance.List) |
240 | f4161783 | Iustin Pop | assignInstance nl il inst pdx sdx = |
241 | f4161783 | Iustin Pop | let pnode = Container.find pdx nl |
242 | f4161783 | Iustin Pop | snode = Container.find sdx nl |
243 | f4161783 | Iustin Pop | maxiidx = if Container.null il |
244 | d5dfae0a | Iustin Pop | then 0 |
245 | d5dfae0a | Iustin Pop | else fst (Container.findMax il) + 1 |
246 | f4161783 | Iustin Pop | inst' = inst { Instance.idx = maxiidx, |
247 | f4161783 | Iustin Pop | Instance.pNode = pdx, Instance.sNode = sdx } |
248 | f4161783 | Iustin Pop | pnode' = Node.setPri pnode inst' |
249 | f4161783 | Iustin Pop | snode' = Node.setSec snode inst' |
250 | f4161783 | Iustin Pop | nl' = Container.addTwo pdx pnode' sdx snode' nl |
251 | f4161783 | Iustin Pop | il' = Container.add maxiidx inst' il |
252 | f4161783 | Iustin Pop | in (nl', il') |
253 | f4161783 | Iustin Pop | |
254 | a2a0bcd8 | Iustin Pop | -- | Generates a list of a given size with non-duplicate elements. |
255 | a2a0bcd8 | Iustin Pop | genUniquesList :: (Eq a, Arbitrary a) => Int -> Gen [a] |
256 | a2a0bcd8 | Iustin Pop | genUniquesList cnt = |
257 | a2a0bcd8 | Iustin Pop | foldM (\lst _ -> do |
258 | a2a0bcd8 | Iustin Pop | newelem <- arbitrary `suchThat` (`notElem` lst) |
259 | a2a0bcd8 | Iustin Pop | return (newelem:lst)) [] [1..cnt] |
260 | a2a0bcd8 | Iustin Pop | |
261 | ac1c0a07 | Iustin Pop | -- | Checks if an instance is mirrored. |
262 | ac1c0a07 | Iustin Pop | isMirrored :: Instance.Instance -> Bool |
263 | fafd0773 | Iustin Pop | isMirrored = (/= Types.MirrorNone) . Instance.mirrorType |
264 | ac1c0a07 | Iustin Pop | |
265 | ac1c0a07 | Iustin Pop | -- | Returns the possible change node types for a disk template. |
266 | ac1c0a07 | Iustin Pop | evacModeOptions :: Types.MirrorType -> [Types.EvacMode] |
267 | ac1c0a07 | Iustin Pop | evacModeOptions Types.MirrorNone = [] |
268 | ac1c0a07 | Iustin Pop | evacModeOptions Types.MirrorInternal = [minBound..maxBound] -- DRBD can do all |
269 | ac1c0a07 | Iustin Pop | evacModeOptions Types.MirrorExternal = [Types.ChangePrimary, Types.ChangeAll] |
270 | ac1c0a07 | Iustin Pop | |
271 | 3fea6959 | Iustin Pop | -- * Arbitrary instances |
272 | 3fea6959 | Iustin Pop | |
273 | 525bfb36 | Iustin Pop | -- | Defines a DNS name. |
274 | a070c426 | Iustin Pop | newtype DNSChar = DNSChar { dnsGetChar::Char } |
275 | 525bfb36 | Iustin Pop | |
276 | a070c426 | Iustin Pop | instance Arbitrary DNSChar where |
277 | d5dfae0a | Iustin Pop | arbitrary = do |
278 | d5dfae0a | Iustin Pop | x <- elements (['a'..'z'] ++ ['0'..'9'] ++ "_-") |
279 | d5dfae0a | Iustin Pop | return (DNSChar x) |
280 | a070c426 | Iustin Pop | |
281 | a2a0bcd8 | Iustin Pop | -- | Generates a single name component. |
282 | a070c426 | Iustin Pop | getName :: Gen String |
283 | a070c426 | Iustin Pop | getName = do |
284 | a070c426 | Iustin Pop | n <- choose (1, 64) |
285 | 5cefb2b2 | Iustin Pop | dn <- vector n |
286 | a070c426 | Iustin Pop | return (map dnsGetChar dn) |
287 | a070c426 | Iustin Pop | |
288 | a2a0bcd8 | Iustin Pop | -- | Generates an entire FQDN. |
289 | a070c426 | Iustin Pop | getFQDN :: Gen String |
290 | a070c426 | Iustin Pop | getFQDN = do |
291 | a070c426 | Iustin Pop | ncomps <- choose (1, 4) |
292 | 5cefb2b2 | Iustin Pop | names <- vectorOf ncomps getName |
293 | a2a0bcd8 | Iustin Pop | return $ intercalate "." names |
294 | a070c426 | Iustin Pop | |
295 | 5cefb2b2 | Iustin Pop | -- | Combinator that generates a 'Maybe' using a sub-combinator. |
296 | 5cefb2b2 | Iustin Pop | getMaybe :: Gen a -> Gen (Maybe a) |
297 | 5cefb2b2 | Iustin Pop | getMaybe subgen = do |
298 | 5cefb2b2 | Iustin Pop | bool <- arbitrary |
299 | 5cefb2b2 | Iustin Pop | if bool |
300 | 5cefb2b2 | Iustin Pop | then Just <$> subgen |
301 | 5cefb2b2 | Iustin Pop | else return Nothing |
302 | 5cefb2b2 | Iustin Pop | |
303 | 5cefb2b2 | Iustin Pop | -- | Generates a fields list. This uses the same character set as a |
304 | 5cefb2b2 | Iustin Pop | -- DNS name (just for simplicity). |
305 | 5cefb2b2 | Iustin Pop | getFields :: Gen [String] |
306 | 5cefb2b2 | Iustin Pop | getFields = do |
307 | 5cefb2b2 | Iustin Pop | n <- choose (1, 32) |
308 | 5cefb2b2 | Iustin Pop | vectorOf n getName |
309 | 5cefb2b2 | Iustin Pop | |
310 | dce9bbb3 | Iustin Pop | -- | Defines a tag type. |
311 | dce9bbb3 | Iustin Pop | newtype TagChar = TagChar { tagGetChar :: Char } |
312 | dce9bbb3 | Iustin Pop | |
313 | dce9bbb3 | Iustin Pop | -- | All valid tag chars. This doesn't need to match _exactly_ |
314 | dce9bbb3 | Iustin Pop | -- Ganeti's own tag regex, just enough for it to be close. |
315 | dce9bbb3 | Iustin Pop | tagChar :: [Char] |
316 | dce9bbb3 | Iustin Pop | tagChar = ['a'..'z'] ++ ['A'..'Z'] ++ ['0'..'9'] ++ ".+*/:@-" |
317 | dce9bbb3 | Iustin Pop | |
318 | dce9bbb3 | Iustin Pop | instance Arbitrary TagChar where |
319 | dce9bbb3 | Iustin Pop | arbitrary = do |
320 | dce9bbb3 | Iustin Pop | c <- elements tagChar |
321 | dce9bbb3 | Iustin Pop | return (TagChar c) |
322 | dce9bbb3 | Iustin Pop | |
323 | dce9bbb3 | Iustin Pop | -- | Generates a tag |
324 | dce9bbb3 | Iustin Pop | genTag :: Gen [TagChar] |
325 | dce9bbb3 | Iustin Pop | genTag = do |
326 | dce9bbb3 | Iustin Pop | -- the correct value would be C.maxTagLen, but that's way too |
327 | dce9bbb3 | Iustin Pop | -- verbose in unittests, and at the moment I don't see any possible |
328 | dce9bbb3 | Iustin Pop | -- bugs with longer tags and the way we use tags in htools |
329 | dce9bbb3 | Iustin Pop | n <- choose (1, 10) |
330 | dce9bbb3 | Iustin Pop | vector n |
331 | dce9bbb3 | Iustin Pop | |
332 | dce9bbb3 | Iustin Pop | -- | Generates a list of tags (correctly upper bounded). |
333 | dce9bbb3 | Iustin Pop | genTags :: Gen [String] |
334 | dce9bbb3 | Iustin Pop | genTags = do |
335 | dce9bbb3 | Iustin Pop | -- the correct value would be C.maxTagsPerObj, but per the comment |
336 | dce9bbb3 | Iustin Pop | -- in genTag, we don't use tags enough in htools to warrant testing |
337 | dce9bbb3 | Iustin Pop | -- such big values |
338 | dce9bbb3 | Iustin Pop | n <- choose (0, 10::Int) |
339 | dce9bbb3 | Iustin Pop | tags <- mapM (const genTag) [1..n] |
340 | dce9bbb3 | Iustin Pop | return $ map (map tagGetChar) tags |
341 | dce9bbb3 | Iustin Pop | |
342 | 7dd14211 | Agata Murawska | instance Arbitrary Types.InstanceStatus where |
343 | e1bf27bb | Agata Murawska | arbitrary = elements [minBound..maxBound] |
344 | 7dd14211 | Agata Murawska | |
345 | 59ed268d | Iustin Pop | -- | Generates a random instance with maximum disk/mem/cpu values. |
346 | 59ed268d | Iustin Pop | genInstanceSmallerThan :: Int -> Int -> Int -> Gen Instance.Instance |
347 | 59ed268d | Iustin Pop | genInstanceSmallerThan lim_mem lim_dsk lim_cpu = do |
348 | 59ed268d | Iustin Pop | name <- getFQDN |
349 | 59ed268d | Iustin Pop | mem <- choose (0, lim_mem) |
350 | 59ed268d | Iustin Pop | dsk <- choose (0, lim_dsk) |
351 | 59ed268d | Iustin Pop | run_st <- arbitrary |
352 | 59ed268d | Iustin Pop | pn <- arbitrary |
353 | 59ed268d | Iustin Pop | sn <- arbitrary |
354 | 59ed268d | Iustin Pop | vcpus <- choose (0, lim_cpu) |
355 | 64946775 | Iustin Pop | dt <- arbitrary |
356 | 64946775 | Iustin Pop | return $ Instance.create name mem dsk vcpus run_st [] True pn sn dt 1 |
357 | 59ed268d | Iustin Pop | |
358 | 59ed268d | Iustin Pop | -- | Generates an instance smaller than a node. |
359 | 59ed268d | Iustin Pop | genInstanceSmallerThanNode :: Node.Node -> Gen Instance.Instance |
360 | 59ed268d | Iustin Pop | genInstanceSmallerThanNode node = |
361 | 59ed268d | Iustin Pop | genInstanceSmallerThan (Node.availMem node `div` 2) |
362 | 59ed268d | Iustin Pop | (Node.availDisk node `div` 2) |
363 | 59ed268d | Iustin Pop | (Node.availCpu node `div` 2) |
364 | 59ed268d | Iustin Pop | |
365 | 15f4c8ca | Iustin Pop | -- let's generate a random instance |
366 | 15f4c8ca | Iustin Pop | instance Arbitrary Instance.Instance where |
367 | 59ed268d | Iustin Pop | arbitrary = genInstanceSmallerThan maxMem maxDsk maxCpu |
368 | 15f4c8ca | Iustin Pop | |
369 | 525bfb36 | Iustin Pop | -- | Generas an arbitrary node based on sizing information. |
370 | 525bfb36 | Iustin Pop | genNode :: Maybe Int -- ^ Minimum node size in terms of units |
371 | 525bfb36 | Iustin Pop | -> Maybe Int -- ^ Maximum node size (when Nothing, bounded |
372 | 525bfb36 | Iustin Pop | -- just by the max... constants) |
373 | 525bfb36 | Iustin Pop | -> Gen Node.Node |
374 | 00c75986 | Iustin Pop | genNode min_multiplier max_multiplier = do |
375 | 00c75986 | Iustin Pop | let (base_mem, base_dsk, base_cpu) = |
376 | d5dfae0a | Iustin Pop | case min_multiplier of |
377 | d5dfae0a | Iustin Pop | Just mm -> (mm * Types.unitMem, |
378 | d5dfae0a | Iustin Pop | mm * Types.unitDsk, |
379 | d5dfae0a | Iustin Pop | mm * Types.unitCpu) |
380 | d5dfae0a | Iustin Pop | Nothing -> (0, 0, 0) |
381 | 00c75986 | Iustin Pop | (top_mem, top_dsk, top_cpu) = |
382 | d5dfae0a | Iustin Pop | case max_multiplier of |
383 | d5dfae0a | Iustin Pop | Just mm -> (mm * Types.unitMem, |
384 | d5dfae0a | Iustin Pop | mm * Types.unitDsk, |
385 | d5dfae0a | Iustin Pop | mm * Types.unitCpu) |
386 | d5dfae0a | Iustin Pop | Nothing -> (maxMem, maxDsk, maxCpu) |
387 | 00c75986 | Iustin Pop | name <- getFQDN |
388 | 00c75986 | Iustin Pop | mem_t <- choose (base_mem, top_mem) |
389 | 00c75986 | Iustin Pop | mem_f <- choose (base_mem, mem_t) |
390 | 00c75986 | Iustin Pop | mem_n <- choose (0, mem_t - mem_f) |
391 | 00c75986 | Iustin Pop | dsk_t <- choose (base_dsk, top_dsk) |
392 | 00c75986 | Iustin Pop | dsk_f <- choose (base_dsk, dsk_t) |
393 | 00c75986 | Iustin Pop | cpu_t <- choose (base_cpu, top_cpu) |
394 | 00c75986 | Iustin Pop | offl <- arbitrary |
395 | 00c75986 | Iustin Pop | let n = Node.create name (fromIntegral mem_t) mem_n mem_f |
396 | 8bc34c7b | Iustin Pop | (fromIntegral dsk_t) dsk_f (fromIntegral cpu_t) offl 1 0 |
397 | d6eec019 | Iustin Pop | n' = Node.setPolicy nullIPolicy n |
398 | d6eec019 | Iustin Pop | return $ Node.buildPeers n' Container.empty |
399 | 00c75986 | Iustin Pop | |
400 | d6f9f5bd | Iustin Pop | -- | Helper function to generate a sane node. |
401 | d6f9f5bd | Iustin Pop | genOnlineNode :: Gen Node.Node |
402 | d6f9f5bd | Iustin Pop | genOnlineNode = do |
403 | d6f9f5bd | Iustin Pop | arbitrary `suchThat` (\n -> not (Node.offline n) && |
404 | d6f9f5bd | Iustin Pop | not (Node.failN1 n) && |
405 | d6f9f5bd | Iustin Pop | Node.availDisk n > 0 && |
406 | d6f9f5bd | Iustin Pop | Node.availMem n > 0 && |
407 | d6f9f5bd | Iustin Pop | Node.availCpu n > 0) |
408 | d6f9f5bd | Iustin Pop | |
409 | 15f4c8ca | Iustin Pop | -- and a random node |
410 | 15f4c8ca | Iustin Pop | instance Arbitrary Node.Node where |
411 | d5dfae0a | Iustin Pop | arbitrary = genNode Nothing Nothing |
412 | 15f4c8ca | Iustin Pop | |
413 | 88f25dd0 | Iustin Pop | -- replace disks |
414 | 88f25dd0 | Iustin Pop | instance Arbitrary OpCodes.ReplaceDisksMode where |
415 | e1bf27bb | Agata Murawska | arbitrary = elements [minBound..maxBound] |
416 | 88f25dd0 | Iustin Pop | |
417 | 88f25dd0 | Iustin Pop | instance Arbitrary OpCodes.OpCode where |
418 | 88f25dd0 | Iustin Pop | arbitrary = do |
419 | 88f25dd0 | Iustin Pop | op_id <- elements [ "OP_TEST_DELAY" |
420 | 88f25dd0 | Iustin Pop | , "OP_INSTANCE_REPLACE_DISKS" |
421 | 88f25dd0 | Iustin Pop | , "OP_INSTANCE_FAILOVER" |
422 | 88f25dd0 | Iustin Pop | , "OP_INSTANCE_MIGRATE" |
423 | 88f25dd0 | Iustin Pop | ] |
424 | 3603605a | Iustin Pop | case op_id of |
425 | 3603605a | Iustin Pop | "OP_TEST_DELAY" -> |
426 | 5cefb2b2 | Iustin Pop | OpCodes.OpTestDelay <$> arbitrary <*> arbitrary |
427 | 5cefb2b2 | Iustin Pop | <*> resize maxNodes (listOf getFQDN) |
428 | 3603605a | Iustin Pop | "OP_INSTANCE_REPLACE_DISKS" -> |
429 | 5cefb2b2 | Iustin Pop | OpCodes.OpInstanceReplaceDisks <$> getFQDN <*> getMaybe getFQDN <*> |
430 | 5cefb2b2 | Iustin Pop | arbitrary <*> resize C.maxDisks arbitrary <*> getMaybe getName |
431 | 3603605a | Iustin Pop | "OP_INSTANCE_FAILOVER" -> |
432 | 5cefb2b2 | Iustin Pop | OpCodes.OpInstanceFailover <$> getFQDN <*> arbitrary <*> |
433 | 5cefb2b2 | Iustin Pop | getMaybe getFQDN |
434 | 3603605a | Iustin Pop | "OP_INSTANCE_MIGRATE" -> |
435 | 5cefb2b2 | Iustin Pop | OpCodes.OpInstanceMigrate <$> getFQDN <*> arbitrary <*> |
436 | 5cefb2b2 | Iustin Pop | arbitrary <*> arbitrary <*> getMaybe getFQDN |
437 | 3603605a | Iustin Pop | _ -> fail "Wrong opcode" |
438 | 88f25dd0 | Iustin Pop | |
439 | db079755 | Iustin Pop | instance Arbitrary Jobs.OpStatus where |
440 | db079755 | Iustin Pop | arbitrary = elements [minBound..maxBound] |
441 | db079755 | Iustin Pop | |
442 | db079755 | Iustin Pop | instance Arbitrary Jobs.JobStatus where |
443 | db079755 | Iustin Pop | arbitrary = elements [minBound..maxBound] |
444 | db079755 | Iustin Pop | |
445 | 525bfb36 | Iustin Pop | newtype SmallRatio = SmallRatio Double deriving Show |
446 | 525bfb36 | Iustin Pop | instance Arbitrary SmallRatio where |
447 | d5dfae0a | Iustin Pop | arbitrary = do |
448 | d5dfae0a | Iustin Pop | v <- choose (0, 1) |
449 | d5dfae0a | Iustin Pop | return $ SmallRatio v |
450 | 525bfb36 | Iustin Pop | |
451 | 3c002a13 | Iustin Pop | instance Arbitrary Types.AllocPolicy where |
452 | 3c002a13 | Iustin Pop | arbitrary = elements [minBound..maxBound] |
453 | 3c002a13 | Iustin Pop | |
454 | 3c002a13 | Iustin Pop | instance Arbitrary Types.DiskTemplate where |
455 | 3c002a13 | Iustin Pop | arbitrary = elements [minBound..maxBound] |
456 | 3c002a13 | Iustin Pop | |
457 | 0047d4e2 | Iustin Pop | instance Arbitrary Types.FailMode where |
458 | d5dfae0a | Iustin Pop | arbitrary = elements [minBound..maxBound] |
459 | 0047d4e2 | Iustin Pop | |
460 | aa1d552d | Iustin Pop | instance Arbitrary Types.EvacMode where |
461 | aa1d552d | Iustin Pop | arbitrary = elements [minBound..maxBound] |
462 | aa1d552d | Iustin Pop | |
463 | 0047d4e2 | Iustin Pop | instance Arbitrary a => Arbitrary (Types.OpResult a) where |
464 | d5dfae0a | Iustin Pop | arbitrary = arbitrary >>= \c -> |
465 | 3603605a | Iustin Pop | if c |
466 | 5cefb2b2 | Iustin Pop | then Types.OpGood <$> arbitrary |
467 | 5cefb2b2 | Iustin Pop | else Types.OpFail <$> arbitrary |
468 | 0047d4e2 | Iustin Pop | |
469 | 00b70680 | Iustin Pop | instance Arbitrary Types.ISpec where |
470 | 00b70680 | Iustin Pop | arbitrary = do |
471 | 7806125e | Iustin Pop | mem_s <- arbitrary::Gen (NonNegative Int) |
472 | 00b70680 | Iustin Pop | dsk_c <- arbitrary::Gen (NonNegative Int) |
473 | 00b70680 | Iustin Pop | dsk_s <- arbitrary::Gen (NonNegative Int) |
474 | 7806125e | Iustin Pop | cpu_c <- arbitrary::Gen (NonNegative Int) |
475 | 7806125e | Iustin Pop | nic_c <- arbitrary::Gen (NonNegative Int) |
476 | d953a965 | Renรฉ Nussbaumer | su <- arbitrary::Gen (NonNegative Int) |
477 | 7806125e | Iustin Pop | return Types.ISpec { Types.iSpecMemorySize = fromIntegral mem_s |
478 | 7806125e | Iustin Pop | , Types.iSpecCpuCount = fromIntegral cpu_c |
479 | 00b70680 | Iustin Pop | , Types.iSpecDiskSize = fromIntegral dsk_s |
480 | 00b70680 | Iustin Pop | , Types.iSpecDiskCount = fromIntegral dsk_c |
481 | 7806125e | Iustin Pop | , Types.iSpecNicCount = fromIntegral nic_c |
482 | d953a965 | Renรฉ Nussbaumer | , Types.iSpecSpindleUse = fromIntegral su |
483 | 00b70680 | Iustin Pop | } |
484 | 00b70680 | Iustin Pop | |
485 | 7806125e | Iustin Pop | -- | Generates an ispec bigger than the given one. |
486 | 7806125e | Iustin Pop | genBiggerISpec :: Types.ISpec -> Gen Types.ISpec |
487 | 7806125e | Iustin Pop | genBiggerISpec imin = do |
488 | 7806125e | Iustin Pop | mem_s <- choose (Types.iSpecMemorySize imin, maxBound) |
489 | 7806125e | Iustin Pop | dsk_c <- choose (Types.iSpecDiskCount imin, maxBound) |
490 | 7806125e | Iustin Pop | dsk_s <- choose (Types.iSpecDiskSize imin, maxBound) |
491 | 7806125e | Iustin Pop | cpu_c <- choose (Types.iSpecCpuCount imin, maxBound) |
492 | 7806125e | Iustin Pop | nic_c <- choose (Types.iSpecNicCount imin, maxBound) |
493 | d953a965 | Renรฉ Nussbaumer | su <- choose (Types.iSpecSpindleUse imin, maxBound) |
494 | 7806125e | Iustin Pop | return Types.ISpec { Types.iSpecMemorySize = fromIntegral mem_s |
495 | 7806125e | Iustin Pop | , Types.iSpecCpuCount = fromIntegral cpu_c |
496 | 7806125e | Iustin Pop | , Types.iSpecDiskSize = fromIntegral dsk_s |
497 | 7806125e | Iustin Pop | , Types.iSpecDiskCount = fromIntegral dsk_c |
498 | 7806125e | Iustin Pop | , Types.iSpecNicCount = fromIntegral nic_c |
499 | d953a965 | Renรฉ Nussbaumer | , Types.iSpecSpindleUse = fromIntegral su |
500 | 7806125e | Iustin Pop | } |
501 | 00b70680 | Iustin Pop | |
502 | 00b70680 | Iustin Pop | instance Arbitrary Types.IPolicy where |
503 | 00b70680 | Iustin Pop | arbitrary = do |
504 | 00b70680 | Iustin Pop | imin <- arbitrary |
505 | 7806125e | Iustin Pop | istd <- genBiggerISpec imin |
506 | 7806125e | Iustin Pop | imax <- genBiggerISpec istd |
507 | 7806125e | Iustin Pop | num_tmpl <- choose (0, length allDiskTemplates) |
508 | 7806125e | Iustin Pop | dts <- genUniquesList num_tmpl |
509 | c22d4dd4 | Iustin Pop | vcpu_ratio <- choose (1.0, maxVcpuRatio) |
510 | c22d4dd4 | Iustin Pop | spindle_ratio <- choose (1.0, maxSpindleRatio) |
511 | 00b70680 | Iustin Pop | return Types.IPolicy { Types.iPolicyMinSpec = imin |
512 | 00b70680 | Iustin Pop | , Types.iPolicyStdSpec = istd |
513 | 00b70680 | Iustin Pop | , Types.iPolicyMaxSpec = imax |
514 | 00b70680 | Iustin Pop | , Types.iPolicyDiskTemplates = dts |
515 | e8fa4ff6 | Iustin Pop | , Types.iPolicyVcpuRatio = vcpu_ratio |
516 | c22d4dd4 | Iustin Pop | , Types.iPolicySpindleRatio = spindle_ratio |
517 | 00b70680 | Iustin Pop | } |
518 | 00b70680 | Iustin Pop | |
519 | 3fea6959 | Iustin Pop | -- * Actual tests |
520 | 8fcf251f | Iustin Pop | |
521 | 525bfb36 | Iustin Pop | -- ** Utils tests |
522 | 525bfb36 | Iustin Pop | |
523 | 468b828e | Iustin Pop | -- | Helper to generate a small string that doesn't contain commas. |
524 | fce98abd | Iustin Pop | genNonCommaString :: Gen [Char] |
525 | 468b828e | Iustin Pop | genNonCommaString = do |
526 | 468b828e | Iustin Pop | size <- choose (0, 20) -- arbitrary max size |
527 | 468b828e | Iustin Pop | vectorOf size (arbitrary `suchThat` ((/=) ',')) |
528 | 468b828e | Iustin Pop | |
529 | 525bfb36 | Iustin Pop | -- | If the list is not just an empty element, and if the elements do |
530 | 525bfb36 | Iustin Pop | -- not contain commas, then join+split should be idempotent. |
531 | fce98abd | Iustin Pop | prop_Utils_commaJoinSplit :: Property |
532 | a1cd7c1e | Iustin Pop | prop_Utils_commaJoinSplit = |
533 | 468b828e | Iustin Pop | forAll (choose (0, 20)) $ \llen -> |
534 | 468b828e | Iustin Pop | forAll (vectorOf llen genNonCommaString `suchThat` ((/=) [""])) $ \lst -> |
535 | d5dfae0a | Iustin Pop | Utils.sepSplit ',' (Utils.commaJoin lst) ==? lst |
536 | a1cd7c1e | Iustin Pop | |
537 | 525bfb36 | Iustin Pop | -- | Split and join should always be idempotent. |
538 | fce98abd | Iustin Pop | prop_Utils_commaSplitJoin :: [Char] -> Property |
539 | 72bb6b4e | Iustin Pop | prop_Utils_commaSplitJoin s = |
540 | d5dfae0a | Iustin Pop | Utils.commaJoin (Utils.sepSplit ',' s) ==? s |
541 | 691dcd2a | Iustin Pop | |
542 | a810ad21 | Iustin Pop | -- | fromObjWithDefault, we test using the Maybe monad and an integer |
543 | 525bfb36 | Iustin Pop | -- value. |
544 | fce98abd | Iustin Pop | prop_Utils_fromObjWithDefault :: Integer -> String -> Bool |
545 | a810ad21 | Iustin Pop | prop_Utils_fromObjWithDefault def_value random_key = |
546 | d5dfae0a | Iustin Pop | -- a missing key will be returned with the default |
547 | b69be409 | Iustin Pop | JSON.fromObjWithDefault [] random_key def_value == Just def_value && |
548 | d5dfae0a | Iustin Pop | -- a found key will be returned as is, not with default |
549 | b69be409 | Iustin Pop | JSON.fromObjWithDefault [(random_key, J.showJSON def_value)] |
550 | d5dfae0a | Iustin Pop | random_key (def_value+1) == Just def_value |
551 | a810ad21 | Iustin Pop | |
552 | bfe6c954 | Guido Trotter | -- | Test that functional if' behaves like the syntactic sugar if. |
553 | 72bb6b4e | Iustin Pop | prop_Utils_if'if :: Bool -> Int -> Int -> Gen Prop |
554 | 72bb6b4e | Iustin Pop | prop_Utils_if'if cnd a b = |
555 | d5dfae0a | Iustin Pop | Utils.if' cnd a b ==? if cnd then a else b |
556 | bfe6c954 | Guido Trotter | |
557 | 22fac87d | Guido Trotter | -- | Test basic select functionality |
558 | 72bb6b4e | Iustin Pop | prop_Utils_select :: Int -- ^ Default result |
559 | 72bb6b4e | Iustin Pop | -> [Int] -- ^ List of False values |
560 | 72bb6b4e | Iustin Pop | -> [Int] -- ^ List of True values |
561 | 72bb6b4e | Iustin Pop | -> Gen Prop -- ^ Test result |
562 | 22fac87d | Guido Trotter | prop_Utils_select def lst1 lst2 = |
563 | 3603605a | Iustin Pop | Utils.select def (flist ++ tlist) ==? expectedresult |
564 | ba1260ba | Iustin Pop | where expectedresult = Utils.if' (null lst2) def (head lst2) |
565 | ba1260ba | Iustin Pop | flist = zip (repeat False) lst1 |
566 | ba1260ba | Iustin Pop | tlist = zip (repeat True) lst2 |
567 | 22fac87d | Guido Trotter | |
568 | 22fac87d | Guido Trotter | -- | Test basic select functionality with undefined default |
569 | 72bb6b4e | Iustin Pop | prop_Utils_select_undefd :: [Int] -- ^ List of False values |
570 | 22fac87d | Guido Trotter | -> NonEmptyList Int -- ^ List of True values |
571 | 72bb6b4e | Iustin Pop | -> Gen Prop -- ^ Test result |
572 | 22fac87d | Guido Trotter | prop_Utils_select_undefd lst1 (NonEmpty lst2) = |
573 | 3603605a | Iustin Pop | Utils.select undefined (flist ++ tlist) ==? head lst2 |
574 | ba1260ba | Iustin Pop | where flist = zip (repeat False) lst1 |
575 | ba1260ba | Iustin Pop | tlist = zip (repeat True) lst2 |
576 | 22fac87d | Guido Trotter | |
577 | 22fac87d | Guido Trotter | -- | Test basic select functionality with undefined list values |
578 | 72bb6b4e | Iustin Pop | prop_Utils_select_undefv :: [Int] -- ^ List of False values |
579 | 22fac87d | Guido Trotter | -> NonEmptyList Int -- ^ List of True values |
580 | 72bb6b4e | Iustin Pop | -> Gen Prop -- ^ Test result |
581 | 22fac87d | Guido Trotter | prop_Utils_select_undefv lst1 (NonEmpty lst2) = |
582 | 72bb6b4e | Iustin Pop | Utils.select undefined cndlist ==? head lst2 |
583 | ba1260ba | Iustin Pop | where flist = zip (repeat False) lst1 |
584 | ba1260ba | Iustin Pop | tlist = zip (repeat True) lst2 |
585 | ba1260ba | Iustin Pop | cndlist = flist ++ tlist ++ [undefined] |
586 | bfe6c954 | Guido Trotter | |
587 | fce98abd | Iustin Pop | prop_Utils_parseUnit :: NonNegative Int -> Property |
588 | 1cb92fac | Iustin Pop | prop_Utils_parseUnit (NonNegative n) = |
589 | 1cdcf8f3 | Iustin Pop | Utils.parseUnit (show n) ==? Types.Ok n .&&. |
590 | 1cdcf8f3 | Iustin Pop | Utils.parseUnit (show n ++ "m") ==? Types.Ok n .&&. |
591 | 1cdcf8f3 | Iustin Pop | Utils.parseUnit (show n ++ "M") ==? Types.Ok (truncate n_mb::Int) .&&. |
592 | 1cdcf8f3 | Iustin Pop | Utils.parseUnit (show n ++ "g") ==? Types.Ok (n*1024) .&&. |
593 | 1cdcf8f3 | Iustin Pop | Utils.parseUnit (show n ++ "G") ==? Types.Ok (truncate n_gb::Int) .&&. |
594 | 1cdcf8f3 | Iustin Pop | Utils.parseUnit (show n ++ "t") ==? Types.Ok (n*1048576) .&&. |
595 | 1cdcf8f3 | Iustin Pop | Utils.parseUnit (show n ++ "T") ==? Types.Ok (truncate n_tb::Int) .&&. |
596 | 1cdcf8f3 | Iustin Pop | printTestCase "Internal error/overflow?" |
597 | 1cdcf8f3 | Iustin Pop | (n_mb >=0 && n_gb >= 0 && n_tb >= 0) .&&. |
598 | 1cdcf8f3 | Iustin Pop | property (Types.isBad (Utils.parseUnit (show n ++ "x")::Types.Result Int)) |
599 | fce98abd | Iustin Pop | where n_mb = (fromIntegral n::Rational) * 1000 * 1000 / 1024 / 1024 |
600 | 1cdcf8f3 | Iustin Pop | n_gb = n_mb * 1000 |
601 | 1cdcf8f3 | Iustin Pop | n_tb = n_gb * 1000 |
602 | 1cb92fac | Iustin Pop | |
603 | 525bfb36 | Iustin Pop | -- | Test list for the Utils module. |
604 | 23fe06c2 | Iustin Pop | testSuite "Utils" |
605 | d5dfae0a | Iustin Pop | [ 'prop_Utils_commaJoinSplit |
606 | d5dfae0a | Iustin Pop | , 'prop_Utils_commaSplitJoin |
607 | d5dfae0a | Iustin Pop | , 'prop_Utils_fromObjWithDefault |
608 | d5dfae0a | Iustin Pop | , 'prop_Utils_if'if |
609 | d5dfae0a | Iustin Pop | , 'prop_Utils_select |
610 | d5dfae0a | Iustin Pop | , 'prop_Utils_select_undefd |
611 | d5dfae0a | Iustin Pop | , 'prop_Utils_select_undefv |
612 | d5dfae0a | Iustin Pop | , 'prop_Utils_parseUnit |
613 | d5dfae0a | Iustin Pop | ] |
614 | 691dcd2a | Iustin Pop | |
615 | 525bfb36 | Iustin Pop | -- ** PeerMap tests |
616 | 525bfb36 | Iustin Pop | |
617 | 525bfb36 | Iustin Pop | -- | Make sure add is idempotent. |
618 | fce98abd | Iustin Pop | prop_PeerMap_addIdempotent :: PeerMap.PeerMap |
619 | fce98abd | Iustin Pop | -> PeerMap.Key -> PeerMap.Elem -> Property |
620 | fbb95f28 | Iustin Pop | prop_PeerMap_addIdempotent pmap key em = |
621 | d5dfae0a | Iustin Pop | fn puniq ==? fn (fn puniq) |
622 | fce98abd | Iustin Pop | where fn = PeerMap.add key em |
623 | 7bc82927 | Iustin Pop | puniq = PeerMap.accumArray const pmap |
624 | 15f4c8ca | Iustin Pop | |
625 | 525bfb36 | Iustin Pop | -- | Make sure remove is idempotent. |
626 | fce98abd | Iustin Pop | prop_PeerMap_removeIdempotent :: PeerMap.PeerMap -> PeerMap.Key -> Property |
627 | 15f4c8ca | Iustin Pop | prop_PeerMap_removeIdempotent pmap key = |
628 | d5dfae0a | Iustin Pop | fn puniq ==? fn (fn puniq) |
629 | fce98abd | Iustin Pop | where fn = PeerMap.remove key |
630 | 15f4c8ca | Iustin Pop | puniq = PeerMap.accumArray const pmap |
631 | 15f4c8ca | Iustin Pop | |
632 | 525bfb36 | Iustin Pop | -- | Make sure a missing item returns 0. |
633 | fce98abd | Iustin Pop | prop_PeerMap_findMissing :: PeerMap.PeerMap -> PeerMap.Key -> Property |
634 | 15f4c8ca | Iustin Pop | prop_PeerMap_findMissing pmap key = |
635 | d5dfae0a | Iustin Pop | PeerMap.find key (PeerMap.remove key puniq) ==? 0 |
636 | fce98abd | Iustin Pop | where puniq = PeerMap.accumArray const pmap |
637 | 15f4c8ca | Iustin Pop | |
638 | 525bfb36 | Iustin Pop | -- | Make sure an added item is found. |
639 | fce98abd | Iustin Pop | prop_PeerMap_addFind :: PeerMap.PeerMap |
640 | fce98abd | Iustin Pop | -> PeerMap.Key -> PeerMap.Elem -> Property |
641 | fbb95f28 | Iustin Pop | prop_PeerMap_addFind pmap key em = |
642 | d5dfae0a | Iustin Pop | PeerMap.find key (PeerMap.add key em puniq) ==? em |
643 | fce98abd | Iustin Pop | where puniq = PeerMap.accumArray const pmap |
644 | 15f4c8ca | Iustin Pop | |
645 | 525bfb36 | Iustin Pop | -- | Manual check that maxElem returns the maximum indeed, or 0 for null. |
646 | fce98abd | Iustin Pop | prop_PeerMap_maxElem :: PeerMap.PeerMap -> Property |
647 | 15f4c8ca | Iustin Pop | prop_PeerMap_maxElem pmap = |
648 | d5dfae0a | Iustin Pop | PeerMap.maxElem puniq ==? if null puniq then 0 |
649 | 72bb6b4e | Iustin Pop | else (maximum . snd . unzip) puniq |
650 | fce98abd | Iustin Pop | where puniq = PeerMap.accumArray const pmap |
651 | 15f4c8ca | Iustin Pop | |
652 | 525bfb36 | Iustin Pop | -- | List of tests for the PeerMap module. |
653 | 23fe06c2 | Iustin Pop | testSuite "PeerMap" |
654 | d5dfae0a | Iustin Pop | [ 'prop_PeerMap_addIdempotent |
655 | d5dfae0a | Iustin Pop | , 'prop_PeerMap_removeIdempotent |
656 | d5dfae0a | Iustin Pop | , 'prop_PeerMap_maxElem |
657 | d5dfae0a | Iustin Pop | , 'prop_PeerMap_addFind |
658 | d5dfae0a | Iustin Pop | , 'prop_PeerMap_findMissing |
659 | d5dfae0a | Iustin Pop | ] |
660 | 7dd5ee6c | Iustin Pop | |
661 | 525bfb36 | Iustin Pop | -- ** Container tests |
662 | 095d7ac0 | Iustin Pop | |
663 | 3603605a | Iustin Pop | -- we silence the following due to hlint bug fixed in later versions |
664 | 3603605a | Iustin Pop | {-# ANN prop_Container_addTwo "HLint: ignore Avoid lambda" #-} |
665 | fce98abd | Iustin Pop | prop_Container_addTwo :: [Container.Key] -> Int -> Int -> Bool |
666 | 095d7ac0 | Iustin Pop | prop_Container_addTwo cdata i1 i2 = |
667 | d5dfae0a | Iustin Pop | fn i1 i2 cont == fn i2 i1 cont && |
668 | d5dfae0a | Iustin Pop | fn i1 i2 cont == fn i1 i2 (fn i1 i2 cont) |
669 | fce98abd | Iustin Pop | where cont = foldl (\c x -> Container.add x x c) Container.empty cdata |
670 | 095d7ac0 | Iustin Pop | fn x1 x2 = Container.addTwo x1 x1 x2 x2 |
671 | 095d7ac0 | Iustin Pop | |
672 | fce98abd | Iustin Pop | prop_Container_nameOf :: Node.Node -> Property |
673 | 5ef78537 | Iustin Pop | prop_Container_nameOf node = |
674 | 5ef78537 | Iustin Pop | let nl = makeSmallCluster node 1 |
675 | 5ef78537 | Iustin Pop | fnode = head (Container.elems nl) |
676 | 72bb6b4e | Iustin Pop | in Container.nameOf nl (Node.idx fnode) ==? Node.name fnode |
677 | 5ef78537 | Iustin Pop | |
678 | 525bfb36 | Iustin Pop | -- | We test that in a cluster, given a random node, we can find it by |
679 | 5ef78537 | Iustin Pop | -- its name and alias, as long as all names and aliases are unique, |
680 | 525bfb36 | Iustin Pop | -- and that we fail to find a non-existing name. |
681 | fce98abd | Iustin Pop | prop_Container_findByName :: Node.Node -> Property |
682 | a2a0bcd8 | Iustin Pop | prop_Container_findByName node = |
683 | 5ef78537 | Iustin Pop | forAll (choose (1, 20)) $ \ cnt -> |
684 | 5ef78537 | Iustin Pop | forAll (choose (0, cnt - 1)) $ \ fidx -> |
685 | a2a0bcd8 | Iustin Pop | forAll (genUniquesList (cnt * 2)) $ \ allnames -> |
686 | a2a0bcd8 | Iustin Pop | forAll (arbitrary `suchThat` (`notElem` allnames)) $ \ othername -> |
687 | a2a0bcd8 | Iustin Pop | let names = zip (take cnt allnames) (drop cnt allnames) |
688 | a2a0bcd8 | Iustin Pop | nl = makeSmallCluster node cnt |
689 | 5ef78537 | Iustin Pop | nodes = Container.elems nl |
690 | 5ef78537 | Iustin Pop | nodes' = map (\((name, alias), nn) -> (Node.idx nn, |
691 | 5ef78537 | Iustin Pop | nn { Node.name = name, |
692 | 5ef78537 | Iustin Pop | Node.alias = alias })) |
693 | 5ef78537 | Iustin Pop | $ zip names nodes |
694 | cb0c77ff | Iustin Pop | nl' = Container.fromList nodes' |
695 | 5ef78537 | Iustin Pop | target = snd (nodes' !! fidx) |
696 | 5ef78537 | Iustin Pop | in Container.findByName nl' (Node.name target) == Just target && |
697 | 5ef78537 | Iustin Pop | Container.findByName nl' (Node.alias target) == Just target && |
698 | 3603605a | Iustin Pop | isNothing (Container.findByName nl' othername) |
699 | 5ef78537 | Iustin Pop | |
700 | 23fe06c2 | Iustin Pop | testSuite "Container" |
701 | d5dfae0a | Iustin Pop | [ 'prop_Container_addTwo |
702 | d5dfae0a | Iustin Pop | , 'prop_Container_nameOf |
703 | d5dfae0a | Iustin Pop | , 'prop_Container_findByName |
704 | d5dfae0a | Iustin Pop | ] |
705 | 095d7ac0 | Iustin Pop | |
706 | 525bfb36 | Iustin Pop | -- ** Instance tests |
707 | 525bfb36 | Iustin Pop | |
708 | 7bc82927 | Iustin Pop | -- Simple instance tests, we only have setter/getters |
709 | 7bc82927 | Iustin Pop | |
710 | fce98abd | Iustin Pop | prop_Instance_creat :: Instance.Instance -> Property |
711 | 39d11971 | Iustin Pop | prop_Instance_creat inst = |
712 | d5dfae0a | Iustin Pop | Instance.name inst ==? Instance.alias inst |
713 | 39d11971 | Iustin Pop | |
714 | fce98abd | Iustin Pop | prop_Instance_setIdx :: Instance.Instance -> Types.Idx -> Property |
715 | 7bc82927 | Iustin Pop | prop_Instance_setIdx inst idx = |
716 | d5dfae0a | Iustin Pop | Instance.idx (Instance.setIdx inst idx) ==? idx |
717 | 7bc82927 | Iustin Pop | |
718 | fce98abd | Iustin Pop | prop_Instance_setName :: Instance.Instance -> String -> Bool |
719 | 7bc82927 | Iustin Pop | prop_Instance_setName inst name = |
720 | d5dfae0a | Iustin Pop | Instance.name newinst == name && |
721 | d5dfae0a | Iustin Pop | Instance.alias newinst == name |
722 | fce98abd | Iustin Pop | where newinst = Instance.setName inst name |
723 | 39d11971 | Iustin Pop | |
724 | fce98abd | Iustin Pop | prop_Instance_setAlias :: Instance.Instance -> String -> Bool |
725 | 39d11971 | Iustin Pop | prop_Instance_setAlias inst name = |
726 | d5dfae0a | Iustin Pop | Instance.name newinst == Instance.name inst && |
727 | d5dfae0a | Iustin Pop | Instance.alias newinst == name |
728 | fce98abd | Iustin Pop | where newinst = Instance.setAlias inst name |
729 | 7bc82927 | Iustin Pop | |
730 | fce98abd | Iustin Pop | prop_Instance_setPri :: Instance.Instance -> Types.Ndx -> Property |
731 | 7bc82927 | Iustin Pop | prop_Instance_setPri inst pdx = |
732 | d5dfae0a | Iustin Pop | Instance.pNode (Instance.setPri inst pdx) ==? pdx |
733 | 7bc82927 | Iustin Pop | |
734 | fce98abd | Iustin Pop | prop_Instance_setSec :: Instance.Instance -> Types.Ndx -> Property |
735 | 7bc82927 | Iustin Pop | prop_Instance_setSec inst sdx = |
736 | d5dfae0a | Iustin Pop | Instance.sNode (Instance.setSec inst sdx) ==? sdx |
737 | 7bc82927 | Iustin Pop | |
738 | fce98abd | Iustin Pop | prop_Instance_setBoth :: Instance.Instance -> Types.Ndx -> Types.Ndx -> Bool |
739 | 7bc82927 | Iustin Pop | prop_Instance_setBoth inst pdx sdx = |
740 | d5dfae0a | Iustin Pop | Instance.pNode si == pdx && Instance.sNode si == sdx |
741 | fce98abd | Iustin Pop | where si = Instance.setBoth inst pdx sdx |
742 | 7bc82927 | Iustin Pop | |
743 | fce98abd | Iustin Pop | prop_Instance_shrinkMG :: Instance.Instance -> Property |
744 | 8fcf251f | Iustin Pop | prop_Instance_shrinkMG inst = |
745 | d5dfae0a | Iustin Pop | Instance.mem inst >= 2 * Types.unitMem ==> |
746 | d5dfae0a | Iustin Pop | case Instance.shrinkByType inst Types.FailMem of |
747 | d5dfae0a | Iustin Pop | Types.Ok inst' -> Instance.mem inst' == Instance.mem inst - Types.unitMem |
748 | d5dfae0a | Iustin Pop | _ -> False |
749 | 8fcf251f | Iustin Pop | |
750 | fce98abd | Iustin Pop | prop_Instance_shrinkMF :: Instance.Instance -> Property |
751 | 8fcf251f | Iustin Pop | prop_Instance_shrinkMF inst = |
752 | d5dfae0a | Iustin Pop | forAll (choose (0, 2 * Types.unitMem - 1)) $ \mem -> |
753 | 41085bd3 | Iustin Pop | let inst' = inst { Instance.mem = mem} |
754 | 41085bd3 | Iustin Pop | in Types.isBad $ Instance.shrinkByType inst' Types.FailMem |
755 | 8fcf251f | Iustin Pop | |
756 | fce98abd | Iustin Pop | prop_Instance_shrinkCG :: Instance.Instance -> Property |
757 | 8fcf251f | Iustin Pop | prop_Instance_shrinkCG inst = |
758 | d5dfae0a | Iustin Pop | Instance.vcpus inst >= 2 * Types.unitCpu ==> |
759 | d5dfae0a | Iustin Pop | case Instance.shrinkByType inst Types.FailCPU of |
760 | d5dfae0a | Iustin Pop | Types.Ok inst' -> |
761 | d5dfae0a | Iustin Pop | Instance.vcpus inst' == Instance.vcpus inst - Types.unitCpu |
762 | d5dfae0a | Iustin Pop | _ -> False |
763 | 8fcf251f | Iustin Pop | |
764 | fce98abd | Iustin Pop | prop_Instance_shrinkCF :: Instance.Instance -> Property |
765 | 8fcf251f | Iustin Pop | prop_Instance_shrinkCF inst = |
766 | d5dfae0a | Iustin Pop | forAll (choose (0, 2 * Types.unitCpu - 1)) $ \vcpus -> |
767 | 41085bd3 | Iustin Pop | let inst' = inst { Instance.vcpus = vcpus } |
768 | 41085bd3 | Iustin Pop | in Types.isBad $ Instance.shrinkByType inst' Types.FailCPU |
769 | 8fcf251f | Iustin Pop | |
770 | fce98abd | Iustin Pop | prop_Instance_shrinkDG :: Instance.Instance -> Property |
771 | 8fcf251f | Iustin Pop | prop_Instance_shrinkDG inst = |
772 | d5dfae0a | Iustin Pop | Instance.dsk inst >= 2 * Types.unitDsk ==> |
773 | d5dfae0a | Iustin Pop | case Instance.shrinkByType inst Types.FailDisk of |
774 | d5dfae0a | Iustin Pop | Types.Ok inst' -> |
775 | d5dfae0a | Iustin Pop | Instance.dsk inst' == Instance.dsk inst - Types.unitDsk |
776 | d5dfae0a | Iustin Pop | _ -> False |
777 | 8fcf251f | Iustin Pop | |
778 | fce98abd | Iustin Pop | prop_Instance_shrinkDF :: Instance.Instance -> Property |
779 | 8fcf251f | Iustin Pop | prop_Instance_shrinkDF inst = |
780 | d5dfae0a | Iustin Pop | forAll (choose (0, 2 * Types.unitDsk - 1)) $ \dsk -> |
781 | 41085bd3 | Iustin Pop | let inst' = inst { Instance.dsk = dsk } |
782 | 41085bd3 | Iustin Pop | in Types.isBad $ Instance.shrinkByType inst' Types.FailDisk |
783 | 8fcf251f | Iustin Pop | |
784 | fce98abd | Iustin Pop | prop_Instance_setMovable :: Instance.Instance -> Bool -> Property |
785 | 8fcf251f | Iustin Pop | prop_Instance_setMovable inst m = |
786 | d5dfae0a | Iustin Pop | Instance.movable inst' ==? m |
787 | 4a007641 | Iustin Pop | where inst' = Instance.setMovable inst m |
788 | 8fcf251f | Iustin Pop | |
789 | 23fe06c2 | Iustin Pop | testSuite "Instance" |
790 | d5dfae0a | Iustin Pop | [ 'prop_Instance_creat |
791 | d5dfae0a | Iustin Pop | , 'prop_Instance_setIdx |
792 | d5dfae0a | Iustin Pop | , 'prop_Instance_setName |
793 | d5dfae0a | Iustin Pop | , 'prop_Instance_setAlias |
794 | d5dfae0a | Iustin Pop | , 'prop_Instance_setPri |
795 | d5dfae0a | Iustin Pop | , 'prop_Instance_setSec |
796 | d5dfae0a | Iustin Pop | , 'prop_Instance_setBoth |
797 | d5dfae0a | Iustin Pop | , 'prop_Instance_shrinkMG |
798 | d5dfae0a | Iustin Pop | , 'prop_Instance_shrinkMF |
799 | d5dfae0a | Iustin Pop | , 'prop_Instance_shrinkCG |
800 | d5dfae0a | Iustin Pop | , 'prop_Instance_shrinkCF |
801 | d5dfae0a | Iustin Pop | , 'prop_Instance_shrinkDG |
802 | d5dfae0a | Iustin Pop | , 'prop_Instance_shrinkDF |
803 | d5dfae0a | Iustin Pop | , 'prop_Instance_setMovable |
804 | d5dfae0a | Iustin Pop | ] |
805 | 1ae7a904 | Iustin Pop | |
806 | e1dde6ad | Iustin Pop | -- ** Backends |
807 | e1dde6ad | Iustin Pop | |
808 | e1dde6ad | Iustin Pop | -- *** Text backend tests |
809 | 525bfb36 | Iustin Pop | |
810 | 1ae7a904 | Iustin Pop | -- Instance text loader tests |
811 | 1ae7a904 | Iustin Pop | |
812 | fce98abd | Iustin Pop | prop_Text_Load_Instance :: String -> Int -> Int -> Int -> Types.InstanceStatus |
813 | fce98abd | Iustin Pop | -> NonEmptyList Char -> [Char] |
814 | fce98abd | Iustin Pop | -> NonNegative Int -> NonNegative Int -> Bool |
815 | fce98abd | Iustin Pop | -> Types.DiskTemplate -> Int -> Property |
816 | a1cd7c1e | Iustin Pop | prop_Text_Load_Instance name mem dsk vcpus status |
817 | a1cd7c1e | Iustin Pop | (NonEmpty pnode) snode |
818 | 52cc1370 | Renรฉ Nussbaumer | (NonNegative pdx) (NonNegative sdx) autobal dt su = |
819 | d5dfae0a | Iustin Pop | pnode /= snode && pdx /= sdx ==> |
820 | d5dfae0a | Iustin Pop | let vcpus_s = show vcpus |
821 | d5dfae0a | Iustin Pop | dsk_s = show dsk |
822 | d5dfae0a | Iustin Pop | mem_s = show mem |
823 | 52cc1370 | Renรฉ Nussbaumer | su_s = show su |
824 | d5dfae0a | Iustin Pop | status_s = Types.instanceStatusToRaw status |
825 | d5dfae0a | Iustin Pop | ndx = if null snode |
826 | 39d11971 | Iustin Pop | then [(pnode, pdx)] |
827 | 309e7c9a | Iustin Pop | else [(pnode, pdx), (snode, sdx)] |
828 | d5dfae0a | Iustin Pop | nl = Data.Map.fromList ndx |
829 | d5dfae0a | Iustin Pop | tags = "" |
830 | d5dfae0a | Iustin Pop | sbal = if autobal then "Y" else "N" |
831 | d5dfae0a | Iustin Pop | sdt = Types.diskTemplateToRaw dt |
832 | d5dfae0a | Iustin Pop | inst = Text.loadInst nl |
833 | d5dfae0a | Iustin Pop | [name, mem_s, dsk_s, vcpus_s, status_s, |
834 | 52cc1370 | Renรฉ Nussbaumer | sbal, pnode, snode, sdt, tags, su_s] |
835 | d5dfae0a | Iustin Pop | fail1 = Text.loadInst nl |
836 | d5dfae0a | Iustin Pop | [name, mem_s, dsk_s, vcpus_s, status_s, |
837 | d5dfae0a | Iustin Pop | sbal, pnode, pnode, tags] |
838 | d5dfae0a | Iustin Pop | in case inst of |
839 | 96bc2003 | Iustin Pop | Types.Bad msg -> failTest $ "Failed to load instance: " ++ msg |
840 | d5dfae0a | Iustin Pop | Types.Ok (_, i) -> printTestCase "Mismatch in some field while\ |
841 | d5dfae0a | Iustin Pop | \ loading the instance" $ |
842 | d5dfae0a | Iustin Pop | Instance.name i == name && |
843 | d5dfae0a | Iustin Pop | Instance.vcpus i == vcpus && |
844 | d5dfae0a | Iustin Pop | Instance.mem i == mem && |
845 | d5dfae0a | Iustin Pop | Instance.pNode i == pdx && |
846 | d5dfae0a | Iustin Pop | Instance.sNode i == (if null snode |
847 | d5dfae0a | Iustin Pop | then Node.noSecondary |
848 | d5dfae0a | Iustin Pop | else sdx) && |
849 | d5dfae0a | Iustin Pop | Instance.autoBalance i == autobal && |
850 | ec629280 | Renรฉ Nussbaumer | Instance.spindleUse i == su && |
851 | d5dfae0a | Iustin Pop | Types.isBad fail1 |
852 | 39d11971 | Iustin Pop | |
853 | fce98abd | Iustin Pop | prop_Text_Load_InstanceFail :: [(String, Int)] -> [String] -> Property |
854 | 39d11971 | Iustin Pop | prop_Text_Load_InstanceFail ktn fields = |
855 | 52cc1370 | Renรฉ Nussbaumer | length fields /= 10 && length fields /= 11 ==> |
856 | bc782180 | Iustin Pop | case Text.loadInst nl fields of |
857 | 96bc2003 | Iustin Pop | Types.Ok _ -> failTest "Managed to load instance from invalid data" |
858 | 6429e8d8 | Iustin Pop | Types.Bad msg -> printTestCase ("Unrecognised error message: " ++ msg) $ |
859 | 6429e8d8 | Iustin Pop | "Invalid/incomplete instance data: '" `isPrefixOf` msg |
860 | 99b63608 | Iustin Pop | where nl = Data.Map.fromList ktn |
861 | 39d11971 | Iustin Pop | |
862 | fce98abd | Iustin Pop | prop_Text_Load_Node :: String -> Int -> Int -> Int -> Int -> Int |
863 | fce98abd | Iustin Pop | -> Int -> Bool -> Bool |
864 | 39d11971 | Iustin Pop | prop_Text_Load_Node name tm nm fm td fd tc fo = |
865 | d5dfae0a | Iustin Pop | let conv v = if v < 0 |
866 | d5dfae0a | Iustin Pop | then "?" |
867 | d5dfae0a | Iustin Pop | else show v |
868 | d5dfae0a | Iustin Pop | tm_s = conv tm |
869 | d5dfae0a | Iustin Pop | nm_s = conv nm |
870 | d5dfae0a | Iustin Pop | fm_s = conv fm |
871 | d5dfae0a | Iustin Pop | td_s = conv td |
872 | d5dfae0a | Iustin Pop | fd_s = conv fd |
873 | d5dfae0a | Iustin Pop | tc_s = conv tc |
874 | d5dfae0a | Iustin Pop | fo_s = if fo |
875 | 39d11971 | Iustin Pop | then "Y" |
876 | 39d11971 | Iustin Pop | else "N" |
877 | d5dfae0a | Iustin Pop | any_broken = any (< 0) [tm, nm, fm, td, fd, tc] |
878 | d5dfae0a | Iustin Pop | gid = Group.uuid defGroup |
879 | d5dfae0a | Iustin Pop | in case Text.loadNode defGroupAssoc |
880 | d5dfae0a | Iustin Pop | [name, tm_s, nm_s, fm_s, td_s, fd_s, tc_s, fo_s, gid] of |
881 | d5dfae0a | Iustin Pop | Nothing -> False |
882 | d5dfae0a | Iustin Pop | Just (name', node) -> |
883 | d5dfae0a | Iustin Pop | if fo || any_broken |
884 | d5dfae0a | Iustin Pop | then Node.offline node |
885 | d5dfae0a | Iustin Pop | else Node.name node == name' && name' == name && |
886 | d5dfae0a | Iustin Pop | Node.alias node == name && |
887 | d5dfae0a | Iustin Pop | Node.tMem node == fromIntegral tm && |
888 | d5dfae0a | Iustin Pop | Node.nMem node == nm && |
889 | d5dfae0a | Iustin Pop | Node.fMem node == fm && |
890 | d5dfae0a | Iustin Pop | Node.tDsk node == fromIntegral td && |
891 | d5dfae0a | Iustin Pop | Node.fDsk node == fd && |
892 | d5dfae0a | Iustin Pop | Node.tCpu node == fromIntegral tc |
893 | 39d11971 | Iustin Pop | |
894 | fce98abd | Iustin Pop | prop_Text_Load_NodeFail :: [String] -> Property |
895 | 39d11971 | Iustin Pop | prop_Text_Load_NodeFail fields = |
896 | d5dfae0a | Iustin Pop | length fields /= 8 ==> isNothing $ Text.loadNode Data.Map.empty fields |
897 | 1ae7a904 | Iustin Pop | |
898 | fce98abd | Iustin Pop | prop_Text_NodeLSIdempotent :: Node.Node -> Property |
899 | 50811e2c | Iustin Pop | prop_Text_NodeLSIdempotent node = |
900 | d5dfae0a | Iustin Pop | (Text.loadNode defGroupAssoc. |
901 | 487e1962 | Iustin Pop | Utils.sepSplit '|' . Text.serializeNode defGroupList) n ==? |
902 | d5dfae0a | Iustin Pop | Just (Node.name n, n) |
903 | 50811e2c | Iustin Pop | -- override failN1 to what loadNode returns by default |
904 | 487e1962 | Iustin Pop | where n = Node.setPolicy Types.defIPolicy $ |
905 | 487e1962 | Iustin Pop | node { Node.failN1 = True, Node.offline = False } |
906 | 50811e2c | Iustin Pop | |
907 | fce98abd | Iustin Pop | prop_Text_ISpecIdempotent :: Types.ISpec -> Property |
908 | bcd17bf0 | Iustin Pop | prop_Text_ISpecIdempotent ispec = |
909 | bcd17bf0 | Iustin Pop | case Text.loadISpec "dummy" . Utils.sepSplit ',' . |
910 | bcd17bf0 | Iustin Pop | Text.serializeISpec $ ispec of |
911 | 96bc2003 | Iustin Pop | Types.Bad msg -> failTest $ "Failed to load ispec: " ++ msg |
912 | bcd17bf0 | Iustin Pop | Types.Ok ispec' -> ispec ==? ispec' |
913 | bcd17bf0 | Iustin Pop | |
914 | fce98abd | Iustin Pop | prop_Text_IPolicyIdempotent :: Types.IPolicy -> Property |
915 | bcd17bf0 | Iustin Pop | prop_Text_IPolicyIdempotent ipol = |
916 | bcd17bf0 | Iustin Pop | case Text.loadIPolicy . Utils.sepSplit '|' $ |
917 | bcd17bf0 | Iustin Pop | Text.serializeIPolicy owner ipol of |
918 | 96bc2003 | Iustin Pop | Types.Bad msg -> failTest $ "Failed to load ispec: " ++ msg |
919 | bcd17bf0 | Iustin Pop | Types.Ok res -> (owner, ipol) ==? res |
920 | bcd17bf0 | Iustin Pop | where owner = "dummy" |
921 | bcd17bf0 | Iustin Pop | |
922 | dce9bbb3 | Iustin Pop | -- | This property, while being in the text tests, does more than just |
923 | dce9bbb3 | Iustin Pop | -- test end-to-end the serialisation and loading back workflow; it |
924 | dce9bbb3 | Iustin Pop | -- also tests the Loader.mergeData and the actuall |
925 | dce9bbb3 | Iustin Pop | -- Cluster.iterateAlloc (for well-behaving w.r.t. instance |
926 | dce9bbb3 | Iustin Pop | -- allocations, not for the business logic). As such, it's a quite |
927 | dce9bbb3 | Iustin Pop | -- complex and slow test, and that's the reason we restrict it to |
928 | dce9bbb3 | Iustin Pop | -- small cluster sizes. |
929 | fce98abd | Iustin Pop | prop_Text_CreateSerialise :: Property |
930 | dce9bbb3 | Iustin Pop | prop_Text_CreateSerialise = |
931 | dce9bbb3 | Iustin Pop | forAll genTags $ \ctags -> |
932 | dce9bbb3 | Iustin Pop | forAll (choose (1, 20)) $ \maxiter -> |
933 | dce9bbb3 | Iustin Pop | forAll (choose (2, 10)) $ \count -> |
934 | dce9bbb3 | Iustin Pop | forAll genOnlineNode $ \node -> |
935 | 59ed268d | Iustin Pop | forAll (genInstanceSmallerThanNode node) $ \inst -> |
936 | a7667ba6 | Iustin Pop | let nl = makeSmallCluster node count |
937 | c6e8fb9c | Iustin Pop | reqnodes = Instance.requiredNodes $ Instance.diskTemplate inst |
938 | dce9bbb3 | Iustin Pop | in case Cluster.genAllocNodes defGroupList nl reqnodes True >>= \allocn -> |
939 | a7667ba6 | Iustin Pop | Cluster.iterateAlloc nl Container.empty (Just maxiter) inst allocn [] [] |
940 | dce9bbb3 | Iustin Pop | of |
941 | 96bc2003 | Iustin Pop | Types.Bad msg -> failTest $ "Failed to allocate: " ++ msg |
942 | dce9bbb3 | Iustin Pop | Types.Ok (_, _, _, [], _) -> printTestCase |
943 | dce9bbb3 | Iustin Pop | "Failed to allocate: no allocations" False |
944 | dce9bbb3 | Iustin Pop | Types.Ok (_, nl', il', _, _) -> |
945 | dce9bbb3 | Iustin Pop | let cdata = Loader.ClusterData defGroupList nl' il' ctags |
946 | dce9bbb3 | Iustin Pop | Types.defIPolicy |
947 | dce9bbb3 | Iustin Pop | saved = Text.serializeCluster cdata |
948 | dce9bbb3 | Iustin Pop | in case Text.parseData saved >>= Loader.mergeData [] [] [] [] of |
949 | 96bc2003 | Iustin Pop | Types.Bad msg -> failTest $ "Failed to load/merge: " ++ msg |
950 | dce9bbb3 | Iustin Pop | Types.Ok (Loader.ClusterData gl2 nl2 il2 ctags2 cpol2) -> |
951 | dce9bbb3 | Iustin Pop | ctags ==? ctags2 .&&. |
952 | dce9bbb3 | Iustin Pop | Types.defIPolicy ==? cpol2 .&&. |
953 | dce9bbb3 | Iustin Pop | il' ==? il2 .&&. |
954 | b37f4a76 | Iustin Pop | defGroupList ==? gl2 .&&. |
955 | b37f4a76 | Iustin Pop | nl' ==? nl2 |
956 | dce9bbb3 | Iustin Pop | |
957 | 23fe06c2 | Iustin Pop | testSuite "Text" |
958 | d5dfae0a | Iustin Pop | [ 'prop_Text_Load_Instance |
959 | d5dfae0a | Iustin Pop | , 'prop_Text_Load_InstanceFail |
960 | d5dfae0a | Iustin Pop | , 'prop_Text_Load_Node |
961 | d5dfae0a | Iustin Pop | , 'prop_Text_Load_NodeFail |
962 | d5dfae0a | Iustin Pop | , 'prop_Text_NodeLSIdempotent |
963 | bcd17bf0 | Iustin Pop | , 'prop_Text_ISpecIdempotent |
964 | bcd17bf0 | Iustin Pop | , 'prop_Text_IPolicyIdempotent |
965 | dce9bbb3 | Iustin Pop | , 'prop_Text_CreateSerialise |
966 | d5dfae0a | Iustin Pop | ] |
967 | 7dd5ee6c | Iustin Pop | |
968 | e1dde6ad | Iustin Pop | -- *** Simu backend |
969 | e1dde6ad | Iustin Pop | |
970 | e1dde6ad | Iustin Pop | -- | Generates a tuple of specs for simulation. |
971 | e1dde6ad | Iustin Pop | genSimuSpec :: Gen (String, Int, Int, Int, Int) |
972 | e1dde6ad | Iustin Pop | genSimuSpec = do |
973 | e1dde6ad | Iustin Pop | pol <- elements [C.allocPolicyPreferred, |
974 | e1dde6ad | Iustin Pop | C.allocPolicyLastResort, C.allocPolicyUnallocable, |
975 | e1dde6ad | Iustin Pop | "p", "a", "u"] |
976 | e1dde6ad | Iustin Pop | -- should be reasonable (nodes/group), bigger values only complicate |
977 | e1dde6ad | Iustin Pop | -- the display of failed tests, and we don't care (in this particular |
978 | e1dde6ad | Iustin Pop | -- test) about big node groups |
979 | e1dde6ad | Iustin Pop | nodes <- choose (0, 20) |
980 | e1dde6ad | Iustin Pop | dsk <- choose (0, maxDsk) |
981 | e1dde6ad | Iustin Pop | mem <- choose (0, maxMem) |
982 | e1dde6ad | Iustin Pop | cpu <- choose (0, maxCpu) |
983 | e1dde6ad | Iustin Pop | return (pol, nodes, dsk, mem, cpu) |
984 | e1dde6ad | Iustin Pop | |
985 | e1dde6ad | Iustin Pop | -- | Checks that given a set of corrects specs, we can load them |
986 | e1dde6ad | Iustin Pop | -- successfully, and that at high-level the values look right. |
987 | fce98abd | Iustin Pop | prop_SimuLoad :: Property |
988 | e1dde6ad | Iustin Pop | prop_SimuLoad = |
989 | e1dde6ad | Iustin Pop | forAll (choose (0, 10)) $ \ngroups -> |
990 | e1dde6ad | Iustin Pop | forAll (replicateM ngroups genSimuSpec) $ \specs -> |
991 | e1dde6ad | Iustin Pop | let strspecs = map (\(p, n, d, m, c) -> printf "%s,%d,%d,%d,%d" |
992 | e1dde6ad | Iustin Pop | p n d m c::String) specs |
993 | e1dde6ad | Iustin Pop | totnodes = sum $ map (\(_, n, _, _, _) -> n) specs |
994 | e1dde6ad | Iustin Pop | mdc_in = concatMap (\(_, n, d, m, c) -> |
995 | e1dde6ad | Iustin Pop | replicate n (fromIntegral m, fromIntegral d, |
996 | e1dde6ad | Iustin Pop | fromIntegral c, |
997 | fce98abd | Iustin Pop | fromIntegral m, fromIntegral d)) |
998 | fce98abd | Iustin Pop | specs :: [(Double, Double, Double, Int, Int)] |
999 | e1dde6ad | Iustin Pop | in case Simu.parseData strspecs of |
1000 | e1dde6ad | Iustin Pop | Types.Bad msg -> failTest $ "Failed to load specs: " ++ msg |
1001 | e1dde6ad | Iustin Pop | Types.Ok (Loader.ClusterData gl nl il tags ipol) -> |
1002 | e1dde6ad | Iustin Pop | let nodes = map snd $ IntMap.toAscList nl |
1003 | e1dde6ad | Iustin Pop | nidx = map Node.idx nodes |
1004 | e1dde6ad | Iustin Pop | mdc_out = map (\n -> (Node.tMem n, Node.tDsk n, Node.tCpu n, |
1005 | e1dde6ad | Iustin Pop | Node.fMem n, Node.fDsk n)) nodes |
1006 | e1dde6ad | Iustin Pop | in |
1007 | e1dde6ad | Iustin Pop | Container.size gl ==? ngroups .&&. |
1008 | e1dde6ad | Iustin Pop | Container.size nl ==? totnodes .&&. |
1009 | e1dde6ad | Iustin Pop | Container.size il ==? 0 .&&. |
1010 | e1dde6ad | Iustin Pop | length tags ==? 0 .&&. |
1011 | e1dde6ad | Iustin Pop | ipol ==? Types.defIPolicy .&&. |
1012 | e1dde6ad | Iustin Pop | nidx ==? [1..totnodes] .&&. |
1013 | e1dde6ad | Iustin Pop | mdc_in ==? mdc_out .&&. |
1014 | e1dde6ad | Iustin Pop | map Group.iPolicy (Container.elems gl) ==? |
1015 | e1dde6ad | Iustin Pop | replicate ngroups Types.defIPolicy |
1016 | e1dde6ad | Iustin Pop | |
1017 | e1dde6ad | Iustin Pop | testSuite "Simu" |
1018 | e1dde6ad | Iustin Pop | [ 'prop_SimuLoad |
1019 | e1dde6ad | Iustin Pop | ] |
1020 | e1dde6ad | Iustin Pop | |
1021 | 525bfb36 | Iustin Pop | -- ** Node tests |
1022 | 7dd5ee6c | Iustin Pop | |
1023 | fce98abd | Iustin Pop | prop_Node_setAlias :: Node.Node -> String -> Bool |
1024 | 82ea2874 | Iustin Pop | prop_Node_setAlias node name = |
1025 | d5dfae0a | Iustin Pop | Node.name newnode == Node.name node && |
1026 | d5dfae0a | Iustin Pop | Node.alias newnode == name |
1027 | fce98abd | Iustin Pop | where newnode = Node.setAlias node name |
1028 | 82ea2874 | Iustin Pop | |
1029 | fce98abd | Iustin Pop | prop_Node_setOffline :: Node.Node -> Bool -> Property |
1030 | 82ea2874 | Iustin Pop | prop_Node_setOffline node status = |
1031 | d5dfae0a | Iustin Pop | Node.offline newnode ==? status |
1032 | 82ea2874 | Iustin Pop | where newnode = Node.setOffline node status |
1033 | 82ea2874 | Iustin Pop | |
1034 | fce98abd | Iustin Pop | prop_Node_setXmem :: Node.Node -> Int -> Property |
1035 | 82ea2874 | Iustin Pop | prop_Node_setXmem node xm = |
1036 | d5dfae0a | Iustin Pop | Node.xMem newnode ==? xm |
1037 | 82ea2874 | Iustin Pop | where newnode = Node.setXmem node xm |
1038 | 82ea2874 | Iustin Pop | |
1039 | fce98abd | Iustin Pop | prop_Node_setMcpu :: Node.Node -> Double -> Property |
1040 | 82ea2874 | Iustin Pop | prop_Node_setMcpu node mc = |
1041 | 487e1962 | Iustin Pop | Types.iPolicyVcpuRatio (Node.iPolicy newnode) ==? mc |
1042 | 82ea2874 | Iustin Pop | where newnode = Node.setMcpu node mc |
1043 | 82ea2874 | Iustin Pop | |
1044 | 525bfb36 | Iustin Pop | -- | Check that an instance add with too high memory or disk will be |
1045 | 525bfb36 | Iustin Pop | -- rejected. |
1046 | fce98abd | Iustin Pop | prop_Node_addPriFM :: Node.Node -> Instance.Instance -> Property |
1047 | d5dfae0a | Iustin Pop | prop_Node_addPriFM node inst = |
1048 | d5dfae0a | Iustin Pop | Instance.mem inst >= Node.fMem node && not (Node.failN1 node) && |
1049 | 7959cbb9 | Iustin Pop | not (Instance.isOffline inst) ==> |
1050 | d5dfae0a | Iustin Pop | case Node.addPri node inst'' of |
1051 | d5dfae0a | Iustin Pop | Types.OpFail Types.FailMem -> True |
1052 | d5dfae0a | Iustin Pop | _ -> False |
1053 | fce98abd | Iustin Pop | where inst' = setInstanceSmallerThanNode node inst |
1054 | d5dfae0a | Iustin Pop | inst'' = inst' { Instance.mem = Instance.mem inst } |
1055 | d5dfae0a | Iustin Pop | |
1056 | 53bddadd | Iustin Pop | -- | Check that adding a primary instance with too much disk fails |
1057 | 53bddadd | Iustin Pop | -- with type FailDisk. |
1058 | fce98abd | Iustin Pop | prop_Node_addPriFD :: Node.Node -> Instance.Instance -> Property |
1059 | d5dfae0a | Iustin Pop | prop_Node_addPriFD node inst = |
1060 | 53bddadd | Iustin Pop | forAll (elements Instance.localStorageTemplates) $ \dt -> |
1061 | d5dfae0a | Iustin Pop | Instance.dsk inst >= Node.fDsk node && not (Node.failN1 node) ==> |
1062 | 53bddadd | Iustin Pop | let inst' = setInstanceSmallerThanNode node inst |
1063 | 53bddadd | Iustin Pop | inst'' = inst' { Instance.dsk = Instance.dsk inst |
1064 | 53bddadd | Iustin Pop | , Instance.diskTemplate = dt } |
1065 | 53bddadd | Iustin Pop | in case Node.addPri node inst'' of |
1066 | 53bddadd | Iustin Pop | Types.OpFail Types.FailDisk -> True |
1067 | 53bddadd | Iustin Pop | _ -> False |
1068 | 53bddadd | Iustin Pop | |
1069 | 53bddadd | Iustin Pop | -- | Check that adding a primary instance with too many VCPUs fails |
1070 | 53bddadd | Iustin Pop | -- with type FailCPU. |
1071 | fce98abd | Iustin Pop | prop_Node_addPriFC :: Property |
1072 | 3c1e4af0 | Iustin Pop | prop_Node_addPriFC = |
1073 | 3c1e4af0 | Iustin Pop | forAll (choose (1, maxCpu)) $ \extra -> |
1074 | 746b7aa6 | Iustin Pop | forAll genOnlineNode $ \node -> |
1075 | 7959cbb9 | Iustin Pop | forAll (arbitrary `suchThat` Instance.notOffline) $ \inst -> |
1076 | 746b7aa6 | Iustin Pop | let inst' = setInstanceSmallerThanNode node inst |
1077 | 746b7aa6 | Iustin Pop | inst'' = inst' { Instance.vcpus = Node.availCpu node + extra } |
1078 | 746b7aa6 | Iustin Pop | in case Node.addPri node inst'' of |
1079 | 746b7aa6 | Iustin Pop | Types.OpFail Types.FailCPU -> property True |
1080 | 746b7aa6 | Iustin Pop | v -> failTest $ "Expected OpFail FailCPU, but got " ++ show v |
1081 | 7bc82927 | Iustin Pop | |
1082 | 525bfb36 | Iustin Pop | -- | Check that an instance add with too high memory or disk will be |
1083 | 525bfb36 | Iustin Pop | -- rejected. |
1084 | fce98abd | Iustin Pop | prop_Node_addSec :: Node.Node -> Instance.Instance -> Int -> Property |
1085 | 15f4c8ca | Iustin Pop | prop_Node_addSec node inst pdx = |
1086 | d5dfae0a | Iustin Pop | ((Instance.mem inst >= (Node.fMem node - Node.rMem node) && |
1087 | 7959cbb9 | Iustin Pop | not (Instance.isOffline inst)) || |
1088 | d5dfae0a | Iustin Pop | Instance.dsk inst >= Node.fDsk node) && |
1089 | d5dfae0a | Iustin Pop | not (Node.failN1 node) ==> |
1090 | d5dfae0a | Iustin Pop | isFailure (Node.addSec node inst pdx) |
1091 | 7dd5ee6c | Iustin Pop | |
1092 | 45c4d54d | Iustin Pop | -- | Check that an offline instance with reasonable disk size but |
1093 | 45c4d54d | Iustin Pop | -- extra mem/cpu can always be added. |
1094 | fce98abd | Iustin Pop | prop_Node_addOfflinePri :: NonNegative Int -> NonNegative Int -> Property |
1095 | c6b7e804 | Iustin Pop | prop_Node_addOfflinePri (NonNegative extra_mem) (NonNegative extra_cpu) = |
1096 | a2a0bcd8 | Iustin Pop | forAll genOnlineNode $ \node -> |
1097 | 45c4d54d | Iustin Pop | forAll (genInstanceSmallerThanNode node) $ \inst -> |
1098 | 45c4d54d | Iustin Pop | let inst' = inst { Instance.runSt = Types.AdminOffline |
1099 | 45c4d54d | Iustin Pop | , Instance.mem = Node.availMem node + extra_mem |
1100 | 45c4d54d | Iustin Pop | , Instance.vcpus = Node.availCpu node + extra_cpu } |
1101 | c6b7e804 | Iustin Pop | in case Node.addPri node inst' of |
1102 | c6b7e804 | Iustin Pop | Types.OpGood _ -> property True |
1103 | c6b7e804 | Iustin Pop | v -> failTest $ "Expected OpGood, but got: " ++ show v |
1104 | c6b7e804 | Iustin Pop | |
1105 | c6b7e804 | Iustin Pop | -- | Check that an offline instance with reasonable disk size but |
1106 | c6b7e804 | Iustin Pop | -- extra mem/cpu can always be added. |
1107 | fce98abd | Iustin Pop | prop_Node_addOfflineSec :: NonNegative Int -> NonNegative Int |
1108 | fce98abd | Iustin Pop | -> Types.Ndx -> Property |
1109 | c6b7e804 | Iustin Pop | prop_Node_addOfflineSec (NonNegative extra_mem) (NonNegative extra_cpu) pdx = |
1110 | c6b7e804 | Iustin Pop | forAll genOnlineNode $ \node -> |
1111 | c6b7e804 | Iustin Pop | forAll (genInstanceSmallerThanNode node) $ \inst -> |
1112 | c6b7e804 | Iustin Pop | let inst' = inst { Instance.runSt = Types.AdminOffline |
1113 | c6b7e804 | Iustin Pop | , Instance.mem = Node.availMem node + extra_mem |
1114 | c6b7e804 | Iustin Pop | , Instance.vcpus = Node.availCpu node + extra_cpu |
1115 | c6b7e804 | Iustin Pop | , Instance.diskTemplate = Types.DTDrbd8 } |
1116 | c6b7e804 | Iustin Pop | in case Node.addSec node inst' pdx of |
1117 | c6b7e804 | Iustin Pop | Types.OpGood _ -> property True |
1118 | 45c4d54d | Iustin Pop | v -> failTest $ "Expected OpGood/OpGood, but got: " ++ show v |
1119 | 61bbbed7 | Agata Murawska | |
1120 | 525bfb36 | Iustin Pop | -- | Checks for memory reservation changes. |
1121 | fce98abd | Iustin Pop | prop_Node_rMem :: Instance.Instance -> Property |
1122 | 752635d3 | Iustin Pop | prop_Node_rMem inst = |
1123 | 7959cbb9 | Iustin Pop | not (Instance.isOffline inst) ==> |
1124 | 5c52dae6 | Iustin Pop | forAll (genOnlineNode `suchThat` ((> Types.unitMem) . Node.fMem)) $ \node -> |
1125 | d5dfae0a | Iustin Pop | -- ab = auto_balance, nb = non-auto_balance |
1126 | d5dfae0a | Iustin Pop | -- we use -1 as the primary node of the instance |
1127 | e7b4d0e1 | Iustin Pop | let inst' = inst { Instance.pNode = -1, Instance.autoBalance = True |
1128 | e7b4d0e1 | Iustin Pop | , Instance.diskTemplate = Types.DTDrbd8 } |
1129 | d5dfae0a | Iustin Pop | inst_ab = setInstanceSmallerThanNode node inst' |
1130 | d5dfae0a | Iustin Pop | inst_nb = inst_ab { Instance.autoBalance = False } |
1131 | d5dfae0a | Iustin Pop | -- now we have the two instances, identical except the |
1132 | d5dfae0a | Iustin Pop | -- autoBalance attribute |
1133 | d5dfae0a | Iustin Pop | orig_rmem = Node.rMem node |
1134 | d5dfae0a | Iustin Pop | inst_idx = Instance.idx inst_ab |
1135 | d5dfae0a | Iustin Pop | node_add_ab = Node.addSec node inst_ab (-1) |
1136 | d5dfae0a | Iustin Pop | node_add_nb = Node.addSec node inst_nb (-1) |
1137 | d5dfae0a | Iustin Pop | node_del_ab = liftM (`Node.removeSec` inst_ab) node_add_ab |
1138 | d5dfae0a | Iustin Pop | node_del_nb = liftM (`Node.removeSec` inst_nb) node_add_nb |
1139 | d5dfae0a | Iustin Pop | in case (node_add_ab, node_add_nb, node_del_ab, node_del_nb) of |
1140 | d5dfae0a | Iustin Pop | (Types.OpGood a_ab, Types.OpGood a_nb, |
1141 | d5dfae0a | Iustin Pop | Types.OpGood d_ab, Types.OpGood d_nb) -> |
1142 | d5dfae0a | Iustin Pop | printTestCase "Consistency checks failed" $ |
1143 | d5dfae0a | Iustin Pop | Node.rMem a_ab > orig_rmem && |
1144 | d5dfae0a | Iustin Pop | Node.rMem a_ab - orig_rmem == Instance.mem inst_ab && |
1145 | d5dfae0a | Iustin Pop | Node.rMem a_nb == orig_rmem && |
1146 | d5dfae0a | Iustin Pop | Node.rMem d_ab == orig_rmem && |
1147 | d5dfae0a | Iustin Pop | Node.rMem d_nb == orig_rmem && |
1148 | d5dfae0a | Iustin Pop | -- this is not related to rMem, but as good a place to |
1149 | d5dfae0a | Iustin Pop | -- test as any |
1150 | d5dfae0a | Iustin Pop | inst_idx `elem` Node.sList a_ab && |
1151 | 3603605a | Iustin Pop | inst_idx `notElem` Node.sList d_ab |
1152 | 96bc2003 | Iustin Pop | x -> failTest $ "Failed to add/remove instances: " ++ show x |
1153 | 9cbc1edb | Iustin Pop | |
1154 | 525bfb36 | Iustin Pop | -- | Check mdsk setting. |
1155 | fce98abd | Iustin Pop | prop_Node_setMdsk :: Node.Node -> SmallRatio -> Bool |
1156 | 8fcf251f | Iustin Pop | prop_Node_setMdsk node mx = |
1157 | d5dfae0a | Iustin Pop | Node.loDsk node' >= 0 && |
1158 | d5dfae0a | Iustin Pop | fromIntegral (Node.loDsk node') <= Node.tDsk node && |
1159 | d5dfae0a | Iustin Pop | Node.availDisk node' >= 0 && |
1160 | d5dfae0a | Iustin Pop | Node.availDisk node' <= Node.fDsk node' && |
1161 | d5dfae0a | Iustin Pop | fromIntegral (Node.availDisk node') <= Node.tDsk node' && |
1162 | d5dfae0a | Iustin Pop | Node.mDsk node' == mx' |
1163 | fce98abd | Iustin Pop | where node' = Node.setMdsk node mx' |
1164 | 8fcf251f | Iustin Pop | SmallRatio mx' = mx |
1165 | 8fcf251f | Iustin Pop | |
1166 | 8fcf251f | Iustin Pop | -- Check tag maps |
1167 | fce98abd | Iustin Pop | prop_Node_tagMaps_idempotent :: Property |
1168 | 15e3d31c | Iustin Pop | prop_Node_tagMaps_idempotent = |
1169 | 15e3d31c | Iustin Pop | forAll genTags $ \tags -> |
1170 | d5dfae0a | Iustin Pop | Node.delTags (Node.addTags m tags) tags ==? m |
1171 | 4a007641 | Iustin Pop | where m = Data.Map.empty |
1172 | 8fcf251f | Iustin Pop | |
1173 | fce98abd | Iustin Pop | prop_Node_tagMaps_reject :: Property |
1174 | 15e3d31c | Iustin Pop | prop_Node_tagMaps_reject = |
1175 | 15e3d31c | Iustin Pop | forAll (genTags `suchThat` (not . null)) $ \tags -> |
1176 | 15e3d31c | Iustin Pop | let m = Node.addTags Data.Map.empty tags |
1177 | 15e3d31c | Iustin Pop | in all (\t -> Node.rejectAddTags m [t]) tags |
1178 | 8fcf251f | Iustin Pop | |
1179 | fce98abd | Iustin Pop | prop_Node_showField :: Node.Node -> Property |
1180 | 82ea2874 | Iustin Pop | prop_Node_showField node = |
1181 | 82ea2874 | Iustin Pop | forAll (elements Node.defaultFields) $ \ field -> |
1182 | 82ea2874 | Iustin Pop | fst (Node.showHeader field) /= Types.unknownField && |
1183 | 82ea2874 | Iustin Pop | Node.showField node field /= Types.unknownField |
1184 | 82ea2874 | Iustin Pop | |
1185 | fce98abd | Iustin Pop | prop_Node_computeGroups :: [Node.Node] -> Bool |
1186 | d8bcd0a8 | Iustin Pop | prop_Node_computeGroups nodes = |
1187 | d8bcd0a8 | Iustin Pop | let ng = Node.computeGroups nodes |
1188 | d8bcd0a8 | Iustin Pop | onlyuuid = map fst ng |
1189 | d8bcd0a8 | Iustin Pop | in length nodes == sum (map (length . snd) ng) && |
1190 | d8bcd0a8 | Iustin Pop | all (\(guuid, ns) -> all ((== guuid) . Node.group) ns) ng && |
1191 | d8bcd0a8 | Iustin Pop | length (nub onlyuuid) == length onlyuuid && |
1192 | cc532bdd | Iustin Pop | (null nodes || not (null ng)) |
1193 | d8bcd0a8 | Iustin Pop | |
1194 | eae69eee | Iustin Pop | -- Check idempotence of add/remove operations |
1195 | fce98abd | Iustin Pop | prop_Node_addPri_idempotent :: Property |
1196 | eae69eee | Iustin Pop | prop_Node_addPri_idempotent = |
1197 | eae69eee | Iustin Pop | forAll genOnlineNode $ \node -> |
1198 | eae69eee | Iustin Pop | forAll (genInstanceSmallerThanNode node) $ \inst -> |
1199 | eae69eee | Iustin Pop | case Node.addPri node inst of |
1200 | eae69eee | Iustin Pop | Types.OpGood node' -> Node.removePri node' inst ==? node |
1201 | eae69eee | Iustin Pop | _ -> failTest "Can't add instance" |
1202 | eae69eee | Iustin Pop | |
1203 | fce98abd | Iustin Pop | prop_Node_addSec_idempotent :: Property |
1204 | eae69eee | Iustin Pop | prop_Node_addSec_idempotent = |
1205 | eae69eee | Iustin Pop | forAll genOnlineNode $ \node -> |
1206 | eae69eee | Iustin Pop | forAll (genInstanceSmallerThanNode node) $ \inst -> |
1207 | eae69eee | Iustin Pop | let pdx = Node.idx node + 1 |
1208 | eae69eee | Iustin Pop | inst' = Instance.setPri inst pdx |
1209 | 90669369 | Iustin Pop | inst'' = inst' { Instance.diskTemplate = Types.DTDrbd8 } |
1210 | 90669369 | Iustin Pop | in case Node.addSec node inst'' pdx of |
1211 | 90669369 | Iustin Pop | Types.OpGood node' -> Node.removeSec node' inst'' ==? node |
1212 | eae69eee | Iustin Pop | _ -> failTest "Can't add instance" |
1213 | eae69eee | Iustin Pop | |
1214 | 23fe06c2 | Iustin Pop | testSuite "Node" |
1215 | d5dfae0a | Iustin Pop | [ 'prop_Node_setAlias |
1216 | d5dfae0a | Iustin Pop | , 'prop_Node_setOffline |
1217 | d5dfae0a | Iustin Pop | , 'prop_Node_setMcpu |
1218 | d5dfae0a | Iustin Pop | , 'prop_Node_setXmem |
1219 | d5dfae0a | Iustin Pop | , 'prop_Node_addPriFM |
1220 | d5dfae0a | Iustin Pop | , 'prop_Node_addPriFD |
1221 | d5dfae0a | Iustin Pop | , 'prop_Node_addPriFC |
1222 | d5dfae0a | Iustin Pop | , 'prop_Node_addSec |
1223 | c6b7e804 | Iustin Pop | , 'prop_Node_addOfflinePri |
1224 | c6b7e804 | Iustin Pop | , 'prop_Node_addOfflineSec |
1225 | d5dfae0a | Iustin Pop | , 'prop_Node_rMem |
1226 | d5dfae0a | Iustin Pop | , 'prop_Node_setMdsk |
1227 | d5dfae0a | Iustin Pop | , 'prop_Node_tagMaps_idempotent |
1228 | d5dfae0a | Iustin Pop | , 'prop_Node_tagMaps_reject |
1229 | d5dfae0a | Iustin Pop | , 'prop_Node_showField |
1230 | d5dfae0a | Iustin Pop | , 'prop_Node_computeGroups |
1231 | eae69eee | Iustin Pop | , 'prop_Node_addPri_idempotent |
1232 | eae69eee | Iustin Pop | , 'prop_Node_addSec_idempotent |
1233 | d5dfae0a | Iustin Pop | ] |
1234 | cf35a869 | Iustin Pop | |
1235 | 525bfb36 | Iustin Pop | -- ** Cluster tests |
1236 | cf35a869 | Iustin Pop | |
1237 | 525bfb36 | Iustin Pop | -- | Check that the cluster score is close to zero for a homogeneous |
1238 | 525bfb36 | Iustin Pop | -- cluster. |
1239 | fce98abd | Iustin Pop | prop_Score_Zero :: Node.Node -> Property |
1240 | 8e4f6d56 | Iustin Pop | prop_Score_Zero node = |
1241 | d5dfae0a | Iustin Pop | forAll (choose (1, 1024)) $ \count -> |
1242 | 3a3c1eb4 | Iustin Pop | (not (Node.offline node) && not (Node.failN1 node) && (count > 0) && |
1243 | 2060348b | Iustin Pop | (Node.tDsk node > 0) && (Node.tMem node > 0)) ==> |
1244 | d5dfae0a | Iustin Pop | let fn = Node.buildPeers node Container.empty |
1245 | d5dfae0a | Iustin Pop | nlst = replicate count fn |
1246 | d5dfae0a | Iustin Pop | score = Cluster.compCVNodes nlst |
1247 | d5dfae0a | Iustin Pop | -- we can't say == 0 here as the floating point errors accumulate; |
1248 | d5dfae0a | Iustin Pop | -- this should be much lower than the default score in CLI.hs |
1249 | d5dfae0a | Iustin Pop | in score <= 1e-12 |
1250 | cf35a869 | Iustin Pop | |
1251 | 525bfb36 | Iustin Pop | -- | Check that cluster stats are sane. |
1252 | fce98abd | Iustin Pop | prop_CStats_sane :: Property |
1253 | d6f9f5bd | Iustin Pop | prop_CStats_sane = |
1254 | d5dfae0a | Iustin Pop | forAll (choose (1, 1024)) $ \count -> |
1255 | d6f9f5bd | Iustin Pop | forAll genOnlineNode $ \node -> |
1256 | d5dfae0a | Iustin Pop | let fn = Node.buildPeers node Container.empty |
1257 | d5dfae0a | Iustin Pop | nlst = zip [1..] $ replicate count fn::[(Types.Ndx, Node.Node)] |
1258 | d5dfae0a | Iustin Pop | nl = Container.fromList nlst |
1259 | d5dfae0a | Iustin Pop | cstats = Cluster.totalResources nl |
1260 | d5dfae0a | Iustin Pop | in Cluster.csAdsk cstats >= 0 && |
1261 | d5dfae0a | Iustin Pop | Cluster.csAdsk cstats <= Cluster.csFdsk cstats |
1262 | 8fcf251f | Iustin Pop | |
1263 | 3fea6959 | Iustin Pop | -- | Check that one instance is allocated correctly, without |
1264 | 525bfb36 | Iustin Pop | -- rebalances needed. |
1265 | fce98abd | Iustin Pop | prop_ClusterAlloc_sane :: Instance.Instance -> Property |
1266 | d6f9f5bd | Iustin Pop | prop_ClusterAlloc_sane inst = |
1267 | d5dfae0a | Iustin Pop | forAll (choose (5, 20)) $ \count -> |
1268 | d6f9f5bd | Iustin Pop | forAll genOnlineNode $ \node -> |
1269 | 3603605a | Iustin Pop | let (nl, il, inst') = makeSmallEmptyCluster node count inst |
1270 | c6e8fb9c | Iustin Pop | reqnodes = Instance.requiredNodes $ Instance.diskTemplate inst |
1271 | c6e8fb9c | Iustin Pop | in case Cluster.genAllocNodes defGroupList nl reqnodes True >>= |
1272 | d5dfae0a | Iustin Pop | Cluster.tryAlloc nl il inst' of |
1273 | d5dfae0a | Iustin Pop | Types.Bad _ -> False |
1274 | d5dfae0a | Iustin Pop | Types.Ok as -> |
1275 | d5dfae0a | Iustin Pop | case Cluster.asSolution as of |
1276 | d5dfae0a | Iustin Pop | Nothing -> False |
1277 | d5dfae0a | Iustin Pop | Just (xnl, xi, _, cv) -> |
1278 | d5dfae0a | Iustin Pop | let il' = Container.add (Instance.idx xi) xi il |
1279 | d5dfae0a | Iustin Pop | tbl = Cluster.Table xnl il' cv [] |
1280 | d5dfae0a | Iustin Pop | in not (canBalance tbl True True False) |
1281 | 3fea6959 | Iustin Pop | |
1282 | 3fea6959 | Iustin Pop | -- | Checks that on a 2-5 node cluster, we can allocate a random |
1283 | 3fea6959 | Iustin Pop | -- instance spec via tiered allocation (whatever the original instance |
1284 | 37483aa5 | Iustin Pop | -- spec), on either one or two nodes. Furthermore, we test that |
1285 | 37483aa5 | Iustin Pop | -- computed allocation statistics are correct. |
1286 | fce98abd | Iustin Pop | prop_ClusterCanTieredAlloc :: Instance.Instance -> Property |
1287 | d6f9f5bd | Iustin Pop | prop_ClusterCanTieredAlloc inst = |
1288 | d5dfae0a | Iustin Pop | forAll (choose (2, 5)) $ \count -> |
1289 | d6f9f5bd | Iustin Pop | forAll (genOnlineNode `suchThat` (isNodeBig 4)) $ \node -> |
1290 | d5dfae0a | Iustin Pop | let nl = makeSmallCluster node count |
1291 | d5dfae0a | Iustin Pop | il = Container.empty |
1292 | c6e8fb9c | Iustin Pop | rqnodes = Instance.requiredNodes $ Instance.diskTemplate inst |
1293 | d5dfae0a | Iustin Pop | allocnodes = Cluster.genAllocNodes defGroupList nl rqnodes True |
1294 | d5dfae0a | Iustin Pop | in case allocnodes >>= \allocnodes' -> |
1295 | d5dfae0a | Iustin Pop | Cluster.tieredAlloc nl il (Just 1) inst allocnodes' [] [] of |
1296 | 37483aa5 | Iustin Pop | Types.Bad msg -> failTest $ "Failed to tiered alloc: " ++ msg |
1297 | 37483aa5 | Iustin Pop | Types.Ok (_, nl', il', ixes, cstats) -> |
1298 | 37483aa5 | Iustin Pop | let (ai_alloc, ai_pool, ai_unav) = |
1299 | 37483aa5 | Iustin Pop | Cluster.computeAllocationDelta |
1300 | 37483aa5 | Iustin Pop | (Cluster.totalResources nl) |
1301 | 37483aa5 | Iustin Pop | (Cluster.totalResources nl') |
1302 | 37483aa5 | Iustin Pop | all_nodes = Container.elems nl |
1303 | 37483aa5 | Iustin Pop | in property (not (null ixes)) .&&. |
1304 | 37483aa5 | Iustin Pop | IntMap.size il' ==? length ixes .&&. |
1305 | 37483aa5 | Iustin Pop | length ixes ==? length cstats .&&. |
1306 | 37483aa5 | Iustin Pop | sum (map Types.allocInfoVCpus [ai_alloc, ai_pool, ai_unav]) ==? |
1307 | 37483aa5 | Iustin Pop | sum (map Node.hiCpu all_nodes) .&&. |
1308 | 37483aa5 | Iustin Pop | sum (map Types.allocInfoNCpus [ai_alloc, ai_pool, ai_unav]) ==? |
1309 | 37483aa5 | Iustin Pop | sum (map Node.tCpu all_nodes) .&&. |
1310 | 37483aa5 | Iustin Pop | sum (map Types.allocInfoMem [ai_alloc, ai_pool, ai_unav]) ==? |
1311 | 37483aa5 | Iustin Pop | truncate (sum (map Node.tMem all_nodes)) .&&. |
1312 | 37483aa5 | Iustin Pop | sum (map Types.allocInfoDisk [ai_alloc, ai_pool, ai_unav]) ==? |
1313 | 37483aa5 | Iustin Pop | truncate (sum (map Node.tDsk all_nodes)) |
1314 | 3fea6959 | Iustin Pop | |
1315 | 6a855aaa | Iustin Pop | -- | Helper function to create a cluster with the given range of nodes |
1316 | 6a855aaa | Iustin Pop | -- and allocate an instance on it. |
1317 | fce98abd | Iustin Pop | genClusterAlloc :: Int -> Node.Node -> Instance.Instance |
1318 | fce98abd | Iustin Pop | -> Types.Result (Node.List, Instance.List, Instance.Instance) |
1319 | 6a855aaa | Iustin Pop | genClusterAlloc count node inst = |
1320 | 6a855aaa | Iustin Pop | let nl = makeSmallCluster node count |
1321 | c6e8fb9c | Iustin Pop | reqnodes = Instance.requiredNodes $ Instance.diskTemplate inst |
1322 | c6e8fb9c | Iustin Pop | in case Cluster.genAllocNodes defGroupList nl reqnodes True >>= |
1323 | 6a855aaa | Iustin Pop | Cluster.tryAlloc nl Container.empty inst of |
1324 | 6a855aaa | Iustin Pop | Types.Bad _ -> Types.Bad "Can't allocate" |
1325 | d5dfae0a | Iustin Pop | Types.Ok as -> |
1326 | d5dfae0a | Iustin Pop | case Cluster.asSolution as of |
1327 | 6a855aaa | Iustin Pop | Nothing -> Types.Bad "Empty solution?" |
1328 | d5dfae0a | Iustin Pop | Just (xnl, xi, _, _) -> |
1329 | 6a855aaa | Iustin Pop | let xil = Container.add (Instance.idx xi) xi Container.empty |
1330 | 6a855aaa | Iustin Pop | in Types.Ok (xnl, xil, xi) |
1331 | 6a855aaa | Iustin Pop | |
1332 | 6a855aaa | Iustin Pop | -- | Checks that on a 4-8 node cluster, once we allocate an instance, |
1333 | 6a855aaa | Iustin Pop | -- we can also relocate it. |
1334 | fce98abd | Iustin Pop | prop_ClusterAllocRelocate :: Property |
1335 | 6a855aaa | Iustin Pop | prop_ClusterAllocRelocate = |
1336 | 6a855aaa | Iustin Pop | forAll (choose (4, 8)) $ \count -> |
1337 | 6a855aaa | Iustin Pop | forAll (genOnlineNode `suchThat` (isNodeBig 4)) $ \node -> |
1338 | 7018af9c | Iustin Pop | forAll (genInstanceSmallerThanNode node `suchThat` isMirrored) $ \inst -> |
1339 | 6a855aaa | Iustin Pop | case genClusterAlloc count node inst of |
1340 | 6a855aaa | Iustin Pop | Types.Bad msg -> failTest msg |
1341 | 6a855aaa | Iustin Pop | Types.Ok (nl, il, inst') -> |
1342 | 6a855aaa | Iustin Pop | case IAlloc.processRelocate defGroupList nl il |
1343 | 7018af9c | Iustin Pop | (Instance.idx inst) 1 |
1344 | 7018af9c | Iustin Pop | [(if Instance.diskTemplate inst' == Types.DTDrbd8 |
1345 | 7018af9c | Iustin Pop | then Instance.sNode |
1346 | 7018af9c | Iustin Pop | else Instance.pNode) inst'] of |
1347 | 7018af9c | Iustin Pop | Types.Ok _ -> property True |
1348 | 6a855aaa | Iustin Pop | Types.Bad msg -> failTest $ "Failed to relocate: " ++ msg |
1349 | 6a855aaa | Iustin Pop | |
1350 | 6a855aaa | Iustin Pop | -- | Helper property checker for the result of a nodeEvac or |
1351 | 6a855aaa | Iustin Pop | -- changeGroup operation. |
1352 | fce98abd | Iustin Pop | check_EvacMode :: Group.Group -> Instance.Instance |
1353 | fce98abd | Iustin Pop | -> Types.Result (Node.List, Instance.List, Cluster.EvacSolution) |
1354 | fce98abd | Iustin Pop | -> Property |
1355 | 6a855aaa | Iustin Pop | check_EvacMode grp inst result = |
1356 | 6a855aaa | Iustin Pop | case result of |
1357 | 6a855aaa | Iustin Pop | Types.Bad msg -> failTest $ "Couldn't evacuate/change group:" ++ msg |
1358 | 6a855aaa | Iustin Pop | Types.Ok (_, _, es) -> |
1359 | 6a855aaa | Iustin Pop | let moved = Cluster.esMoved es |
1360 | 6a855aaa | Iustin Pop | failed = Cluster.esFailed es |
1361 | 6a855aaa | Iustin Pop | opcodes = not . null $ Cluster.esOpCodes es |
1362 | 6a855aaa | Iustin Pop | in failmsg ("'failed' not empty: " ++ show failed) (null failed) .&&. |
1363 | 6a855aaa | Iustin Pop | failmsg "'opcodes' is null" opcodes .&&. |
1364 | 6a855aaa | Iustin Pop | case moved of |
1365 | 6a855aaa | Iustin Pop | [(idx', gdx, _)] -> failmsg "invalid instance moved" (idx == idx') |
1366 | 6a855aaa | Iustin Pop | .&&. |
1367 | 6a855aaa | Iustin Pop | failmsg "wrong target group" |
1368 | 6a855aaa | Iustin Pop | (gdx == Group.idx grp) |
1369 | 6a855aaa | Iustin Pop | v -> failmsg ("invalid solution: " ++ show v) False |
1370 | fce98abd | Iustin Pop | where failmsg :: String -> Bool -> Property |
1371 | fce98abd | Iustin Pop | failmsg = \msg -> printTestCase ("Failed to evacuate: " ++ msg) |
1372 | 6a855aaa | Iustin Pop | idx = Instance.idx inst |
1373 | 6a855aaa | Iustin Pop | |
1374 | 6a855aaa | Iustin Pop | -- | Checks that on a 4-8 node cluster, once we allocate an instance, |
1375 | 6a855aaa | Iustin Pop | -- we can also node-evacuate it. |
1376 | fce98abd | Iustin Pop | prop_ClusterAllocEvacuate :: Property |
1377 | 6a855aaa | Iustin Pop | prop_ClusterAllocEvacuate = |
1378 | 6a855aaa | Iustin Pop | forAll (choose (4, 8)) $ \count -> |
1379 | 6a855aaa | Iustin Pop | forAll (genOnlineNode `suchThat` (isNodeBig 4)) $ \node -> |
1380 | ac1c0a07 | Iustin Pop | forAll (genInstanceSmallerThanNode node `suchThat` isMirrored) $ \inst -> |
1381 | 6a855aaa | Iustin Pop | case genClusterAlloc count node inst of |
1382 | 6a855aaa | Iustin Pop | Types.Bad msg -> failTest msg |
1383 | 6a855aaa | Iustin Pop | Types.Ok (nl, il, inst') -> |
1384 | 6a855aaa | Iustin Pop | conjoin $ map (\mode -> check_EvacMode defGroup inst' $ |
1385 | 6a855aaa | Iustin Pop | Cluster.tryNodeEvac defGroupList nl il mode |
1386 | ac1c0a07 | Iustin Pop | [Instance.idx inst']) . |
1387 | fafd0773 | Iustin Pop | evacModeOptions . |
1388 | fafd0773 | Iustin Pop | Instance.mirrorType $ inst' |
1389 | 6a855aaa | Iustin Pop | |
1390 | 6a855aaa | Iustin Pop | -- | Checks that on a 4-8 node cluster with two node groups, once we |
1391 | 6a855aaa | Iustin Pop | -- allocate an instance on the first node group, we can also change |
1392 | 6a855aaa | Iustin Pop | -- its group. |
1393 | fce98abd | Iustin Pop | prop_ClusterAllocChangeGroup :: Property |
1394 | 6a855aaa | Iustin Pop | prop_ClusterAllocChangeGroup = |
1395 | 6a855aaa | Iustin Pop | forAll (choose (4, 8)) $ \count -> |
1396 | 6a855aaa | Iustin Pop | forAll (genOnlineNode `suchThat` (isNodeBig 4)) $ \node -> |
1397 | ac1c0a07 | Iustin Pop | forAll (genInstanceSmallerThanNode node `suchThat` isMirrored) $ \inst -> |
1398 | 6a855aaa | Iustin Pop | case genClusterAlloc count node inst of |
1399 | 6a855aaa | Iustin Pop | Types.Bad msg -> failTest msg |
1400 | 6a855aaa | Iustin Pop | Types.Ok (nl, il, inst') -> |
1401 | 6a855aaa | Iustin Pop | -- we need to add a second node group and nodes to the cluster |
1402 | 6a855aaa | Iustin Pop | let nl2 = Container.elems $ makeSmallCluster node count |
1403 | 6a855aaa | Iustin Pop | grp2 = Group.setIdx defGroup (Group.idx defGroup + 1) |
1404 | 6a855aaa | Iustin Pop | maxndx = maximum . map Node.idx $ nl2 |
1405 | 6a855aaa | Iustin Pop | nl3 = map (\n -> n { Node.group = Group.idx grp2 |
1406 | 6a855aaa | Iustin Pop | , Node.idx = Node.idx n + maxndx }) nl2 |
1407 | 6a855aaa | Iustin Pop | nl4 = Container.fromList . map (\n -> (Node.idx n, n)) $ nl3 |
1408 | 6a855aaa | Iustin Pop | gl' = Container.add (Group.idx grp2) grp2 defGroupList |
1409 | 6a855aaa | Iustin Pop | nl' = IntMap.union nl nl4 |
1410 | 6a855aaa | Iustin Pop | in check_EvacMode grp2 inst' $ |
1411 | 6a855aaa | Iustin Pop | Cluster.tryChangeGroup gl' nl' il [] [Instance.idx inst'] |
1412 | 3fea6959 | Iustin Pop | |
1413 | 3fea6959 | Iustin Pop | -- | Check that allocating multiple instances on a cluster, then |
1414 | 525bfb36 | Iustin Pop | -- adding an empty node, results in a valid rebalance. |
1415 | fce98abd | Iustin Pop | prop_ClusterAllocBalance :: Property |
1416 | 00c75986 | Iustin Pop | prop_ClusterAllocBalance = |
1417 | d5dfae0a | Iustin Pop | forAll (genNode (Just 5) (Just 128)) $ \node -> |
1418 | d5dfae0a | Iustin Pop | forAll (choose (3, 5)) $ \count -> |
1419 | d5dfae0a | Iustin Pop | not (Node.offline node) && not (Node.failN1 node) ==> |
1420 | d5dfae0a | Iustin Pop | let nl = makeSmallCluster node count |
1421 | d5dfae0a | Iustin Pop | (hnode, nl') = IntMap.deleteFindMax nl |
1422 | d5dfae0a | Iustin Pop | il = Container.empty |
1423 | d5dfae0a | Iustin Pop | allocnodes = Cluster.genAllocNodes defGroupList nl' 2 True |
1424 | d5dfae0a | Iustin Pop | i_templ = createInstance Types.unitMem Types.unitDsk Types.unitCpu |
1425 | d5dfae0a | Iustin Pop | in case allocnodes >>= \allocnodes' -> |
1426 | d5dfae0a | Iustin Pop | Cluster.iterateAlloc nl' il (Just 5) i_templ allocnodes' [] [] of |
1427 | 96bc2003 | Iustin Pop | Types.Bad msg -> failTest $ "Failed to allocate: " ++ msg |
1428 | 96bc2003 | Iustin Pop | Types.Ok (_, _, _, [], _) -> failTest "Failed to allocate: no instances" |
1429 | d5dfae0a | Iustin Pop | Types.Ok (_, xnl, il', _, _) -> |
1430 | d5dfae0a | Iustin Pop | let ynl = Container.add (Node.idx hnode) hnode xnl |
1431 | d5dfae0a | Iustin Pop | cv = Cluster.compCV ynl |
1432 | d5dfae0a | Iustin Pop | tbl = Cluster.Table ynl il' cv [] |
1433 | 6cff91f5 | Iustin Pop | in printTestCase "Failed to rebalance" $ |
1434 | 6cff91f5 | Iustin Pop | canBalance tbl True True False |
1435 | 3fea6959 | Iustin Pop | |
1436 | 525bfb36 | Iustin Pop | -- | Checks consistency. |
1437 | fce98abd | Iustin Pop | prop_ClusterCheckConsistency :: Node.Node -> Instance.Instance -> Bool |
1438 | 32b8d9c0 | Iustin Pop | prop_ClusterCheckConsistency node inst = |
1439 | 32b8d9c0 | Iustin Pop | let nl = makeSmallCluster node 3 |
1440 | 32b8d9c0 | Iustin Pop | [node1, node2, node3] = Container.elems nl |
1441 | 10ef6b4e | Iustin Pop | node3' = node3 { Node.group = 1 } |
1442 | 32b8d9c0 | Iustin Pop | nl' = Container.add (Node.idx node3') node3' nl |
1443 | 32b8d9c0 | Iustin Pop | inst1 = Instance.setBoth inst (Node.idx node1) (Node.idx node2) |
1444 | 32b8d9c0 | Iustin Pop | inst2 = Instance.setBoth inst (Node.idx node1) Node.noSecondary |
1445 | 32b8d9c0 | Iustin Pop | inst3 = Instance.setBoth inst (Node.idx node1) (Node.idx node3) |
1446 | cb0c77ff | Iustin Pop | ccheck = Cluster.findSplitInstances nl' . Container.fromList |
1447 | 32b8d9c0 | Iustin Pop | in null (ccheck [(0, inst1)]) && |
1448 | 32b8d9c0 | Iustin Pop | null (ccheck [(0, inst2)]) && |
1449 | 32b8d9c0 | Iustin Pop | (not . null $ ccheck [(0, inst3)]) |
1450 | 32b8d9c0 | Iustin Pop | |
1451 | 525bfb36 | Iustin Pop | -- | For now, we only test that we don't lose instances during the split. |
1452 | fce98abd | Iustin Pop | prop_ClusterSplitCluster :: Node.Node -> Instance.Instance -> Property |
1453 | f4161783 | Iustin Pop | prop_ClusterSplitCluster node inst = |
1454 | f4161783 | Iustin Pop | forAll (choose (0, 100)) $ \icnt -> |
1455 | f4161783 | Iustin Pop | let nl = makeSmallCluster node 2 |
1456 | f4161783 | Iustin Pop | (nl', il') = foldl (\(ns, is) _ -> assignInstance ns is inst 0 1) |
1457 | f4161783 | Iustin Pop | (nl, Container.empty) [1..icnt] |
1458 | f4161783 | Iustin Pop | gni = Cluster.splitCluster nl' il' |
1459 | f4161783 | Iustin Pop | in sum (map (Container.size . snd . snd) gni) == icnt && |
1460 | f4161783 | Iustin Pop | all (\(guuid, (nl'', _)) -> all ((== guuid) . Node.group) |
1461 | f4161783 | Iustin Pop | (Container.elems nl'')) gni |
1462 | 32b8d9c0 | Iustin Pop | |
1463 | 00b70680 | Iustin Pop | -- | Helper function to check if we can allocate an instance on a |
1464 | 00b70680 | Iustin Pop | -- given node list. |
1465 | 00b70680 | Iustin Pop | canAllocOn :: Node.List -> Int -> Instance.Instance -> Bool |
1466 | 00b70680 | Iustin Pop | canAllocOn nl reqnodes inst = |
1467 | 00b70680 | Iustin Pop | case Cluster.genAllocNodes defGroupList nl reqnodes True >>= |
1468 | 00b70680 | Iustin Pop | Cluster.tryAlloc nl (Container.empty) inst of |
1469 | 00b70680 | Iustin Pop | Types.Bad _ -> False |
1470 | 00b70680 | Iustin Pop | Types.Ok as -> |
1471 | 00b70680 | Iustin Pop | case Cluster.asSolution as of |
1472 | 00b70680 | Iustin Pop | Nothing -> False |
1473 | 00b70680 | Iustin Pop | Just _ -> True |
1474 | 00b70680 | Iustin Pop | |
1475 | 00b70680 | Iustin Pop | -- | Checks that allocation obeys minimum and maximum instance |
1476 | fce98abd | Iustin Pop | -- policies. The unittest generates a random node, duplicates it /count/ |
1477 | 00b70680 | Iustin Pop | -- times, and generates a random instance that can be allocated on |
1478 | 00b70680 | Iustin Pop | -- this mini-cluster; it then checks that after applying a policy that |
1479 | 00b70680 | Iustin Pop | -- the instance doesn't fits, the allocation fails. |
1480 | fce98abd | Iustin Pop | prop_ClusterAllocPolicy :: Node.Node -> Property |
1481 | 00b70680 | Iustin Pop | prop_ClusterAllocPolicy node = |
1482 | 00b70680 | Iustin Pop | -- rqn is the required nodes (1 or 2) |
1483 | 00b70680 | Iustin Pop | forAll (choose (1, 2)) $ \rqn -> |
1484 | 00b70680 | Iustin Pop | forAll (choose (5, 20)) $ \count -> |
1485 | 00b70680 | Iustin Pop | forAll (arbitrary `suchThat` (canAllocOn (makeSmallCluster node count) rqn)) |
1486 | 00b70680 | Iustin Pop | $ \inst -> |
1487 | 00b70680 | Iustin Pop | forAll (arbitrary `suchThat` (isFailure . |
1488 | 00b70680 | Iustin Pop | Instance.instMatchesPolicy inst)) $ \ipol -> |
1489 | 00b70680 | Iustin Pop | let node' = Node.setPolicy ipol node |
1490 | 00b70680 | Iustin Pop | nl = makeSmallCluster node' count |
1491 | 00b70680 | Iustin Pop | in not $ canAllocOn nl rqn inst |
1492 | 00b70680 | Iustin Pop | |
1493 | 23fe06c2 | Iustin Pop | testSuite "Cluster" |
1494 | d5dfae0a | Iustin Pop | [ 'prop_Score_Zero |
1495 | d5dfae0a | Iustin Pop | , 'prop_CStats_sane |
1496 | d5dfae0a | Iustin Pop | , 'prop_ClusterAlloc_sane |
1497 | d5dfae0a | Iustin Pop | , 'prop_ClusterCanTieredAlloc |
1498 | 6a855aaa | Iustin Pop | , 'prop_ClusterAllocRelocate |
1499 | 6a855aaa | Iustin Pop | , 'prop_ClusterAllocEvacuate |
1500 | 6a855aaa | Iustin Pop | , 'prop_ClusterAllocChangeGroup |
1501 | d5dfae0a | Iustin Pop | , 'prop_ClusterAllocBalance |
1502 | d5dfae0a | Iustin Pop | , 'prop_ClusterCheckConsistency |
1503 | d5dfae0a | Iustin Pop | , 'prop_ClusterSplitCluster |
1504 | 00b70680 | Iustin Pop | , 'prop_ClusterAllocPolicy |
1505 | d5dfae0a | Iustin Pop | ] |
1506 | 88f25dd0 | Iustin Pop | |
1507 | 525bfb36 | Iustin Pop | -- ** OpCodes tests |
1508 | 88f25dd0 | Iustin Pop | |
1509 | 525bfb36 | Iustin Pop | -- | Check that opcode serialization is idempotent. |
1510 | fce98abd | Iustin Pop | prop_OpCodes_serialization :: OpCodes.OpCode -> Property |
1511 | 88f25dd0 | Iustin Pop | prop_OpCodes_serialization op = |
1512 | 88f25dd0 | Iustin Pop | case J.readJSON (J.showJSON op) of |
1513 | 96bc2003 | Iustin Pop | J.Error e -> failTest $ "Cannot deserialise: " ++ e |
1514 | 72bb6b4e | Iustin Pop | J.Ok op' -> op ==? op' |
1515 | 88f25dd0 | Iustin Pop | |
1516 | 23fe06c2 | Iustin Pop | testSuite "OpCodes" |
1517 | d5dfae0a | Iustin Pop | [ 'prop_OpCodes_serialization ] |
1518 | c088674b | Iustin Pop | |
1519 | 525bfb36 | Iustin Pop | -- ** Jobs tests |
1520 | 525bfb36 | Iustin Pop | |
1521 | 525bfb36 | Iustin Pop | -- | Check that (queued) job\/opcode status serialization is idempotent. |
1522 | fce98abd | Iustin Pop | prop_OpStatus_serialization :: Jobs.OpStatus -> Property |
1523 | db079755 | Iustin Pop | prop_OpStatus_serialization os = |
1524 | db079755 | Iustin Pop | case J.readJSON (J.showJSON os) of |
1525 | 96bc2003 | Iustin Pop | J.Error e -> failTest $ "Cannot deserialise: " ++ e |
1526 | 72bb6b4e | Iustin Pop | J.Ok os' -> os ==? os' |
1527 | db079755 | Iustin Pop | |
1528 | fce98abd | Iustin Pop | prop_JobStatus_serialization :: Jobs.JobStatus -> Property |
1529 | db079755 | Iustin Pop | prop_JobStatus_serialization js = |
1530 | db079755 | Iustin Pop | case J.readJSON (J.showJSON js) of |
1531 | 96bc2003 | Iustin Pop | J.Error e -> failTest $ "Cannot deserialise: " ++ e |
1532 | 72bb6b4e | Iustin Pop | J.Ok js' -> js ==? js' |
1533 | db079755 | Iustin Pop | |
1534 | 23fe06c2 | Iustin Pop | testSuite "Jobs" |
1535 | d5dfae0a | Iustin Pop | [ 'prop_OpStatus_serialization |
1536 | d5dfae0a | Iustin Pop | , 'prop_JobStatus_serialization |
1537 | d5dfae0a | Iustin Pop | ] |
1538 | db079755 | Iustin Pop | |
1539 | 525bfb36 | Iustin Pop | -- ** Loader tests |
1540 | c088674b | Iustin Pop | |
1541 | fce98abd | Iustin Pop | prop_Loader_lookupNode :: [(String, Int)] -> String -> String -> Property |
1542 | c088674b | Iustin Pop | prop_Loader_lookupNode ktn inst node = |
1543 | 72bb6b4e | Iustin Pop | Loader.lookupNode nl inst node ==? Data.Map.lookup node nl |
1544 | d5dfae0a | Iustin Pop | where nl = Data.Map.fromList ktn |
1545 | c088674b | Iustin Pop | |
1546 | fce98abd | Iustin Pop | prop_Loader_lookupInstance :: [(String, Int)] -> String -> Property |
1547 | c088674b | Iustin Pop | prop_Loader_lookupInstance kti inst = |
1548 | 72bb6b4e | Iustin Pop | Loader.lookupInstance il inst ==? Data.Map.lookup inst il |
1549 | d5dfae0a | Iustin Pop | where il = Data.Map.fromList kti |
1550 | 99b63608 | Iustin Pop | |
1551 | fce98abd | Iustin Pop | prop_Loader_assignIndices :: Property |
1552 | 3074ccaf | Iustin Pop | prop_Loader_assignIndices = |
1553 | 3074ccaf | Iustin Pop | -- generate nodes with unique names |
1554 | 3074ccaf | Iustin Pop | forAll (arbitrary `suchThat` |
1555 | 3074ccaf | Iustin Pop | (\nodes -> |
1556 | 3074ccaf | Iustin Pop | let names = map Node.name nodes |
1557 | 3074ccaf | Iustin Pop | in length names == length (nub names))) $ \nodes -> |
1558 | 3074ccaf | Iustin Pop | let (nassoc, kt) = |
1559 | 3074ccaf | Iustin Pop | Loader.assignIndices (map (\n -> (Node.name n, n)) nodes) |
1560 | 3074ccaf | Iustin Pop | in Data.Map.size nassoc == length nodes && |
1561 | 3074ccaf | Iustin Pop | Container.size kt == length nodes && |
1562 | 3074ccaf | Iustin Pop | if not (null nodes) |
1563 | 3074ccaf | Iustin Pop | then maximum (IntMap.keys kt) == length nodes - 1 |
1564 | 3074ccaf | Iustin Pop | else True |
1565 | c088674b | Iustin Pop | |
1566 | c088674b | Iustin Pop | -- | Checks that the number of primary instances recorded on the nodes |
1567 | 525bfb36 | Iustin Pop | -- is zero. |
1568 | fce98abd | Iustin Pop | prop_Loader_mergeData :: [Node.Node] -> Bool |
1569 | c088674b | Iustin Pop | prop_Loader_mergeData ns = |
1570 | cb0c77ff | Iustin Pop | let na = Container.fromList $ map (\n -> (Node.idx n, n)) ns |
1571 | 2d1708e0 | Guido Trotter | in case Loader.mergeData [] [] [] [] |
1572 | f4f6eb0b | Iustin Pop | (Loader.emptyCluster {Loader.cdNodes = na}) of |
1573 | c088674b | Iustin Pop | Types.Bad _ -> False |
1574 | 71375ef7 | Iustin Pop | Types.Ok (Loader.ClusterData _ nl il _ _) -> |
1575 | c088674b | Iustin Pop | let nodes = Container.elems nl |
1576 | c088674b | Iustin Pop | instances = Container.elems il |
1577 | c088674b | Iustin Pop | in (sum . map (length . Node.pList)) nodes == 0 && |
1578 | 4a007641 | Iustin Pop | null instances |
1579 | c088674b | Iustin Pop | |
1580 | efe98965 | Guido Trotter | -- | Check that compareNameComponent on equal strings works. |
1581 | efe98965 | Guido Trotter | prop_Loader_compareNameComponent_equal :: String -> Bool |
1582 | efe98965 | Guido Trotter | prop_Loader_compareNameComponent_equal s = |
1583 | efe98965 | Guido Trotter | Loader.compareNameComponent s s == |
1584 | efe98965 | Guido Trotter | Loader.LookupResult Loader.ExactMatch s |
1585 | efe98965 | Guido Trotter | |
1586 | efe98965 | Guido Trotter | -- | Check that compareNameComponent on prefix strings works. |
1587 | efe98965 | Guido Trotter | prop_Loader_compareNameComponent_prefix :: NonEmptyList Char -> String -> Bool |
1588 | efe98965 | Guido Trotter | prop_Loader_compareNameComponent_prefix (NonEmpty s1) s2 = |
1589 | efe98965 | Guido Trotter | Loader.compareNameComponent (s1 ++ "." ++ s2) s1 == |
1590 | efe98965 | Guido Trotter | Loader.LookupResult Loader.PartialMatch s1 |
1591 | efe98965 | Guido Trotter | |
1592 | 23fe06c2 | Iustin Pop | testSuite "Loader" |
1593 | d5dfae0a | Iustin Pop | [ 'prop_Loader_lookupNode |
1594 | d5dfae0a | Iustin Pop | , 'prop_Loader_lookupInstance |
1595 | d5dfae0a | Iustin Pop | , 'prop_Loader_assignIndices |
1596 | d5dfae0a | Iustin Pop | , 'prop_Loader_mergeData |
1597 | d5dfae0a | Iustin Pop | , 'prop_Loader_compareNameComponent_equal |
1598 | d5dfae0a | Iustin Pop | , 'prop_Loader_compareNameComponent_prefix |
1599 | d5dfae0a | Iustin Pop | ] |
1600 | 3c002a13 | Iustin Pop | |
1601 | 3c002a13 | Iustin Pop | -- ** Types tests |
1602 | 3c002a13 | Iustin Pop | |
1603 | fce98abd | Iustin Pop | prop_Types_AllocPolicy_serialisation :: Types.AllocPolicy -> Property |
1604 | 0047d4e2 | Iustin Pop | prop_Types_AllocPolicy_serialisation apol = |
1605 | d5dfae0a | Iustin Pop | case J.readJSON (J.showJSON apol) of |
1606 | aa1d552d | Iustin Pop | J.Ok p -> p ==? apol |
1607 | 96bc2003 | Iustin Pop | J.Error s -> failTest $ "Failed to deserialise: " ++ s |
1608 | 0047d4e2 | Iustin Pop | |
1609 | fce98abd | Iustin Pop | prop_Types_DiskTemplate_serialisation :: Types.DiskTemplate -> Property |
1610 | 0047d4e2 | Iustin Pop | prop_Types_DiskTemplate_serialisation dt = |
1611 | d5dfae0a | Iustin Pop | case J.readJSON (J.showJSON dt) of |
1612 | aa1d552d | Iustin Pop | J.Ok p -> p ==? dt |
1613 | 96bc2003 | Iustin Pop | J.Error s -> failTest $ "Failed to deserialise: " ++ s |
1614 | 0047d4e2 | Iustin Pop | |
1615 | fce98abd | Iustin Pop | prop_Types_ISpec_serialisation :: Types.ISpec -> Property |
1616 | aa1d552d | Iustin Pop | prop_Types_ISpec_serialisation ispec = |
1617 | aa1d552d | Iustin Pop | case J.readJSON (J.showJSON ispec) of |
1618 | aa1d552d | Iustin Pop | J.Ok p -> p ==? ispec |
1619 | 96bc2003 | Iustin Pop | J.Error s -> failTest $ "Failed to deserialise: " ++ s |
1620 | aa1d552d | Iustin Pop | |
1621 | fce98abd | Iustin Pop | prop_Types_IPolicy_serialisation :: Types.IPolicy -> Property |
1622 | aa1d552d | Iustin Pop | prop_Types_IPolicy_serialisation ipol = |
1623 | aa1d552d | Iustin Pop | case J.readJSON (J.showJSON ipol) of |
1624 | aa1d552d | Iustin Pop | J.Ok p -> p ==? ipol |
1625 | 96bc2003 | Iustin Pop | J.Error s -> failTest $ "Failed to deserialise: " ++ s |
1626 | aa1d552d | Iustin Pop | |
1627 | fce98abd | Iustin Pop | prop_Types_EvacMode_serialisation :: Types.EvacMode -> Property |
1628 | aa1d552d | Iustin Pop | prop_Types_EvacMode_serialisation em = |
1629 | aa1d552d | Iustin Pop | case J.readJSON (J.showJSON em) of |
1630 | aa1d552d | Iustin Pop | J.Ok p -> p ==? em |
1631 | 96bc2003 | Iustin Pop | J.Error s -> failTest $ "Failed to deserialise: " ++ s |
1632 | aa1d552d | Iustin Pop | |
1633 | fce98abd | Iustin Pop | prop_Types_opToResult :: Types.OpResult Int -> Bool |
1634 | 0047d4e2 | Iustin Pop | prop_Types_opToResult op = |
1635 | d5dfae0a | Iustin Pop | case op of |
1636 | d5dfae0a | Iustin Pop | Types.OpFail _ -> Types.isBad r |
1637 | d5dfae0a | Iustin Pop | Types.OpGood v -> case r of |
1638 | d5dfae0a | Iustin Pop | Types.Bad _ -> False |
1639 | d5dfae0a | Iustin Pop | Types.Ok v' -> v == v' |
1640 | d5dfae0a | Iustin Pop | where r = Types.opToResult op |
1641 | 0047d4e2 | Iustin Pop | |
1642 | fce98abd | Iustin Pop | prop_Types_eitherToResult :: Either String Int -> Bool |
1643 | 0047d4e2 | Iustin Pop | prop_Types_eitherToResult ei = |
1644 | d5dfae0a | Iustin Pop | case ei of |
1645 | d5dfae0a | Iustin Pop | Left _ -> Types.isBad r |
1646 | d5dfae0a | Iustin Pop | Right v -> case r of |
1647 | d5dfae0a | Iustin Pop | Types.Bad _ -> False |
1648 | d5dfae0a | Iustin Pop | Types.Ok v' -> v == v' |
1649 | 0047d4e2 | Iustin Pop | where r = Types.eitherToResult ei |
1650 | 3c002a13 | Iustin Pop | |
1651 | 23fe06c2 | Iustin Pop | testSuite "Types" |
1652 | d5dfae0a | Iustin Pop | [ 'prop_Types_AllocPolicy_serialisation |
1653 | d5dfae0a | Iustin Pop | , 'prop_Types_DiskTemplate_serialisation |
1654 | aa1d552d | Iustin Pop | , 'prop_Types_ISpec_serialisation |
1655 | aa1d552d | Iustin Pop | , 'prop_Types_IPolicy_serialisation |
1656 | aa1d552d | Iustin Pop | , 'prop_Types_EvacMode_serialisation |
1657 | d5dfae0a | Iustin Pop | , 'prop_Types_opToResult |
1658 | d5dfae0a | Iustin Pop | , 'prop_Types_eitherToResult |
1659 | d5dfae0a | Iustin Pop | ] |
1660 | 8b5a517a | Iustin Pop | |
1661 | 8b5a517a | Iustin Pop | -- ** CLI tests |
1662 | 8b5a517a | Iustin Pop | |
1663 | 8b5a517a | Iustin Pop | -- | Test correct parsing. |
1664 | fce98abd | Iustin Pop | prop_CLI_parseISpec :: String -> Int -> Int -> Int -> Property |
1665 | 8b5a517a | Iustin Pop | prop_CLI_parseISpec descr dsk mem cpu = |
1666 | fce98abd | Iustin Pop | let str = printf "%d,%d,%d" dsk mem cpu::String |
1667 | 8b5a517a | Iustin Pop | in CLI.parseISpecString descr str ==? Types.Ok (Types.RSpec cpu mem dsk) |
1668 | 8b5a517a | Iustin Pop | |
1669 | 8b5a517a | Iustin Pop | -- | Test parsing failure due to wrong section count. |
1670 | fce98abd | Iustin Pop | prop_CLI_parseISpecFail :: String -> Property |
1671 | 8b5a517a | Iustin Pop | prop_CLI_parseISpecFail descr = |
1672 | 8b5a517a | Iustin Pop | forAll (choose (0,100) `suchThat` ((/=) 3)) $ \nelems -> |
1673 | 8b5a517a | Iustin Pop | forAll (replicateM nelems arbitrary) $ \values -> |
1674 | 8b5a517a | Iustin Pop | let str = intercalate "," $ map show (values::[Int]) |
1675 | 8b5a517a | Iustin Pop | in case CLI.parseISpecString descr str of |
1676 | 8b5a517a | Iustin Pop | Types.Ok v -> failTest $ "Expected failure, got " ++ show v |
1677 | 8b5a517a | Iustin Pop | _ -> property True |
1678 | 8b5a517a | Iustin Pop | |
1679 | a7ea861a | Iustin Pop | -- | Test parseYesNo. |
1680 | fce98abd | Iustin Pop | prop_CLI_parseYesNo :: Bool -> Bool -> [Char] -> Property |
1681 | a7ea861a | Iustin Pop | prop_CLI_parseYesNo def testval val = |
1682 | a7ea861a | Iustin Pop | forAll (elements [val, "yes", "no"]) $ \actual_val -> |
1683 | a7ea861a | Iustin Pop | if testval |
1684 | a7ea861a | Iustin Pop | then CLI.parseYesNo def Nothing ==? Types.Ok def |
1685 | a7ea861a | Iustin Pop | else let result = CLI.parseYesNo def (Just actual_val) |
1686 | a7ea861a | Iustin Pop | in if actual_val `elem` ["yes", "no"] |
1687 | a7ea861a | Iustin Pop | then result ==? Types.Ok (actual_val == "yes") |
1688 | a7ea861a | Iustin Pop | else property $ Types.isBad result |
1689 | a7ea861a | Iustin Pop | |
1690 | 89298c04 | Iustin Pop | -- | Helper to check for correct parsing of string arg. |
1691 | fce98abd | Iustin Pop | checkStringArg :: [Char] |
1692 | fce98abd | Iustin Pop | -> (GetOpt.OptDescr (CLI.Options -> Types.Result CLI.Options), |
1693 | fce98abd | Iustin Pop | CLI.Options -> Maybe [Char]) |
1694 | fce98abd | Iustin Pop | -> Property |
1695 | 89298c04 | Iustin Pop | checkStringArg val (opt, fn) = |
1696 | 89298c04 | Iustin Pop | let GetOpt.Option _ longs _ _ = opt |
1697 | 89298c04 | Iustin Pop | in case longs of |
1698 | 89298c04 | Iustin Pop | [] -> failTest "no long options?" |
1699 | 89298c04 | Iustin Pop | cmdarg:_ -> |
1700 | 89298c04 | Iustin Pop | case CLI.parseOptsInner ["--" ++ cmdarg ++ "=" ++ val] "prog" [opt] of |
1701 | 89298c04 | Iustin Pop | Left e -> failTest $ "Failed to parse option: " ++ show e |
1702 | 89298c04 | Iustin Pop | Right (options, _) -> fn options ==? Just val |
1703 | 89298c04 | Iustin Pop | |
1704 | 89298c04 | Iustin Pop | -- | Test a few string arguments. |
1705 | fce98abd | Iustin Pop | prop_CLI_StringArg :: [Char] -> Property |
1706 | 89298c04 | Iustin Pop | prop_CLI_StringArg argument = |
1707 | 89298c04 | Iustin Pop | let args = [ (CLI.oDataFile, CLI.optDataFile) |
1708 | 89298c04 | Iustin Pop | , (CLI.oDynuFile, CLI.optDynuFile) |
1709 | 89298c04 | Iustin Pop | , (CLI.oSaveCluster, CLI.optSaveCluster) |
1710 | 89298c04 | Iustin Pop | , (CLI.oReplay, CLI.optReplay) |
1711 | 89298c04 | Iustin Pop | , (CLI.oPrintCommands, CLI.optShowCmds) |
1712 | 89298c04 | Iustin Pop | , (CLI.oLuxiSocket, CLI.optLuxi) |
1713 | 89298c04 | Iustin Pop | ] |
1714 | 89298c04 | Iustin Pop | in conjoin $ map (checkStringArg argument) args |
1715 | 89298c04 | Iustin Pop | |
1716 | a292b4e0 | Iustin Pop | -- | Helper to test that a given option is accepted OK with quick exit. |
1717 | fce98abd | Iustin Pop | checkEarlyExit :: String -> [CLI.OptType] -> String -> Property |
1718 | a292b4e0 | Iustin Pop | checkEarlyExit name options param = |
1719 | a292b4e0 | Iustin Pop | case CLI.parseOptsInner [param] name options of |
1720 | a292b4e0 | Iustin Pop | Left (code, _) -> if code == 0 |
1721 | a292b4e0 | Iustin Pop | then property True |
1722 | a292b4e0 | Iustin Pop | else failTest $ "Program " ++ name ++ |
1723 | a292b4e0 | Iustin Pop | " returns invalid code " ++ show code ++ |
1724 | a292b4e0 | Iustin Pop | " for option " ++ param |
1725 | a292b4e0 | Iustin Pop | _ -> failTest $ "Program " ++ name ++ " doesn't consider option " ++ |
1726 | a292b4e0 | Iustin Pop | param ++ " as early exit one" |
1727 | a292b4e0 | Iustin Pop | |
1728 | a292b4e0 | Iustin Pop | -- | Test that all binaries support some common options. There is |
1729 | a292b4e0 | Iustin Pop | -- nothing actually random about this test... |
1730 | fce98abd | Iustin Pop | prop_CLI_stdopts :: Property |
1731 | a292b4e0 | Iustin Pop | prop_CLI_stdopts = |
1732 | a292b4e0 | Iustin Pop | let params = ["-h", "--help", "-V", "--version"] |
1733 | a292b4e0 | Iustin Pop | opts = map (\(name, (_, o)) -> (name, o)) Program.personalities |
1734 | a292b4e0 | Iustin Pop | -- apply checkEarlyExit across the cartesian product of params and opts |
1735 | a292b4e0 | Iustin Pop | in conjoin [checkEarlyExit n o p | p <- params, (n, o) <- opts] |
1736 | a292b4e0 | Iustin Pop | |
1737 | 8b5a517a | Iustin Pop | testSuite "CLI" |
1738 | 8b5a517a | Iustin Pop | [ 'prop_CLI_parseISpec |
1739 | 8b5a517a | Iustin Pop | , 'prop_CLI_parseISpecFail |
1740 | a7ea861a | Iustin Pop | , 'prop_CLI_parseYesNo |
1741 | 89298c04 | Iustin Pop | , 'prop_CLI_StringArg |
1742 | a292b4e0 | Iustin Pop | , 'prop_CLI_stdopts |
1743 | 8b5a517a | Iustin Pop | ] |
1744 | 3ad57194 | Iustin Pop | |
1745 | 3ad57194 | Iustin Pop | -- * JSON tests |
1746 | 3ad57194 | Iustin Pop | |
1747 | 3ad57194 | Iustin Pop | prop_JSON_toArray :: [Int] -> Property |
1748 | 3ad57194 | Iustin Pop | prop_JSON_toArray intarr = |
1749 | 3ad57194 | Iustin Pop | let arr = map J.showJSON intarr in |
1750 | 3ad57194 | Iustin Pop | case JSON.toArray (J.JSArray arr) of |
1751 | 3ad57194 | Iustin Pop | Types.Ok arr' -> arr ==? arr' |
1752 | 3ad57194 | Iustin Pop | Types.Bad err -> failTest $ "Failed to parse array: " ++ err |
1753 | 3ad57194 | Iustin Pop | |
1754 | 3ad57194 | Iustin Pop | prop_JSON_toArrayFail :: Int -> String -> Bool -> Property |
1755 | 3ad57194 | Iustin Pop | prop_JSON_toArrayFail i s b = |
1756 | 3ad57194 | Iustin Pop | -- poor man's instance Arbitrary JSValue |
1757 | 3ad57194 | Iustin Pop | forAll (elements [J.showJSON i, J.showJSON s, J.showJSON b]) $ \item -> |
1758 | 3ad57194 | Iustin Pop | case JSON.toArray item of |
1759 | 3ad57194 | Iustin Pop | Types.Bad _ -> property True |
1760 | 3ad57194 | Iustin Pop | Types.Ok result -> failTest $ "Unexpected parse, got " ++ show result |
1761 | 3ad57194 | Iustin Pop | |
1762 | 3ad57194 | Iustin Pop | testSuite "JSON" |
1763 | 3ad57194 | Iustin Pop | [ 'prop_JSON_toArray |
1764 | 3ad57194 | Iustin Pop | , 'prop_JSON_toArrayFail |
1765 | 3ad57194 | Iustin Pop | ] |
1766 | cdd495ae | Iustin Pop | |
1767 | cdd495ae | Iustin Pop | -- * Luxi tests |
1768 | cdd495ae | Iustin Pop | |
1769 | cdd495ae | Iustin Pop | instance Arbitrary Luxi.LuxiReq where |
1770 | cdd495ae | Iustin Pop | arbitrary = elements [minBound..maxBound] |
1771 | cdd495ae | Iustin Pop | |
1772 | cdd495ae | Iustin Pop | instance Arbitrary Luxi.QrViaLuxi where |
1773 | cdd495ae | Iustin Pop | arbitrary = elements [minBound..maxBound] |
1774 | cdd495ae | Iustin Pop | |
1775 | cdd495ae | Iustin Pop | instance Arbitrary Luxi.LuxiOp where |
1776 | cdd495ae | Iustin Pop | arbitrary = do |
1777 | cdd495ae | Iustin Pop | lreq <- arbitrary |
1778 | cdd495ae | Iustin Pop | case lreq of |
1779 | cdd495ae | Iustin Pop | Luxi.ReqQuery -> Luxi.Query <$> arbitrary <*> getFields <*> arbitrary |
1780 | cdd495ae | Iustin Pop | Luxi.ReqQueryNodes -> Luxi.QueryNodes <$> (listOf getFQDN) <*> |
1781 | cdd495ae | Iustin Pop | getFields <*> arbitrary |
1782 | cdd495ae | Iustin Pop | Luxi.ReqQueryGroups -> Luxi.QueryGroups <$> arbitrary <*> |
1783 | cdd495ae | Iustin Pop | arbitrary <*> arbitrary |
1784 | cdd495ae | Iustin Pop | Luxi.ReqQueryInstances -> Luxi.QueryInstances <$> (listOf getFQDN) <*> |
1785 | cdd495ae | Iustin Pop | getFields <*> arbitrary |
1786 | cdd495ae | Iustin Pop | Luxi.ReqQueryJobs -> Luxi.QueryJobs <$> arbitrary <*> getFields |
1787 | cdd495ae | Iustin Pop | Luxi.ReqQueryExports -> Luxi.QueryExports <$> |
1788 | cdd495ae | Iustin Pop | (listOf getFQDN) <*> arbitrary |
1789 | cdd495ae | Iustin Pop | Luxi.ReqQueryConfigValues -> Luxi.QueryConfigValues <$> getFields |
1790 | cdd495ae | Iustin Pop | Luxi.ReqQueryClusterInfo -> pure Luxi.QueryClusterInfo |
1791 | cdd495ae | Iustin Pop | Luxi.ReqQueryTags -> Luxi.QueryTags <$> getName <*> getFQDN |
1792 | cdd495ae | Iustin Pop | Luxi.ReqSubmitJob -> Luxi.SubmitJob <$> (resize maxOpCodes arbitrary) |
1793 | cdd495ae | Iustin Pop | Luxi.ReqSubmitManyJobs -> Luxi.SubmitManyJobs <$> |
1794 | cdd495ae | Iustin Pop | (resize maxOpCodes arbitrary) |
1795 | cdd495ae | Iustin Pop | Luxi.ReqWaitForJobChange -> Luxi.WaitForJobChange <$> arbitrary <*> |
1796 | cdd495ae | Iustin Pop | getFields <*> pure J.JSNull <*> |
1797 | cdd495ae | Iustin Pop | pure J.JSNull <*> arbitrary |
1798 | cdd495ae | Iustin Pop | Luxi.ReqArchiveJob -> Luxi.ArchiveJob <$> arbitrary |
1799 | cdd495ae | Iustin Pop | Luxi.ReqAutoArchiveJobs -> Luxi.AutoArchiveJobs <$> arbitrary <*> |
1800 | cdd495ae | Iustin Pop | arbitrary |
1801 | cdd495ae | Iustin Pop | Luxi.ReqCancelJob -> Luxi.CancelJob <$> arbitrary |
1802 | cdd495ae | Iustin Pop | Luxi.ReqSetDrainFlag -> Luxi.SetDrainFlag <$> arbitrary |
1803 | cdd495ae | Iustin Pop | Luxi.ReqSetWatcherPause -> Luxi.SetWatcherPause <$> arbitrary |
1804 | cdd495ae | Iustin Pop | |
1805 | cdd495ae | Iustin Pop | -- | Simple check that encoding/decoding of LuxiOp works. |
1806 | cdd495ae | Iustin Pop | prop_Luxi_CallEncoding :: Luxi.LuxiOp -> Property |
1807 | cdd495ae | Iustin Pop | prop_Luxi_CallEncoding op = |
1808 | cdd495ae | Iustin Pop | (Luxi.validateCall (Luxi.buildCall op) >>= Luxi.decodeCall) ==? Types.Ok op |
1809 | cdd495ae | Iustin Pop | |
1810 | cdd495ae | Iustin Pop | testSuite "LUXI" |
1811 | cdd495ae | Iustin Pop | [ 'prop_Luxi_CallEncoding |
1812 | cdd495ae | Iustin Pop | ] |
1813 | c5b4a186 | Iustin Pop | |
1814 | c5b4a186 | Iustin Pop | -- * Ssconf tests |
1815 | c5b4a186 | Iustin Pop | |
1816 | c5b4a186 | Iustin Pop | instance Arbitrary Ssconf.SSKey where |
1817 | c5b4a186 | Iustin Pop | arbitrary = elements [minBound..maxBound] |
1818 | c5b4a186 | Iustin Pop | |
1819 | fce98abd | Iustin Pop | prop_Ssconf_filename :: Ssconf.SSKey -> Property |
1820 | c5b4a186 | Iustin Pop | prop_Ssconf_filename key = |
1821 | c5b4a186 | Iustin Pop | printTestCase "Key doesn't start with correct prefix" $ |
1822 | c5b4a186 | Iustin Pop | Ssconf.sSFilePrefix `isPrefixOf` Ssconf.keyToFilename (Just "") key |
1823 | c5b4a186 | Iustin Pop | |
1824 | c5b4a186 | Iustin Pop | testSuite "Ssconf" |
1825 | c5b4a186 | Iustin Pop | [ 'prop_Ssconf_filename |
1826 | c5b4a186 | Iustin Pop | ] |