root / test / hs / Test / Ganeti / HTools / Container.hs @ c324da14
History | View | Annotate | Download (3.2 kB)
1 | e1ee7d5a | Iustin Pop | {-# LANGUAGE TemplateHaskell #-} |
---|---|---|---|
2 | e1ee7d5a | Iustin Pop | {-# OPTIONS_GHC -fno-warn-orphans #-} |
3 | e1ee7d5a | Iustin Pop | |
4 | e1ee7d5a | Iustin Pop | {-| Unittests for ganeti-htools. |
5 | e1ee7d5a | Iustin Pop | |
6 | e1ee7d5a | Iustin Pop | -} |
7 | e1ee7d5a | Iustin Pop | |
8 | e1ee7d5a | Iustin Pop | {- |
9 | e1ee7d5a | Iustin Pop | |
10 | 72747d91 | Iustin Pop | Copyright (C) 2009, 2010, 2011, 2012, 2013 Google Inc. |
11 | e1ee7d5a | Iustin Pop | |
12 | e1ee7d5a | Iustin Pop | This program is free software; you can redistribute it and/or modify |
13 | e1ee7d5a | Iustin Pop | it under the terms of the GNU General Public License as published by |
14 | e1ee7d5a | Iustin Pop | the Free Software Foundation; either version 2 of the License, or |
15 | e1ee7d5a | Iustin Pop | (at your option) any later version. |
16 | e1ee7d5a | Iustin Pop | |
17 | e1ee7d5a | Iustin Pop | This program is distributed in the hope that it will be useful, but |
18 | e1ee7d5a | Iustin Pop | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | e1ee7d5a | Iustin Pop | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
20 | e1ee7d5a | Iustin Pop | General Public License for more details. |
21 | e1ee7d5a | Iustin Pop | |
22 | e1ee7d5a | Iustin Pop | You should have received a copy of the GNU General Public License |
23 | e1ee7d5a | Iustin Pop | along with this program; if not, write to the Free Software |
24 | e1ee7d5a | Iustin Pop | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
25 | e1ee7d5a | Iustin Pop | 02110-1301, USA. |
26 | e1ee7d5a | Iustin Pop | |
27 | e1ee7d5a | Iustin Pop | -} |
28 | e1ee7d5a | Iustin Pop | |
29 | e09c1fa0 | Iustin Pop | module Test.Ganeti.HTools.Container (testHTools_Container) where |
30 | e1ee7d5a | Iustin Pop | |
31 | e1ee7d5a | Iustin Pop | import Test.QuickCheck |
32 | e1ee7d5a | Iustin Pop | |
33 | e1ee7d5a | Iustin Pop | import Data.Maybe |
34 | e1ee7d5a | Iustin Pop | |
35 | e1ee7d5a | Iustin Pop | import Test.Ganeti.TestHelper |
36 | e1ee7d5a | Iustin Pop | import Test.Ganeti.TestCommon |
37 | e1ee7d5a | Iustin Pop | import Test.Ganeti.TestHTools |
38 | e1ee7d5a | Iustin Pop | import Test.Ganeti.HTools.Node (genNode) |
39 | e1ee7d5a | Iustin Pop | |
40 | e1ee7d5a | Iustin Pop | import qualified Ganeti.HTools.Container as Container |
41 | e1ee7d5a | Iustin Pop | import qualified Ganeti.HTools.Node as Node |
42 | e1ee7d5a | Iustin Pop | |
43 | e1ee7d5a | Iustin Pop | -- we silence the following due to hlint bug fixed in later versions |
44 | 20bc5360 | Iustin Pop | {-# ANN prop_addTwo "HLint: ignore Avoid lambda" #-} |
45 | 20bc5360 | Iustin Pop | prop_addTwo :: [Container.Key] -> Int -> Int -> Bool |
46 | 20bc5360 | Iustin Pop | prop_addTwo cdata i1 i2 = |
47 | e1ee7d5a | Iustin Pop | fn i1 i2 cont == fn i2 i1 cont && |
48 | e1ee7d5a | Iustin Pop | fn i1 i2 cont == fn i1 i2 (fn i1 i2 cont) |
49 | e1ee7d5a | Iustin Pop | where cont = foldl (\c x -> Container.add x x c) Container.empty cdata |
50 | e1ee7d5a | Iustin Pop | fn x1 x2 = Container.addTwo x1 x1 x2 x2 |
51 | e1ee7d5a | Iustin Pop | |
52 | 20bc5360 | Iustin Pop | prop_nameOf :: Node.Node -> Property |
53 | 20bc5360 | Iustin Pop | prop_nameOf node = |
54 | e1ee7d5a | Iustin Pop | let nl = makeSmallCluster node 1 |
55 | 72747d91 | Iustin Pop | in case Container.elems nl of |
56 | 72747d91 | Iustin Pop | [] -> failTest "makeSmallCluster 1 returned empty cluster?" |
57 | 72747d91 | Iustin Pop | _:_:_ -> failTest "makeSmallCluster 1 returned >1 node?" |
58 | 72747d91 | Iustin Pop | fnode:_ -> Container.nameOf nl (Node.idx fnode) ==? Node.name fnode |
59 | e1ee7d5a | Iustin Pop | |
60 | e1ee7d5a | Iustin Pop | -- | We test that in a cluster, given a random node, we can find it by |
61 | e1ee7d5a | Iustin Pop | -- its name and alias, as long as all names and aliases are unique, |
62 | e1ee7d5a | Iustin Pop | -- and that we fail to find a non-existing name. |
63 | 20bc5360 | Iustin Pop | prop_findByName :: Property |
64 | 20bc5360 | Iustin Pop | prop_findByName = |
65 | e1ee7d5a | Iustin Pop | forAll (genNode (Just 1) Nothing) $ \node -> |
66 | e1ee7d5a | Iustin Pop | forAll (choose (1, 20)) $ \ cnt -> |
67 | e1ee7d5a | Iustin Pop | forAll (choose (0, cnt - 1)) $ \ fidx -> |
68 | df8578fb | Iustin Pop | forAll (genUniquesList (cnt * 2) arbitrary) $ \ allnames -> |
69 | e1ee7d5a | Iustin Pop | forAll (arbitrary `suchThat` (`notElem` allnames)) $ \ othername -> |
70 | e1ee7d5a | Iustin Pop | let names = zip (take cnt allnames) (drop cnt allnames) |
71 | e1ee7d5a | Iustin Pop | nl = makeSmallCluster node cnt |
72 | e1ee7d5a | Iustin Pop | nodes = Container.elems nl |
73 | e1ee7d5a | Iustin Pop | nodes' = map (\((name, alias), nn) -> (Node.idx nn, |
74 | e1ee7d5a | Iustin Pop | nn { Node.name = name, |
75 | e1ee7d5a | Iustin Pop | Node.alias = alias })) |
76 | e1ee7d5a | Iustin Pop | $ zip names nodes |
77 | e1ee7d5a | Iustin Pop | nl' = Container.fromList nodes' |
78 | e1ee7d5a | Iustin Pop | target = snd (nodes' !! fidx) |
79 | 942a9a6a | Iustin Pop | in conjoin |
80 | 942a9a6a | Iustin Pop | [ Container.findByName nl' (Node.name target) ==? Just target |
81 | 942a9a6a | Iustin Pop | , Container.findByName nl' (Node.alias target) ==? Just target |
82 | 942a9a6a | Iustin Pop | , printTestCase "Found non-existing name" |
83 | 942a9a6a | Iustin Pop | (isNothing (Container.findByName nl' othername)) |
84 | 942a9a6a | Iustin Pop | ] |
85 | e1ee7d5a | Iustin Pop | |
86 | e09c1fa0 | Iustin Pop | testSuite "HTools/Container" |
87 | 20bc5360 | Iustin Pop | [ 'prop_addTwo |
88 | 20bc5360 | Iustin Pop | , 'prop_nameOf |
89 | 20bc5360 | Iustin Pop | , 'prop_findByName |
90 | e1ee7d5a | Iustin Pop | ] |