root / htest / Test / Ganeti / TestCommon.hs @ 61899e64
History | View | Annotate | Download (6.4 kB)
1 |
{-| Unittest helpers for ganeti-htools. |
---|---|
2 |
|
3 |
-} |
4 |
|
5 |
{- |
6 |
|
7 |
Copyright (C) 2009, 2010, 2011, 2012 Google Inc. |
8 |
|
9 |
This program is free software; you can redistribute it and/or modify |
10 |
it under the terms of the GNU General Public License as published by |
11 |
the Free Software Foundation; either version 2 of the License, or |
12 |
(at your option) any later version. |
13 |
|
14 |
This program is distributed in the hope that it will be useful, but |
15 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 |
General Public License for more details. |
18 |
|
19 |
You should have received a copy of the GNU General Public License |
20 |
along with this program; if not, write to the Free Software |
21 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
22 |
02110-1301, USA. |
23 |
|
24 |
-} |
25 |
|
26 |
module Test.Ganeti.TestCommon where |
27 |
|
28 |
import Control.Applicative |
29 |
import Control.Exception (catchJust) |
30 |
import Control.Monad |
31 |
import Data.List |
32 |
import qualified Test.HUnit as HUnit |
33 |
import Test.QuickCheck |
34 |
import Test.QuickCheck.Monadic |
35 |
import qualified Text.JSON as J |
36 |
import System.Environment (getEnv) |
37 |
import System.Exit (ExitCode(..)) |
38 |
import System.IO.Error (isDoesNotExistError) |
39 |
import System.Process (readProcessWithExitCode) |
40 |
|
41 |
import qualified Ganeti.BasicTypes as BasicTypes |
42 |
|
43 |
-- * Constants |
44 |
|
45 |
-- | Maximum memory (1TiB, somewhat random value). |
46 |
maxMem :: Int |
47 |
maxMem = 1024 * 1024 |
48 |
|
49 |
-- | Maximum disk (8TiB, somewhat random value). |
50 |
maxDsk :: Int |
51 |
maxDsk = 1024 * 1024 * 8 |
52 |
|
53 |
-- | Max CPUs (1024, somewhat random value). |
54 |
maxCpu :: Int |
55 |
maxCpu = 1024 |
56 |
|
57 |
-- | Max vcpu ratio (random value). |
58 |
maxVcpuRatio :: Double |
59 |
maxVcpuRatio = 1024.0 |
60 |
|
61 |
-- | Max spindle ratio (random value). |
62 |
maxSpindleRatio :: Double |
63 |
maxSpindleRatio = 1024.0 |
64 |
|
65 |
-- | Max nodes, used just to limit arbitrary instances for smaller |
66 |
-- opcode definitions (e.g. list of nodes in OpTestDelay). |
67 |
maxNodes :: Int |
68 |
maxNodes = 32 |
69 |
|
70 |
-- | Max opcodes or jobs in a submit job and submit many jobs. |
71 |
maxOpCodes :: Int |
72 |
maxOpCodes = 16 |
73 |
|
74 |
-- * Helper functions |
75 |
|
76 |
-- | Checks for equality with proper annotation. The first argument is |
77 |
-- the computed value, the second one the expected value. |
78 |
(==?) :: (Show a, Eq a) => a -> a -> Property |
79 |
(==?) x y = printTestCase |
80 |
("Expected equality, but got mismatch\nexpected: " ++ |
81 |
show y ++ "\n but got: " ++ show x) (x == y) |
82 |
infix 3 ==? |
83 |
|
84 |
-- | Checks for inequality with proper annotation. The first argument |
85 |
-- is the computed value, the second one the expected (not equal) |
86 |
-- value. |
87 |
(/=?) :: (Show a, Eq a) => a -> a -> Property |
88 |
(/=?) x y = printTestCase |
89 |
("Expected inequality, but got equality: '" ++ |
90 |
show x ++ "'.") (x /= y) |
91 |
infix 3 /=? |
92 |
|
93 |
-- | Show a message and fail the test. |
94 |
failTest :: String -> Property |
95 |
failTest msg = printTestCase msg False |
96 |
|
97 |
-- | A 'True' property. |
98 |
passTest :: Property |
99 |
passTest = property True |
100 |
|
101 |
-- | Return the python binary to use. If the PYTHON environment |
102 |
-- variable is defined, use its value, otherwise use just \"python\". |
103 |
pythonCmd :: IO String |
104 |
pythonCmd = catchJust (guard . isDoesNotExistError) |
105 |
(getEnv "PYTHON") (const (return "python")) |
106 |
|
107 |
-- | Run Python with an expression, returning the exit code, standard |
108 |
-- output and error. |
109 |
runPython :: String -> String -> IO (ExitCode, String, String) |
110 |
runPython expr stdin = do |
111 |
py_binary <- pythonCmd |
112 |
readProcessWithExitCode py_binary ["-c", expr] stdin |
113 |
|
114 |
-- | Check python exit code, and fail via HUnit assertions if |
115 |
-- non-zero. Otherwise, return the standard output. |
116 |
checkPythonResult :: (ExitCode, String, String) -> IO String |
117 |
checkPythonResult (py_code, py_stdout, py_stderr) = do |
118 |
HUnit.assertEqual ("python exited with error: " ++ py_stderr) |
119 |
ExitSuccess py_code |
120 |
return py_stdout |
121 |
|
122 |
-- * Arbitrary instances |
123 |
|
124 |
-- | Defines a DNS name. |
125 |
newtype DNSChar = DNSChar { dnsGetChar::Char } |
126 |
|
127 |
instance Arbitrary DNSChar where |
128 |
arbitrary = do |
129 |
x <- elements (['a'..'z'] ++ ['0'..'9'] ++ "_-") |
130 |
return (DNSChar x) |
131 |
|
132 |
instance Show DNSChar where |
133 |
show = show . dnsGetChar |
134 |
|
135 |
-- | Generates a single name component. |
136 |
getName :: Gen String |
137 |
getName = do |
138 |
n <- choose (1, 64) |
139 |
dn <- vector n |
140 |
return (map dnsGetChar dn) |
141 |
|
142 |
-- | Generates an entire FQDN. |
143 |
getFQDN :: Gen String |
144 |
getFQDN = do |
145 |
ncomps <- choose (1, 4) |
146 |
names <- vectorOf ncomps getName |
147 |
return $ intercalate "." names |
148 |
|
149 |
-- | Combinator that generates a 'Maybe' using a sub-combinator. |
150 |
getMaybe :: Gen a -> Gen (Maybe a) |
151 |
getMaybe subgen = do |
152 |
bool <- arbitrary |
153 |
if bool |
154 |
then Just <$> subgen |
155 |
else return Nothing |
156 |
|
157 |
-- | Defines a tag type. |
158 |
newtype TagChar = TagChar { tagGetChar :: Char } |
159 |
|
160 |
-- | All valid tag chars. This doesn't need to match _exactly_ |
161 |
-- Ganeti's own tag regex, just enough for it to be close. |
162 |
tagChar :: String |
163 |
tagChar = ['a'..'z'] ++ ['A'..'Z'] ++ ['0'..'9'] ++ ".+*/:@-" |
164 |
|
165 |
instance Arbitrary TagChar where |
166 |
arbitrary = do |
167 |
c <- elements tagChar |
168 |
return (TagChar c) |
169 |
|
170 |
-- | Generates a tag |
171 |
genTag :: Gen [TagChar] |
172 |
genTag = do |
173 |
-- the correct value would be C.maxTagLen, but that's way too |
174 |
-- verbose in unittests, and at the moment I don't see any possible |
175 |
-- bugs with longer tags and the way we use tags in htools |
176 |
n <- choose (1, 10) |
177 |
vector n |
178 |
|
179 |
-- | Generates a list of tags (correctly upper bounded). |
180 |
genTags :: Gen [String] |
181 |
genTags = do |
182 |
-- the correct value would be C.maxTagsPerObj, but per the comment |
183 |
-- in genTag, we don't use tags enough in htools to warrant testing |
184 |
-- such big values |
185 |
n <- choose (0, 10::Int) |
186 |
tags <- mapM (const genTag) [1..n] |
187 |
return $ map (map tagGetChar) tags |
188 |
|
189 |
-- | Generates a fields list. This uses the same character set as a |
190 |
-- DNS name (just for simplicity). |
191 |
getFields :: Gen [String] |
192 |
getFields = do |
193 |
n <- choose (1, 32) |
194 |
vectorOf n getName |
195 |
|
196 |
-- | Generates a list of a given size with non-duplicate elements. |
197 |
genUniquesList :: (Eq a, Arbitrary a) => Int -> Gen [a] |
198 |
genUniquesList cnt = |
199 |
foldM (\lst _ -> do |
200 |
newelem <- arbitrary `suchThat` (`notElem` lst) |
201 |
return (newelem:lst)) [] [1..cnt] |
202 |
|
203 |
newtype SmallRatio = SmallRatio Double deriving Show |
204 |
instance Arbitrary SmallRatio where |
205 |
arbitrary = do |
206 |
v <- choose (0, 1) |
207 |
return $ SmallRatio v |
208 |
|
209 |
-- | Checks for serialisation idempotence. |
210 |
testSerialisation :: (Eq a, Show a, J.JSON a) => a -> Property |
211 |
testSerialisation a = |
212 |
case J.readJSON (J.showJSON a) of |
213 |
J.Error msg -> failTest $ "Failed to deserialise: " ++ msg |
214 |
J.Ok a' -> a ==? a' |
215 |
|
216 |
-- | Result to PropertyM IO. |
217 |
resultProp :: (Show a) => BasicTypes.GenericResult a b -> PropertyM IO b |
218 |
resultProp (BasicTypes.Bad err) = stop . failTest $ show err |
219 |
resultProp (BasicTypes.Ok val) = return val |