TestHTools.hs 5.45 KB
Newer Older
1
2
3
4
5
6
7
8
{-# OPTIONS_GHC -fno-warn-orphans #-}

{-| Common functionality for htools-related unittests.

-}

{-

9
Copyright (C) 2009, 2010, 2011, 2012, 2013 Google Inc.
Klaus Aehlig's avatar
Klaus Aehlig committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35
36

-}

37
38
module Test.Ganeti.TestHTools
  ( nullIPolicy
Klaus Aehlig's avatar
Klaus Aehlig committed
39
  , nullISpec
40
41
42
43
44
45
46
  , defGroup
  , defGroupList
  , defGroupAssoc
  , createInstance
  , makeSmallCluster
  , setInstanceSmallerThanNode
  ) where
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

import qualified Data.Map as Map

import Test.Ganeti.TestCommon

import qualified Ganeti.Constants as C
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.Types as Types

-- * Helpers

Klaus Aehlig's avatar
Klaus Aehlig committed
62
63
64
65
66
67
68
69
70
71
-- | An ISpec with 0 resources.
nullISpec :: Types.ISpec
nullISpec = Types.ISpec { Types.iSpecMemorySize = 0
                        , Types.iSpecCpuCount   = 0
                        , Types.iSpecDiskSize   = 0
                        , Types.iSpecDiskCount  = 0
                        , Types.iSpecNicCount   = 0
                        , Types.iSpecSpindleUse = 0
                        }

72
73
74
-- | Null iPolicy, and by null we mean very liberal.
nullIPolicy :: Types.IPolicy
nullIPolicy = Types.IPolicy
75
  { Types.iPolicyMinMaxISpecs = [Types.MinMaxISpecs
Klaus Aehlig's avatar
Klaus Aehlig committed
76
    { Types.minMaxISpecsMinSpec = nullISpec
77
78
79
80
81
82
83
84
    , Types.minMaxISpecsMaxSpec = Types.ISpec
      { Types.iSpecMemorySize = maxBound
      , Types.iSpecCpuCount   = maxBound
      , Types.iSpecDiskSize   = maxBound
      , Types.iSpecDiskCount  = C.maxDisks
      , Types.iSpecNicCount   = C.maxNics
      , Types.iSpecSpindleUse = maxBound
      }
85
    }]
86
87
88
89
90
91
92
93
94
95
96
97
98
  , Types.iPolicyStdSpec = Types.ISpec { Types.iSpecMemorySize = Types.unitMem
                                       , Types.iSpecCpuCount   = Types.unitCpu
                                       , Types.iSpecDiskSize   = Types.unitDsk
                                       , Types.iSpecDiskCount  = 1
                                       , Types.iSpecNicCount   = 1
                                       , Types.iSpecSpindleUse = 1
                                       }
  , Types.iPolicyDiskTemplates = [minBound..maxBound]
  , Types.iPolicyVcpuRatio = maxVcpuRatio -- somewhat random value, high
                                          -- enough to not impact us
  , Types.iPolicySpindleRatio = maxSpindleRatio
  }

99
-- | Default group definition.
100
101
102
defGroup :: Group.Group
defGroup = flip Group.setIdx 0 $
             Group.create "default" Types.defaultGroupID Types.AllocPreferred
Thomas Thrainer's avatar
Thomas Thrainer committed
103
                  [] nullIPolicy []
104

105
-- | Default group, as a (singleton) 'Group.List'.
106
107
108
defGroupList :: Group.List
defGroupList = Container.fromList [(Group.idx defGroup, defGroup)]

109
-- | Default group, as a string map.
110
111
112
113
114
115
defGroupAssoc :: Map.Map String Types.Gdx
defGroupAssoc = Map.singleton (Group.uuid defGroup) (Group.idx defGroup)

-- | Create an instance given its spec.
createInstance :: Int -> Int -> Int -> Instance.Instance
createInstance mem dsk vcpus =
116
117
  Instance.create "inst-unnamed" mem dsk [Instance.Disk dsk Nothing] vcpus
    Types.Running [] True (-1) (-1) Types.DTDrbd8 1 []
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

-- | Create a small cluster by repeating a node spec.
makeSmallCluster :: Node.Node -> Int -> Node.List
makeSmallCluster node count =
  let origname = Node.name node
      origalias = Node.alias node
      nodes = map (\idx -> node { Node.name = origname ++ "-" ++ show idx
                                , Node.alias = origalias ++ "-" ++ show idx })
              [1..count]
      fn = flip Node.buildPeers Container.empty
      namelst = map (\n -> (Node.name n, fn n)) nodes
      (_, nlst) = Loader.assignIndices namelst
  in nlst

-- | Update an instance to be smaller than a node.
setInstanceSmallerThanNode :: Node.Node
                           -> Instance.Instance -> Instance.Instance
setInstanceSmallerThanNode node inst =
136
137
138
139
140
141
142
143
144
  let new_dsk = Node.availDisk node `div` 2
  in inst { Instance.mem = Node.availMem node `div` 2
          , Instance.dsk = new_dsk
          , Instance.vcpus = Node.availCpu node `div` 2
          , Instance.disks = [Instance.Disk new_dsk
                              (if Node.exclStorage node
                               then Just $ Node.fSpindles node `div` 2
                               else Nothing)]
          }