Commit 8b5a517a authored by Iustin Pop's avatar Iustin Pop
Browse files

Fix bug in CLI.parseISpecString and add test



'zip' finishes on the shortest list, so parseISpecString accepted any
string of 3 values or more, which is not good. Fix this and add test
for both successful and failed parsing.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarRené Nussbaumer <rn@google.com>
parent e1dde6ad
......@@ -8,7 +8,7 @@ used in many other places and this is more IO oriented.
{-
Copyright (C) 2009, 2010, 2011 Google Inc.
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -31,6 +31,7 @@ module Ganeti.HTools.CLI
( Options(..)
, OptType
, parseOpts
, parseISpecString
, shTemplate
, defaultLuxiSocket
, maybePrintNodes
......@@ -187,15 +188,17 @@ type OptType = OptDescr (Options -> Result Options)
parseISpecString :: String -> String -> Result RSpec
parseISpecString descr inp = do
let sp = sepSplit ',' inp
err = Bad ("Invalid " ++ descr ++ " specification: '" ++ inp ++
"', expected disk,ram,cpu")
when (length sp /= 3) err
prs <- mapM (\(fn, val) -> fn val) $
zip [ annotateResult (descr ++ " specs memory") . parseUnit
, annotateResult (descr ++ " specs disk") . parseUnit
zip [ annotateResult (descr ++ " specs disk") . parseUnit
, annotateResult (descr ++ " specs memory") . parseUnit
, tryRead (descr ++ " specs cpus")
] sp
case prs of
[dsk, ram, cpu] -> return $ RSpec cpu ram dsk
_ -> Bad $ "Invalid " ++ descr ++ " specification: '" ++ inp ++
"', expected disk,ram,cpu"
_ -> err
-- * Command line options
......
......@@ -38,6 +38,7 @@ module Ganeti.HTools.QC
, testCluster
, testLoader
, testTypes
, testCLI
) where
import Test.QuickCheck
......@@ -1460,3 +1461,24 @@ testSuite "Types"
, 'prop_Types_opToResult
, 'prop_Types_eitherToResult
]
-- ** CLI tests
-- | Test correct parsing.
prop_CLI_parseISpec descr dsk mem cpu =
let str = printf "%d,%d,%d" dsk mem cpu
in CLI.parseISpecString descr str ==? Types.Ok (Types.RSpec cpu mem dsk)
-- | Test parsing failure due to wrong section count.
prop_CLI_parseISpecFail descr =
forAll (choose (0,100) `suchThat` ((/=) 3)) $ \nelems ->
forAll (replicateM nelems arbitrary) $ \values ->
let str = intercalate "," $ map show (values::[Int])
in case CLI.parseISpecString descr str of
Types.Ok v -> failTest $ "Expected failure, got " ++ show v
_ -> property True
testSuite "CLI"
[ 'prop_CLI_parseISpec
, 'prop_CLI_parseISpecFail
]
......@@ -120,6 +120,7 @@ allTests =
, (fast, testJobs)
, (fast, testLoader)
, (fast, testTypes)
, (fast, testCLI)
, (slow, testCluster)
]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment