Commit a2a0bcd8 authored by Iustin Pop's avatar Iustin Pop
Browse files

Fix a few issues in unittest data generation



This patch fixes two ugly but non-critical issues:

- the getFQDN wrongly generated only the first component as
  limited-length name, the rest could be any length, and that makes
  the generated name bad for display purposes
- the random generator had real issues in finding an offline instance
  with the right properties when starting from certain seeds (found
  accidentally); this meant the test took 1 minute, instead of ~300ms,
  for the node_addSecOffline

Furthermore, for the same evil seed, the Container_findByName test
wasn't able to generate enough good data, so we change its
implementation to directly generate an uniques list, instead of
generating random lists and filtering the unique ones.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarRené Nussbaumer <rn@google.com>
parent 6a855aaa
......@@ -209,6 +209,13 @@ assignInstance nl il inst pdx sdx =
il' = Container.add maxiidx inst' il
in (nl', il')
-- | Generates a list of a given size with non-duplicate elements.
genUniquesList :: (Eq a, Arbitrary a) => Int -> Gen [a]
genUniquesList cnt =
foldM (\lst _ -> do
newelem <- arbitrary `suchThat` (`notElem` lst)
return (newelem:lst)) [] [1..cnt]
-- * Arbitrary instances
-- | Defines a DNS name.
......@@ -219,19 +226,19 @@ instance Arbitrary DNSChar where
x <- elements (['a'..'z'] ++ ['0'..'9'] ++ "_-")
return (DNSChar x)
-- | Generates a single name component.
getName :: Gen String
getName = do
n <- choose (1, 64)
dn <- vector n::Gen [DNSChar]
return (map dnsGetChar dn)
-- | Generates an entire FQDN.
getFQDN :: Gen String
getFQDN = do
felem <- getName
ncomps <- choose (1, 4)
frest <- vector ncomps::Gen [[DNSChar]]
let frest' = map (map dnsGetChar) frest
return (felem ++ "." ++ intercalate "." frest')
names <- mapM (const getName) [1..ncomps::Int]
return $ intercalate "." names
-- | Defines a tag type.
newtype TagChar = TagChar { tagGetChar :: Char }
......@@ -566,14 +573,13 @@ prop_Container_nameOf node =
-- | We test that in a cluster, given a random node, we can find it by
-- its name and alias, as long as all names and aliases are unique,
-- and that we fail to find a non-existing name.
prop_Container_findByName node othername =
prop_Container_findByName node =
forAll (choose (1, 20)) $ \ cnt ->
forAll (choose (0, cnt - 1)) $ \ fidx ->
forAll (vector cnt) $ \ names ->
(length . nub) (map fst names ++ map snd names) ==
length names * 2 &&
othername `notElem` (map fst names ++ map snd names) ==>
let nl = makeSmallCluster node cnt
forAll (genUniquesList (cnt * 2)) $ \ allnames ->
forAll (arbitrary `suchThat` (`notElem` allnames)) $ \ othername ->
let names = zip (take cnt allnames) (drop cnt allnames)
nl = makeSmallCluster node cnt
nodes = Container.elems nl
nodes' = map (\((name, alias), nn) -> (Node.idx nn,
nn { Node.name = name,
......@@ -906,11 +912,10 @@ prop_Node_addPriOffline =
_ -> False
prop_Node_addSecOffline pdx =
forAll (arbitrary `suchThat` ((> 0) . Node.fMem)) $ \node ->
forAll genOnlineNode $ \node ->
forAll (arbitrary `suchThat`
(\ x -> (Instance.dsk x < Node.fDsk node) &&
Instance.instanceOffline x)) $ \inst ->
case Node.addSec node inst pdx of
(\ inst -> Instance.dsk inst < Node.availDisk node)) $ \inst ->
case Node.addSec node (inst { Instance.runSt = Types.AdminOffline }) pdx of
Types.OpGood _ -> True
_ -> False
......
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