Statistics
| Branch: | Tag: | Revision:

root / test / hs / Test / Ganeti / HTools / CLI.hs @ 914c6df4

History | View | Annotate | Download (4.6 kB)

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 Google Inc.
11

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

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

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

    
27
-}
28

    
29
module Test.Ganeti.HTools.CLI (testHTools_CLI) where
30

    
31
import Test.HUnit
32
import Test.QuickCheck
33

    
34
import Control.Monad
35
import Data.List
36
import Text.Printf (printf)
37

    
38
import Test.Ganeti.TestHelper
39
import Test.Ganeti.TestCommon
40
import Test.Ganeti.Common
41

    
42
import Ganeti.BasicTypes
43
import Ganeti.HTools.CLI as CLI
44
import qualified Ganeti.HTools.Program.Main as Program
45
import qualified Ganeti.HTools.Types as Types
46

    
47
{-# ANN module "HLint: ignore Use camelCase" #-}
48

    
49
-- | Test correct parsing.
50
prop_parseISpec :: String -> Int -> Int -> Int -> Maybe Int -> Property
51
prop_parseISpec descr dsk mem cpu spn =
52
  let (str, spn') = case spn of
53
                      Nothing -> (printf "%d,%d,%d" dsk mem cpu::String, 1)
54
                      Just spn'' ->
55
                        (printf "%d,%d,%d,%d" dsk mem cpu spn''::String, spn'')
56
  in parseISpecString descr str ==? Ok (Types.RSpec cpu mem dsk spn')
57

    
58
-- | Test parsing failure due to wrong section count.
59
prop_parseISpecFail :: String -> Property
60
prop_parseISpecFail descr =
61
  forAll (choose (0,100) `suchThat` (not . flip elem [3, 4])) $ \nelems ->
62
  forAll (replicateM nelems arbitrary) $ \values ->
63
  let str = intercalate "," $ map show (values::[Int])
64
  in case parseISpecString descr str of
65
       Ok v -> failTest $ "Expected failure, got " ++ show v
66
       _ -> passTest
67

    
68
-- | Test a few string arguments.
69
prop_string_arg :: String -> Property
70
prop_string_arg argument =
71
  let args = [ (oDataFile,      optDataFile)
72
             , (oDynuFile,      optDynuFile)
73
             , (oSaveCluster,   optSaveCluster)
74
             , (oPrintCommands, optShowCmds)
75
             , (genOLuxiSocket "", optLuxi)
76
             , (oIAllocSrc,     optIAllocSrc)
77
             ]
78
  in conjoin $ map (\(o, opt) ->
79
                      checkOpt Just defaultOptions
80
                      failTest (const (==?)) Just (argument, o, opt)) args
81

    
82
-- | Test a few positive arguments.
83
prop_numeric_arg :: Positive Double -> Property
84
prop_numeric_arg (Positive argument) =
85
  let args = [ (oMaxCpu,     optMcpu)
86
             , (oMinDisk,    Just . optMdsk)
87
             , (oMinGain,    Just . optMinGain)
88
             , (oMinGainLim, Just . optMinGainLim)
89
             , (oMinScore,   Just . optMinScore)
90
             ]
91
  in conjoin $
92
     map (\(x, y) -> checkOpt (Just . show) defaultOptions
93
                     failTest (const (==?)) Just (argument, x, y)) args
94

    
95
-- | Test a few boolean arguments.
96
case_bool_arg :: Assertion
97
case_bool_arg =
98
  mapM_ (checkOpt (const Nothing) defaultOptions assertFailure
99
                  assertEqual id)
100
        [ (False, oDiskMoves,    optDiskMoves)
101
        , (False, oInstMoves,    optInstMoves)
102
        , (True,  oEvacMode,     optEvacMode)
103
        , (True,  oExecJobs,     optExecJobs)
104
        , (True,  oNoHeaders,    optNoHeaders)
105
        , (True,  oNoSimulation, optNoSimulation)
106
        ]
107

    
108
-- | Tests a few invalid arguments.
109
case_wrong_arg :: Assertion
110
case_wrong_arg =
111
  mapM_ (passFailOpt defaultOptions assertFailure (return ()))
112
        [ (oSpindleUse,   "-1", "1")
113
        , (oSpindleUse,   "a",  "1")
114
        , (oMaxCpu,       "-1", "1")
115
        , (oMinDisk,      "a",  "1")
116
        , (oMinGainLim,   "a",  "1")
117
        , (oMaxSolLength, "x",  "10")
118
        , (oStdSpec,      "no-such-spec", "1,1,1")
119
        , (oTieredSpec,   "no-such-spec", "1,1,1")
120
        ]
121

    
122
-- | Test that all binaries support some common options.
123
case_stdopts :: Assertion
124
case_stdopts =
125
  mapM_ (\(name, (_, o, a, _)) -> do
126
           o' <- o
127
           checkEarlyExit defaultOptions name
128
             (o' ++ genericOpts) a) Program.personalities
129

    
130
testSuite "HTools/CLI"
131
          [ 'prop_parseISpec
132
          , 'prop_parseISpecFail
133
          , 'prop_string_arg
134
          , 'prop_numeric_arg
135
          , 'case_bool_arg
136
          , 'case_wrong_arg
137
          , 'case_stdopts
138
          ]