Statistics
| Branch: | Tag: | Revision:

root / htools / test.hs @ 8e4f6d56

History | View | Annotate | Download (3.4 kB)

1
{-| Unittest runner for ganeti-htools
2

    
3
-}
4

    
5
{-
6

    
7
Copyright (C) 2009, 2011 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.IORef
29
import Test.QuickCheck
30
import System.IO
31
import System.Exit
32
import System (getArgs)
33
import Text.Printf
34

    
35
import Ganeti.HTools.QC
36

    
37
fast :: Args
38
fast = stdArgs
39
       { maxSuccess = 500
40
       , chatty     = False
41
       }
42

    
43
slow :: Args
44
slow = stdArgs
45
       { maxSuccess = 50
46
       , chatty     = False
47
       }
48

    
49
incIORef :: IORef Int -> IO ()
50
incIORef ir = atomicModifyIORef ir (\x -> (x + 1, ()))
51

    
52
-- | Wrapper over a test runner with error counting
53
wrapTest :: IORef Int
54
         -> (Args -> IO Result)
55
         -> Args
56
         -> IO (Result, Char)
57
wrapTest ir test opts = do
58
  r <- test opts
59
  c <- case r of
60
         Success {} -> return '.'
61
         GaveUp  {} -> return '?'
62
         Failure {} -> incIORef ir >> return '#'
63
         NoExpectedFailure {} -> incIORef ir >> return '*'
64
  return (r, c)
65

    
66
runTests name opts tests max_count = do
67
  _ <- printf "%25s : " name
68
  hFlush stdout
69
  results <- mapM (\t -> do
70
                     (r, c) <- t opts
71
                     putChar c
72
                     hFlush stdout
73
                     return r
74
                  ) tests
75
  let alldone = sum . map numTests $ results
76
  _ <- printf "%*s(%d)\n" (max_count - length tests + 1) " " alldone
77
  mapM_ (\(idx, r) ->
78
             case r of
79
               Failure { output = o, usedSeed = u, usedSize = size } ->
80
                   printf "Test %d failed (seed was %s, test size %d): %s\n"
81
                          idx (show u) size o
82
               GaveUp { numTests = passed } ->
83
                   printf "Test %d incomplete: gave up with only %d\
84
                          \ passes after discarding %d tests\n"
85
                          idx passed (maxDiscard opts)
86
               _ -> return ()
87
        ) $ zip ([1..]::[Int]) results
88
  return results
89

    
90
allTests :: [(String, Args, [Args -> IO Result])]
91
allTests =
92
  [ ("Utils", fast, testUtils)
93
  , ("PeerMap", fast, testPeerMap)
94
  , ("Container", fast, testContainer)
95
  , ("Instance", fast, testInstance)
96
  , ("Node", fast, testNode)
97
  , ("Text", fast, testText)
98
  , ("OpCodes", fast, testOpCodes)
99
  , ("Jobs", fast, testJobs)
100
  , ("Loader", fast, testLoader)
101
  , ("Cluster", slow, testCluster)
102
  ]
103

    
104
main :: IO ()
105
main = do
106
  errs <- newIORef 0
107
  let wrap = map (wrapTest errs)
108
  args <- getArgs
109
  let tests = if null args
110
              then allTests
111
              else filter (\(name, _, _) -> name `elem` args) allTests
112
      max_count = maximum $ map (\(_, _, t) -> length t) tests
113
  mapM_ (\(name, opts, tl) -> runTests name opts (wrap tl) max_count) tests
114
  terr <- readIORef errs
115
  (if terr > 0
116
   then do
117
     hPutStrLn stderr $ "A total of " ++ show terr ++ " tests failed."
118
     exitWith $ ExitFailure 1
119
   else putStrLn "All tests succeeded.")