Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / QC.hs @ b37f4a76

History | View | Annotate | Download (47 kB)

1 23fe06c2 Iustin Pop
{-# LANGUAGE TemplateHaskell #-}
2 23fe06c2 Iustin Pop
3 525bfb36 Iustin Pop
{-| Unittests for ganeti-htools.
4 e2fa2baf Iustin Pop
5 e2fa2baf Iustin Pop
-}
6 e2fa2baf Iustin Pop
7 e2fa2baf Iustin Pop
{-
8 e2fa2baf Iustin Pop
9 d6eec019 Iustin Pop
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
10 e2fa2baf Iustin Pop
11 e2fa2baf Iustin Pop
This program is free software; you can redistribute it and/or modify
12 e2fa2baf Iustin Pop
it under the terms of the GNU General Public License as published by
13 e2fa2baf Iustin Pop
the Free Software Foundation; either version 2 of the License, or
14 e2fa2baf Iustin Pop
(at your option) any later version.
15 e2fa2baf Iustin Pop
16 e2fa2baf Iustin Pop
This program is distributed in the hope that it will be useful, but
17 e2fa2baf Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
18 e2fa2baf Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 e2fa2baf Iustin Pop
General Public License for more details.
20 e2fa2baf Iustin Pop
21 e2fa2baf Iustin Pop
You should have received a copy of the GNU General Public License
22 e2fa2baf Iustin Pop
along with this program; if not, write to the Free Software
23 e2fa2baf Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 e2fa2baf Iustin Pop
02110-1301, USA.
25 e2fa2baf Iustin Pop
26 e2fa2baf Iustin Pop
-}
27 e2fa2baf Iustin Pop
28 15f4c8ca Iustin Pop
module Ganeti.HTools.QC
29 d5dfae0a Iustin Pop
  ( testUtils
30 d5dfae0a Iustin Pop
  , testPeerMap
31 d5dfae0a Iustin Pop
  , testContainer
32 d5dfae0a Iustin Pop
  , testInstance
33 d5dfae0a Iustin Pop
  , testNode
34 d5dfae0a Iustin Pop
  , testText
35 d5dfae0a Iustin Pop
  , testOpCodes
36 d5dfae0a Iustin Pop
  , testJobs
37 d5dfae0a Iustin Pop
  , testCluster
38 d5dfae0a Iustin Pop
  , testLoader
39 d5dfae0a Iustin Pop
  , testTypes
40 d5dfae0a Iustin Pop
  ) where
41 15f4c8ca Iustin Pop
42 15f4c8ca Iustin Pop
import Test.QuickCheck
43 bc782180 Iustin Pop
import Data.List (findIndex, intercalate, nub, isPrefixOf)
44 15f4c8ca Iustin Pop
import Data.Maybe
45 88f25dd0 Iustin Pop
import Control.Monad
46 88f25dd0 Iustin Pop
import qualified Text.JSON as J
47 8fcf251f Iustin Pop
import qualified Data.Map
48 3fea6959 Iustin Pop
import qualified Data.IntMap as IntMap
49 88f25dd0 Iustin Pop
import qualified Ganeti.OpCodes as OpCodes
50 db079755 Iustin Pop
import qualified Ganeti.Jobs as Jobs
51 223dbe53 Iustin Pop
import qualified Ganeti.Luxi
52 15f4c8ca Iustin Pop
import qualified Ganeti.HTools.CLI as CLI
53 15f4c8ca Iustin Pop
import qualified Ganeti.HTools.Cluster as Cluster
54 15f4c8ca Iustin Pop
import qualified Ganeti.HTools.Container as Container
55 223dbe53 Iustin Pop
import qualified Ganeti.HTools.ExtLoader
56 15f4c8ca Iustin Pop
import qualified Ganeti.HTools.IAlloc as IAlloc
57 15f4c8ca Iustin Pop
import qualified Ganeti.HTools.Instance as Instance
58 b69be409 Iustin Pop
import qualified Ganeti.HTools.JSON as JSON
59 15f4c8ca Iustin Pop
import qualified Ganeti.HTools.Loader as Loader
60 223dbe53 Iustin Pop
import qualified Ganeti.HTools.Luxi
61 15f4c8ca Iustin Pop
import qualified Ganeti.HTools.Node as Node
62 10ef6b4e Iustin Pop
import qualified Ganeti.HTools.Group as Group
63 15f4c8ca Iustin Pop
import qualified Ganeti.HTools.PeerMap as PeerMap
64 c478f837 Iustin Pop
import qualified Ganeti.HTools.Rapi
65 223dbe53 Iustin Pop
import qualified Ganeti.HTools.Simu
66 15f4c8ca Iustin Pop
import qualified Ganeti.HTools.Text as Text
67 15f4c8ca Iustin Pop
import qualified Ganeti.HTools.Types as Types
68 15f4c8ca Iustin Pop
import qualified Ganeti.HTools.Utils as Utils
69 223dbe53 Iustin Pop
import qualified Ganeti.HTools.Version
70 e82271f8 Iustin Pop
import qualified Ganeti.Constants as C
71 15f4c8ca Iustin Pop
72 33b9d92d Iustin Pop
import qualified Ganeti.HTools.Program.Hail
73 33b9d92d Iustin Pop
import qualified Ganeti.HTools.Program.Hbal
74 33b9d92d Iustin Pop
import qualified Ganeti.HTools.Program.Hscan
75 33b9d92d Iustin Pop
import qualified Ganeti.HTools.Program.Hspace
76 33b9d92d Iustin Pop
77 23fe06c2 Iustin Pop
import Ganeti.HTools.QCHelper (testSuite)
78 8e4f6d56 Iustin Pop
79 3fea6959 Iustin Pop
-- * Constants
80 3fea6959 Iustin Pop
81 525bfb36 Iustin Pop
-- | Maximum memory (1TiB, somewhat random value).
82 8fcf251f Iustin Pop
maxMem :: Int
83 8fcf251f Iustin Pop
maxMem = 1024 * 1024
84 8fcf251f Iustin Pop
85 525bfb36 Iustin Pop
-- | Maximum disk (8TiB, somewhat random value).
86 8fcf251f Iustin Pop
maxDsk :: Int
87 49f9627a Iustin Pop
maxDsk = 1024 * 1024 * 8
88 8fcf251f Iustin Pop
89 525bfb36 Iustin Pop
-- | Max CPUs (1024, somewhat random value).
90 8fcf251f Iustin Pop
maxCpu :: Int
91 8fcf251f Iustin Pop
maxCpu = 1024
92 8fcf251f Iustin Pop
93 6cff91f5 Iustin Pop
-- | Null iPolicy, and by null we mean very liberal.
94 6cff91f5 Iustin Pop
nullIPolicy = Types.IPolicy
95 6cff91f5 Iustin Pop
  { Types.iPolicyMinSpec = Types.ISpec { Types.iSpecMemorySize = 0
96 6cff91f5 Iustin Pop
                                       , Types.iSpecCpuCount   = 0
97 6cff91f5 Iustin Pop
                                       , Types.iSpecDiskSize   = 0
98 6cff91f5 Iustin Pop
                                       , Types.iSpecDiskCount  = 0
99 6cff91f5 Iustin Pop
                                       , Types.iSpecNicCount   = 0
100 6cff91f5 Iustin Pop
                                       }
101 6cff91f5 Iustin Pop
  , Types.iPolicyMaxSpec = Types.ISpec { Types.iSpecMemorySize = maxBound
102 6cff91f5 Iustin Pop
                                       , Types.iSpecCpuCount   = maxBound
103 6cff91f5 Iustin Pop
                                       , Types.iSpecDiskSize   = maxBound
104 6cff91f5 Iustin Pop
                                       , Types.iSpecDiskCount  = C.maxDisks
105 6cff91f5 Iustin Pop
                                       , Types.iSpecNicCount   = C.maxNics
106 6cff91f5 Iustin Pop
                                       }
107 6cff91f5 Iustin Pop
  , Types.iPolicyStdSpec = Types.ISpec { Types.iSpecMemorySize = Types.unitMem
108 6cff91f5 Iustin Pop
                                       , Types.iSpecCpuCount   = Types.unitCpu
109 6cff91f5 Iustin Pop
                                       , Types.iSpecDiskSize   = Types.unitDsk
110 6cff91f5 Iustin Pop
                                       , Types.iSpecDiskCount  = 1
111 6cff91f5 Iustin Pop
                                       , Types.iSpecNicCount   = 1
112 6cff91f5 Iustin Pop
                                       }
113 6cff91f5 Iustin Pop
  , Types.iPolicyDiskTemplates = [Types.DTDrbd8, Types.DTPlain]
114 6cff91f5 Iustin Pop
  }
115 6cff91f5 Iustin Pop
116 6cff91f5 Iustin Pop
117 10ef6b4e Iustin Pop
defGroup :: Group.Group
118 10ef6b4e Iustin Pop
defGroup = flip Group.setIdx 0 $
119 f3f76ccc Iustin Pop
             Group.create "default" Types.defaultGroupID Types.AllocPreferred
120 6cff91f5 Iustin Pop
                  nullIPolicy
121 10ef6b4e Iustin Pop
122 10ef6b4e Iustin Pop
defGroupList :: Group.List
123 cb0c77ff Iustin Pop
defGroupList = Container.fromList [(Group.idx defGroup, defGroup)]
124 10ef6b4e Iustin Pop
125 10ef6b4e Iustin Pop
defGroupAssoc :: Data.Map.Map String Types.Gdx
126 10ef6b4e Iustin Pop
defGroupAssoc = Data.Map.singleton (Group.uuid defGroup) (Group.idx defGroup)
127 10ef6b4e Iustin Pop
128 3fea6959 Iustin Pop
-- * Helper functions
129 3fea6959 Iustin Pop
130 525bfb36 Iustin Pop
-- | Simple checker for whether OpResult is fail or pass.
131 79a72ce7 Iustin Pop
isFailure :: Types.OpResult a -> Bool
132 79a72ce7 Iustin Pop
isFailure (Types.OpFail _) = True
133 79a72ce7 Iustin Pop
isFailure _ = False
134 79a72ce7 Iustin Pop
135 72bb6b4e Iustin Pop
-- | Checks for equality with proper annotation.
136 72bb6b4e Iustin Pop
(==?) :: (Show a, Eq a) => a -> a -> Property
137 72bb6b4e Iustin Pop
(==?) x y = printTestCase
138 72bb6b4e Iustin Pop
            ("Expected equality, but '" ++
139 72bb6b4e Iustin Pop
             show x ++ "' /= '" ++ show y ++ "'") (x == y)
140 72bb6b4e Iustin Pop
infix 3 ==?
141 72bb6b4e Iustin Pop
142 525bfb36 Iustin Pop
-- | Update an instance to be smaller than a node.
143 3fea6959 Iustin Pop
setInstanceSmallerThanNode node inst =
144 d5dfae0a Iustin Pop
  inst { Instance.mem = Node.availMem node `div` 2
145 d5dfae0a Iustin Pop
       , Instance.dsk = Node.availDisk node `div` 2
146 d5dfae0a Iustin Pop
       , Instance.vcpus = Node.availCpu node `div` 2
147 d5dfae0a Iustin Pop
       }
148 3fea6959 Iustin Pop
149 dce9bbb3 Iustin Pop
-- | Check if an instance is smaller than a node.
150 dce9bbb3 Iustin Pop
isInstanceSmallerThanNode node inst =
151 dce9bbb3 Iustin Pop
  Instance.mem inst   <= Node.availMem node `div` 2 &&
152 dce9bbb3 Iustin Pop
  Instance.dsk inst   <= Node.availDisk node `div` 2 &&
153 dce9bbb3 Iustin Pop
  Instance.vcpus inst <= Node.availCpu node `div` 2
154 dce9bbb3 Iustin Pop
155 525bfb36 Iustin Pop
-- | Create an instance given its spec.
156 3fea6959 Iustin Pop
createInstance mem dsk vcpus =
157 d5dfae0a Iustin Pop
  Instance.create "inst-unnamed" mem dsk vcpus Types.Running [] True (-1) (-1)
158 d5dfae0a Iustin Pop
    Types.DTDrbd8
159 3fea6959 Iustin Pop
160 525bfb36 Iustin Pop
-- | Create a small cluster by repeating a node spec.
161 3fea6959 Iustin Pop
makeSmallCluster :: Node.Node -> Int -> Node.List
162 3fea6959 Iustin Pop
makeSmallCluster node count =
163 e73c5fe2 Iustin Pop
  let origname = Node.name node
164 e73c5fe2 Iustin Pop
      origalias = Node.alias node
165 e73c5fe2 Iustin Pop
      nodes = map (\idx -> node { Node.name = origname ++ "-" ++ show idx
166 e73c5fe2 Iustin Pop
                                , Node.alias = origalias ++ "-" ++ show idx })
167 e73c5fe2 Iustin Pop
              [1..count]
168 e73c5fe2 Iustin Pop
      fn = flip Node.buildPeers Container.empty
169 e73c5fe2 Iustin Pop
      namelst = map (\n -> (Node.name n, fn n)) nodes
170 d5dfae0a Iustin Pop
      (_, nlst) = Loader.assignIndices namelst
171 d5dfae0a Iustin Pop
  in nlst
172 3fea6959 Iustin Pop
173 3603605a Iustin Pop
-- | Make a small cluster, both nodes and instances.
174 3603605a Iustin Pop
makeSmallEmptyCluster :: Node.Node -> Int -> Instance.Instance
175 3603605a Iustin Pop
                      -> (Node.List, Instance.List, Instance.Instance)
176 3603605a Iustin Pop
makeSmallEmptyCluster node count inst =
177 3603605a Iustin Pop
  (makeSmallCluster node count, Container.empty,
178 3603605a Iustin Pop
   setInstanceSmallerThanNode node inst)
179 3603605a Iustin Pop
180 525bfb36 Iustin Pop
-- | Checks if a node is "big" enough.
181 d6f9f5bd Iustin Pop
isNodeBig :: Int -> Node.Node -> Bool
182 d6f9f5bd Iustin Pop
isNodeBig size node = Node.availDisk node > size * Types.unitDsk
183 3fea6959 Iustin Pop
                      && Node.availMem node > size * Types.unitMem
184 3fea6959 Iustin Pop
                      && Node.availCpu node > size * Types.unitCpu
185 3fea6959 Iustin Pop
186 e08424a8 Guido Trotter
canBalance :: Cluster.Table -> Bool -> Bool -> Bool -> Bool
187 e08424a8 Guido Trotter
canBalance tbl dm im evac = isJust $ Cluster.tryBalance tbl dm im evac 0 0
188 3fea6959 Iustin Pop
189 f4161783 Iustin Pop
-- | Assigns a new fresh instance to a cluster; this is not
190 525bfb36 Iustin Pop
-- allocation, so no resource checks are done.
191 f4161783 Iustin Pop
assignInstance :: Node.List -> Instance.List -> Instance.Instance ->
192 f4161783 Iustin Pop
                  Types.Idx -> Types.Idx ->
193 f4161783 Iustin Pop
                  (Node.List, Instance.List)
194 f4161783 Iustin Pop
assignInstance nl il inst pdx sdx =
195 f4161783 Iustin Pop
  let pnode = Container.find pdx nl
196 f4161783 Iustin Pop
      snode = Container.find sdx nl
197 f4161783 Iustin Pop
      maxiidx = if Container.null il
198 d5dfae0a Iustin Pop
                  then 0
199 d5dfae0a Iustin Pop
                  else fst (Container.findMax il) + 1
200 f4161783 Iustin Pop
      inst' = inst { Instance.idx = maxiidx,
201 f4161783 Iustin Pop
                     Instance.pNode = pdx, Instance.sNode = sdx }
