| 1 |
{-# LANGUAGE TemplateHaskell, FlexibleInstances, TypeSynonymInstances #-} |
| 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.Types |
| 39 |
( testHTools_Types |
| 40 |
, Types.AllocPolicy(..) |
| 41 |
, Types.DiskTemplate(..) |
| 42 |
, Types.FailMode(..) |
| 43 |
, Types.ISpec(..) |
| 44 |
, Types.IPolicy(..) |
| 45 |
, nullIPolicy |
| 46 |
) where |
| 47 |
|
| 48 |
import Test.QuickCheck hiding (Result) |
| 49 |
import Test.HUnit |
| 50 |
|
| 51 |
import Control.Monad (replicateM) |
| 52 |
|
| 53 |
import Test.Ganeti.TestHelper |
| 54 |
import Test.Ganeti.TestCommon |
| 55 |
import Test.Ganeti.TestHTools |
| 56 |
import Test.Ganeti.Types (allDiskTemplates) |
| 57 |
|
| 58 |
import Ganeti.BasicTypes |
| 59 |
import qualified Ganeti.Constants as C |
| 60 |
import Ganeti.ConstantUtils |
| 61 |
import qualified Ganeti.HTools.Types as Types |
| 62 |
|
| 63 |
{-# ANN module "HLint: ignore Use camelCase" #-} |
| 64 |
|
| 65 |
-- * Helpers |
| 66 |
|
| 67 |
-- * Arbitrary instance |
| 68 |
|
| 69 |
$(genArbitrary ''Types.FailMode) |
| 70 |
|
| 71 |
instance Arbitrary a => Arbitrary (Types.OpResult a) where |
| 72 |
arbitrary = arbitrary >>= \c -> |
| 73 |
if c |
| 74 |
then Ok <$> arbitrary |
| 75 |
else Bad <$> arbitrary |
| 76 |
|
| 77 |
instance Arbitrary Types.ISpec where |
| 78 |
arbitrary = do |
| 79 |
mem_s <- arbitrary::Gen (NonNegative Int) |
| 80 |
dsk_c <- arbitrary::Gen (NonNegative Int) |
| 81 |
dsk_s <- arbitrary::Gen (NonNegative Int) |
| 82 |
cpu_c <- arbitrary::Gen (NonNegative Int) |
| 83 |
nic_c <- arbitrary::Gen (NonNegative Int) |
| 84 |
su <- arbitrary::Gen (NonNegative Int) |
| 85 |
return Types.ISpec { Types.iSpecMemorySize = fromEnum mem_s |
| 86 |
, Types.iSpecCpuCount = fromEnum cpu_c |
| 87 |
, Types.iSpecDiskSize = fromEnum dsk_s |
| 88 |
, Types.iSpecDiskCount = fromEnum dsk_c |
| 89 |
, Types.iSpecNicCount = fromEnum nic_c |
| 90 |
, Types.iSpecSpindleUse = fromEnum su |
| 91 |
} |
| 92 |
|
| 93 |
-- | Generates an ispec bigger than the given one. |
| 94 |
genBiggerISpec :: Types.ISpec -> Gen Types.ISpec |
| 95 |
genBiggerISpec imin = do |
| 96 |
mem_s <- choose (Types.iSpecMemorySize imin, maxBound) |
| 97 |
dsk_c <- choose (Types.iSpecDiskCount imin, maxBound) |
| 98 |
dsk_s <- choose (Types.iSpecDiskSize imin, maxBound) |
| 99 |
cpu_c <- choose (Types.iSpecCpuCount imin, maxBound) |
| 100 |
nic_c <- choose (Types.iSpecNicCount imin, maxBound) |
| 101 |
su <- choose (Types.iSpecSpindleUse imin, maxBound) |
| 102 |
return Types.ISpec { Types.iSpecMemorySize = fromEnum mem_s |
| 103 |
, Types.iSpecCpuCount = fromEnum cpu_c |
| 104 |
, Types.iSpecDiskSize = fromEnum dsk_s |
| 105 |
, Types.iSpecDiskCount = fromEnum dsk_c |
| 106 |
, Types.iSpecNicCount = fromEnum nic_c |
| 107 |
, Types.iSpecSpindleUse = fromEnum su |
| 108 |
} |
| 109 |
|
| 110 |
genMinMaxISpecs :: Gen Types.MinMaxISpecs |
| 111 |
genMinMaxISpecs = do |
| 112 |
imin <- arbitrary |
| 113 |
imax <- genBiggerISpec imin |
| 114 |
return Types.MinMaxISpecs { Types.minMaxISpecsMinSpec = imin |
| 115 |
, Types.minMaxISpecsMaxSpec = imax |
| 116 |
} |
| 117 |
|
| 118 |
instance Arbitrary Types.MinMaxISpecs where |
| 119 |
arbitrary = genMinMaxISpecs |
| 120 |
|
| 121 |
genMinMaxStdISpecs :: Gen (Types.MinMaxISpecs, Types.ISpec) |
| 122 |
genMinMaxStdISpecs = do |
| 123 |
imin <- arbitrary |
| 124 |
istd <- genBiggerISpec imin |
| 125 |
imax <- genBiggerISpec istd |
| 126 |
return (Types.MinMaxISpecs { Types.minMaxISpecsMinSpec = imin |
| 127 |
, Types.minMaxISpecsMaxSpec = imax |
| 128 |
}, |
| 129 |
istd) |
| 130 |
|
| 131 |
genIPolicySpecs :: Gen ([Types.MinMaxISpecs], Types.ISpec) |
| 132 |
genIPolicySpecs = do |
| 133 |
num_mm <- choose (1, 6) -- 6 is just an arbitrary limit |
| 134 |
std_compl <- choose (1, num_mm) |
| 135 |
mm_head <- replicateM (std_compl - 1) genMinMaxISpecs |
| 136 |
(mm_middle, istd) <- genMinMaxStdISpecs |
| 137 |
mm_tail <- replicateM (num_mm - std_compl) genMinMaxISpecs |
| 138 |
return (mm_head ++ (mm_middle : mm_tail), istd) |
| 139 |
|
| 140 |
|
| 141 |
instance Arbitrary Types.IPolicy where |
| 142 |
arbitrary = do |
| 143 |
(iminmax, istd) <- genIPolicySpecs |
| 144 |
num_tmpl <- choose (0, length allDiskTemplates) |
| 145 |
dts <- genUniquesList num_tmpl arbitrary |
| 146 |
vcpu_ratio <- choose (1.0, maxVcpuRatio) |
| 147 |
spindle_ratio <- choose (1.0, maxSpindleRatio) |
| 148 |
return Types.IPolicy { Types.iPolicyMinMaxISpecs = iminmax |
| 149 |
, Types.iPolicyStdSpec = istd |
| 150 |
, Types.iPolicyDiskTemplates = dts |
| 151 |
, Types.iPolicyVcpuRatio = vcpu_ratio |
| 152 |
, Types.iPolicySpindleRatio = spindle_ratio |
| 153 |
} |
| 154 |
|
| 155 |
-- * Test cases |
| 156 |
|
| 157 |
prop_ISpec_serialisation :: Types.ISpec -> Property |
| 158 |
prop_ISpec_serialisation = testSerialisation |
| 159 |
|
| 160 |
prop_IPolicy_serialisation :: Types.IPolicy -> Property |
| 161 |
prop_IPolicy_serialisation = testSerialisation |
| 162 |
|
| 163 |
prop_opToResult :: Types.OpResult Int -> Property |
| 164 |
prop_opToResult op = |
| 165 |
case op of |
| 166 |
Bad _ -> counterexample ("expected bad but got " ++ show r) $ isBad r |
| 167 |
Ok v -> case r of |
| 168 |
Bad msg -> failTest ("expected Ok but got Bad " ++ msg) |
| 169 |
Ok v' -> v ==? v' |
| 170 |
where r = Types.opToResult op |
| 171 |
|
| 172 |
prop_eitherToResult :: Either String Int -> Bool |
| 173 |
prop_eitherToResult ei = |
| 174 |
case ei of |
| 175 |
Left _ -> isBad r |
| 176 |
Right v -> case r of |
| 177 |
Bad _ -> False |
| 178 |
Ok v' -> v == v' |
| 179 |
where r = eitherToResult ei :: Result Int |
| 180 |
|
| 181 |
-- | Test 'AutoRepairType' ordering is as expected and consistent with Python |
| 182 |
-- codebase. |
| 183 |
case_AutoRepairType_sort :: Assertion |
| 184 |
case_AutoRepairType_sort = do |
| 185 |
let expected = [ Types.ArFixStorage |
| 186 |
, Types.ArMigrate |
| 187 |
, Types.ArFailover |
| 188 |
, Types.ArReinstall |
| 189 |
] |
| 190 |
all_hs_raw = mkSet $ map Types.autoRepairTypeToRaw [minBound..maxBound] |
| 191 |
assertEqual "Haskell order" expected [minBound..maxBound] |
| 192 |
assertEqual "consistent with Python" C.autoRepairAllTypes all_hs_raw |
| 193 |
|
| 194 |
-- | Test 'AutoRepairResult' type is equivalent with Python codebase. |
| 195 |
case_AutoRepairResult_pyequiv :: Assertion |
| 196 |
case_AutoRepairResult_pyequiv = do |
| 197 |
let all_py_results = C.autoRepairAllResults |
| 198 |
all_hs_results = mkSet $ |
| 199 |
map Types.autoRepairResultToRaw [minBound..maxBound] |
| 200 |
assertEqual "for AutoRepairResult equivalence" all_py_results all_hs_results |
| 201 |
|
| 202 |
testSuite "HTools/Types" |
| 203 |
[ 'prop_ISpec_serialisation |
| 204 |
, 'prop_IPolicy_serialisation |
| 205 |
, 'prop_opToResult |
| 206 |
, 'prop_eitherToResult |
| 207 |
, 'case_AutoRepairType_sort |
| 208 |
, 'case_AutoRepairResult_pyequiv |
| 209 |
] |