Debian Codegraph @

test/hs/Test/Ganeti/HTools/CLI.hs

raw
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 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.CLI (testHTools_CLI) where
39
40 import Test.HUnit
41 import Test.QuickCheck
42
43 import Control.Monad
44 import Data.List
45 import Text.Printf (printf)
46
47 import Test.Ganeti.TestHelper
48 import Test.Ganeti.TestCommon
49 import Test.Ganeti.Common
50
51 import Ganeti.BasicTypes
52 import Ganeti.HTools.CLI as CLI
53 import qualified Ganeti.HTools.Program.Main as Program
54 import qualified Ganeti.HTools.Types as Types
55
56 {-# ANN module "HLint: ignore Use camelCase" #-}
57
58 -- | Test correct parsing.
59 prop_parseISpec :: String -> Int -> Int -> Int -> Maybe Int -> Property
60 prop_parseISpec descr dsk mem cpu spn =
61 let (str, spn') = case spn of
62 Nothing -> (printf "%d,%d,%d" dsk mem cpu::String, 1)
63 Just spn'' ->
64 (printf "%d,%d,%d,%d" dsk mem cpu spn''::String, spn'')
65 in parseISpecString descr str ==? Ok (Types.RSpec cpu mem dsk spn')
66
67 -- | Test parsing failure due to wrong section count.
68 prop_parseISpecFail :: String -> Property
69 prop_parseISpecFail descr =
70 forAll (choose (0,100) `suchThat` (not . flip elem [3, 4])) $ \nelems ->
71 forAll (replicateM nelems arbitrary) $ \values ->
72 let str = intercalate "," $ map show (values::[Int])
73 in case parseISpecString descr str of
74 Ok v -> failTest $ "Expected failure, got " ++ show v
75 _ -> passTest
76
77 -- | Test a few string arguments.
78 prop_string_arg :: String -> Property
79 prop_string_arg argument =
80 let args = [ (oDataFile, optDataFile)
81 , (oDynuFile, optDynuFile)
82 , (oSaveCluster, optSaveCluster)
83 , (oPrintCommands, optShowCmds)
84 , (genOLuxiSocket "", optLuxi)
85 , (oIAllocSrc, optIAllocSrc)
86 ]
87 in conjoin $ map (\(o, opt) ->
88 checkOpt Just defaultOptions
89 failTest (const (==?)) Just (argument, o, opt)) args
90
91 -- | Test a few positive arguments.
92 prop_numeric_arg :: Positive Double -> Property
93 prop_numeric_arg (Positive argument) =
94 let args = [ (oMaxCpu, optMcpu)
95 , (oMinDisk, Just . optMdsk)
96 , (oMinGain, Just . optMinGain)
97 , (oMinGainLim, Just . optMinGainLim)
98 , (oMinScore, Just . optMinScore)
99 ]
100 in conjoin $
101 map (\(x, y) -> checkOpt (Just . show) defaultOptions
102 failTest (const (==?)) Just (argument, x, y)) args
103
104 -- | Test a few boolean arguments.
105 case_bool_arg :: Assertion
106 case_bool_arg =
107 mapM_ (checkOpt (const Nothing) defaultOptions assertFailure
108 assertEqual id)
109 [ (False, oDiskMoves, optDiskMoves)
110 , (False, oInstMoves, optInstMoves)
111 , (True, oEvacMode, optEvacMode)
112 , (True, oExecJobs, optExecJobs)
113 , (True, oNoHeaders, optNoHeaders)
114 , (True, oNoSimulation, optNoSimulation)
115 ]
116
117 -- | Tests a few invalid arguments.
118 case_wrong_arg :: Assertion
119 case_wrong_arg =
120 mapM_ (passFailOpt defaultOptions assertFailure (return ()))
121 [ (oSpindleUse, "-1", "1")
122 , (oSpindleUse, "a", "1")
123 , (oMaxCpu, "-1", "1")
124 , (oMinDisk, "a", "1")
125 , (oMinGainLim, "a", "1")
126 , (oMaxSolLength, "x", "10")
127 , (oStdSpec, "no-such-spec", "1,1,1")
128 , (oTieredSpec, "no-such-spec", "1,1,1")
129 ]
130
131 -- | Test that all binaries support some common options.
132 case_stdopts :: Assertion
133 case_stdopts =
134 mapM_ (\(name, (_, o, a, _)) -> do
135 o' <- o
136 checkEarlyExit defaultOptions name
137 (o' ++ genericOpts) a) Program.personalities
138
139 testSuite "HTools/CLI"
140 [ 'prop_parseISpec
141 , 'prop_parseISpecFail
142 , 'prop_string_arg
143 , 'prop_numeric_arg
144 , 'case_bool_arg
145 , 'case_wrong_arg
146 , 'case_stdopts
147 ]