202 f4161783 Iustin Pop
      pnode' = Node.setPri pnode inst'
203 f4161783 Iustin Pop
      snode' = Node.setSec snode inst'
204 f4161783 Iustin Pop
      nl' = Container.addTwo pdx pnode' sdx snode' nl
205 f4161783 Iustin Pop
      il' = Container.add maxiidx inst' il
206 f4161783 Iustin Pop
  in (nl', il')
207 f4161783 Iustin Pop
208 3fea6959 Iustin Pop
-- * Arbitrary instances
209 3fea6959 Iustin Pop
210 525bfb36 Iustin Pop
-- | Defines a DNS name.
211 a070c426 Iustin Pop
newtype DNSChar = DNSChar { dnsGetChar::Char }
212 525bfb36 Iustin Pop
213 a070c426 Iustin Pop
instance Arbitrary DNSChar where
214 d5dfae0a Iustin Pop
  arbitrary = do
215 d5dfae0a Iustin Pop
    x <- elements (['a'..'z'] ++ ['0'..'9'] ++ "_-")
216 d5dfae0a Iustin Pop
    return (DNSChar x)
217 a070c426 Iustin Pop
218 a070c426 Iustin Pop
getName :: Gen String
219 a070c426 Iustin Pop
getName = do
220 a070c426 Iustin Pop
  n <- choose (1, 64)
221 a070c426 Iustin Pop
  dn <- vector n::Gen [DNSChar]
222 a070c426 Iustin Pop
  return (map dnsGetChar dn)
223 a070c426 Iustin Pop
224 a070c426 Iustin Pop
getFQDN :: Gen String
225 a070c426 Iustin Pop
getFQDN = do
226 a070c426 Iustin Pop
  felem <- getName
227 a070c426 Iustin Pop
  ncomps <- choose (1, 4)
228 a070c426 Iustin Pop
  frest <- vector ncomps::Gen [[DNSChar]]
229 a070c426 Iustin Pop
  let frest' = map (map dnsGetChar) frest
