| 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.Container (testHTools_Container) where |
| 39 |
|
| 40 |
import Test.QuickCheck |
| 41 |
|
| 42 |
import Data.Maybe |
| 43 |
|
| 44 |
import Test.Ganeti.TestHelper |
| 45 |
import Test.Ganeti.TestCommon |
| 46 |
import Test.Ganeti.TestHTools |
| 47 |
import Test.Ganeti.HTools.Node (genNode) |
| 48 |
|
| 49 |
import qualified Ganeti.HTools.Container as Container |
| 50 |
import qualified Ganeti.HTools.Node as Node |
| 51 |
|
| 52 |
-- we silence the following due to hlint bug fixed in later versions |
| 53 |
{-# ANN prop_addTwo "HLint: ignore Avoid lambda" #-} |
| 54 |
prop_addTwo :: [Container.Key] -> Int -> Int -> Bool |
| 55 |
prop_addTwo cdata i1 i2 = |
| 56 |
fn i1 i2 cont == fn i2 i1 cont && |
| 57 |
fn i1 i2 cont == fn i1 i2 (fn i1 i2 cont) |
| 58 |
where cont = foldl (\c x -> Container.add x x c) Container.empty cdata |
| 59 |
fn x1 x2 = Container.addTwo x1 x1 x2 x2 |
| 60 |
|
| 61 |
prop_nameOf :: Node.Node -> Property |
| 62 |
prop_nameOf node = |
| 63 |
let nl = makeSmallCluster node 1 |
| 64 |
in case Container.elems nl of |
| 65 |
[] -> failTest "makeSmallCluster 1 returned empty cluster?" |
| 66 |
_:_:_ -> failTest "makeSmallCluster 1 returned >1 node?" |
| 67 |
fnode:_ -> Container.nameOf nl (Node.idx fnode) ==? Node.name fnode |
| 68 |
|
| 69 |
-- | We test that in a cluster, given a random node, we can find it by |
| 70 |
-- its name and alias, as long as all names and aliases are unique, |
| 71 |
-- and that we fail to find a non-existing name. |
| 72 |
prop_findByName :: Property |
| 73 |
prop_findByName = |
| 74 |
forAll (genNode (Just 1) Nothing) $ \node -> |
| 75 |
forAll (choose (1, 20)) $ \ cnt -> |
| 76 |
forAll (choose (0, cnt - 1)) $ \ fidx -> |
| 77 |
forAll (genUniquesList (cnt * 2) arbitrary) $ \ allnames -> |
| 78 |
forAll (arbitrary `suchThat` (`notElem` allnames)) $ \ othername -> |
| 79 |
let names = zip (take cnt allnames) (drop cnt allnames) |
| 80 |
nl = makeSmallCluster node cnt |
| 81 |
nodes = Container.elems nl |
| 82 |
nodes' = map (\((name, alias), nn) -> (Node.idx nn, |
| 83 |
nn { Node.name = name, |
| 84 |
Node.alias = alias })) |
| 85 |
$ zip names nodes |
| 86 |
nl' = Container.fromList nodes' |
| 87 |
target = snd (nodes' !! fidx) |
| 88 |
in conjoin |
| 89 |
[ Container.findByName nl' (Node.name target) ==? Just target |
| 90 |
, Container.findByName nl' (Node.alias target) ==? Just target |
| 91 |
, counterexample "Found non-existing name" |
| 92 |
(isNothing (Container.findByName nl' othername)) |
| 93 |
] |
| 94 |
|
| 95 |
testSuite "HTools/Container" |
| 96 |
[ 'prop_addTwo |
| 97 |
, 'prop_nameOf |
| 98 |
, 'prop_findByName |
| 99 |
] |