| 1 |
{-# LANGUAGE TemplateHaskell #-} |
| 2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 3 |
|
| 4 |
{-| Unittests for ganeti-htools. |
| 5 |
|
| 6 |
-} |
| 7 |
|
| 8 |
{- |
| 9 |
|
| 10 |
Copyright (C) 2009, 2010, 2011, 2012, 2013 Google Inc. |
| 11 |
All rights reserved. |
| 12 |
|
| 13 |
Redistribution and use in source and binary forms, with or without |
| 14 |
modification, are permitted provided that the following conditions are |
| 15 |
met: |
| 16 |
|
| 17 |
1. Redistributions of source code must retain the above copyright notice, |
| 18 |
this list of conditions and the following disclaimer. |
| 19 |
|
| 20 |
2. Redistributions in binary form must reproduce the above copyright |
| 21 |
notice, this list of conditions and the following disclaimer in the |
| 22 |
documentation and/or other materials provided with the distribution. |
| 23 |
|
| 24 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 25 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 26 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 27 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 28 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 29 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 30 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 31 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 32 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 33 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 |
|
| 36 |
-} |
| 37 |
|
| 38 |
module Test.Ganeti.HTools.Instance |
| 39 |
( testHTools_Instance |
| 40 |
, genInstanceSmallerThanNode |
| 41 |
, genInstanceMaybeBiggerThanNode |
| 42 |
, genInstanceOnNodeList |
| 43 |
, genInstanceList |
| 44 |
, Instance.Instance(..) |
| 45 |
) where |
| 46 |
|
| 47 |
import Control.Arrow ((&&&)) |
| 48 |
import Control.Monad (liftM) |
| 49 |
import Test.QuickCheck hiding (Result) |
| 50 |
|
| 51 |
import Test.Ganeti.TestHTools (nullISpec) |
| 52 |
import Test.Ganeti.TestHelper |
| 53 |
import Test.Ganeti.TestCommon |
| 54 |
import Test.Ganeti.HTools.Types () |
| 55 |
|
| 56 |
import Ganeti.BasicTypes |
| 57 |
import qualified Ganeti.HTools.Instance as Instance |
| 58 |
import qualified Ganeti.HTools.Node as Node |
| 59 |
import qualified Ganeti.HTools.Container as Container |
| 60 |
import qualified Ganeti.HTools.Loader as Loader |
| 61 |
import qualified Ganeti.HTools.Types as Types |
| 62 |
|
| 63 |
-- * Arbitrary instances |
| 64 |
|
| 65 |
-- | Generates a random instance with maximum and minimum disk/mem/cpu values. |
| 66 |
genInstanceWithin :: Int -> Int -> Int -> Int |
| 67 |
-> Int -> Int -> Int -> Maybe Int |
| 68 |
-> Gen Instance.Instance |
| 69 |
genInstanceWithin min_mem min_dsk min_cpu min_spin |
| 70 |
max_mem max_dsk max_cpu max_spin = do |
| 71 |
name <- genFQDN |
| 72 |
mem <- choose (min_mem, max_mem) |
| 73 |
dsk <- choose (min_dsk, max_dsk) |
| 74 |
run_st <- arbitrary |
| 75 |
pn <- arbitrary |
| 76 |
sn <- arbitrary |
| 77 |
vcpus <- choose (min_cpu, max_cpu) |
| 78 |
dt <- arbitrary |
| 79 |
spindles <- case max_spin of |
| 80 |
Nothing -> genMaybe $ choose (min_spin, maxSpindles) |
| 81 |
Just ls -> liftM Just $ choose (min_spin, ls) |
| 82 |
forthcoming <- arbitrary |
| 83 |
let disk = Instance.Disk dsk spindles |
| 84 |
return $ Instance.create |
| 85 |
name mem dsk [disk] vcpus run_st [] True pn sn dt 1 [] forthcoming |
| 86 |
|
| 87 |
-- | Generate an instance with maximum disk/mem/cpu values. |
| 88 |
genInstanceSmallerThan :: Int -> Int -> Int -> Maybe Int |
| 89 |
-> Gen Instance.Instance |
| 90 |
genInstanceSmallerThan = genInstanceWithin 1 0 1 0 |
| 91 |
|
| 92 |
-- | Generates an instance smaller than a node. |
| 93 |
genInstanceSmallerThanNode :: Node.Node -> Gen Instance.Instance |
| 94 |
genInstanceSmallerThanNode node = |
| 95 |
genInstanceSmallerThan (Node.availMem node `div` 2) |
| 96 |
(Node.availDisk node `div` 2) |
| 97 |
(Node.availCpu node `div` 2) |
| 98 |
(if Node.exclStorage node |
| 99 |
then Just $ Node.fSpindlesForth node `div` 2 |
| 100 |
else Nothing) |
| 101 |
|
| 102 |
-- | Generates an instance possibly bigger than a node. |
| 103 |
-- In any case, that instance will be bigger than the node's ipolicy's lower |
| 104 |
-- bound. |
| 105 |
genInstanceMaybeBiggerThanNode :: Node.Node -> Gen Instance.Instance |
| 106 |
genInstanceMaybeBiggerThanNode node = |
| 107 |
let minISpec = runListHead nullISpec Types.minMaxISpecsMinSpec |
| 108 |
. Types.iPolicyMinMaxISpecs $ Node.iPolicy node |
| 109 |
in genInstanceWithin (Types.iSpecMemorySize minISpec) |
| 110 |
(Types.iSpecDiskSize minISpec) |
| 111 |
(Types.iSpecCpuCount minISpec) |
| 112 |
(Types.iSpecSpindleUse minISpec) |
| 113 |
(Node.availMem node + Types.unitMem * 2) |
| 114 |
(Node.availDisk node + Types.unitDsk * 3) |
| 115 |
(Node.availCpu node + Types.unitCpu * 4) |
| 116 |
(if Node.exclStorage node |
| 117 |
then Just $ Node.fSpindles node + |
| 118 |
Types.unitSpindle * 5 |
| 119 |
else Nothing) |
| 120 |
|
| 121 |
-- | Generates an instance with nodes on a node list. |
| 122 |
-- The following rules are respected: |
| 123 |
-- 1. The instance is never bigger than its primary node |
| 124 |
-- 2. If possible the instance has different pnode and snode |
| 125 |
-- 3. Else disk templates which require secondary nodes are disabled |
| 126 |
genInstanceOnNodeList :: Node.List -> Gen Instance.Instance |
| 127 |
genInstanceOnNodeList nl = do |
| 128 |
let nsize = Container.size nl |
| 129 |
pnode <- choose (0, nsize-1) |
| 130 |
let (snodefilter, dtfilter) = |
| 131 |
if nsize >= 2 |
| 132 |
then ((/= pnode), const True) |
| 133 |
else (const True, not . Instance.hasSecondary) |
| 134 |
snode <- choose (0, nsize-1) `suchThat` snodefilter |
| 135 |
i <- genInstanceSmallerThanNode (Container.find pnode nl) `suchThat` dtfilter |
| 136 |
return $ i { Instance.pNode = pnode, Instance.sNode = snode } |
| 137 |
|
| 138 |
-- | Generates an instance list given an instance generator. |
| 139 |
genInstanceList :: Gen Instance.Instance -> Gen Instance.List |
| 140 |
genInstanceList igen = fmap (snd . Loader.assignIndices) names_instances |
| 141 |
where names_instances = |
| 142 |
map (Instance.name &&& id) <$> listOf igen |
| 143 |
|
| 144 |
-- let's generate a random instance |
| 145 |
instance Arbitrary Instance.Instance where |
| 146 |
arbitrary = genInstanceSmallerThan maxMem maxDsk maxCpu Nothing |
| 147 |
|
| 148 |
-- * Test cases |
| 149 |
|
| 150 |
-- Simple instance tests, we only have setter/getters |
| 151 |
|
| 152 |
prop_creat :: Instance.Instance -> Property |
| 153 |
prop_creat inst = |
| 154 |
Instance.name inst ==? Instance.alias inst |
| 155 |
|
| 156 |
prop_setIdx :: Instance.Instance -> Types.Idx -> Property |
| 157 |
prop_setIdx inst idx = |
| 158 |
Instance.idx (Instance.setIdx inst idx) ==? idx |
| 159 |
|
| 160 |
prop_setName :: Instance.Instance -> String -> Bool |
| 161 |
prop_setName inst name = |
| 162 |
Instance.name newinst == name && |
| 163 |
Instance.alias newinst == name |
| 164 |
where newinst = Instance.setName inst name |
| 165 |
|
| 166 |
prop_setAlias :: Instance.Instance -> String -> Bool |
| 167 |
prop_setAlias inst name = |
| 168 |
Instance.name newinst == Instance.name inst && |
| 169 |
Instance.alias newinst == name |
| 170 |
where newinst = Instance.setAlias inst name |
| 171 |
|
| 172 |
prop_setPri :: Instance.Instance -> Types.Ndx -> Property |
| 173 |
prop_setPri inst pdx = |
| 174 |
Instance.pNode (Instance.setPri inst pdx) ==? pdx |
| 175 |
|
| 176 |
prop_setSec :: Instance.Instance -> Types.Ndx -> Property |
| 177 |
prop_setSec inst sdx = |
| 178 |
Instance.sNode (Instance.setSec inst sdx) ==? sdx |
| 179 |
|
| 180 |
prop_setBoth :: Instance.Instance -> Types.Ndx -> Types.Ndx -> Bool |
| 181 |
prop_setBoth inst pdx sdx = |
| 182 |
Instance.pNode si == pdx && Instance.sNode si == sdx |
| 183 |
where si = Instance.setBoth inst pdx sdx |
| 184 |
|
| 185 |
prop_shrinkMG :: Instance.Instance -> Property |
| 186 |
prop_shrinkMG inst = |
| 187 |
Instance.mem inst >= 2 * Types.unitMem ==> |
| 188 |
case Instance.shrinkByType inst Types.FailMem of |
| 189 |
Ok inst' -> Instance.mem inst' ==? Instance.mem inst - Types.unitMem |
| 190 |
Bad msg -> failTest msg |
| 191 |
|
| 192 |
prop_shrinkMF :: Instance.Instance -> Property |
| 193 |
prop_shrinkMF inst = |
| 194 |
forAll (choose (0, 2 * Types.unitMem - 1)) $ \mem -> |
| 195 |
let inst' = inst { Instance.mem = mem} |
| 196 |
in isBad $ Instance.shrinkByType inst' Types.FailMem |
| 197 |
|
| 198 |
prop_shrinkCG :: Instance.Instance -> Property |
| 199 |
prop_shrinkCG inst = |
| 200 |
Instance.vcpus inst >= 2 * Types.unitCpu ==> |
| 201 |
case Instance.shrinkByType inst Types.FailCPU of |
| 202 |
Ok inst' -> Instance.vcpus inst' ==? Instance.vcpus inst - Types.unitCpu |
| 203 |
Bad msg -> failTest msg |
| 204 |
|
| 205 |
prop_shrinkCF :: Instance.Instance -> Property |
| 206 |
prop_shrinkCF inst = |
| 207 |
forAll (choose (0, 2 * Types.unitCpu - 1)) $ \vcpus -> |
| 208 |
let inst' = inst { Instance.vcpus = vcpus } |
| 209 |
in isBad $ Instance.shrinkByType inst' Types.FailCPU |
| 210 |
|
| 211 |
prop_shrinkDG :: Instance.Instance -> Property |
| 212 |
prop_shrinkDG inst = |
| 213 |
Instance.dsk inst >= 2 * Types.unitDsk ==> |
| 214 |
case Instance.shrinkByType inst Types.FailDisk of |
| 215 |
Ok inst' -> Instance.dsk inst' ==? Instance.dsk inst - Types.unitDsk |
| 216 |
Bad msg -> failTest msg |
| 217 |
|
| 218 |
prop_shrinkDF :: Instance.Instance -> Property |
| 219 |
prop_shrinkDF inst = |
| 220 |
forAll (choose (0, 2 * Types.unitDsk - 1)) $ \dsk -> |
| 221 |
let inst' = inst { Instance.dsk = dsk |
| 222 |
, Instance.disks = [Instance.Disk dsk Nothing] } |
| 223 |
in isBad $ Instance.shrinkByType inst' Types.FailDisk |
| 224 |
|
| 225 |
prop_setMovable :: Instance.Instance -> Bool -> Property |
| 226 |
prop_setMovable inst m = |
| 227 |
Instance.movable inst' ==? m |
| 228 |
where inst' = Instance.setMovable inst m |
| 229 |
|
| 230 |
testSuite "HTools/Instance" |
| 231 |
[ 'prop_creat |
| 232 |
, 'prop_setIdx |
| 233 |
, 'prop_setName |
| 234 |
, 'prop_setAlias |
| 235 |
, 'prop_setPri |
| 236 |
, 'prop_setSec |
| 237 |
, 'prop_setBoth |
| 238 |
, 'prop_shrinkMG |
| 239 |
, 'prop_shrinkMF |
| 240 |
, 'prop_shrinkCG |
| 241 |
, 'prop_shrinkCF |
| 242 |
, 'prop_shrinkDG |
| 243 |
, 'prop_shrinkDF |
| 244 |
, 'prop_setMovable |
| 245 |
] |