Debian Codegraph @

test/hs/Test/Ganeti/Common.hs

raw
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3
4 {-| Unittests for the 'Ganeti.Common' module.
5
6 -}
7
8 {-
9
10 Copyright (C) 2009, 2010, 2011, 2012, 2013 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.Common
39 ( testCommon
40 , checkOpt
41 , passFailOpt
42 , checkEarlyExit
43 ) where
44
45 import Test.QuickCheck hiding (Result)
46 import Test.HUnit
47
48 import qualified System.Console.GetOpt as GetOpt
49 import System.Exit
50
51 import Test.Ganeti.TestHelper
52 import Test.Ganeti.TestCommon
53
54 import Ganeti.BasicTypes
55 import Ganeti.Common
56 import Ganeti.HTools.Program.Main (personalities)
57
58 {-# ANN module "HLint: ignore Use camelCase" #-}
59
60 -- | Helper to check for correct parsing of an option.
61 checkOpt :: (StandardOptions b) =>
62 (a -> Maybe String) -- ^ Converts the value into a cmdline form
63 -> b -- ^ The default options
64 -> (String -> c) -- ^ Fail test function
65 -> (String -> d -> d -> c) -- ^ Check for equality function
66 -> (a -> d) -- ^ Transforms the value to a compare val
67 -> (a, GenericOptType b, b -> d) -- ^ Triple of value, the
68 -- option, function to
69 -- extract the set value
70 -- from the options
71 -> c
72 checkOpt repr defaults failfn eqcheck valfn
73 (val, opt@(GetOpt.Option _ longs _ _, _), fn) =
74 case longs of
75 [] -> failfn "no long options?"
76 cmdarg:_ ->
77 case parseOptsInner defaults
78 ["--" ++ cmdarg ++ maybe "" ("=" ++) (repr val)]
79 "prog" [opt] [] of
80 Left e -> failfn $ "Failed to parse option '" ++ cmdarg ++ ": " ++
81 show e
82 Right (options, _) -> eqcheck ("Wrong value in option " ++
83 cmdarg ++ "?") (valfn val) (fn options)
84
85 -- | Helper to check for correct and incorrect parsing of an option.
86 passFailOpt :: (StandardOptions b) =>
87 b -- ^ The default options
88 -> (String -> c) -- ^ Fail test function
89 -> c -- ^ Pass function
90 -> (GenericOptType b, String, String)
91 -- ^ The list of enabled options, fail value and pass value
92 -> c
93 passFailOpt defaults failfn passfn
94 (opt@(GetOpt.Option _ longs _ _, _), bad, good) =
95 let first_opt = case longs of
96 [] -> error "no long options?"
97 x:_ -> x
98 prefix = "--" ++ first_opt ++ "="
99 good_cmd = prefix ++ good
100 bad_cmd = prefix ++ bad in
101 case (parseOptsInner defaults [bad_cmd] "prog" [opt] [],
102 parseOptsInner defaults [good_cmd] "prog" [opt] []) of
103 (Left _, Right _) -> passfn
104 (Right _, Right _) -> failfn $ "Command line '" ++ bad_cmd ++
105 "' succeeded when it shouldn't"
106 (Left _, Left _) -> failfn $ "Command line '" ++ good_cmd ++
107 "' failed when it shouldn't"
108 (Right _, Left _) ->
109 failfn $ "Command line '" ++ bad_cmd ++
110 "' succeeded when it shouldn't, while command line '" ++
111 good_cmd ++ "' failed when it shouldn't"
112
113 -- | Helper to test that a given option is accepted OK with quick exit.
114 checkEarlyExit :: (StandardOptions a) =>
115 a -> String -> [GenericOptType a] -> [ArgCompletion]
116 -> Assertion
117 checkEarlyExit defaults name options arguments =
118 mapM_ (\param ->
119 case parseOptsInner defaults [param] name options arguments of
120 Left (code, _) ->
121 assertEqual ("Program " ++ name ++
122 " returns invalid code " ++ show code ++
123 " for option " ++ param) ExitSuccess code
124 _ -> assertFailure $ "Program " ++ name ++
125 " doesn't consider option " ++
126 param ++ " as early exit one"
127 ) ["-h", "--help", "-V", "--version"]
128
129 -- | Test parseYesNo.
130 prop_parse_yes_no :: Bool -> Bool -> String -> Property
131 prop_parse_yes_no def testval val =
132 forAll (elements [val, "yes", "no"]) $ \actual_val ->
133 if testval
134 then parseYesNo def Nothing ==? Ok def
135 else let result = parseYesNo def (Just actual_val)
136 in if actual_val `elem` ["yes", "no"]
137 then result ==? Ok (actual_val == "yes")
138 else property $ isBad result
139
140 -- | Check that formatCmdUsage works similar to Python _FormatUsage.
141 case_formatCommands :: Assertion
142 case_formatCommands =
143 assertEqual "proper wrap for HTools Main"
144 resCmdTest (formatCommands personalities)
145 where resCmdTest :: [String]
146 resCmdTest =
147 [ " hail - Ganeti IAllocator plugin that implements the instance\
148 \ placement and"
149 , " movement using the same algorithm as hbal(1)"
150 , " harep - auto-repair tool that detects certain kind of problems\
151 \ with"
152 , " instances and applies the allowed set of solutions"
153 , " hbal - cluster balancer that looks at the current state of\
154 \ the cluster and"
155 , " computes a series of steps designed to bring the\
156 \ cluster into a"
157 , " better state"
158 , " hcheck - cluster checker; prints information about cluster's\
159 \ health and"
160 , " checks whether a rebalance done using hbal would help"
161 , " hinfo - cluster information printer; it prints information\
162 \ about the current"
163 , " cluster state and its residing nodes/instances"
164 , " hroller - cluster rolling maintenance helper; it helps\
165 \ scheduling node reboots"
166 , " in a manner that doesn't conflict with the instances'\
167 \ topology"
168 , " hscan - tool for scanning clusters via RAPI and saving their\
169 \ data in the"
170 , " input format used by hbal(1) and hspace(1)"
171 , " hspace - computes how many additional instances can be fit on a\
172 \ cluster,"
173 , " while maintaining N+1 status."
174 , " hsqueeze - cluster dynamic power management; it powers up and\
175 \ down nodes to"
176 , " keep the amount of free online resources in a given\
177 \ range"
178 ]
179
180 testSuite "Common"
181 [ 'prop_parse_yes_no
182 , 'case_formatCommands
183 ]