230 a070c426 Iustin Pop
  return (felem ++ "." ++ intercalate "." frest')
231 a070c426 Iustin Pop
232 dce9bbb3 Iustin Pop
-- | Defines a tag type.
233 dce9bbb3 Iustin Pop
newtype TagChar = TagChar { tagGetChar :: Char }
234 dce9bbb3 Iustin Pop
235 dce9bbb3 Iustin Pop
-- | All valid tag chars. This doesn't need to match _exactly_
236 dce9bbb3 Iustin Pop
-- Ganeti's own tag regex, just enough for it to be close.
237 dce9bbb3 Iustin Pop
tagChar :: [Char]
238 dce9bbb3 Iustin Pop
tagChar = ['a'..'z'] ++ ['A'..'Z'] ++ ['0'..'9'] ++ ".+*/:@-"
239 dce9bbb3 Iustin Pop
240 dce9bbb3 Iustin Pop
instance Arbitrary TagChar where
241 dce9bbb3 Iustin Pop
  arbitrary = do
242 dce9bbb3 Iustin Pop
    c <- elements tagChar
243 dce9bbb3 Iustin Pop
    return (TagChar c)
244 dce9bbb3 Iustin Pop
245 dce9bbb3 Iustin Pop
-- | Generates a tag
246 dce9bbb3 Iustin Pop
genTag :: Gen [TagChar]
247 dce9bbb3 Iustin Pop
genTag = do
248 dce9bbb3 Iustin Pop
  -- the correct value would be C.maxTagLen, but that's way too
249 dce9bbb3 Iustin Pop
  -- verbose in unittests, and at the moment I don't see any possible
250 dce9bbb3 Iustin Pop
  -- bugs with longer tags and the way we use tags in htools
251 dce9bbb3 Iustin Pop
  n <- choose (1, 10)
252 dce9bbb3 Iustin Pop
  vector n
253 dce9bbb3 Iustin Pop
254 dce9bbb3 Iustin Pop
-- | Generates a list of tags (correctly upper bounded).
255 dce9bbb3 Iustin Pop
genTags :: Gen [String]
256 dce9bbb3 Iustin Pop
genTags = do
257 dce9bbb3 Iustin Pop
  -- the correct value would be C.maxTagsPerObj, but per the comment
258 dce9bbb3 Iustin Pop
  -- in genTag, we don't use tags enough in htools to warrant testing
259 dce9bbb3 Iustin Pop
  -- such big values
260 dce9bbb3 Iustin Pop
  n <- choose (0, 10::Int)
261 dce9bbb3 Iustin Pop
  tags <- mapM (const genTag) [1..n]
262 dce9bbb3 Iustin Pop
  return $ map (map tagGetChar) tags
263 dce9bbb3 Iustin Pop
264 7dd14211 Agata Murawska
instance Arbitrary Types.InstanceStatus where
265 e1bf27bb Agata Murawska
    arbitrary = elements [minBound..maxBound]
266 7dd14211 Agata Murawska
267 15f4c8ca Iustin Pop
-- let's generate a random instance
268 15f4c8ca Iustin Pop
instance Arbitrary Instance.Instance where
269 d5dfae0a Iustin Pop
  arbitrary = do
270 d5dfae0a Iustin Pop
    name <- getFQDN
271 d5dfae0a Iustin Pop
    mem <- choose (0, maxMem)
272 d5dfae0a Iustin Pop
    dsk <- choose (0, maxDsk)
273 d5dfae0a Iustin Pop
    run_st <- arbitrary
274 d5dfae0a Iustin Pop
    pn <- arbitrary
275 d5dfae0a Iustin Pop
    sn <- arbitrary
276 d5dfae0a Iustin Pop
    vcpus <- choose (0, maxCpu)
277 d5dfae0a Iustin Pop
    return $ Instance.create name mem dsk vcpus run_st [] True pn sn
278 d5dfae0a Iustin Pop
              Types.DTDrbd8
279 15f4c8ca Iustin Pop
280 525bfb36 Iustin Pop
-- | Generas an arbitrary node based on sizing information.
281 525bfb36 Iustin Pop
genNode :: Maybe Int -- ^ Minimum node size in terms of units
282 525bfb36 Iustin Pop
        -> Maybe Int -- ^ Maximum node size (when Nothing, bounded
283 525bfb36 Iustin Pop
                     -- just by the max... constants)
284 525bfb36 Iustin Pop
        -> Gen Node.Node
285 00c75986 Iustin Pop
genNode min_multiplier max_multiplier = do
286 00c75986 Iustin Pop
  let (base_mem, base_dsk, base_cpu) =
287 d5dfae0a Iustin Pop
        case min_multiplier of
288 d5dfae0a Iustin Pop
          Just mm -> (mm * Types.unitMem,
289 d5dfae0a Iustin Pop
                      mm * Types.unitDsk,
290 d5dfae0a Iustin Pop
                      mm * Types.unitCpu)
291 d5dfae0a Iustin Pop
          Nothing -> (0, 0, 0)
292 00c75986 Iustin Pop
      (top_mem, top_dsk, top_cpu)  =
293 d5dfae0a Iustin Pop
        case max_multiplier of
294 d5dfae0a Iustin Pop
          Just mm -> (mm * Types.unitMem,
295 d5dfae0a Iustin Pop
                      mm * Types.unitDsk,
296 d5dfae0a Iustin Pop
                      mm * Types.unitCpu)
297 d5dfae0a Iustin Pop
          Nothing -> (maxMem, maxDsk, maxCpu)
298 00c75986 Iustin Pop
  name  <- getFQDN
299 00c75986 Iustin Pop
  mem_t <- choose (base_mem, top_mem)
300 00c75986 Iustin Pop
  mem_f <- choose (base_mem, mem_t)
301 00c75986 Iustin Pop
  mem_n <- choose (0, mem_t - mem_f)
302 00c75986 Iustin Pop
  dsk_t <- choose (base_dsk, top_dsk)
303 00c75986 Iustin Pop
  dsk_f <- choose (base_dsk, dsk_t)
304 00c75986 Iustin Pop
  cpu_t <- choose (base_cpu, top_cpu)
305 00c75986 Iustin Pop
  offl  <- arbitrary
306 00c75986 Iustin Pop
  let n = Node.create name (fromIntegral mem_t) mem_n mem_f
307 00c75986 Iustin Pop
          (fromIntegral dsk_t) dsk_f (fromIntegral cpu_t) offl 0
308 d6eec019 Iustin Pop
      n' = Node.setPolicy nullIPolicy n
309 d6eec019 Iustin Pop
  return $ Node.buildPeers n' Container.empty
310 00c75986 Iustin Pop
311 d6f9f5bd Iustin Pop
-- | Helper function to generate a sane node.
312 d6f9f5bd Iustin Pop
genOnlineNode :: Gen Node.Node
313 d6f9f5bd Iustin Pop
genOnlineNode = do
314 d6f9f5bd Iustin Pop
  arbitrary `suchThat` (\n -> not (Node.offline n) &&
315 d6f9f5bd Iustin Pop
                              not (Node.failN1 n) &&
316 d6f9f5bd Iustin Pop
                              Node.availDisk n > 0 &&
317 d6f9f5bd Iustin Pop
                              Node.availMem n > 0 &&
318 d6f9f5bd Iustin Pop
                              Node.availCpu n > 0)
319 d6f9f5bd Iustin Pop
320 15f4c8ca Iustin Pop
-- and a random node
321 15f4c8ca Iustin Pop
instance Arbitrary Node.Node where
322 d5dfae0a Iustin Pop
  arbitrary = genNode Nothing Nothing
323 15f4c8ca Iustin Pop
324 88f25dd0 Iustin Pop
-- replace disks
325 88f25dd0 Iustin Pop
instance Arbitrary OpCodes.ReplaceDisksMode where
326 e1bf27bb Agata Murawska
  arbitrary = elements [minBound..maxBound]
327 88f25dd0 Iustin Pop
328 88f25dd0 Iustin Pop
instance Arbitrary OpCodes.OpCode where
329 88f25dd0 Iustin Pop
  arbitrary = do
330 88f25dd0 Iustin Pop
    op_id <- elements [ "OP_TEST_DELAY"
331 88f25dd0 Iustin Pop
                      , "OP_INSTANCE_REPLACE_DISKS"
332 88f25dd0 Iustin Pop
                      , "OP_INSTANCE_FAILOVER"
333 88f25dd0 Iustin Pop
                      , "OP_INSTANCE_MIGRATE"
334 88f25dd0 Iustin Pop
                      ]
335 3603605a Iustin Pop
    case op_id of
336 3603605a Iustin Pop
      "OP_TEST_DELAY" ->
337 3603605a Iustin Pop
        liftM3 OpCodes.OpTestDelay arbitrary arbitrary arbitrary
338 3603605a Iustin Pop
      "OP_INSTANCE_REPLACE_DISKS" ->
339 3603605a Iustin Pop
        liftM5 OpCodes.OpInstanceReplaceDisks arbitrary arbitrary
340 3603605a Iustin Pop
          arbitrary arbitrary arbitrary
341 3603605a Iustin Pop
      "OP_INSTANCE_FAILOVER" ->
342 3603605a Iustin Pop
        liftM3 OpCodes.OpInstanceFailover arbitrary arbitrary
343 3603605a Iustin Pop
          arbitrary
344 3603605a Iustin Pop
      "OP_INSTANCE_MIGRATE" ->
345 3603605a Iustin Pop
        liftM5 OpCodes.OpInstanceMigrate arbitrary arbitrary
346 3603605a Iustin Pop
          arbitrary arbitrary arbitrary
347 3603605a Iustin Pop
      _ -> fail "Wrong opcode"
348 88f25dd0 Iustin Pop
349 db079755 Iustin Pop
instance Arbitrary Jobs.OpStatus where
350 db079755 Iustin Pop
  arbitrary = elements [minBound..maxBound]
351 db079755 Iustin Pop
352 db079755 Iustin Pop
instance Arbitrary Jobs.JobStatus where
353 db079755 Iustin Pop
  arbitrary = elements [minBound..maxBound]
354 db079755 Iustin Pop
355 525bfb36 Iustin Pop
newtype SmallRatio = SmallRatio Double deriving Show
356 525bfb36 Iustin Pop
instance Arbitrary SmallRatio where
357 d5dfae0a Iustin Pop
  arbitrary = do
358 d5dfae0a Iustin Pop
    v <- choose (0, 1)
359 d5dfae0a Iustin Pop
    return $ SmallRatio v
360 525bfb36 Iustin Pop
361 3c002a13 Iustin Pop
instance Arbitrary Types.AllocPolicy where
362 3c002a13 Iustin Pop
  arbitrary = elements [minBound..maxBound]
363 3c002a13 Iustin Pop
364 3c002a13 Iustin Pop
instance Arbitrary Types.DiskTemplate where
365 3c002a13 Iustin Pop
  arbitrary = elements [minBound..maxBound]
366 3c002a13 Iustin Pop
367 0047d4e2 Iustin Pop
instance Arbitrary Types.FailMode where
368 d5dfae0a Iustin Pop
  arbitrary = elements [minBound..maxBound]
369 0047d4e2 Iustin Pop
370 0047d4e2 Iustin Pop
instance Arbitrary a => Arbitrary (Types.OpResult a) where
371 d5dfae0a Iustin Pop
  arbitrary = arbitrary >>= \c ->
372 3603605a Iustin Pop
              if c
373 3603605a Iustin Pop
                then liftM Types.OpGood arbitrary
374 3603605a Iustin Pop
                else liftM Types.OpFail arbitrary
375 0047d4e2 Iustin Pop
376 00b70680 Iustin Pop
instance Arbitrary Types.ISpec where
377 00b70680 Iustin Pop
  arbitrary = do
378 00b70680 Iustin Pop
    mem <- arbitrary::Gen (NonNegative Int)
379 00b70680 Iustin Pop
    dsk_c <- arbitrary::Gen (NonNegative Int)
380 00b70680 Iustin Pop
    dsk_s <- arbitrary::Gen (NonNegative Int)
381 00b70680 Iustin Pop
    cpu <- arbitrary::Gen (NonNegative Int)
382 00b70680 Iustin Pop
    nic <- arbitrary::Gen (NonNegative Int)
383 00b70680 Iustin Pop
    return Types.ISpec { Types.iSpecMemorySize = fromIntegral mem
384 00b70680 Iustin Pop
                       , Types.iSpecCpuCount   = fromIntegral cpu
385 00b70680 Iustin Pop
                       , Types.iSpecDiskSize   = fromIntegral dsk_s
386 00b70680 Iustin Pop
                       , Types.iSpecDiskCount  = fromIntegral dsk_c
387 00b70680 Iustin Pop
                       , Types.iSpecNicCount   = fromIntegral nic
388 00b70680 Iustin Pop
                       }
389 00b70680 Iustin Pop
390 00b70680 Iustin Pop
-- | Helper function to check whether a spec is LTE than another
391 00b70680 Iustin Pop
iSpecSmaller :: Types.ISpec -> Types.ISpec -> Bool
392 00b70680 Iustin Pop
iSpecSmaller imin imax =
393 00b70680 Iustin Pop
  Types.iSpecMemorySize imin <= Types.iSpecMemorySize imax &&
394 00b70680 Iustin Pop
  Types.iSpecCpuCount imin   <= Types.iSpecCpuCount imax &&
395 00b70680 Iustin Pop
  Types.iSpecDiskSize imin   <= Types.iSpecDiskSize imax &&
396 00b70680 Iustin Pop
  Types.iSpecDiskCount imin  <= Types.iSpecDiskCount imax &&
397 00b70680 Iustin Pop
  Types.iSpecNicCount imin   <= Types.iSpecNicCount imax
398 00b70680 Iustin Pop
399 00b70680 Iustin Pop
instance Arbitrary Types.IPolicy where
400 00b70680 Iustin Pop
  arbitrary = do
401 00b70680 Iustin Pop
    imin <- arbitrary
402 00b70680 Iustin Pop
    istd <- arbitrary `suchThat` (iSpecSmaller imin)
403 00b70680 Iustin Pop
    imax <- arbitrary `suchThat` (iSpecSmaller istd)
404 00b70680 Iustin Pop
    dts  <- arbitrary
405 00b70680 Iustin Pop
    return Types.IPolicy { Types.iPolicyMinSpec = imin
406 00b70680 Iustin Pop
                         , Types.iPolicyStdSpec = istd
407 00b70680 Iustin Pop
                         , Types.iPolicyMaxSpec = imax
408 00b70680 Iustin Pop
                         , Types.iPolicyDiskTemplates = dts
409 00b70680 Iustin Pop
                         }
410 00b70680 Iustin Pop
411 3fea6959 Iustin Pop
-- * Actual tests
412 8fcf251f Iustin Pop
413 525bfb36 Iustin Pop
-- ** Utils tests
414 525bfb36 Iustin Pop
415 525bfb36 Iustin Pop
-- | If the list is not just an empty element, and if the elements do
416 525bfb36 Iustin Pop
-- not contain commas, then join+split should be idempotent.
417 a1cd7c1e Iustin Pop
prop_Utils_commaJoinSplit =
418 d5dfae0a Iustin Pop
  forAll (arbitrary `suchThat`
419 3603605a Iustin Pop
          (\l -> l /= [""] && all (notElem ',') l )) $ \lst ->
420 d5dfae0a Iustin Pop
  Utils.sepSplit ',' (Utils.commaJoin lst) ==? lst
421 a1cd7c1e Iustin Pop
422 525bfb36 Iustin Pop
-- | Split and join should always be idempotent.
423 72bb6b4e Iustin Pop
prop_Utils_commaSplitJoin s =
424 d5dfae0a Iustin Pop
  Utils.commaJoin (Utils.sepSplit ',' s) ==? s
425 691dcd2a Iustin Pop
426 a810ad21 Iustin Pop
-- | fromObjWithDefault, we test using the Maybe monad and an integer
427 525bfb36 Iustin Pop
-- value.
428 a810ad21 Iustin Pop
prop_Utils_fromObjWithDefault def_value random_key =
429 d5dfae0a Iustin Pop
  -- a missing key will be returned with the default
430 b69be409 Iustin Pop
  JSON.fromObjWithDefault [] random_key def_value == Just def_value &&
431 d5dfae0a Iustin Pop
  -- a found key will be returned as is, not with default
432 b69be409 Iustin Pop
  JSON.fromObjWithDefault [(random_key, J.showJSON def_value)]
433 d5dfae0a Iustin Pop
       random_key (def_value+1) == Just def_value
434 d5dfae0a Iustin Pop
    where _types = def_value :: Integer
435 a810ad21 Iustin Pop
436 bfe6c954 Guido Trotter
-- | Test that functional if' behaves like the syntactic sugar if.
437 72bb6b4e Iustin Pop
prop_Utils_if'if :: Bool -> Int -> Int -> Gen Prop
438 72bb6b4e Iustin Pop
prop_Utils_if'if cnd a b =
439 d5dfae0a Iustin Pop
  Utils.if' cnd a b ==? if cnd then a else b
440 bfe6c954 Guido Trotter
441 22fac87d Guido Trotter
-- | Test basic select functionality
442 72bb6b4e Iustin Pop
prop_Utils_select :: Int      -- ^ Default result
443 72bb6b4e Iustin Pop
                  -> [Int]    -- ^ List of False values
444 72bb6b4e Iustin Pop
                  -> [Int]    -- ^ List of True values
445 72bb6b4e Iustin Pop
                  -> Gen Prop -- ^ Test result
446 22fac87d Guido Trotter
prop_Utils_select def lst1 lst2 =
447 3603605a Iustin Pop
  Utils.select def (flist ++ tlist) ==? expectedresult
448 ba1260ba Iustin Pop
    where expectedresult = Utils.if' (null lst2) def (head lst2)
449 ba1260ba Iustin Pop
          flist = zip (repeat False) lst1
450 ba1260ba Iustin Pop
          tlist = zip (repeat True)  lst2
451 22fac87d Guido Trotter
452 22fac87d Guido Trotter
-- | Test basic select functionality with undefined default
453 72bb6b4e Iustin Pop
prop_Utils_select_undefd :: [Int]            -- ^ List of False values
454 22fac87d Guido Trotter
                         -> NonEmptyList Int -- ^ List of True values
455 72bb6b4e Iustin Pop
                         -> Gen Prop         -- ^ Test result
456 22fac87d Guido Trotter
prop_Utils_select_undefd lst1 (NonEmpty lst2) =
457 3603605a Iustin Pop
  Utils.select undefined (flist ++ tlist) ==? head lst2
458 ba1260ba Iustin Pop
    where flist = zip (repeat False) lst1
459 ba1260ba Iustin Pop
          tlist = zip (repeat True)  lst2
460 22fac87d Guido Trotter
461 22fac87d Guido Trotter
-- | Test basic select functionality with undefined list values
462 72bb6b4e Iustin Pop
prop_Utils_select_undefv :: [Int]            -- ^ List of False values
463 22fac87d Guido Trotter
                         -> NonEmptyList Int -- ^ List of True values
464 72bb6b4e Iustin Pop
                         -> Gen Prop         -- ^ Test result
465 22fac87d Guido Trotter
prop_Utils_select_undefv lst1 (NonEmpty lst2) =
466 72bb6b4e Iustin Pop
  Utils.select undefined cndlist ==? head lst2
467 ba1260ba Iustin Pop
    where flist = zip (repeat False) lst1
468 ba1260ba Iustin Pop
          tlist = zip (repeat True)  lst2
469 ba1260ba Iustin Pop
          cndlist = flist ++ tlist ++ [undefined]
470 bfe6c954 Guido Trotter
471 1cb92fac Iustin Pop
prop_Utils_parseUnit (NonNegative n) =
472 d5dfae0a Iustin Pop
  Utils.parseUnit (show n) == Types.Ok n &&
473 d5dfae0a Iustin Pop
  Utils.parseUnit (show n ++ "m") == Types.Ok n &&
474 d5dfae0a Iustin Pop
  (case Utils.parseUnit (show n ++ "M") of
475 d5dfae0a Iustin Pop
     Types.Ok m -> if n > 0
476 d5dfae0a Iustin Pop
                     then m < n  -- for positive values, X MB is < than X MiB
477 d5dfae0a Iustin Pop
                     else m == 0 -- but for 0, 0 MB == 0 MiB
478 d5dfae0a Iustin Pop
     Types.Bad _ -> False) &&
479 d5dfae0a Iustin Pop
  Utils.parseUnit (show n ++ "g") == Types.Ok (n*1024) &&
480 d5dfae0a Iustin Pop
  Utils.parseUnit (show n ++ "t") == Types.Ok (n*1048576) &&
481 d5dfae0a Iustin Pop
  Types.isBad (Utils.parseUnit (show n ++ "x")::Types.Result Int)
482 1b0a6356 Iustin Pop
    where _types = n::Int
483 1cb92fac Iustin Pop
484 525bfb36 Iustin Pop
-- | Test list for the Utils module.
485 23fe06c2 Iustin Pop
testSuite "Utils"
486 d5dfae0a Iustin Pop
            [ 'prop_Utils_commaJoinSplit
487 d5dfae0a Iustin Pop
            , 'prop_Utils_commaSplitJoin
488 d5dfae0a Iustin Pop
            , 'prop_Utils_fromObjWithDefault
489 d5dfae0a Iustin Pop
            , 'prop_Utils_if'if
490 d5dfae0a Iustin Pop
            , 'prop_Utils_select
491 d5dfae0a Iustin Pop
            , 'prop_Utils_select_undefd
492 d5dfae0a Iustin Pop
            , 'prop_Utils_select_undefv
493 d5dfae0a Iustin Pop
            , 'prop_Utils_parseUnit
494 d5dfae0a Iustin Pop
            ]
495 691dcd2a Iustin Pop
496 525bfb36 Iustin Pop
-- ** PeerMap tests
497 525bfb36 Iustin Pop
498 525bfb36 Iustin Pop
-- | Make sure add is idempotent.
499 fbb95f28 Iustin Pop
prop_PeerMap_addIdempotent pmap key em =
500 d5dfae0a Iustin Pop
  fn puniq ==? fn (fn puniq)
501 7bc82927 Iustin Pop
    where _types = (pmap::PeerMap.PeerMap,
502 fbb95f28 Iustin Pop
                    key::PeerMap.Key, em::PeerMap.Elem)
503 fbb95f28 Iustin Pop
          fn = PeerMap.add key em
504 7bc82927 Iustin Pop
          puniq = PeerMap.accumArray const pmap
505 15f4c8ca Iustin Pop
506 525bfb36 Iustin Pop
-- | Make sure remove is idempotent.
507 15f4c8ca Iustin Pop
prop_PeerMap_removeIdempotent pmap key =
508 d5dfae0a Iustin Pop
  fn puniq ==? fn (fn puniq)
509 7bc82927 Iustin Pop
    where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key)
