Statistics
| Branch: | Tag: | Revision:

root / htools / test.hs @ e1dde6ad

History | View | Annotate | Download (5.1 kB)

1
{-| Unittest runner for ganeti-htools.
2

    
3
-}
4

    
5
{-
6

    
7
Copyright (C) 2009, 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 Main(main) where
27

    
28
import Data.Char
29
import Data.IORef
30
import Data.List
31
import System.Console.GetOpt ()
32
import System.Environment (getArgs)
33
import System.Exit
34
import System.IO
35
import Test.QuickCheck
36
import Text.Printf
37

    
38
import Ganeti.HTools.QC
39
import Ganeti.HTools.CLI
40
import Ganeti.HTools.Utils (sepSplit)
41

    
42
-- | Options list and functions.
43
options :: [OptType]
44
options =
45
  [ oReplay
46
  , oVerbose
47
  , oShowVer
48
  , oShowHelp
49
  ]
50

    
51
fast :: Args
52
fast = stdArgs
53
       { maxSuccess = 500
54
       , chatty     = False
55
       }
56

    
57
slow :: Args
58
slow = stdArgs
59
       { maxSuccess = 50
60
       , chatty     = False
61
       }
62

    
63
incIORef :: IORef Int -> IO ()
64
incIORef ir = atomicModifyIORef ir (\x -> (x + 1, ()))
65

    
66
-- | Wrapper over a test runner with error counting.
67
wrapTest :: IORef Int
68
         -> (Args -> IO Result, String)
69
         -> Args
70
         -> IO (Result, Char, String)
71
wrapTest ir (test, desc) opts = do
72
  r <- test opts
73
  c <- case r of
74
         Success {} -> return '.'
75
         GaveUp  {} -> return '?'
76
         Failure {} -> incIORef ir >> return '#'
77
         NoExpectedFailure {} -> incIORef ir >> return '*'
78
  return (r, c, desc)
79

    
80
runTests :: String
81
         -> Args
82
         -> [Args -> IO (Result, Char, String)]
83
         -> Int
84
         -> IO [(Result, String)]
85

    
86
runTests name opts tests max_count = do
87
  _ <- printf "%25s : " name
88
  hFlush stdout
89
  results <- mapM (\t -> do
90
                     (r, c, desc) <- t opts
91
                     putChar c
92
                     hFlush stdout
93
                     return (r, desc)
94
                  ) tests
95
  let alldone = sum . map (numTests . fst) $ results
96
  _ <- printf "%*s(%d)\n" (max_count - length tests + 1) " " alldone
97
  mapM_ (\(r, desc) ->
98
             case r of
99
               Failure { output = o, usedSeed = u, usedSize = size } ->
100
                   printf "Test %s failed (seed was %s, test size %d): %s\n"
101
                          desc (show u) size o
102
               GaveUp { numTests = passed } ->
103
                   printf "Test %s incomplete: gave up with only %d\
104
                          \ passes after discarding %d tests\n"
105
                          desc passed (maxDiscard opts)
106
               _ -> return ()
107
        ) results
108
  return results
109

    
110
allTests :: [(Args, (String, [(Args -> IO Result, String)]))]
111
allTests =
112
  [ (fast, testUtils)
113
  , (fast, testPeerMap)
114
  , (fast, testContainer)
115
  , (fast, testInstance)
116
  , (fast, testNode)
117
  , (fast, testText)
118
  , (fast, testSimu)
119
  , (fast, testOpCodes)
120
  , (fast, testJobs)
121
  , (fast, testLoader)
122
  , (fast, testTypes)
123
  , (slow, testCluster)
124
  ]
125

    
126
-- | Extracts the name of a test group.
127
extractName :: (Args, (String, [(Args -> IO Result, String)])) -> String
128
extractName (_, (name, _)) = name
129

    
130
-- | Lowercase a string.
131
lower :: String -> String
132
lower = map toLower
133

    
134
transformTestOpts :: Args -> Options -> IO Args
135
transformTestOpts args opts = do
136
  r <- case optReplay opts of
137
         Nothing -> return Nothing
138
         Just str -> do
139
           let vs = sepSplit ',' str
140
           case vs of
141
             [rng, size] -> return $ Just (read rng, read size)
142
             _ -> fail "Invalid state given"
143
  return args { chatty = optVerbose opts > 1,
144
                replay = r
145
              }
146

    
147
main :: IO ()
148
main = do
149
  errs <- newIORef 0
150
  let wrap = map (wrapTest errs)
151
  cmd_args <- getArgs
152
  (opts, args) <- parseOpts cmd_args "test" options
153
  tests <- if null args
154
             then return allTests
155
             else let args' = map lower args
156
                      selected = filter ((`elem` args') . lower .
157
                                         extractName) allTests
158
                  in if null selected
159
                       then do
160
                         hPutStrLn stderr $ "No tests matching '"
161
                            ++ unwords args ++ "', available tests: "
162
                            ++ intercalate ", " (map extractName allTests)
163
                         exitWith $ ExitFailure 1
164
                       else return selected
165

    
166
  let max_count = maximum $ map (\(_, (_, t)) -> length t) tests
167
  mapM_ (\(targs, (name, tl)) ->
168
           transformTestOpts targs opts >>= \newargs ->
169
           runTests name newargs (wrap tl) max_count) tests
170
  terr <- readIORef errs
171
  if terr > 0
172
    then do
173
      hPutStrLn stderr $ "A total of " ++ show terr ++ " tests failed."
174
      exitWith $ ExitFailure 1
175
    else putStrLn "All tests succeeded."