module Test.Ganeti.Rpc (testRpc) where
import Test.QuickCheck
import Test.QuickCheck.Monadic (monadicIO, run, stop)
import qualified Data.Map as Map
import Test.Ganeti.TestHelper
import Test.Ganeti.TestCommon
import Test.Ganeti.Objects (genInst)
import qualified Ganeti.Rpc as Rpc
import qualified Ganeti.Objects as Objects
import qualified Ganeti.Types as Types
import qualified Ganeti.JSON as JSON
import Ganeti.Types
instance Arbitrary Rpc.RpcCallInstanceConsoleInfo where
arbitrary = Rpc.RpcCallInstanceConsoleInfo <$> genConsoleInfoCallParams
instance Arbitrary Rpc.Compressed where
arbitrary = Rpc.toCompressed <$> arbitrary
genStorageUnit :: Gen StorageUnit
genStorageUnit = do
storage_type <- arbitrary
storage_key <- genName
storage_es <- arbitrary
return $ addParamsToStorageUnit storage_es (SURaw storage_type storage_key)
genStorageUnits :: Gen [StorageUnit]
genStorageUnits = do
num_storage_units <- choose (0, 5)
vectorOf num_storage_units genStorageUnit
genHvParams :: Gen Objects.HvParams
genHvParams = return $ JSON.GenericContainer Map.empty
genHvSpecs :: Gen [(Types.Hypervisor, Objects.HvParams)]
genHvSpecs = do
numhv <- choose (0, 5)
hvs <- vectorOf numhv arbitrary
hvparams <- vectorOf numhv genHvParams
let specs = zip hvs hvparams
return specs
instance Arbitrary Rpc.RpcCallAllInstancesInfo where
arbitrary = Rpc.RpcCallAllInstancesInfo <$> genHvSpecs
instance Arbitrary Rpc.RpcCallInstanceList where
arbitrary = Rpc.RpcCallInstanceList <$> arbitrary
instance Arbitrary Rpc.RpcCallNodeInfo where
arbitrary = Rpc.RpcCallNodeInfo <$> genStorageUnits <*> genHvSpecs
genConsoleInfoCallParams :: Gen [(String, Rpc.InstanceConsoleInfoParams)]
genConsoleInfoCallParams = do
numInstances <- choose (0, 3)
names <- vectorOf numInstances arbitrary
params <- vectorOf numInstances genInstanceConsoleInfoParams
return $ zip names params
genInstanceConsoleInfoParams :: Gen Rpc.InstanceConsoleInfoParams
genInstanceConsoleInfoParams = Rpc.InstanceConsoleInfoParams <$>
genInst <*> arbitrary <*> arbitrary <*> genHvParams <*> arbitrary
runOfflineTest :: (Rpc.Rpc a b, Eq b, Show b) => a -> Property
runOfflineTest call =
forAll (arbitrary `suchThat` Objects.nodeOffline) $ \node -> monadicIO $ do
res <- run $ Rpc.executeRpcCall [node] call
_ <- stop $ res ==? [(node, Left Rpc.OfflineNodeError)]
return ()
prop_noffl_request_allinstinfo :: Rpc.RpcCallAllInstancesInfo -> Property
prop_noffl_request_allinstinfo = runOfflineTest
prop_noffl_request_instconsinfo :: Rpc.RpcCallInstanceConsoleInfo -> Property
prop_noffl_request_instconsinfo = runOfflineTest
prop_noffl_request_instlist :: Rpc.RpcCallInstanceList -> Property
prop_noffl_request_instlist = runOfflineTest
prop_noffl_request_nodeinfo :: Rpc.RpcCallNodeInfo -> Property
prop_noffl_request_nodeinfo = runOfflineTest
prop_Compressed_serialisation :: Rpc.Compressed -> Property
prop_Compressed_serialisation = testSerialisation
testSuite "Rpc"
[ 'prop_noffl_request_allinstinfo
, 'prop_noffl_request_instconsinfo
, 'prop_noffl_request_instlist
, 'prop_noffl_request_nodeinfo
, 'prop_Compressed_serialisation
]