510 7bc82927 Iustin Pop
          fn = PeerMap.remove key
511 15f4c8ca Iustin Pop
          puniq = PeerMap.accumArray const pmap
512 15f4c8ca Iustin Pop
513 525bfb36 Iustin Pop
-- | Make sure a missing item returns 0.
514 15f4c8ca Iustin Pop
prop_PeerMap_findMissing pmap key =
515 d5dfae0a Iustin Pop
  PeerMap.find key (PeerMap.remove key puniq) ==? 0
516 7bc82927 Iustin Pop
    where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key)
517 15f4c8ca Iustin Pop
          puniq = PeerMap.accumArray const pmap
518 15f4c8ca Iustin Pop
519 525bfb36 Iustin Pop
-- | Make sure an added item is found.
520 fbb95f28 Iustin Pop
prop_PeerMap_addFind pmap key em =
521 d5dfae0a Iustin Pop
  PeerMap.find key (PeerMap.add key em puniq) ==? em
522 7bc82927 Iustin Pop
    where _types = (pmap::PeerMap.PeerMap,
523 fbb95f28 Iustin Pop
                    key::PeerMap.Key, em::PeerMap.Elem)
524 7bc82927 Iustin Pop
          puniq = PeerMap.accumArray const pmap
525 15f4c8ca Iustin Pop
526 525bfb36 Iustin Pop
-- | Manual check that maxElem returns the maximum indeed, or 0 for null.
527 15f4c8ca Iustin Pop
prop_PeerMap_maxElem pmap =
528 d5dfae0a Iustin Pop
  PeerMap.maxElem puniq ==? if null puniq then 0
529 72bb6b4e Iustin Pop
                              else (maximum . snd . unzip) puniq
530 7bc82927 Iustin Pop
    where _types = pmap::PeerMap.PeerMap
531 15f4c8ca Iustin Pop
          puniq = PeerMap.accumArray const pmap
532 15f4c8ca Iustin Pop
533 525bfb36 Iustin Pop
-- | List of tests for the PeerMap module.
534 23fe06c2 Iustin Pop
testSuite "PeerMap"
535 d5dfae0a Iustin Pop
            [ 'prop_PeerMap_addIdempotent
536 d5dfae0a Iustin Pop
            , 'prop_PeerMap_removeIdempotent
537 d5dfae0a Iustin Pop
            , 'prop_PeerMap_maxElem
538 d5dfae0a Iustin Pop
            , 'prop_PeerMap_addFind
539 d5dfae0a Iustin Pop
            , 'prop_PeerMap_findMissing
540 d5dfae0a Iustin Pop
            ]
541 7dd5ee6c Iustin Pop
542 525bfb36 Iustin Pop
-- ** Container tests
543 095d7ac0 Iustin Pop
544 3603605a Iustin Pop
-- we silence the following due to hlint bug fixed in later versions
545 3603605a Iustin Pop
{-# ANN prop_Container_addTwo "HLint: ignore Avoid lambda" #-}
546 095d7ac0 Iustin Pop
prop_Container_addTwo cdata i1 i2 =
547 d5dfae0a Iustin Pop
  fn i1 i2 cont == fn i2 i1 cont &&
548 d5dfae0a Iustin Pop
  fn i1 i2 cont == fn i1 i2 (fn i1 i2 cont)
549 095d7ac0 Iustin Pop
    where _types = (cdata::[Int],
550 095d7ac0 Iustin Pop
                    i1::Int, i2::Int)
551 095d7ac0 Iustin Pop
          cont = foldl (\c x -> Container.add x x c) Container.empty cdata
552 095d7ac0 Iustin Pop
          fn x1 x2 = Container.addTwo x1 x1 x2 x2
553 095d7ac0 Iustin Pop
554 5ef78537 Iustin Pop
prop_Container_nameOf node =
555 5ef78537 Iustin Pop
  let nl = makeSmallCluster node 1
556 5ef78537 Iustin Pop
      fnode = head (Container.elems nl)
557 72bb6b4e Iustin Pop
  in Container.nameOf nl (Node.idx fnode) ==? Node.name fnode
558 5ef78537 Iustin Pop
559 525bfb36 Iustin Pop
-- | We test that in a cluster, given a random node, we can find it by
560 5ef78537 Iustin Pop
-- its name and alias, as long as all names and aliases are unique,
561 525bfb36 Iustin Pop
-- and that we fail to find a non-existing name.
562 5ef78537 Iustin Pop
prop_Container_findByName node othername =
563 5ef78537 Iustin Pop
  forAll (choose (1, 20)) $ \ cnt ->
564 5ef78537 Iustin Pop
  forAll (choose (0, cnt - 1)) $ \ fidx ->
565 5ef78537 Iustin Pop
  forAll (vector cnt) $ \ names ->
566 5ef78537 Iustin Pop
  (length . nub) (map fst names ++ map snd names) ==
567 5ef78537 Iustin Pop
  length names * 2 &&
568 3603605a Iustin Pop
  othername `notElem` (map fst names ++ map snd names) ==>
569 5ef78537 Iustin Pop
  let nl = makeSmallCluster node cnt
570 5ef78537 Iustin Pop
      nodes = Container.elems nl
571 5ef78537 Iustin Pop
      nodes' = map (\((name, alias), nn) -> (Node.idx nn,
572 5ef78537 Iustin Pop
                                             nn { Node.name = name,
573 5ef78537 Iustin Pop
                                                  Node.alias = alias }))
574 5ef78537 Iustin Pop
               $ zip names nodes
575 cb0c77ff Iustin Pop
      nl' = Container.fromList nodes'
