module Test.Ganeti.Query.Instance
( testQuery_Instance
) where
import qualified Data.Map as Map
import qualified Data.Set as Set
import Ganeti.JSON
import Ganeti.Objects
import Ganeti.Query.Instance
import Ganeti.Rpc
import Ganeti.Types
import Test.Ganeti.TestHelper
import Test.HUnit
createInstance :: String -> String -> AdminState -> AdminStateSource -> Instance
createInstance name pnodeUuid adminState adminStateSource =
Instance name pnodeUuid "" Kvm
(GenericContainer Map.empty)
(PartialBeParams Nothing Nothing Nothing Nothing Nothing Nothing)
(GenericContainer Map.empty)
adminState adminStateSource [] [] DTDrbd8 False Nothing 0.0 0.0 "" 0
Set.empty
fakeInstanceInfo :: InstanceInfo
fakeInstanceInfo = InstanceInfo 0 InstanceStateRunning 0 0
responseError :: String -> (String, ERpcError a)
responseError name = (name, Left . RpcResultError $ "Insignificant error")
responseSuccess :: String
-> [String]
-> (String, ERpcError RpcResultAllInstancesInfo)
responseSuccess name instNames = (name, Right .
RpcResultAllInstancesInfo . map (, fakeInstanceInfo) $ instNames)
waldoInstance :: Instance
waldoInstance = createInstance "Waldo" "prim" AdminUp AdminSource
case_nodeOffline :: Assertion
case_nodeOffline =
let responses = [ responseError "prim"
, responseError "second"
, responseSuccess "node" ["NotWaldo", "DefinitelyNotWaldo"]
]
in case getInstanceInfo responses waldoInstance of
Left _ -> return ()
Right _ -> assertFailure
"Error occurred when instance info is missing and node is offline"
case_nodeOnlineNoInfo :: Assertion
case_nodeOnlineNoInfo =
let responses = [ responseSuccess "prim" ["NotWaldo1"]
, responseSuccess "second" ["NotWaldo2"]
, responseError "node"
]
in case getInstanceInfo responses waldoInstance of
Left _ -> assertFailure
"Error occurred when instance info could be found on primary"
Right Nothing -> return ()
Right _ -> assertFailure
"Some instance info found when none should be"
case_infoOnPrimary :: Assertion
case_infoOnPrimary =
let responses = [ responseSuccess "prim" ["NotWaldo1", "Waldo"]
, responseSuccess "second" ["NotWaldo2"]
, responseSuccess "node" ["NotWaldo3"]
]
in case getInstanceInfo responses waldoInstance of
Left _ -> assertFailure
"Cannot retrieve instance info when present on primary node"
Right (Just (_, True)) -> return ()
Right _ -> assertFailure
"Instance info not found on primary node, despite being there"
case_infoOnSecondary :: Assertion
case_infoOnSecondary =
let responses = [ responseSuccess "prim" ["NotWaldo1"]
, responseSuccess "second" ["Waldo", "NotWaldo2"]
, responseError "node"
]
in case getInstanceInfo responses waldoInstance of
Left _ -> assertFailure
"Cannot retrieve instance info when present on secondary node"
Right (Just (_, False)) -> return ()
Right _ -> assertFailure
"Instance info not found on secondary node, despite being there"
testSuite "Query_Instance"
[ 'case_nodeOffline
, 'case_nodeOnlineNoInfo
, 'case_infoOnPrimary
, 'case_infoOnSecondary
]