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

Read/write auto_balance via Text



This also means _another_ change in the text format; we really should
move to json…

The unittests are also update for the new 9-column layout and
additionally a bit of improvement is done.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarAdeodato Simo <dato@google.com>
parent a041ebb5
......@@ -38,7 +38,7 @@ module Ganeti.HTools.QC
import Test.QuickCheck
import Test.QuickCheck.Batch
import Data.List (findIndex, intercalate, nub)
import Data.List (findIndex, intercalate, nub, isPrefixOf)
import Data.Maybe
import Control.Monad
import qualified Text.JSON as J
......@@ -453,7 +453,7 @@ testInstance =
-- Instance text loader tests
prop_Text_Load_Instance name mem dsk vcpus status pnode snode pdx sdx =
prop_Text_Load_Instance name mem dsk vcpus status pnode snode pdx sdx autobal =
not (null pnode) && pdx >= 0 && sdx >= 0 ==>
let vcpus_s = show vcpus
dsk_s = show dsk
......@@ -466,16 +466,18 @@ prop_Text_Load_Instance name mem dsk vcpus status pnode snode pdx sdx =
else [(pnode, pdx), (snode, rsdx)]
nl = Data.Map.fromList ndx
tags = ""
sbal = if autobal then "Y" else "N"
inst = Text.loadInst nl
[name, mem_s, dsk_s, vcpus_s, status, pnode, snode, tags]::
Maybe (String, Instance.Instance)
[name, mem_s, dsk_s, vcpus_s, status,
sbal, pnode, snode, tags]:: Maybe (String, Instance.Instance)
fail1 = Text.loadInst nl
[name, mem_s, dsk_s, vcpus_s, status, pnode, pnode, tags]::
Maybe (String, Instance.Instance)
[name, mem_s, dsk_s, vcpus_s, status,
sbal, pnode, pnode, tags]:: Maybe (String, Instance.Instance)
_types = ( name::String, mem::Int, dsk::Int
, vcpus::Int, status::String
, pnode::String, snode::String
, pdx::Types.Ndx, sdx::Types.Ndx)
, pdx::Types.Ndx, sdx::Types.Ndx
, autobal::Bool)
in
case inst of
Nothing -> False
......@@ -487,10 +489,14 @@ prop_Text_Load_Instance name mem dsk vcpus status pnode snode pdx sdx =
Instance.sNode i == (if null snode
then Node.noSecondary
else rsdx) &&
Instance.auto_balance i == autobal &&
isNothing fail1)
prop_Text_Load_InstanceFail ktn fields =
length fields /= 8 ==> isNothing $ Text.loadInst nl fields
length fields /= 9 ==>
case Text.loadInst nl fields of
Right _ -> False
Left msg -> isPrefixOf "Invalid/incomplete instance data: '" msg
where nl = Data.Map.fromList ktn
prop_Text_Load_Node name tm nm fm td fd tc fo =
......
......@@ -86,9 +86,10 @@ serializeInstance nl inst =
then ""
else Container.nameOf nl sidx)
in
printf "%s|%d|%d|%d|%s|%s|%s|%s"
printf "%s|%d|%d|%d|%s|%s|%s|%s|%s"
iname (Instance.mem inst) (Instance.dsk inst)
(Instance.vcpus inst) (Instance.runSt inst)
(if Instance.auto_balance inst then "Y" else "N")
pnode snode (intercalate "," (Instance.tags inst))
-- | Generate instance file data from instance objects
......@@ -134,18 +135,23 @@ loadNode _ s = fail $ "Invalid/incomplete node data: '" ++ show s ++ "'"
-- | Load an instance from a field list.
loadInst :: (Monad m) =>
NameAssoc -> [String] -> m (String, Instance.Instance)
loadInst ktn [name, mem, dsk, vcpus, status, pnode, snode, tags] = do
loadInst ktn [name, mem, dsk, vcpus, status, auto_bal, pnode, snode, tags] = do
pidx <- lookupNode ktn name pnode
sidx <- (if null snode then return Node.noSecondary
else lookupNode ktn name snode)
vmem <- tryRead name mem
vdsk <- tryRead name dsk
vvcpus <- tryRead name vcpus
auto_balance <- case auto_bal of
"Y" -> return True
"N" -> return False
_ -> fail $ "Invalid auto_balance value '" ++ auto_bal ++
"' for instance " ++ name
when (sidx == pidx) $ fail $ "Instance " ++ name ++
" has same primary and secondary node - " ++ pnode
let vtags = sepSplit ',' tags
newinst = Instance.create name vmem vdsk vvcpus status vtags
True pidx sidx
auto_balance pidx sidx
return (name, newinst)
loadInst _ s = fail $ "Invalid/incomplete instance data: '" ++ show s ++ "'"
......
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