| 1 |
{-# LANGUAGE TemplateHaskell, CPP, ScopedTypeVariables #-} |
| 2 |
|
| 3 |
{-| Unittests for ganeti-htools. |
| 4 |
|
| 5 |
-} |
| 6 |
|
| 7 |
{- |
| 8 |
|
| 9 |
Copyright (C) 2009, 2010, 2011, 2012, 2013 Google Inc. |
| 10 |
All rights reserved. |
| 11 |
|
| 12 |
Redistribution and use in source and binary forms, with or without |
| 13 |
modification, are permitted provided that the following conditions are |
| 14 |
met: |
| 15 |
|
| 16 |
1. Redistributions of source code must retain the above copyright notice, |
| 17 |
this list of conditions and the following disclaimer. |
| 18 |
|
| 19 |
2. Redistributions in binary form must reproduce the above copyright |
| 20 |
notice, this list of conditions and the following disclaimer in the |
| 21 |
documentation and/or other materials provided with the distribution. |
| 22 |
|
| 23 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 24 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 25 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 26 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 27 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 28 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 29 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 30 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 31 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 32 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 |
|
| 35 |
-} |
| 36 |
|
| 37 |
module Test.Ganeti.Utils (testUtils) where |
| 38 |
|
| 39 |
import Test.QuickCheck hiding (Result) |
| 40 |
import Test.HUnit |
| 41 |
|
| 42 |
import Data.Char (isSpace) |
| 43 |
import qualified Data.Either as Either |
| 44 |
import qualified Data.List.NonEmpty as NonEmpty |
| 45 |
import Data.List.NonEmpty (NonEmpty((:|))) |
| 46 |
import Data.List |
| 47 |
import Data.Maybe (listToMaybe) |
| 48 |
import qualified Data.Set as S |
| 49 |
import qualified Text.JSON as J |
| 50 |
import Ganeti.Query.RegEx ((=~)) |
| 51 |
|
| 52 |
import Test.Ganeti.TestHelper |
| 53 |
import Test.Ganeti.TestCommon |
| 54 |
|
| 55 |
import Ganeti.BasicTypes |
| 56 |
import qualified Ganeti.Constants as C |
| 57 |
import qualified Ganeti.JSON as JSON |
| 58 |
import Ganeti.Utils as Utils |
| 59 |
|
| 60 |
{-# ANN module "HLint: ignore Use camelCase" #-} |
| 61 |
|
| 62 |
|
| 63 |
-- | Helper to generate a small string that doesn't contain commas. |
| 64 |
genNonCommaString :: Gen String |
| 65 |
genNonCommaString = do |
| 66 |
size <- choose (0, 20) -- arbitrary max size |
| 67 |
vectorOf size (arbitrary `suchThat` (/=) ',') |
| 68 |
|
| 69 |
genNonEmpty :: (Arbitrary a) => Gen (NonEmpty a) |
| 70 |
genNonEmpty = fmap (uncurry (:|)) arbitrary |
| 71 |
|
| 72 |
-- | If the list is not just an empty element, and if the elements do |
| 73 |
-- not contain commas, then join+split should be idempotent. |
| 74 |
prop_commaJoinSplit :: Property |
| 75 |
prop_commaJoinSplit = |
| 76 |
forAll (choose (0, 20)) $ \llen -> |
| 77 |
forAll (vectorOf llen genNonCommaString `suchThat` (/=) [""]) $ \lst -> |
| 78 |
sepSplit ',' (commaJoin lst) ==? lst |
| 79 |
|
| 80 |
-- | Split and join should always be idempotent. |
| 81 |
prop_commaSplitJoin :: String -> Property |
| 82 |
prop_commaSplitJoin s = |
| 83 |
commaJoin (sepSplit ',' s) ==? s |
| 84 |
|
| 85 |
-- | Test 'findFirst' on several possible inputs. |
| 86 |
prop_findFirst :: Property |
| 87 |
prop_findFirst = |
| 88 |
forAll (genSublist [0..5 :: Int]) $ \xs -> |
| 89 |
forAll (choose (-2, 7)) $ \base -> |
| 90 |
counterexample "findFirst utility function" $ |
| 91 |
let r = findFirst base (S.fromList xs) |
| 92 |
(ss, es) = partition (< r) $ dropWhile (< base) xs |
| 93 |
-- the prefix must be a range of numbers |
| 94 |
-- and the suffix must not start with 'r' |
| 95 |
in conjoin [ and $ zipWith ((==) . (+ 1)) ss (drop 1 ss) |
| 96 |
, maybe True (> r) (listToMaybe es) |
| 97 |
] |
| 98 |
|
| 99 |
|
| 100 |
-- | fromObjWithDefault, we test using the Maybe monad and an integer |
| 101 |
-- value. |
| 102 |
prop_fromObjWithDefault :: Integer -> String -> Bool |
| 103 |
prop_fromObjWithDefault def_value random_key = |
| 104 |
-- a missing key will be returned with the default |
| 105 |
JSON.fromObjWithDefault [] random_key def_value == Just def_value && |
| 106 |
-- a found key will be returned as is, not with default |
| 107 |
JSON.fromObjWithDefault [(random_key, J.showJSON def_value)] |
| 108 |
random_key (def_value+1) == Just def_value |
| 109 |
|
| 110 |
-- | Test that functional if' behaves like the syntactic sugar if. |
| 111 |
prop_if'if :: Bool -> Int -> Int -> Property |
| 112 |
prop_if'if cnd a b = |
| 113 |
if' cnd a b ==? if cnd then a else b |
| 114 |
|
| 115 |
-- | Test basic select functionality |
| 116 |
prop_select :: Int -- ^ Default result |
| 117 |
-> [Int] -- ^ List of False values |
| 118 |
-> [Int] -- ^ List of True values |
| 119 |
-> Property -- ^ Test result |
| 120 |
prop_select def lst1 lst2 = |
| 121 |
select def (flist ++ tlist) ==? expectedresult |
| 122 |
where expectedresult = defaultHead def lst2 |
| 123 |
flist = zip (repeat False) lst1 |
| 124 |
tlist = zip (repeat True) lst2 |
| 125 |
|
| 126 |
-- | Test basic select functionality with undefined default |
| 127 |
prop_select_undefd_plain |
| 128 |
:: [Int] -- ^ List of False values |
| 129 |
-> NonEmpty Int -- ^ List of True values |
| 130 |
-> Property -- ^ Test result |
| 131 |
prop_select_undefd_plain lst1 lst2 = |
| 132 |
select undefined (flist ++ tlist) ==? NonEmpty.head lst2 |
| 133 |
where flist = zip (repeat False) lst1 |
| 134 |
tlist = zip (repeat True) (NonEmpty.toList lst2) |
| 135 |
|
| 136 |
prop_select_undefd :: [Int] -> Property |
| 137 |
prop_select_undefd lst1 = forAll genNonEmpty $ prop_select_undefd_plain lst1 |
| 138 |
|
| 139 |
-- | Test basic select functionality with undefined list values |
| 140 |
prop_select_undefv_plain |
| 141 |
:: [Int] -- ^ List of False values |
| 142 |
-> NonEmpty Int -- ^ List of True values |
| 143 |
-> Property -- ^ Test result |
| 144 |
prop_select_undefv_plain lst1 lst2 = |
| 145 |
select undefined cndlist ==? NonEmpty.head lst2 |
| 146 |
where flist = zip (repeat False) lst1 |
| 147 |
tlist = zip (repeat True) (NonEmpty.toList lst2) |
| 148 |
cndlist = flist ++ tlist ++ [undefined] |
| 149 |
|
| 150 |
prop_select_undefv :: [Int] -> Property |
| 151 |
prop_select_undefv lst1 = forAll genNonEmpty $ prop_select_undefv_plain lst1 |
| 152 |
|
| 153 |
prop_parseUnit :: NonNegative Int -> Property |
| 154 |
prop_parseUnit (NonNegative n) = |
| 155 |
conjoin |
| 156 |
[ parseUnit (show n) ==? (Ok n::Result Int) |
| 157 |
, parseUnit (show n ++ "m") ==? (Ok n::Result Int) |
| 158 |
, parseUnit (show n ++ "M") ==? (Ok (truncate n_mb)::Result Int) |
| 159 |
, parseUnit (show n ++ "g") ==? (Ok (n*1024)::Result Int) |
| 160 |
, parseUnit (show n ++ "G") ==? (Ok (truncate n_gb)::Result Int) |
| 161 |
, parseUnit (show n ++ "t") ==? (Ok (n*1048576)::Result Int) |
| 162 |
, parseUnit (show n ++ "T") ==? (Ok (truncate n_tb)::Result Int) |
| 163 |
, counterexample "Internal error/overflow?" |
| 164 |
(n_mb >=0 && n_gb >= 0 && n_tb >= 0) |
| 165 |
, property (isBad (parseUnit (show n ++ "x")::Result Int)) |
| 166 |
] |
| 167 |
where n_mb = (fromIntegral n::Rational) * 1000 * 1000 / 1024 / 1024 |
| 168 |
n_gb = n_mb * 1000 |
| 169 |
n_tb = n_gb * 1000 |
| 170 |
|
| 171 |
{-# ANN case_niceSort_static "HLint: ignore Use camelCase" #-} |
| 172 |
|
| 173 |
case_niceSort_static :: Assertion |
| 174 |
case_niceSort_static = do |
| 175 |
assertEqual "empty list" [] $ niceSort [] |
| 176 |
assertEqual "punctuation" [",", "."] $ niceSort [",", "."] |
| 177 |
assertEqual "decimal numbers" ["0.1", "0.2"] $ niceSort ["0.1", "0.2"] |
| 178 |
assertEqual "various numbers" ["0,099", "0.1", "0.2", "0;099"] $ |
| 179 |
niceSort ["0;099", "0,099", "0.1", "0.2"] |
| 180 |
|
| 181 |
assertEqual "simple concat" ["0000", "a0", "a1", "a2", "a20", "a99", |
| 182 |
"b00", "b10", "b70"] $ |
| 183 |
niceSort ["a0", "a1", "a99", "a20", "a2", "b10", "b70", "b00", "0000"] |
| 184 |
|
| 185 |
assertEqual "ranges" ["A", "Z", "a0-0", "a0-4", "a1-0", "a9-1", "a09-2", |
| 186 |
"a20-3", "a99-3", "a99-10", "b"] $ |
| 187 |
niceSort ["a0-0", "a1-0", "a99-10", "a20-3", "a0-4", "a99-3", "a09-2", |
| 188 |
"Z", "a9-1", "A", "b"] |
| 189 |
|
| 190 |
assertEqual "large" |
| 191 |
["3jTwJPtrXOY22bwL2YoW", "Eegah9ei", "KOt7vn1dWXi", |
| 192 |
"KVQqLPDjcPjf8T3oyzjcOsfkb", "WvNJd91OoXvLzdEiEXa6", |
| 193 |
"Z8Ljf1Pf5eBfNg171wJR", "a07h8feON165N67PIE", "bH4Q7aCu3PUPjK3JtH", |
| 194 |
"cPRi0lM7HLnSuWA2G9", "guKJkXnkULealVC8CyF1xefym", |
| 195 |
"pqF8dkU5B1cMnyZuREaSOADYx", "uHXAyYYftCSG1o7qcCqe", |
| 196 |
"xij88brTulHYAv8IEOyU", "xpIUJeVT1Rp"] $ |
| 197 |
niceSort ["Eegah9ei", "xij88brTulHYAv8IEOyU", "3jTwJPtrXOY22bwL2YoW", |
| 198 |
"Z8Ljf1Pf5eBfNg171wJR", "WvNJd91OoXvLzdEiEXa6", |
| 199 |
"uHXAyYYftCSG1o7qcCqe", "xpIUJeVT1Rp", "KOt7vn1dWXi", |
| 200 |
"a07h8feON165N67PIE", "bH4Q7aCu3PUPjK3JtH", |
| 201 |
"cPRi0lM7HLnSuWA2G9", "KVQqLPDjcPjf8T3oyzjcOsfkb", |
| 202 |
"guKJkXnkULealVC8CyF1xefym", "pqF8dkU5B1cMnyZuREaSOADYx"] |
| 203 |
|
| 204 |
assertEqual "hostnames" |
| 205 |
["host1.example.com", "host2.example.com", "host03.example.com", |
| 206 |
"host11.example.com", "host255.example.com"] $ |
| 207 |
niceSort ["host2.example.com", "host11.example.com", "host03.example.com", |
| 208 |
"host1.example.com", "host255.example.com"] |
| 209 |
|
| 210 |
-- | Tests single-string behaviour of 'niceSort'. |
| 211 |
prop_niceSort_single :: Property |
| 212 |
prop_niceSort_single = |
| 213 |
forAll genName $ \name -> |
| 214 |
conjoin |
| 215 |
[ counterexample "single string" $ [name] ==? niceSort [name] |
| 216 |
, counterexample "single plus empty" $ ["", name] ==? niceSort [name, ""] |
| 217 |
] |
| 218 |
|
| 219 |
-- | Tests some generic 'niceSort' properties. Note that the last test |
| 220 |
-- must add a non-digit prefix; a digit one might change ordering. |
| 221 |
prop_niceSort_generic :: Property |
| 222 |
prop_niceSort_generic = |
| 223 |
forAll (resize 20 arbitrary) $ \names -> |
| 224 |
let n_sorted = niceSort names in |
| 225 |
conjoin [ counterexample "length" $ length names ==? length n_sorted |
| 226 |
, counterexample "same strings" $ sort names ==? sort n_sorted |
| 227 |
, counterexample "idempotence" $ n_sorted ==? niceSort n_sorted |
| 228 |
, counterexample "static prefix" $ n_sorted ==? |
| 229 |
map tail (niceSort $ map (" "++) names) |
| 230 |
] |
| 231 |
|
| 232 |
-- | Tests that niceSorting numbers is identical to actual sorting |
| 233 |
-- them (in numeric form). |
| 234 |
prop_niceSort_numbers :: Property |
| 235 |
prop_niceSort_numbers = |
| 236 |
forAll (listOf (arbitrary::Gen (NonNegative Int))) $ \numbers -> |
| 237 |
map show (sort numbers) ==? niceSort (map show numbers) |
| 238 |
|
| 239 |
-- | Tests that 'niceSort' and 'niceSortKey' are equivalent. |
| 240 |
prop_niceSortKey_equiv :: Property |
| 241 |
prop_niceSortKey_equiv = |
| 242 |
forAll (resize 20 arbitrary) $ \names -> |
| 243 |
forAll (vectorOf (length names) (arbitrary::Gen Int)) $ \numbers -> |
| 244 |
let n_sorted = niceSort names in |
| 245 |
conjoin |
| 246 |
[ counterexample "key id" $ n_sorted ==? niceSortKey id names |
| 247 |
, counterexample "key rev" $ niceSort (map reverse names) ==? |
| 248 |
map reverse (niceSortKey reverse names) |
| 249 |
, counterexample "key snd" $ n_sorted ==? map snd (niceSortKey snd $ |
| 250 |
zip numbers names) |
| 251 |
] |
| 252 |
|
| 253 |
-- | Tests 'rStripSpace'. |
| 254 |
prop_rStripSpace :: NonEmptyList Char -> Property |
| 255 |
prop_rStripSpace (NonEmpty str) = |
| 256 |
forAll (resize 50 $ listOf1 (arbitrary `suchThat` isSpace)) $ \whitespace -> |
| 257 |
conjoin [ counterexample "arb. string last char is not space" $ |
| 258 |
case rStripSpace str of |
| 259 |
[] -> True |
| 260 |
xs -> not . isSpace $ last xs |
| 261 |
, counterexample "whitespace suffix is stripped" $ |
| 262 |
rStripSpace str ==? rStripSpace (str ++ whitespace) |
| 263 |
, counterexample "whitespace reduced to null" $ |
| 264 |
rStripSpace whitespace ==? "" |
| 265 |
, counterexample "idempotent on empty strings" $ |
| 266 |
rStripSpace "" ==? "" |
| 267 |
] |
| 268 |
|
| 269 |
-- | Tests that the newUUID function produces valid UUIDs. |
| 270 |
case_new_uuid :: Assertion |
| 271 |
case_new_uuid = do |
| 272 |
uuid <- newUUID |
| 273 |
assertBool "newUUID" $ isUUID uuid |
| 274 |
|
| 275 |
{-# ANN case_new_uuid_regex "HLint: ignore Use camelCase" #-} |
| 276 |
|
| 277 |
-- | Tests that the newUUID function produces valid UUIDs. |
| 278 |
case_new_uuid_regex :: Assertion |
| 279 |
case_new_uuid_regex = do |
| 280 |
uuid <- newUUID |
| 281 |
assertBool "newUUID" $ uuid =~ C.uuidRegex |
| 282 |
|
| 283 |
|
| 284 |
-- | Test normal operation for 'chompPrefix'. |
| 285 |
-- |
| 286 |
-- Any random prefix of a string must be stripped correctly, including the empty |
| 287 |
-- prefix, and the whole string. |
| 288 |
prop_chompPrefix_normal :: String -> Property |
| 289 |
prop_chompPrefix_normal str = |
| 290 |
forAll (choose (0, length str)) $ \size -> |
| 291 |
chompPrefix (take size str) str ==? (Just $ drop size str) |
| 292 |
|
| 293 |
-- | Test that 'chompPrefix' correctly allows the last char (the separator) to |
| 294 |
-- be absent if the string terminates there. |
| 295 |
prop_chompPrefix_last :: Property |
| 296 |
prop_chompPrefix_last = |
| 297 |
forAll (choose (1, 20)) $ \len -> |
| 298 |
forAll (vectorOf len arbitrary) $ \pfx -> |
| 299 |
chompPrefix pfx pfx ==? Just "" .&&. |
| 300 |
chompPrefix pfx (init pfx) ==? Just "" |
| 301 |
|
| 302 |
-- | Test that chompPrefix on the empty string always returns Nothing for |
| 303 |
-- prefixes of length 2 or more. |
| 304 |
prop_chompPrefix_empty_string :: Property |
| 305 |
prop_chompPrefix_empty_string = |
| 306 |
forAll (choose (2, 20)) $ \len -> |
| 307 |
forAll (vectorOf len arbitrary) $ \pfx -> |
| 308 |
chompPrefix pfx "" ==? Nothing |
| 309 |
|
| 310 |
-- | Test 'chompPrefix' returns Nothing when the prefix doesn't match. |
| 311 |
prop_chompPrefix_nothing :: Property |
| 312 |
prop_chompPrefix_nothing = |
| 313 |
forAll (choose (1, 20)) $ \len -> |
| 314 |
forAll (vectorOf len arbitrary) $ \pfx -> |
| 315 |
forAll (arbitrary `suchThat` |
| 316 |
(\s -> not (pfx `isPrefixOf` s) && s /= init pfx)) $ \str -> |
| 317 |
chompPrefix pfx str ==? Nothing |
| 318 |
|
| 319 |
-- | Tests 'trim'. |
| 320 |
prop_trim :: NonEmptyList Char -> Property |
| 321 |
prop_trim (NonEmpty str) = |
| 322 |
forAll (listOf1 $ elements " \t\n\r\f") $ \whitespace -> |
| 323 |
forAll (choose (0, length whitespace)) $ \n -> |
| 324 |
let (preWS, postWS) = splitAt n whitespace in |
| 325 |
conjoin [ counterexample "arb. string first and last char are not space" $ |
| 326 |
case trim str of |
| 327 |
[] -> True |
| 328 |
xs -> (not . isSpace . head) xs && (not . isSpace . last) xs |
| 329 |
, counterexample "whitespace is striped" $ |
| 330 |
trim str ==? trim (preWS ++ str ++ postWS) |
| 331 |
, counterexample "whitespace reduced to null" $ |
| 332 |
trim whitespace ==? "" |
| 333 |
, counterexample "idempotent on empty strings" $ |
| 334 |
trim "" ==? "" |
| 335 |
] |
| 336 |
|
| 337 |
-- | Tests 'splitEithers' and 'recombineEithers'. |
| 338 |
prop_splitRecombineEithers :: [Either Int Int] -> Property |
| 339 |
prop_splitRecombineEithers es = |
| 340 |
conjoin |
| 341 |
[ counterexample "only lefts are mapped correctly" $ |
| 342 |
splitEithers (map Left lefts) ==? (reverse lefts, emptylist, falses) |
| 343 |
, counterexample "only rights are mapped correctly" $ |
| 344 |
splitEithers (map Right rights) ==? (emptylist, reverse rights, trues) |
| 345 |
, counterexample "recombination is no-op" $ |
| 346 |
recombineEithers splitleft splitright trail ==? Ok es |
| 347 |
, counterexample "fail on too long lefts" $ |
| 348 |
isBad (recombineEithers (0:splitleft) splitright trail) |
| 349 |
, counterexample "fail on too long rights" $ |
| 350 |
isBad (recombineEithers splitleft (0:splitright) trail) |
| 351 |
, counterexample "fail on too long trail" $ |
| 352 |
isBad (recombineEithers splitleft splitright (True:trail)) |
| 353 |
] |
| 354 |
where (lefts, rights) = Either.partitionEithers es |
| 355 |
falses = map (const False) lefts |
| 356 |
trues = map (const True) rights |
| 357 |
(splitleft, splitright, trail) = splitEithers es |
| 358 |
emptylist = []::[Int] |
| 359 |
|
| 360 |
testSuite "Utils" |
| 361 |
[ 'prop_commaJoinSplit |
| 362 |
, 'prop_commaSplitJoin |
| 363 |
, 'prop_findFirst |
| 364 |
, 'prop_fromObjWithDefault |
| 365 |
, 'prop_if'if |
| 366 |
, 'prop_select |
| 367 |
, 'prop_select_undefd |
| 368 |
, 'prop_select_undefv |
| 369 |
, 'prop_parseUnit |
| 370 |
, 'case_niceSort_static |
| 371 |
, 'prop_niceSort_single |
| 372 |
, 'prop_niceSort_generic |
| 373 |
, 'prop_niceSort_numbers |
| 374 |
, 'prop_niceSortKey_equiv |
| 375 |
, 'prop_rStripSpace |
| 376 |
, 'prop_trim |
| 377 |
, 'case_new_uuid |
| 378 |
, 'case_new_uuid_regex |
| 379 |
, 'prop_chompPrefix_normal |
| 380 |
, 'prop_chompPrefix_last |
| 381 |
, 'prop_chompPrefix_empty_string |
| 382 |
, 'prop_chompPrefix_nothing |
| 383 |
, 'prop_splitRecombineEithers |
| 384 |
] |