Text.hs 7.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

{-| Unittests for ganeti-htools.

-}

{-

Copyright (C) 2009, 2010, 2011, 2012 Google Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

-}

Iustin Pop's avatar
Iustin Pop committed
29
module Test.Ganeti.HTools.Text (testHTools_Text) where
30
31
32
33
34
35
36
37
38
39
40
41
42

import Test.QuickCheck

import qualified Data.Map as Map
import Data.List
import Data.Maybe

import Test.Ganeti.TestHelper
import Test.Ganeti.TestCommon
import Test.Ganeti.TestHTools
import Test.Ganeti.HTools.Instance (genInstanceSmallerThanNode)
import Test.Ganeti.HTools.Node (genNode, genOnlineNode)

43
import Ganeti.BasicTypes
44
45
46
47
48
49
50
51
import qualified Ganeti.HTools.Cluster as Cluster
import qualified Ganeti.HTools.Container as Container
import qualified Ganeti.HTools.Group as Group
import qualified Ganeti.HTools.Instance as Instance
import qualified Ganeti.HTools.Loader as Loader
import qualified Ganeti.HTools.Node as Node
import qualified Ganeti.HTools.Text as Text
import qualified Ganeti.HTools.Types as Types
52
import qualified Ganeti.Utils as Utils
53
54
55

-- * Instance text loader tests

56
prop_Load_Instance :: String -> Int -> Int -> Int -> Types.InstanceStatus
Iustin Pop's avatar
Iustin Pop committed
57
                   -> NonEmptyList Char -> String
58
59
60
61
62
                   -> NonNegative Int -> NonNegative Int -> Bool
                   -> Types.DiskTemplate -> Int -> Property
prop_Load_Instance name mem dsk vcpus status
                   (NonEmpty pnode) snode
                   (NonNegative pdx) (NonNegative sdx) autobal dt su =
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  pnode /= snode && pdx /= sdx ==>
  let vcpus_s = show vcpus
      dsk_s = show dsk
      mem_s = show mem
      su_s = show su
      status_s = Types.instanceStatusToRaw status
      ndx = if null snode
              then [(pnode, pdx)]
              else [(pnode, pdx), (snode, sdx)]
      nl = Map.fromList ndx
      tags = ""
      sbal = if autobal then "Y" else "N"
      sdt = Types.diskTemplateToRaw dt
      inst = Text.loadInst nl
             [name, mem_s, dsk_s, vcpus_s, status_s,
              sbal, pnode, snode, sdt, tags, su_s]
      fail1 = Text.loadInst nl
              [name, mem_s, dsk_s, vcpus_s, status_s,
               sbal, pnode, pnode, tags]
  in case inst of
83
84
85
       Bad msg -> failTest $ "Failed to load instance: " ++ msg
       Ok (_, i) -> printTestCase "Mismatch in some field while\
                                  \ loading the instance" $
86
87
88
89
90
91
92
93
94
               Instance.name i == name &&
               Instance.vcpus i == vcpus &&
               Instance.mem i == mem &&
               Instance.pNode i == pdx &&
               Instance.sNode i == (if null snode
                                      then Node.noSecondary
                                      else sdx) &&
               Instance.autoBalance i == autobal &&
               Instance.spindleUse i == su &&
95
               isBad fail1
96

97
98
prop_Load_InstanceFail :: [(String, Int)] -> [String] -> Property
prop_Load_InstanceFail ktn fields =
99
100
  length fields /= 10 && length fields /= 11 ==>
    case Text.loadInst nl fields of
101
102
103
      Ok _ -> failTest "Managed to load instance from invalid data"
      Bad msg -> printTestCase ("Unrecognised error message: " ++ msg) $
                 "Invalid/incomplete instance data: '" `isPrefixOf` msg
104
105
    where nl = Map.fromList ktn

106
107
108
prop_Load_Node :: String -> Int -> Int -> Int -> Int -> Int
               -> Int -> Bool -> Bool
