| 1 |
{-# LANGUAGE TemplateHaskell #-} |
| 2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 3 |
|
| 4 |
{-| Unittests for @xl list --long@ parser -} |
| 5 |
|
| 6 |
{- |
| 7 |
|
| 8 |
Copyright (C) 2013 Google Inc. |
| 9 |
All rights reserved. |
| 10 |
|
| 11 |
Redistribution and use in source and binary forms, with or without |
| 12 |
modification, are permitted provided that the following conditions are |
| 13 |
met: |
| 14 |
|
| 15 |
1. Redistributions of source code must retain the above copyright notice, |
| 16 |
this list of conditions and the following disclaimer. |
| 17 |
|
| 18 |
2. Redistributions in binary form must reproduce the above copyright |
| 19 |
notice, this list of conditions and the following disclaimer in the |
| 20 |
documentation and/or other materials provided with the distribution. |
| 21 |
|
| 22 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 23 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 24 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 25 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 26 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 27 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 28 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 29 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 30 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 31 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 32 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
|
| 34 |
-} |
| 35 |
|
| 36 |
module Test.Ganeti.Hypervisor.Xen.XlParser |
| 37 |
( testHypervisor_Xen_XlParser |
| 38 |
) where |
| 39 |
|
| 40 |
import Test.HUnit |
| 41 |
import Test.QuickCheck as QuickCheck hiding (Result) |
| 42 |
|
| 43 |
import Test.Ganeti.TestHelper |
| 44 |
import Test.Ganeti.TestCommon |
| 45 |
|
| 46 |
import Control.Monad (liftM) |
| 47 |
import qualified Data.Attoparsec.Text as A |
| 48 |
import Data.Text (pack) |
| 49 |
import Data.Char |
| 50 |
import qualified Data.Map as Map |
| 51 |
import Text.Printf |
| 52 |
|
| 53 |
import Ganeti.Hypervisor.Xen.Types |
| 54 |
import Ganeti.Hypervisor.Xen.XlParser |
| 55 |
|
| 56 |
{-# ANN module "HLint: ignore Use camelCase" #-} |
| 57 |
|
| 58 |
-- * Arbitraries |
| 59 |
|
| 60 |
-- | Generator for 'ListConfig'. |
| 61 |
-- |
| 62 |
-- A completely arbitrary configuration would contain too many lists |
| 63 |
-- and its size would be to big to be actually parsable in reasonable |
| 64 |
-- time. This generator builds a random Config that is still of a |
| 65 |
-- reasonable size, and it also Avoids generating strings that might |
| 66 |
-- be interpreted as numbers. |
| 67 |
genConfig :: Int -> Gen LispConfig |
| 68 |
genConfig 0 = |
| 69 |
-- only terminal values for size 0 |
| 70 |
frequency [ (5, liftM LCString (genName `suchThat` (not . canBeNumber))) |
| 71 |
, (5, liftM LCDouble arbitrary) |
| 72 |
] |
| 73 |
genConfig n = |
| 74 |
-- for size greater than 0, allow "some" lists |
| 75 |
frequency [ (5, liftM LCString (resize n genName `suchThat` |
| 76 |
(not . canBeNumber))) |
| 77 |
, (5, liftM LCDouble arbitrary) |
| 78 |
, (1, liftM LCList (choose (1, n) >>= |
| 79 |
(\n' -> vectorOf n' (genConfig $ n `div` n')))) |
| 80 |
] |
| 81 |
|
| 82 |
-- | Arbitrary instance for 'LispConfig' using 'genConfig'. |
| 83 |
instance Arbitrary LispConfig where |
| 84 |
arbitrary = sized genConfig |
| 85 |
|
| 86 |
-- | Determines conservatively whether a string could be a number. |
| 87 |
canBeNumber :: String -> Bool |
| 88 |
canBeNumber [] = False |
| 89 |
canBeNumber [c] = canBeNumberChar c |
| 90 |
canBeNumber (c:xs) = canBeNumberChar c && canBeNumber xs |
| 91 |
|
| 92 |
-- | Determines whether a char can be part of the string representation of a |
| 93 |
-- number (even in scientific notation). |
| 94 |
canBeNumberChar :: Char -> Bool |
| 95 |
canBeNumberChar c = isDigit c || (c `elem` "eE-") |
| 96 |
|
| 97 |
-- | Generates an arbitrary @xl uptime@ output line. |
| 98 |
instance Arbitrary UptimeInfo where |
| 99 |
arbitrary = do |
| 100 |
name <- genFQDN |
| 101 |
NonNegative idNum <- arbitrary :: Gen (NonNegative Int) |
| 102 |
NonNegative days <- arbitrary :: Gen (NonNegative Int) |
| 103 |
hours <- choose (0, 23) :: Gen Int |
| 104 |
mins <- choose (0, 59) :: Gen Int |
| 105 |
secs <- choose (0, 59) :: Gen Int |
| 106 |
let uptime :: String |
| 107 |
uptime = |
| 108 |
if days /= 0 |
| 109 |
then printf "%d days, %d:%d:%d" days hours mins secs |
| 110 |
else printf "%d:%d:%d" hours mins secs |
| 111 |
return $ UptimeInfo name idNum uptime |
| 112 |
|
| 113 |
-- * Helper functions for tests |
| 114 |
|
| 115 |
-- | Function for testing whether a domain configuration is parsed correctly. |
| 116 |
testDomain :: String -> Map.Map String Domain -> Assertion |
| 117 |
testDomain fileName expectedContent = do |
| 118 |
fileContent <- readTestData fileName |
| 119 |
case A.parseOnly xlListParser $ pack fileContent of |
| 120 |
Left msg -> assertFailure $ "Parsing failed: " ++ msg |
| 121 |
Right obtained -> assertEqual fileName expectedContent obtained |
| 122 |
|
| 123 |
-- | Function for testing whether a @xl uptime@ output (stored in a file) |
| 124 |
-- is parsed correctly. |
| 125 |
testUptimeInfo :: String -> Map.Map Int UptimeInfo -> Assertion |
| 126 |
testUptimeInfo fileName expectedContent = do |
| 127 |
fileContent <- readTestData fileName |
| 128 |
case A.parseOnly xlUptimeParser $ pack fileContent of |
| 129 |
Left msg -> assertFailure $ "Parsing failed: " ++ msg |
| 130 |
Right obtained -> assertEqual fileName expectedContent obtained |
| 131 |
|
| 132 |
-- | Determines whether two LispConfig are equal, with the exception of Double |
| 133 |
-- values, that just need to be \"almost equal\". |
| 134 |
-- |
| 135 |
-- Meant mainly for testing purposes, given that Double values may be slightly |
| 136 |
-- rounded during parsing. |
| 137 |
isAlmostEqual :: LispConfig -> LispConfig -> Property |
| 138 |
isAlmostEqual (LCList c1) (LCList c2) = |
| 139 |
(length c1 ==? length c2) .&&. |
| 140 |
conjoin (zipWith isAlmostEqual c1 c2) |
| 141 |
isAlmostEqual (LCString s1) (LCString s2) = s1 ==? s2 |
| 142 |
isAlmostEqual (LCDouble d1) (LCDouble d2) = counterexample msg $ rel <= 1e-12 |
| 143 |
where rel = relativeError d1 d2 |
| 144 |
msg = "Relative error " ++ show rel ++ " not smaller than 1e-12\n" ++ |
| 145 |
"expected: " ++ show d2 ++ "\n but got: " ++ show d1 |
| 146 |
isAlmostEqual a b = |
| 147 |
failTest $ "Comparing different types: '" ++ show a ++ "' with '" ++ |
| 148 |
show b ++ "'" |
| 149 |
|
| 150 |
-- | Function to serialize LispConfigs in such a way that they can be rebuilt |
| 151 |
-- again by the lispConfigParser. |
| 152 |
serializeConf :: LispConfig -> String |
| 153 |
serializeConf (LCList c) = "(" ++ unwords (map serializeConf c) ++ ")" |
| 154 |
serializeConf (LCString s) = s |
| 155 |
serializeConf (LCDouble d) = show d |
| 156 |
|
| 157 |
-- | Function to serialize UptimeInfos in such a way that they can be rebuilt |
| 158 |
-- againg by the uptimeLineParser. |
| 159 |
serializeUptime :: UptimeInfo -> String |
| 160 |
serializeUptime (UptimeInfo name idNum uptime) = |
| 161 |
printf "%s\t%d\t%s" name idNum uptime |
| 162 |
|
| 163 |
-- | Test whether a randomly generated config can be parsed. |
| 164 |
-- Implicitly, this also tests that the Show instance of Config is correct. |
| 165 |
prop_config :: LispConfig -> Property |
| 166 |
prop_config conf = |
| 167 |
case A.parseOnly lispConfigParser . pack . serializeConf $ conf of |
| 168 |
Left msg -> failTest $ "Parsing failed: " ++ msg |
| 169 |
Right obtained -> counterexample "Failing almost equal check" $ |
| 170 |
isAlmostEqual obtained conf |
| 171 |
|
| 172 |
-- | Test whether a randomly generated UptimeInfo text line can be parsed. |
| 173 |
prop_uptimeInfo :: UptimeInfo -> Property |
| 174 |
prop_uptimeInfo uInfo = |
| 175 |
case A.parseOnly uptimeLineParser . pack . serializeUptime $ uInfo of |
| 176 |
Left msg -> failTest $ "Parsing failed: " ++ msg |
| 177 |
Right obtained -> obtained ==? uInfo |
| 178 |
|
| 179 |
-- | Test a Xen 4.0.1 @xl list --long@ output. |
| 180 |
case_xen401list :: Assertion |
| 181 |
case_xen401list = testDomain "xen-xl-list-long-4.0.1.txt" $ |
| 182 |
Map.fromList |
| 183 |
[ ("Domain-0", Domain 0 "Domain-0" 184000.41332 ActualRunning Nothing) |
| 184 |
, ("instance1.example.com", Domain 119 "instance1.example.com" 24.116146647 |
| 185 |
ActualBlocked Nothing) |
| 186 |
] |
| 187 |
|
| 188 |
-- | Test a Xen 4.0.1 @xl uptime@ output. |
| 189 |
case_xen401uptime :: Assertion |
| 190 |
case_xen401uptime = testUptimeInfo "xen-xl-uptime-4.0.1.txt" $ |
| 191 |
Map.fromList |
| 192 |
[ (0, UptimeInfo "Domain-0" 0 "98 days, 2:27:44") |
| 193 |
, (119, UptimeInfo "instance1.example.com" 119 "15 days, 20:57:07") |
| 194 |
] |
| 195 |
|
| 196 |
testSuite "Hypervisor/Xen/XlParser" |
| 197 |
[ 'prop_config |
| 198 |
, 'prop_uptimeInfo |
| 199 |
, 'case_xen401list |
| 200 |
, 'case_xen401uptime |
| 201 |
] |