576 5ef78537 Iustin Pop
      target = snd (nodes' !! fidx)
577 5ef78537 Iustin Pop
  in Container.findByName nl' (Node.name target) == Just target &&
578 5ef78537 Iustin Pop
     Container.findByName nl' (Node.alias target) == Just target &&
579 3603605a Iustin Pop
     isNothing (Container.findByName nl' othername)
580 5ef78537 Iustin Pop
581 23fe06c2 Iustin Pop
testSuite "Container"
582 d5dfae0a Iustin Pop
            [ 'prop_Container_addTwo
583 d5dfae0a Iustin Pop
            , 'prop_Container_nameOf
584 d5dfae0a Iustin Pop
            , 'prop_Container_findByName
585 d5dfae0a Iustin Pop
            ]
586 095d7ac0 Iustin Pop
587 525bfb36 Iustin Pop
-- ** Instance tests
588 525bfb36 Iustin Pop
589 7bc82927 Iustin Pop
-- Simple instance tests, we only have setter/getters
590 7bc82927 Iustin Pop
591 39d11971 Iustin Pop
prop_Instance_creat inst =
592 d5dfae0a Iustin Pop
  Instance.name inst ==? Instance.alias inst
593 39d11971 Iustin Pop
594 7bc82927 Iustin Pop
prop_Instance_setIdx inst idx =
595 d5dfae0a Iustin Pop
  Instance.idx (Instance.setIdx inst idx) ==? idx
596 7bc82927 Iustin Pop
    where _types = (inst::Instance.Instance, idx::Types.Idx)
597 7bc82927 Iustin Pop
598 7bc82927 Iustin Pop
prop_Instance_setName inst name =
599 d5dfae0a Iustin Pop
  Instance.name newinst == name &&
600 d5dfae0a Iustin Pop
  Instance.alias newinst == name
601 39d11971 Iustin Pop
    where _types = (inst::Instance.Instance, name::String)
602 39d11971 Iustin Pop
          newinst = Instance.setName inst name
603 39d11971 Iustin Pop
604 39d11971 Iustin Pop
prop_Instance_setAlias inst name =
605 d5dfae0a Iustin Pop
  Instance.name newinst == Instance.name inst &&
606 d5dfae0a Iustin Pop
  Instance.alias newinst == name
607 7bc82927 Iustin Pop
    where _types = (inst::Instance.Instance, name::String)
608 39d11971 Iustin Pop
          newinst = Instance.setAlias inst name
609 7bc82927 Iustin Pop
610 7bc82927 Iustin Pop
prop_Instance_setPri inst pdx =
611 d5dfae0a Iustin Pop
  Instance.pNode (Instance.setPri inst pdx) ==? pdx
612 7bc82927 Iustin Pop
    where _types = (inst::Instance.Instance, pdx::Types.Ndx)
613 7bc82927 Iustin Pop
614 7bc82927 Iustin Pop
prop_Instance_setSec inst sdx =
615 d5dfae0a Iustin Pop
  Instance.sNode (Instance.setSec inst sdx) ==? sdx
616 7bc82927 Iustin Pop
    where _types = (inst::Instance.Instance, sdx::Types.Ndx)
617 7bc82927 Iustin Pop
618 7bc82927 Iustin Pop
prop_Instance_setBoth inst pdx sdx =
619 d5dfae0a Iustin Pop
  Instance.pNode si == pdx && Instance.sNode si == sdx
620 7bc82927 Iustin Pop
    where _types = (inst::Instance.Instance, pdx::Types.Ndx, sdx::Types.Ndx)
621 7bc82927 Iustin Pop
          si = Instance.setBoth inst pdx sdx
622 7bc82927 Iustin Pop
623 8fcf251f Iustin Pop
prop_Instance_shrinkMG inst =
624 d5dfae0a Iustin Pop
  Instance.mem inst >= 2 * Types.unitMem ==>
625 d5dfae0a Iustin Pop
    case Instance.shrinkByType inst Types.FailMem of
626 d5dfae0a Iustin Pop
      Types.Ok inst' -> Instance.mem inst' == Instance.mem inst - Types.unitMem
627 d5dfae0a Iustin Pop
      _ -> False
628 8fcf251f Iustin Pop
629 8fcf251f Iustin Pop
prop_Instance_shrinkMF inst =
630 d5dfae0a Iustin Pop
  forAll (choose (0, 2 * Types.unitMem - 1)) $ \mem ->
631 41085bd3 Iustin Pop
    let inst' = inst { Instance.mem = mem}
632 41085bd3 Iustin Pop
    in Types.isBad $ Instance.shrinkByType inst' Types.FailMem
633 8fcf251f Iustin Pop
634 8fcf251f Iustin Pop
prop_Instance_shrinkCG inst =
635 d5dfae0a Iustin Pop
  Instance.vcpus inst >= 2 * Types.unitCpu ==>
636 d5dfae0a Iustin Pop
    case Instance.shrinkByType inst Types.FailCPU of
637 d5dfae0a Iustin Pop
      Types.Ok inst' ->
638 d5dfae0a Iustin Pop
        Instance.vcpus inst' == Instance.vcpus inst - Types.unitCpu
639 d5dfae0a Iustin Pop
      _ -> False
640 8fcf251f Iustin Pop
641 8fcf251f Iustin Pop
prop_Instance_shrinkCF inst =
642 d5dfae0a Iustin Pop
  forAll (choose (0, 2 * Types.unitCpu - 1)) $ \vcpus ->
643 41085bd3 Iustin Pop
    let inst' = inst { Instance.vcpus = vcpus }
644 41085bd3 Iustin Pop
    in Types.isBad $ Instance.shrinkByType inst' Types.FailCPU
645 8fcf251f Iustin Pop
646 8fcf251f Iustin Pop
prop_Instance_shrinkDG inst =
647 d5dfae0a Iustin Pop
  Instance.dsk inst >= 2 * Types.unitDsk ==>
648 d5dfae0a Iustin Pop
    case Instance.shrinkByType inst Types.FailDisk of
649 d5dfae0a Iustin Pop
      Types.Ok inst' ->
650 d5dfae0a Iustin Pop
        Instance.dsk inst' == Instance.dsk inst - Types.unitDsk
651 d5dfae0a Iustin Pop
      _ -> False
652 8fcf251f Iustin Pop
653 8fcf251f Iustin Pop
prop_Instance_shrinkDF inst =
654 d5dfae0a Iustin Pop
  forAll (choose (0, 2 * Types.unitDsk - 1)) $ \dsk ->
655 41085bd3 Iustin Pop
    let inst' = inst { Instance.dsk = dsk }
656 41085bd3 Iustin Pop
    in Types.isBad $ Instance.shrinkByType inst' Types.FailDisk
657 8fcf251f Iustin Pop
658 8fcf251f Iustin Pop
prop_Instance_setMovable inst m =
659 d5dfae0a Iustin Pop
  Instance.movable inst' ==? m
660 4a007641 Iustin Pop
    where inst' = Instance.setMovable inst m
661 8fcf251f Iustin Pop
662 23fe06c2 Iustin Pop
testSuite "Instance"
663 d5dfae0a Iustin Pop
            [ 'prop_Instance_creat
664 d5dfae0a Iustin Pop
            , 'prop_Instance_setIdx
665 d5dfae0a Iustin Pop
            , 'prop_Instance_setName
666 d5dfae0a Iustin Pop
            , 'prop_Instance_setAlias
667 d5dfae0a Iustin Pop
            , 'prop_Instance_setPri
668 d5dfae0a Iustin Pop
            , 'prop_Instance_setSec
669 d5dfae0a Iustin Pop
            , 'prop_Instance_setBoth
670 d5dfae0a Iustin Pop
            , 'prop_Instance_shrinkMG
671 d5dfae0a Iustin Pop
            , 'prop_Instance_shrinkMF
672 d5dfae0a Iustin Pop
            , 'prop_Instance_shrinkCG
673 d5dfae0a Iustin Pop
            , 'prop_Instance_shrinkCF
674 d5dfae0a Iustin Pop
            , 'prop_Instance_shrinkDG
675 d5dfae0a Iustin Pop
            , 'prop_Instance_shrinkDF
676 d5dfae0a Iustin Pop
            , 'prop_Instance_setMovable
677 d5dfae0a Iustin Pop
            ]
678 1ae7a904 Iustin Pop
679 525bfb36 Iustin Pop
-- ** Text backend tests
680 525bfb36 Iustin Pop
681 1ae7a904 Iustin Pop
-- Instance text loader tests
682 1ae7a904 Iustin Pop
683 a1cd7c1e Iustin Pop
prop_Text_Load_Instance name mem dsk vcpus status
684 a1cd7c1e Iustin Pop
                        (NonEmpty pnode) snode
685 6429e8d8 Iustin Pop
                        (NonNegative pdx) (NonNegative sdx) autobal dt =
686 d5dfae0a Iustin Pop
  pnode /= snode && pdx /= sdx ==>
687 d5dfae0a Iustin Pop
  let vcpus_s = show vcpus
688 d5dfae0a Iustin Pop
      dsk_s = show dsk
689 d5dfae0a Iustin Pop
      mem_s = show mem
690 d5dfae0a Iustin Pop
      status_s = Types.instanceStatusToRaw status
691 d5dfae0a Iustin Pop
      ndx = if null snode
692 39d11971 Iustin Pop
              then [(pnode, pdx)]
693 309e7c9a Iustin Pop
              else [(pnode, pdx), (snode, sdx)]
694 d5dfae0a Iustin Pop
      nl = Data.Map.fromList ndx
695 d5dfae0a Iustin Pop
      tags = ""
696 d5dfae0a Iustin Pop
      sbal = if autobal then "Y" else "N"
697 d5dfae0a Iustin Pop
      sdt = Types.diskTemplateToRaw dt
698 d5dfae0a Iustin Pop
      inst = Text.loadInst nl
699 d5dfae0a Iustin Pop
             [name, mem_s, dsk_s, vcpus_s, status_s,
700 d5dfae0a Iustin Pop
              sbal, pnode, snode, sdt, tags]
701 d5dfae0a Iustin Pop
      fail1 = Text.loadInst nl
702 d5dfae0a Iustin Pop
              [name, mem_s, dsk_s, vcpus_s, status_s,
703 d5dfae0a Iustin Pop
               sbal, pnode, pnode, tags]
704 d5dfae0a Iustin Pop
      _types = ( name::String, mem::Int, dsk::Int
705 d5dfae0a Iustin Pop
               , vcpus::Int, status::Types.InstanceStatus
706 d5dfae0a Iustin Pop
               , snode::String
707 d5dfae0a Iustin Pop
               , autobal::Bool)
708 d5dfae0a Iustin Pop
  in case inst of
709 d5dfae0a Iustin Pop
       Types.Bad msg -> printTestCase ("Failed to load instance: " ++ msg)
710 d5dfae0a Iustin Pop
                        False
711 d5dfae0a Iustin Pop
       Types.Ok (_, i) -> printTestCase "Mismatch in some field while\
712 d5dfae0a Iustin Pop
                                        \ loading the instance" $
713 d5dfae0a Iustin Pop
               Instance.name i == name &&
714 d5dfae0a Iustin Pop
               Instance.vcpus i == vcpus &&
715 d5dfae0a Iustin Pop
               Instance.mem i == mem &&
716 d5dfae0a Iustin Pop
               Instance.pNode i == pdx &&
717 d5dfae0a Iustin Pop
               Instance.sNode i == (if null snode
718 d5dfae0a Iustin Pop
                                      then Node.noSecondary
719 d5dfae0a Iustin Pop
                                      else sdx) &&
720 d5dfae0a Iustin Pop
               Instance.autoBalance i == autobal &&
721 d5dfae0a Iustin Pop
               Types.isBad fail1
722 39d11971 Iustin Pop
723 39d11971 Iustin Pop
prop_Text_Load_InstanceFail ktn fields =
724 d5dfae0a Iustin Pop
  length fields /= 10 ==>
725 bc782180 Iustin Pop
    case Text.loadInst nl fields of
726 6429e8d8 Iustin Pop
      Types.Ok _ -> printTestCase "Managed to load instance from invalid\
727 6429e8d8 Iustin Pop
                                  \ data" False
728 6429e8d8 Iustin Pop
      Types.Bad msg -> printTestCase ("Unrecognised error message: " ++ msg) $
729 6429e8d8 Iustin Pop
                       "Invalid/incomplete instance data: '" `isPrefixOf` msg
730 99b63608 Iustin Pop
    where nl = Data.Map.fromList ktn
731 39d11971 Iustin Pop
732 39d11971 Iustin Pop
prop_Text_Load_Node name tm nm fm td fd tc fo =
733 d5dfae0a Iustin Pop
  let conv v = if v < 0
734 d5dfae0a Iustin Pop
                 then "?"
735 d5dfae0a Iustin Pop
                 else show v
736 d5dfae0a Iustin Pop
      tm_s = conv tm
737 d5dfae0a Iustin Pop
      nm_s = conv nm
738 d5dfae0a Iustin Pop
      fm_s = conv fm
739 d5dfae0a Iustin Pop
      td_s = conv td
740 d5dfae0a Iustin Pop
      fd_s = conv fd
741 d5dfae0a Iustin Pop
      tc_s = conv tc
742 d5dfae0a Iustin Pop
      fo_s = if fo
743 39d11971 Iustin Pop
               then "Y"
744 39d11971 Iustin Pop
               else "N"
745 d5dfae0a Iustin Pop
      any_broken = any (< 0) [tm, nm, fm, td, fd, tc]
746 d5dfae0a Iustin Pop
      gid = Group.uuid defGroup
747 d5dfae0a Iustin Pop
  in case Text.loadNode defGroupAssoc
748 d5dfae0a Iustin Pop
       [name, tm_s, nm_s, fm_s, td_s, fd_s, tc_s, fo_s, gid] of
749 d5dfae0a Iustin Pop
       Nothing -> False
750 d5dfae0a Iustin Pop
       Just (name', node) ->
751 d5dfae0a Iustin Pop
         if fo || any_broken
752 d5dfae0a Iustin Pop
           then Node.offline node
753 d5dfae0a Iustin Pop
           else Node.name node == name' && name' == name &&
754 d5dfae0a Iustin Pop
                Node.alias node == name &&
755 d5dfae0a Iustin Pop
                Node.tMem node == fromIntegral tm &&
756 d5dfae0a Iustin Pop
                Node.nMem node == nm &&
757 d5dfae0a Iustin Pop
                Node.fMem node == fm &&
758 d5dfae0a Iustin Pop
                Node.tDsk node == fromIntegral td &&
759 d5dfae0a Iustin Pop
                Node.fDsk node == fd &&
760 d5dfae0a Iustin Pop
                Node.tCpu node == fromIntegral tc
761 39d11971 Iustin Pop
762 39d11971 Iustin Pop
prop_Text_Load_NodeFail fields =
763 d5dfae0a Iustin Pop
  length fields /= 8 ==> isNothing $ Text.loadNode Data.Map.empty fields
764 1ae7a904 Iustin Pop
765 50811e2c Iustin Pop
prop_Text_NodeLSIdempotent node =
766 d5dfae0a Iustin Pop
  (Text.loadNode defGroupAssoc.
767 d5dfae0a Iustin Pop
       Utils.sepSplit '|' . Text.serializeNode defGroupList) n ==
768 d5dfae0a Iustin Pop
  Just (Node.name n, n)
769 50811e2c Iustin Pop
    -- override failN1 to what loadNode returns by default
770 d6eec019 Iustin Pop
    where n = node { Node.failN1 = True, Node.offline = False
771 d6eec019 Iustin Pop
                   , Node.iPolicy = Types.defIPolicy }
772 50811e2c Iustin Pop
773 dce9bbb3 Iustin Pop
-- | This property, while being in the text tests, does more than just
774 dce9bbb3 Iustin Pop
-- test end-to-end the serialisation and loading back workflow; it
775 dce9bbb3 Iustin Pop
-- also tests the Loader.mergeData and the actuall
776 dce9bbb3 Iustin Pop
-- Cluster.iterateAlloc (for well-behaving w.r.t. instance
777 dce9bbb3 Iustin Pop
-- allocations, not for the business logic). As such, it's a quite
778 dce9bbb3 Iustin Pop
-- complex and slow test, and that's the reason we restrict it to
779 dce9bbb3 Iustin Pop
-- small cluster sizes.
780 dce9bbb3 Iustin Pop
prop_Text_CreateSerialise =
781 dce9bbb3 Iustin Pop
  forAll genTags $ \ctags ->
782 dce9bbb3 Iustin Pop
  forAll (choose (1, 2)) $ \reqnodes ->
783 dce9bbb3 Iustin Pop
  forAll (choose (1, 20)) $ \maxiter ->
784 dce9bbb3 Iustin Pop
  forAll (choose (2, 10)) $ \count ->
785 dce9bbb3 Iustin Pop
  forAll genOnlineNode $ \node ->
786 dce9bbb3 Iustin Pop
  forAll (arbitrary `suchThat` isInstanceSmallerThanNode node) $ \inst ->
787 dce9bbb3 Iustin Pop
  let inst' = Instance.setMovable inst $ Utils.if' (reqnodes == 2) True False
788 dce9bbb3 Iustin Pop
      nl = makeSmallCluster node count
789 dce9bbb3 Iustin Pop
  in case Cluster.genAllocNodes defGroupList nl reqnodes True >>= \allocn ->
790 dce9bbb3 Iustin Pop
     Cluster.iterateAlloc nl Container.empty (Just maxiter) inst' allocn [] []
791 dce9bbb3 Iustin Pop
     of
792 dce9bbb3 Iustin Pop
       Types.Bad msg -> printTestCase ("Failed to allocate: " ++ msg) False
793 dce9bbb3 Iustin Pop
       Types.Ok (_, _, _, [], _) -> printTestCase
794 dce9bbb3 Iustin Pop
                                    "Failed to allocate: no allocations" False
795 dce9bbb3 Iustin Pop
       Types.Ok (_, nl', il', _, _) ->
796 dce9bbb3 Iustin Pop
         let cdata = Loader.ClusterData defGroupList nl' il' ctags
797 dce9bbb3 Iustin Pop
                     Types.defIPolicy
798 dce9bbb3 Iustin Pop
             saved = Text.serializeCluster cdata
799 dce9bbb3 Iustin Pop
         in case Text.parseData saved >>= Loader.mergeData [] [] [] [] of
800 dce9bbb3 Iustin Pop
              Types.Bad msg -> printTestCase ("Failed to load/merge: " ++
801 dce9bbb3 Iustin Pop
                                              msg) False
802 dce9bbb3 Iustin Pop
              Types.Ok (Loader.ClusterData gl2 nl2 il2 ctags2 cpol2) ->
803 dce9bbb3 Iustin Pop
                ctags ==? ctags2 .&&.
804 dce9bbb3 Iustin Pop
                Types.defIPolicy ==? cpol2 .&&.
805 dce9bbb3 Iustin Pop
                il' ==? il2 .&&.
806 b37f4a76 Iustin Pop
                defGroupList ==? gl2 .&&.
807 b37f4a76 Iustin Pop
                nl' ==? nl2
808 dce9bbb3 Iustin Pop
809 23fe06c2 Iustin Pop
testSuite "Text"
810 d5dfae0a Iustin Pop
            [ 'prop_Text_Load_Instance
811 d5dfae0a Iustin Pop
            , 'prop_Text_Load_InstanceFail
812 d5dfae0a Iustin Pop
            , 'prop_Text_Load_Node
813 d5dfae0a Iustin Pop
            , 'prop_Text_Load_NodeFail
814 d5dfae0a Iustin Pop
            , 'prop_Text_NodeLSIdempotent
815 dce9bbb3 Iustin Pop
            , 'prop_Text_CreateSerialise
816 d5dfae0a Iustin Pop
            ]
817 7dd5ee6c Iustin Pop
818 525bfb36 Iustin Pop
-- ** Node tests
819 7dd5ee6c Iustin Pop
820 82ea2874 Iustin Pop
prop_Node_setAlias node name =
821 d5dfae0a Iustin Pop
  Node.name newnode == Node.name node &&
822 d5dfae0a Iustin Pop
  Node.alias newnode == name
823 82ea2874 Iustin Pop
    where _types = (node::Node.Node, name::String)
824 82ea2874 Iustin Pop
          newnode = Node.setAlias node name
825 82ea2874 Iustin Pop
826 82ea2874 Iustin Pop
prop_Node_setOffline node status =
827 d5dfae0a Iustin Pop
  Node.offline newnode ==? status
828 82ea2874 Iustin Pop
    where newnode = Node.setOffline node status
829 82ea2874 Iustin Pop
830 82ea2874 Iustin Pop
prop_Node_setXmem node xm =
831 d5dfae0a Iustin Pop
  Node.xMem newnode ==? xm
832 82ea2874 Iustin Pop
    where newnode = Node.setXmem node xm
833 82ea2874 Iustin Pop
834 82ea2874 Iustin Pop
prop_Node_setMcpu node mc =
835 d5dfae0a Iustin Pop
  Node.mCpu newnode ==? mc
836 82ea2874 Iustin Pop
    where newnode = Node.setMcpu node mc
837 82ea2874 Iustin Pop
838 525bfb36 Iustin Pop
-- | Check that an instance add with too high memory or disk will be
839 525bfb36 Iustin Pop
-- rejected.
840 d5dfae0a Iustin Pop
prop_Node_addPriFM node inst =
841 d5dfae0a Iustin Pop
  Instance.mem inst >= Node.fMem node && not (Node.failN1 node) &&
842 d5dfae0a Iustin Pop
  not (Instance.instanceOffline inst) ==>
843 d5dfae0a Iustin Pop
  case Node.addPri node inst'' of
844 d5dfae0a Iustin Pop
    Types.OpFail Types.FailMem -> True
845 d5dfae0a Iustin Pop
    _ -> False
846 d5dfae0a Iustin Pop
  where _types = (node::Node.Node, inst::Instance.Instance)
847 d5dfae0a Iustin Pop
        inst' = setInstanceSmallerThanNode node inst
848 d5dfae0a Iustin Pop
        inst'' = inst' { Instance.mem = Instance.mem inst }
849 d5dfae0a Iustin Pop
850 d5dfae0a Iustin Pop
prop_Node_addPriFD node inst =
851 d5dfae0a Iustin Pop
  Instance.dsk inst >= Node.fDsk node && not (Node.failN1 node) ==>
852 d5dfae0a Iustin Pop
    case Node.addPri node inst'' of
853 d5dfae0a Iustin Pop
      Types.OpFail Types.FailDisk -> True
854 d5dfae0a Iustin Pop
      _ -> False
855 8fcf251f Iustin Pop
    where _types = (node::Node.Node, inst::Instance.Instance)
856 8fcf251f Iustin Pop
          inst' = setInstanceSmallerThanNode node inst
857 8fcf251f Iustin Pop
          inst'' = inst' { Instance.dsk = Instance.dsk inst }
858 8fcf251f Iustin Pop
859 41085bd3 Iustin Pop
prop_Node_addPriFC node inst (Positive extra) =
860 d5dfae0a Iustin Pop
  not (Node.failN1 node) && not (Instance.instanceOffline inst) ==>
861 d5dfae0a Iustin Pop
      case Node.addPri node inst'' of
862 d5dfae0a Iustin Pop
        Types.OpFail Types.FailCPU -> True
863 d5dfae0a Iustin Pop
        _ -> False
864 8fcf251f Iustin Pop
    where _types = (node::Node.Node, inst::Instance.Instance)
865 8fcf251f Iustin Pop
          inst' = setInstanceSmallerThanNode node inst
866 41085bd3 Iustin Pop
          inst'' = inst' { Instance.vcpus = Node.availCpu node + extra }
867 7bc82927 Iustin Pop
868 525bfb36 Iustin Pop
-- | Check that an instance add with too high memory or disk will be
869 525bfb36 Iustin Pop
-- rejected.
870 15f4c8ca Iustin Pop
prop_Node_addSec node inst pdx =
871 d5dfae0a Iustin Pop
  ((Instance.mem inst >= (Node.fMem node - Node.rMem node) &&
872 d5dfae0a Iustin Pop
    not (Instance.instanceOffline inst)) ||
873 d5dfae0a Iustin Pop
   Instance.dsk inst >= Node.fDsk node) &&
874 d5dfae0a Iustin Pop
  not (Node.failN1 node) ==>
875 d5dfae0a Iustin Pop
      isFailure (Node.addSec node inst pdx)
876 15f4c8ca Iustin Pop
        where _types = (node::Node.Node, inst::Instance.Instance, pdx::Int)
877 7dd5ee6c Iustin Pop
878 61bbbed7 Agata Murawska
-- | Check that an offline instance with reasonable disk size can always
879 61bbbed7 Agata Murawska
-- be added.
880 b99d1638 Iustin Pop
prop_Node_addPriOffline =
881 b99d1638 Iustin Pop
  forAll (arbitrary `suchThat` ((> 0) . Node.fMem)) $ \node ->
882 d5dfae0a Iustin Pop
  forAll (arbitrary `suchThat`
883 d5dfae0a Iustin Pop
          (\ x ->  (Instance.dsk x  < Node.fDsk node) &&
884 d5dfae0a Iustin Pop
                   Instance.instanceOffline x)) $ \inst ->
885 d5dfae0a Iustin Pop
  case Node.addPri node inst of
886 d5dfae0a Iustin Pop
    Types.OpGood _ -> True
887 d5dfae0a Iustin Pop
    _ -> False
888 61bbbed7 Agata Murawska
889 b99d1638 Iustin Pop
prop_Node_addSecOffline pdx =
890 b99d1638 Iustin Pop
  forAll (arbitrary `suchThat` ((> 0) . Node.fMem)) $ \node ->
891 d5dfae0a Iustin Pop
  forAll (arbitrary `suchThat`
892 d5dfae0a Iustin Pop
          (\ x ->  (Instance.dsk x  < Node.fDsk node) &&
893 d5dfae0a Iustin Pop
                   Instance.instanceOffline x)) $ \inst ->
894 d5dfae0a Iustin Pop
  case Node.addSec node inst pdx of
895 d5dfae0a Iustin Pop
    Types.OpGood _ -> True
896 d5dfae0a Iustin Pop
    _ -> False
897 61bbbed7 Agata Murawska
898 525bfb36 Iustin Pop
-- | Checks for memory reservation changes.
899 752635d3 Iustin Pop
prop_Node_rMem inst =
900 d5dfae0a Iustin Pop
  not (Instance.instanceOffline inst) ==>
901 d5dfae0a Iustin Pop
  forAll (arbitrary `suchThat` ((> Types.unitMem) . Node.fMem)) $ \node ->
902 d5dfae0a Iustin Pop
  -- ab = auto_balance, nb = non-auto_balance
903 d5dfae0a Iustin Pop
  -- we use -1 as the primary node of the instance
904 d5dfae0a Iustin Pop
  let inst' = inst { Instance.pNode = -1, Instance.autoBalance = True }
905 d5dfae0a Iustin Pop
      inst_ab = setInstanceSmallerThanNode node inst'
906 d5dfae0a Iustin Pop
      inst_nb = inst_ab { Instance.autoBalance = False }
907 d5dfae0a Iustin Pop
      -- now we have the two instances, identical except the
908 d5dfae0a Iustin Pop
      -- autoBalance attribute
909 d5dfae0a Iustin Pop
      orig_rmem = Node.rMem node
910 d5dfae0a Iustin Pop
      inst_idx = Instance.idx inst_ab
911 d5dfae0a Iustin Pop
      node_add_ab = Node.addSec node inst_ab (-1)
912 d5dfae0a Iustin Pop
      node_add_nb = Node.addSec node inst_nb (-1)
913 d5dfae0a Iustin Pop
      node_del_ab = liftM (`Node.removeSec` inst_ab) node_add_ab
914 d5dfae0a Iustin Pop
      node_del_nb = liftM (`Node.removeSec` inst_nb) node_add_nb
915 d5dfae0a Iustin Pop
  in case (node_add_ab, node_add_nb, node_del_ab, node_del_nb) of
916 d5dfae0a Iustin Pop
       (Types.OpGood a_ab, Types.OpGood a_nb,
917 d5dfae0a Iustin Pop
        Types.OpGood d_ab, Types.OpGood d_nb) ->
918 d5dfae0a Iustin Pop
         printTestCase "Consistency checks failed" $
919 d5dfae0a Iustin Pop
           Node.rMem a_ab >  orig_rmem &&
920 d5dfae0a Iustin Pop
           Node.rMem a_ab - orig_rmem == Instance.mem inst_ab &&
921 d5dfae0a Iustin Pop
           Node.rMem a_nb == orig_rmem &&
922 d5dfae0a Iustin Pop
           Node.rMem d_ab == orig_rmem &&
923 d5dfae0a Iustin Pop
           Node.rMem d_nb == orig_rmem &&
924 d5dfae0a Iustin Pop
           -- this is not related to rMem, but as good a place to
925 d5dfae0a Iustin Pop
           -- test as any
926 d5dfae0a Iustin Pop
           inst_idx `elem` Node.sList a_ab &&
927 3603605a Iustin Pop
           inst_idx `notElem` Node.sList d_ab
928 d5dfae0a Iustin Pop
       x -> printTestCase ("Failed to add/remove instances: " ++ show x) False
929 9cbc1edb Iustin Pop
930 525bfb36 Iustin Pop
-- | Check mdsk setting.
931 8fcf251f Iustin Pop
prop_Node_setMdsk node mx =
932 d5dfae0a Iustin Pop
  Node.loDsk node' >= 0 &&
933 d5dfae0a Iustin Pop
  fromIntegral (Node.loDsk node') <= Node.tDsk node &&
934 d5dfae0a Iustin Pop
  Node.availDisk node' >= 0 &&
935 d5dfae0a Iustin Pop
  Node.availDisk node' <= Node.fDsk node' &&
936 d5dfae0a Iustin Pop
  fromIntegral (Node.availDisk node') <= Node.tDsk node' &&
937 d5dfae0a Iustin Pop
  Node.mDsk node' == mx'
938 8fcf251f Iustin Pop
    where _types = (node::Node.Node, mx::SmallRatio)
939 8fcf251f Iustin Pop
          node' = Node.setMdsk node mx'
940 8fcf251f Iustin Pop
          SmallRatio mx' = mx
941 8fcf251f Iustin Pop
942 8fcf251f Iustin Pop
-- Check tag maps
943 8fcf251f Iustin Pop
prop_Node_tagMaps_idempotent tags =
944 d5dfae0a Iustin Pop
  Node.delTags (Node.addTags m tags) tags ==? m
945 4a007641 Iustin Pop
    where m = Data.Map.empty
946 8fcf251f Iustin Pop
947 8fcf251f Iustin Pop
prop_Node_tagMaps_reject tags =
948 d5dfae0a Iustin Pop
  not (null tags) ==>
949 d5dfae0a Iustin Pop
  all (\t -> Node.rejectAddTags m [t]) tags
950 4a007641 Iustin Pop
    where m = Node.addTags Data.Map.empty tags
951 8fcf251f Iustin Pop
952 82ea2874 Iustin Pop
prop_Node_showField node =
953 82ea2874 Iustin Pop
  forAll (elements Node.defaultFields) $ \ field ->
954 82ea2874 Iustin Pop
  fst (Node.showHeader field) /= Types.unknownField &&
955 82ea2874 Iustin Pop
  Node.showField node field /= Types.unknownField
956 82ea2874 Iustin Pop
957 d8bcd0a8 Iustin Pop
prop_Node_computeGroups nodes =
958 d8bcd0a8 Iustin Pop
  let ng = Node.computeGroups nodes
959 d8bcd0a8 Iustin Pop
      onlyuuid = map fst ng
960 d8bcd0a8 Iustin Pop
  in length nodes == sum (map (length . snd) ng) &&
961 d8bcd0a8 Iustin Pop
     all (\(guuid, ns) -> all ((== guuid) . Node.group) ns) ng &&
962 d8bcd0a8 Iustin Pop
     length (nub onlyuuid) == length onlyuuid &&
963 cc532bdd Iustin Pop
     (null nodes || not (null ng))
964 d8bcd0a8 Iustin Pop
965 23fe06c2 Iustin Pop
testSuite "Node"
966 d5dfae0a Iustin Pop
            [ 'prop_Node_setAlias
967 d5dfae0a Iustin Pop
            , 'prop_Node_setOffline
968 d5dfae0a Iustin Pop
            , 'prop_Node_setMcpu
969 d5dfae0a Iustin Pop
            , 'prop_Node_setXmem
970 d5dfae0a Iustin Pop
            , 'prop_Node_addPriFM
971 d5dfae0a Iustin Pop
            , 'prop_Node_addPriFD
972 d5dfae0a Iustin Pop
            , 'prop_Node_addPriFC
973 d5dfae0a Iustin Pop
            , 'prop_Node_addSec
974 d5dfae0a Iustin Pop
            , 'prop_Node_addPriOffline
975 d5dfae0a Iustin Pop
            , 'prop_Node_addSecOffline
976 d5dfae0a Iustin Pop
            , 'prop_Node_rMem
977 d5dfae0a Iustin Pop
            , 'prop_Node_setMdsk
978 d5dfae0a Iustin Pop
            , 'prop_Node_tagMaps_idempotent
979 d5dfae0a Iustin Pop
            , 'prop_Node_tagMaps_reject
980 d5dfae0a Iustin Pop
            , 'prop_Node_showField
981 d5dfae0a Iustin Pop
            , 'prop_Node_computeGroups
982 d5dfae0a Iustin Pop
            ]
983 cf35a869 Iustin Pop
984 525bfb36 Iustin Pop
-- ** Cluster tests
985 cf35a869 Iustin Pop
986 525bfb36 Iustin Pop
-- | Check that the cluster score is close to zero for a homogeneous
987 525bfb36 Iustin Pop
-- cluster.
988 8e4f6d56 Iustin Pop
prop_Score_Zero node =
989 d5dfae0a Iustin Pop
  forAll (choose (1, 1024)) $ \count ->
990 3a3c1eb4 Iustin Pop
    (not (Node.offline node) && not (Node.failN1 node) && (count > 0) &&
991 2060348b Iustin Pop
     (Node.tDsk node > 0) && (Node.tMem node > 0)) ==>
992 d5dfae0a Iustin Pop
  let fn = Node.buildPeers node Container.empty
993 d5dfae0a Iustin Pop
      nlst = replicate count fn
994 d5dfae0a Iustin Pop
      score = Cluster.compCVNodes nlst
995 d5dfae0a Iustin Pop
  -- we can't say == 0 here as the floating point errors accumulate;
996 d5dfae0a Iustin Pop
  -- this should be much lower than the default score in CLI.hs
997 d5dfae0a Iustin Pop
  in score <= 1e-12
998 cf35a869 Iustin Pop
999 525bfb36 Iustin Pop
-- | Check that cluster stats are sane.
1000 d6f9f5bd Iustin Pop
prop_CStats_sane =
1001 d5dfae0a Iustin Pop
  forAll (choose (1, 1024)) $ \count ->
1002 d6f9f5bd Iustin Pop
  forAll genOnlineNode $ \node ->
1003 d5dfae0a Iustin Pop
  let fn = Node.buildPeers node Container.empty
1004 d5dfae0a Iustin Pop
      nlst = zip [1..] $ replicate count fn::[(Types.Ndx, Node.Node)]
1005 d5dfae0a Iustin Pop
      nl = Container.fromList nlst
1006 d5dfae0a Iustin Pop
      cstats = Cluster.totalResources nl
1007 d5dfae0a Iustin Pop
  in Cluster.csAdsk cstats >= 0 &&
1008 d5dfae0a Iustin Pop
     Cluster.csAdsk cstats <= Cluster.csFdsk cstats
1009 8fcf251f Iustin Pop
1010 3fea6959 Iustin Pop
-- | Check that one instance is allocated correctly, without
1011 525bfb36 Iustin Pop
-- rebalances needed.
1012 d6f9f5bd Iustin Pop
prop_ClusterAlloc_sane inst =
1013 d5dfae0a Iustin Pop
  forAll (choose (5, 20)) $ \count ->
1014 d6f9f5bd Iustin Pop
  forAll genOnlineNode $ \node ->
1015 3603605a Iustin Pop
  let (nl, il, inst') = makeSmallEmptyCluster node count inst
1016 d5dfae0a Iustin Pop
  in case Cluster.genAllocNodes defGroupList nl 2 True >>=
1017 d5dfae0a Iustin Pop
     Cluster.tryAlloc nl il inst' of
1018 d5dfae0a Iustin Pop
       Types.Bad _ -> False
1019 d5dfae0a Iustin Pop
       Types.Ok as ->
1020 d5dfae0a Iustin Pop
         case Cluster.asSolution as of
1021 d5dfae0a Iustin Pop
           Nothing -> False
1022 d5dfae0a Iustin Pop
           Just (xnl, xi, _, cv) ->
1023 d5dfae0a Iustin Pop
             let il' = Container.add (Instance.idx xi) xi il
1024 d5dfae0a Iustin Pop
                 tbl = Cluster.Table xnl il' cv []
1025 d5dfae0a Iustin Pop
             in not (canBalance tbl True True False)
1026 3fea6959 Iustin Pop
1027 3fea6959 Iustin Pop
-- | Checks that on a 2-5 node cluster, we can allocate a random
1028 3fea6959 Iustin Pop
-- instance spec via tiered allocation (whatever the original instance
1029 525bfb36 Iustin Pop
-- spec), on either one or two nodes.
1030 d6f9f5bd Iustin Pop
prop_ClusterCanTieredAlloc inst =
1031 d5dfae0a Iustin Pop
  forAll (choose (2, 5)) $ \count ->
1032 d5dfae0a Iustin Pop
  forAll (choose (1, 2)) $ \rqnodes ->
1033 d6f9f5bd Iustin Pop
  forAll (genOnlineNode `suchThat` (isNodeBig 4)) $ \node ->
1034 d5dfae0a Iustin Pop
  let nl = makeSmallCluster node count
1035 d5dfae0a Iustin Pop
      il = Container.empty
1036 d5dfae0a Iustin Pop
      allocnodes = Cluster.genAllocNodes defGroupList nl rqnodes True
1037 d5dfae0a Iustin Pop
  in case allocnodes >>= \allocnodes' ->
1038 d5dfae0a Iustin Pop
    Cluster.tieredAlloc nl il (Just 1) inst allocnodes' [] [] of
1039 d5dfae0a Iustin Pop
       Types.Bad _ -> False
1040 d5dfae0a Iustin Pop
       Types.Ok (_, _, il', ixes, cstats) -> not (null ixes) &&
1041 d5dfae0a Iustin Pop
                                             IntMap.size il' == length ixes &&
1042 d5dfae0a Iustin Pop
                                             length ixes == length cstats
1043 3fea6959 Iustin Pop
1044 3fea6959 Iustin Pop
-- | Checks that on a 4-8 node cluster, once we allocate an instance,
1045 525bfb36 Iustin Pop
-- we can also evacuate it.
1046 d6f9f5bd Iustin Pop
prop_ClusterAllocEvac inst =
1047 d5dfae0a Iustin Pop
  forAll (choose (4, 8)) $ \count ->
1048 d6f9f5bd Iustin Pop
  forAll (genOnlineNode `suchThat` (isNodeBig 4)) $ \node ->
1049 3603605a Iustin Pop
  let (nl, il, inst') = makeSmallEmptyCluster node count inst
1050 d5dfae0a Iustin Pop
  in case Cluster.genAllocNodes defGroupList nl 2 True >>=
1051 d5dfae0a Iustin Pop
     Cluster.tryAlloc nl il inst' of
1052 d5dfae0a Iustin Pop
       Types.Bad _ -> False
1053 d5dfae0a Iustin Pop
       Types.Ok as ->
1054 d5dfae0a Iustin Pop
         case Cluster.asSolution as of
1055 d5dfae0a Iustin Pop
           Nothing -> False
1056 d5dfae0a Iustin Pop
           Just (xnl, xi, _, _) ->
1057 d5dfae0a Iustin Pop
             let sdx = Instance.sNode xi
1058 d5dfae0a Iustin Pop
                 il' = Container.add (Instance.idx xi) xi il
1059 d5dfae0a Iustin Pop
             in case IAlloc.processRelocate defGroupList xnl il'
1060 d5dfae0a Iustin Pop
                  (Instance.idx xi) 1 [sdx] of
1061 d5dfae0a Iustin Pop
                  Types.Ok _ -> True
1062 d5dfae0a Iustin Pop
                  _ -> False
1063 3fea6959 Iustin Pop
1064 3fea6959 Iustin Pop
-- | Check that allocating multiple instances on a cluster, then
1065 525bfb36 Iustin Pop
-- adding an empty node, results in a valid rebalance.
1066 00c75986 Iustin Pop
prop_ClusterAllocBalance =
1067 d5dfae0a Iustin Pop
  forAll (genNode (Just 5) (Just 128)) $ \node ->
1068 d5dfae0a Iustin Pop
  forAll (choose (3, 5)) $ \count ->
1069 d5dfae0a Iustin Pop
  not (Node.offline node) && not (Node.failN1 node) ==>
1070 d5dfae0a Iustin Pop
  let nl = makeSmallCluster node count
1071 d5dfae0a Iustin Pop
      (hnode, nl') = IntMap.deleteFindMax nl
1072 d5dfae0a Iustin Pop
      il = Container.empty
1073 d5dfae0a Iustin Pop
      allocnodes = Cluster.genAllocNodes defGroupList nl' 2 True
1074 d5dfae0a Iustin Pop
      i_templ = createInstance Types.unitMem Types.unitDsk Types.unitCpu
1075 d5dfae0a Iustin Pop
  in case allocnodes >>= \allocnodes' ->
1076 d5dfae0a Iustin Pop
    Cluster.iterateAlloc nl' il (Just 5) i_templ allocnodes' [] [] of
1077 6cff91f5 Iustin Pop
       Types.Bad _ -> printTestCase "Failed to allocate" False
1078 6cff91f5 Iustin Pop
       Types.Ok (_, _, _, [], _) -> printTestCase "Failed to allocate" False
1079 d5dfae0a Iustin Pop
       Types.Ok (_, xnl, il', _, _) ->
1080 d5dfae0a Iustin Pop
         let ynl = Container.add (Node.idx hnode) hnode xnl
1081 d5dfae0a Iustin Pop
             cv = Cluster.compCV ynl
1082 d5dfae0a Iustin Pop
             tbl = Cluster.Table ynl il' cv []
1083 6cff91f5 Iustin Pop
         in printTestCase "Failed to rebalance" $
1084 6cff91f5 Iustin Pop
            canBalance tbl True True False
1085 3fea6959 Iustin Pop
1086 525bfb36 Iustin Pop
-- | Checks consistency.
1087 32b8d9c0 Iustin Pop
prop_ClusterCheckConsistency node inst =
1088 32b8d9c0 Iustin Pop
  let nl = makeSmallCluster node 3
1089 32b8d9c0 Iustin Pop
      [node1, node2, node3] = Container.elems nl
1090 10ef6b4e Iustin Pop
      node3' = node3 { Node.group = 1 }
1091 32b8d9c0 Iustin Pop
      nl' = Container.add (Node.idx node3') node3' nl
1092 32b8d9c0 Iustin Pop
      inst1 = Instance.setBoth inst (Node.idx node1) (Node.idx node2)
1093 32b8d9c0 Iustin Pop
      inst2 = Instance.setBoth inst (Node.idx node1) Node.noSecondary
1094 32b8d9c0 Iustin Pop
      inst3 = Instance.setBoth inst (Node.idx node1) (Node.idx node3)
1095 cb0c77ff Iustin Pop
      ccheck = Cluster.findSplitInstances nl' . Container.fromList
1096 32b8d9c0 Iustin Pop
  in null (ccheck [(0, inst1)]) &&
1097 32b8d9c0 Iustin Pop
     null (ccheck [(0, inst2)]) &&
1098 32b8d9c0 Iustin Pop
     (not . null $ ccheck [(0, inst3)])
1099 32b8d9c0 Iustin Pop
1100 525bfb36 Iustin Pop
-- | For now, we only test that we don't lose instances during the split.
1101 f4161783 Iustin Pop
prop_ClusterSplitCluster node inst =
1102 f4161783 Iustin Pop
  forAll (choose (0, 100)) $ \icnt ->
1103 f4161783 Iustin Pop
  let nl = makeSmallCluster node 2
1104 f4161783 Iustin Pop
      (nl', il') = foldl (\(ns, is) _ -> assignInstance ns is inst 0 1)
1105 f4161783 Iustin Pop
                   (nl, Container.empty) [1..icnt]
1106 f4161783 Iustin Pop
      gni = Cluster.splitCluster nl' il'
1107 f4161783 Iustin Pop
  in sum (map (Container.size . snd . snd) gni) == icnt &&
1108 f4161783 Iustin Pop
     all (\(guuid, (nl'', _)) -> all ((== guuid) . Node.group)
1109 f4161783 Iustin Pop
                                 (Container.elems nl'')) gni
1110 32b8d9c0 Iustin Pop
1111 00b70680 Iustin Pop
-- | Helper function to check if we can allocate an instance on a
1112 00b70680 Iustin Pop
-- given node list.
1113 00b70680 Iustin Pop
canAllocOn :: Node.List -> Int -> Instance.Instance -> Bool
1114 00b70680 Iustin Pop
canAllocOn nl reqnodes inst =
1115 00b70680 Iustin Pop
  case Cluster.genAllocNodes defGroupList nl reqnodes True >>=
1116 00b70680 Iustin Pop
       Cluster.tryAlloc nl (Container.empty) inst of
1117 00b70680 Iustin Pop
       Types.Bad _ -> False
1118 00b70680 Iustin Pop
       Types.Ok as ->
1119 00b70680 Iustin Pop
         case Cluster.asSolution as of
1120 00b70680 Iustin Pop
           Nothing -> False
1121 00b70680 Iustin Pop
           Just _ -> True
1122 00b70680 Iustin Pop
1123 00b70680 Iustin Pop
-- | Checks that allocation obeys minimum and maximum instance
1124 00b70680 Iustin Pop
-- policies. The unittest generates a random node, duplicates it count
1125 00b70680 Iustin Pop
-- times, and generates a random instance that can be allocated on
1126 00b70680 Iustin Pop
-- this mini-cluster; it then checks that after applying a policy that
1127 00b70680 Iustin Pop
-- the instance doesn't fits, the allocation fails.
1128 00b70680 Iustin Pop
prop_ClusterAllocPolicy node =
1129 00b70680 Iustin Pop
  -- rqn is the required nodes (1 or 2)
1130 00b70680 Iustin Pop
  forAll (choose (1, 2)) $ \rqn ->
1131 00b70680 Iustin Pop
  forAll (choose (5, 20)) $ \count ->
1132 00b70680 Iustin Pop
  forAll (arbitrary `suchThat` (canAllocOn (makeSmallCluster node count) rqn))
1133 00b70680 Iustin Pop
         $ \inst ->
1134 00b70680 Iustin Pop
  forAll (arbitrary `suchThat` (isFailure .
1135 00b70680 Iustin Pop
                                Instance.instMatchesPolicy inst)) $ \ipol ->
1136 00b70680 Iustin Pop
  let node' = Node.setPolicy ipol node
1137 00b70680 Iustin Pop
      nl = makeSmallCluster node' count
1138 00b70680 Iustin Pop
  in not $ canAllocOn nl rqn inst
1139 00b70680 Iustin Pop
1140 23fe06c2 Iustin Pop
testSuite "Cluster"
1141 d5dfae0a Iustin Pop
            [ 'prop_Score_Zero
1142 d5dfae0a Iustin Pop
            , 'prop_CStats_sane
1143 d5dfae0a Iustin Pop
            , 'prop_ClusterAlloc_sane
1144 d5dfae0a Iustin Pop
            , 'prop_ClusterCanTieredAlloc
1145 d5dfae0a Iustin Pop
            , 'prop_ClusterAllocEvac
1146 d5dfae0a Iustin Pop
            , 'prop_ClusterAllocBalance
1147 d5dfae0a Iustin Pop
            , 'prop_ClusterCheckConsistency
1148 d5dfae0a Iustin Pop
            , 'prop_ClusterSplitCluster
1149 00b70680 Iustin Pop
            , 'prop_ClusterAllocPolicy
1150 d5dfae0a Iustin Pop
            ]
1151 88f25dd0 Iustin Pop
1152 525bfb36 Iustin Pop
-- ** OpCodes tests
1153 88f25dd0 Iustin Pop
1154 525bfb36 Iustin Pop
-- | Check that opcode serialization is idempotent.
1155 88f25dd0 Iustin Pop
prop_OpCodes_serialization op =
1156 88f25dd0 Iustin Pop
  case J.readJSON (J.showJSON op) of
1157 72bb6b4e Iustin Pop
    J.Error e -> printTestCase ("Cannot deserialise: " ++ e) False
1158 72bb6b4e Iustin Pop
    J.Ok op' -> op ==? op'
1159 4a007641 Iustin Pop
  where _types = op::OpCodes.OpCode
1160 88f25dd0 Iustin Pop
1161 23fe06c2 Iustin Pop
testSuite "OpCodes"
1162 d5dfae0a Iustin Pop
            [ 'prop_OpCodes_serialization ]
1163 c088674b Iustin Pop
1164 525bfb36 Iustin Pop
-- ** Jobs tests
1165 525bfb36 Iustin Pop
1166 525bfb36 Iustin Pop
-- | Check that (queued) job\/opcode status serialization is idempotent.
1167 db079755 Iustin Pop
prop_OpStatus_serialization os =
1168 db079755 Iustin Pop
  case J.readJSON (J.showJSON os) of
1169 72bb6b4e Iustin Pop
    J.Error e -> printTestCase ("Cannot deserialise: " ++ e) False
1170 72bb6b4e Iustin Pop
    J.Ok os' -> os ==? os'
1171 db079755 Iustin Pop
  where _types = os::Jobs.OpStatus
1172 db079755 Iustin Pop
1173 db079755 Iustin Pop
prop_JobStatus_serialization js =
1174 db079755 Iustin Pop
  case J.readJSON (J.showJSON js) of
1175 72bb6b4e Iustin Pop
    J.Error e -> printTestCase ("Cannot deserialise: " ++ e) False
1176 72bb6b4e Iustin Pop
    J.Ok js' -> js ==? js'
1177 db079755 Iustin Pop
  where _types = js::Jobs.JobStatus
1178 db079755 Iustin Pop
1179 23fe06c2 Iustin Pop
testSuite "Jobs"
1180 d5dfae0a Iustin Pop
            [ 'prop_OpStatus_serialization
1181 d5dfae0a Iustin Pop
            , 'prop_JobStatus_serialization
1182 d5dfae0a Iustin Pop
            ]
1183 db079755 Iustin Pop
1184 525bfb36 Iustin Pop
-- ** Loader tests
1185 c088674b Iustin Pop
1186 c088674b Iustin Pop
prop_Loader_lookupNode ktn inst node =
1187 72bb6b4e Iustin Pop
  Loader.lookupNode nl inst node ==? Data.Map.lookup node nl
1188 d5dfae0a Iustin Pop
    where nl = Data.Map.fromList ktn
1189 c088674b Iustin Pop
1190 c088674b Iustin Pop
prop_Loader_lookupInstance kti inst =
1191 72bb6b4e Iustin Pop
  Loader.lookupInstance il inst ==? Data.Map.lookup inst il
1192 d5dfae0a Iustin Pop
    where il = Data.Map.fromList kti
1193 99b63608 Iustin Pop
1194 99b63608 Iustin Pop
prop_Loader_assignIndices nodes =
1195 99b63608 Iustin Pop
  Data.Map.size nassoc == length nodes &&
1196 99b63608 Iustin Pop
  Container.size kt == length nodes &&
1197 99b63608 Iustin Pop
  (if not (null nodes)
1198 99b63608 Iustin Pop
   then maximum (IntMap.keys kt) == length nodes - 1
1199 c088674b Iustin Pop
   else True)
1200 d5dfae0a Iustin Pop
    where (nassoc, kt) =
1201 d5dfae0a Iustin Pop
            Loader.assignIndices (map (\n -> (Node.name n, n)) nodes)
1202 c088674b Iustin Pop
1203 c088674b Iustin Pop
-- | Checks that the number of primary instances recorded on the nodes
1204 525bfb36 Iustin Pop
-- is zero.
1205 c088674b Iustin Pop
prop_Loader_mergeData ns =
1206 cb0c77ff Iustin Pop
  let na = Container.fromList $ map (\n -> (Node.idx n, n)) ns
1207 2d1708e0 Guido Trotter
  in case Loader.mergeData [] [] [] []
1208 f4f6eb0b Iustin Pop
         (Loader.emptyCluster {Loader.cdNodes = na}) of
1209 c088674b Iustin Pop
    Types.Bad _ -> False
1210 71375ef7 Iustin Pop
    Types.Ok (Loader.ClusterData _ nl il _ _) ->
1211 c088674b Iustin Pop
      let nodes = Container.elems nl
1212 c088674b Iustin Pop
          instances = Container.elems il
1213 c088674b Iustin Pop
      in (sum . map (length . Node.pList)) nodes == 0 &&
1214 4a007641 Iustin Pop
         null instances
1215 c088674b Iustin Pop
1216 efe98965 Guido Trotter
-- | Check that compareNameComponent on equal strings works.
1217 efe98965 Guido Trotter
prop_Loader_compareNameComponent_equal :: String -> Bool
1218 efe98965 Guido Trotter
prop_Loader_compareNameComponent_equal s =
1219 efe98965 Guido Trotter
  Loader.compareNameComponent s s ==
1220 efe98965 Guido Trotter
    Loader.LookupResult Loader.ExactMatch s
1221 efe98965 Guido Trotter
1222 efe98965 Guido Trotter
-- | Check that compareNameComponent on prefix strings works.
1223 efe98965 Guido Trotter
prop_Loader_compareNameComponent_prefix :: NonEmptyList Char -> String -> Bool
1224 efe98965 Guido Trotter
prop_Loader_compareNameComponent_prefix (NonEmpty s1) s2 =
1225 efe98965 Guido Trotter
  Loader.compareNameComponent (s1 ++ "." ++ s2) s1 ==
1226 efe98965 Guido Trotter
    Loader.LookupResult Loader.PartialMatch s1
1227 efe98965 Guido Trotter
1228 23fe06c2 Iustin Pop
testSuite "Loader"
1229 d5dfae0a Iustin Pop
            [ 'prop_Loader_lookupNode
1230 d5dfae0a Iustin Pop
            , 'prop_Loader_lookupInstance
1231 d5dfae0a Iustin Pop
            , 'prop_Loader_assignIndices
1232 d5dfae0a Iustin Pop
            , 'prop_Loader_mergeData
1233 d5dfae0a Iustin Pop
            , 'prop_Loader_compareNameComponent_equal
1234 d5dfae0a Iustin Pop
            , 'prop_Loader_compareNameComponent_prefix
1235 d5dfae0a Iustin Pop
            ]
1236 3c002a13 Iustin Pop
1237 3c002a13 Iustin Pop
-- ** Types tests
1238 3c002a13 Iustin Pop
1239 0047d4e2 Iustin Pop
prop_Types_AllocPolicy_serialisation apol =
1240 d5dfae0a Iustin Pop
  case J.readJSON (J.showJSON apol) of
1241 d5dfae0a Iustin Pop
    J.Ok p -> printTestCase ("invalid deserialisation " ++ show p) $
1242 d5dfae0a Iustin Pop
              p == apol
1243 d5dfae0a Iustin Pop
    J.Error s -> printTestCase ("failed to deserialise: " ++ s) False
1244 d5dfae0a Iustin Pop
      where _types = apol::Types.AllocPolicy
1245 0047d4e2 Iustin Pop
1246 0047d4e2 Iustin Pop
prop_Types_DiskTemplate_serialisation dt =
1247 d5dfae0a Iustin Pop
  case J.readJSON (J.showJSON dt) of
1248 d5dfae0a Iustin Pop
    J.Ok p -> printTestCase ("invalid deserialisation " ++ show p) $
1249 d5dfae0a Iustin Pop
              p == dt
1250 d5dfae0a Iustin Pop
    J.Error s -> printTestCase ("failed to deserialise: " ++ s)
1251 d5dfae0a Iustin Pop
                 False
1252 d5dfae0a Iustin Pop
      where _types = dt::Types.DiskTemplate
1253 0047d4e2 Iustin Pop
1254 0047d4e2 Iustin Pop
prop_Types_opToResult op =
1255 d5dfae0a Iustin Pop
  case op of
1256 d5dfae0a Iustin Pop
    Types.OpFail _ -> Types.isBad r
1257 d5dfae0a Iustin Pop
    Types.OpGood v -> case r of
1258 d5dfae0a Iustin Pop
                        Types.Bad _ -> False
1259 d5dfae0a Iustin Pop
                        Types.Ok v' -> v == v'
1260 d5dfae0a Iustin Pop
  where r = Types.opToResult op
1261 d5dfae0a Iustin Pop
        _types = op::Types.OpResult Int
1262 0047d4e2 Iustin Pop
1263 0047d4e2 Iustin Pop
prop_Types_eitherToResult ei =
1264 d5dfae0a Iustin Pop
  case ei of
1265 d5dfae0a Iustin Pop
    Left _ -> Types.isBad r
1266 d5dfae0a Iustin Pop
    Right v -> case r of
1267 d5dfae0a Iustin Pop
                 Types.Bad _ -> False
1268 d5dfae0a Iustin Pop
                 Types.Ok v' -> v == v'
1269 0047d4e2 Iustin Pop
    where r = Types.eitherToResult ei
1270 0047d4e2 Iustin Pop
          _types = ei::Either String Int
1271 3c002a13 Iustin Pop
1272 23fe06c2 Iustin Pop
testSuite "Types"
1273 d5dfae0a Iustin Pop
            [ 'prop_Types_AllocPolicy_serialisation
1274 d5dfae0a Iustin Pop
            , 'prop_Types_DiskTemplate_serialisation
1275 d5dfae0a Iustin Pop
            , 'prop_Types_opToResult
1276 d5dfae0a Iustin Pop
            , 'prop_Types_eitherToResult
1277 d5dfae0a Iustin Pop
            ]