prop_Load_Node name tm nm fm td fd tc fo =
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  let conv v = if v < 0
                 then "?"
                 else show v
      tm_s = conv tm
      nm_s = conv nm
      fm_s = conv fm
      td_s = conv td
      fd_s = conv fd
      tc_s = conv tc
      fo_s = if fo
               then "Y"
               else "N"
      any_broken = any (< 0) [tm, nm, fm, td, fd, tc]
      gid = Group.uuid defGroup
  in case Text.loadNode defGroupAssoc
       [name, tm_s, nm_s, fm_s, td_s, fd_s, tc_s, fo_s, gid] of
       Nothing -> False
       Just (name', node) ->
         if fo || any_broken
           then Node.offline node
           else Node.name node == name' && name' == name &&
                Node.alias node == name &&
                Node.tMem node == fromIntegral tm &&
                Node.nMem node == nm &&
                Node.fMem node == fm &&
                Node.tDsk node == fromIntegral td &&
                Node.fDsk node == fd &&
                Node.tCpu node == fromIntegral tc

138
139
prop_Load_NodeFail :: [String] -> Property
prop_Load_NodeFail fields =
140
141
  length fields /= 8 ==> isNothing $ Text.loadNode Map.empty fields

142
143
prop_NodeLSIdempotent :: Property
prop_NodeLSIdempotent =
144
145
146
147
148
149
150
151
152
  forAll (genNode (Just 1) Nothing) $ \node ->
  -- override failN1 to what loadNode returns by default
  let n = Node.setPolicy Types.defIPolicy $
          node { Node.failN1 = True, Node.offline = False }
  in
    (Text.loadNode defGroupAssoc.
         Utils.sepSplit '|' . Text.serializeNode defGroupList) n ==?
    Just (Node.name n, n)

153
154
prop_ISpecIdempotent :: Types.ISpec -> Property
prop_ISpecIdempotent ispec =
155
156
  case Text.loadISpec "dummy" . Utils.sepSplit ',' .
       Text.serializeISpec $ ispec of
157
158
    Bad msg -> failTest $ "Failed to load ispec: " ++ msg
    Ok ispec' -> ispec ==? ispec'
159

160
161
prop_IPolicyIdempotent :: Types.IPolicy -> Property
prop_IPolicyIdempotent ipol =
162
163
  case Text.loadIPolicy . Utils.sepSplit '|' $
       Text.serializeIPolicy owner ipol of
164
165
    Bad msg -> failTest $ "Failed to load ispec: " ++ msg
    Ok res -> (owner, ipol) ==? res
166
167
168
169
  where owner = "dummy"

-- | This property, while being in the text tests, does more than just
-- test end-to-end the serialisation and loading back workflow; it
170
-- also tests the Loader.mergeData and the actual
171
172
173
174
-- Cluster.iterateAlloc (for well-behaving w.r.t. instance
-- allocations, not for the business logic). As such, it's a quite
-- complex and slow test, and that's the reason we restrict it to
-- small cluster sizes.
175
176
prop_CreateSerialise :: Property
prop_CreateSerialise =
177
178
179
180
181
182
183
184
185
186
  forAll genTags $ \ctags ->
  forAll (choose (1, 20)) $ \maxiter ->
  forAll (choose (2, 10)) $ \count ->
  forAll genOnlineNode $ \node ->
  forAll (genInstanceSmallerThanNode node) $ \inst ->
  let nl = makeSmallCluster node count
      reqnodes = Instance.requiredNodes $ Instance.diskTemplate inst
  in case Cluster.genAllocNodes defGroupList nl reqnodes True >>= \allocn ->
     Cluster.iterateAlloc nl Container.empty (Just maxiter) inst allocn [] []
     of
187
188
189
190
       Bad msg -> failTest $ "Failed to allocate: " ++ msg
       Ok (_, _, _, [], _) -> printTestCase
                              "Failed to allocate: no allocations" False
       Ok (_, nl', il', _, _) ->
191
192
193
194
         let cdata = Loader.ClusterData defGroupList nl' il' ctags
                     Types.defIPolicy
             saved = Text.serializeCluster cdata
         in case Text.parseData saved >>= Loader.mergeData [] [] [] [] of
195
196
              Bad msg -> failTest $ "Failed to load/merge: " ++ msg
              Ok (Loader.ClusterData gl2 nl2 il2 ctags2 cpol2) ->
197
198
199
200
201
202
                ctags ==? ctags2 .&&.
                Types.defIPolicy ==? cpol2 .&&.
                il' ==? il2 .&&.
                defGroupList ==? gl2 .&&.
                nl' ==? nl2

Iustin Pop's avatar
Iustin Pop committed
203
testSuite "HTools/Text"
204
205
206
207
208
209
210
211
            [ 'prop_Load_Instance
            , 'prop_Load_InstanceFail
            , 'prop_Load_Node
            , 'prop_Load_NodeFail
            , 'prop_NodeLSIdempotent
            , 'prop_ISpecIdempotent
            , 'prop_IPolicyIdempotent
            , 'prop_CreateSerialise
212
            ]