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

Add types, parameters and the opcodes for networks



This completes the last missing opcode group. The only difficulty was
with the ip addresses, where we used simple strings to represent them
and (for IPv4) a few helpers to generate arbitrary instances;
otherwise, the patch is trivial.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarHelga Velroyen <helgav@google.com>
parent 497beee2
......@@ -38,6 +38,7 @@ import Control.Applicative
import Data.List
import qualified Data.Map as Map
import qualified Text.JSON as J
import Text.Printf (printf)
import Test.Ganeti.TestHelper
import Test.Ganeti.TestCommon
......@@ -308,6 +309,25 @@ instance Arbitrary OpCodes.OpCode where
"OP_TEST_DUMMY" ->
OpCodes.OpTestDummy <$> pure J.JSNull <*> pure J.JSNull <*>
pure J.JSNull <*> pure J.JSNull
"OP_NETWORK_ADD" ->
OpCodes.OpNetworkAdd <$> genNameNE <*> arbitrary <*> genIp4Net <*>
getMaybe genIp4Addr <*> pure Nothing <*> pure Nothing <*>
getMaybe genMacPrefix <*> getMaybe (listOf genIp4Addr) <*>
(genTags >>= mapM mkNonEmpty)
"OP_NETWORK_REMOVE" ->
OpCodes.OpNetworkRemove <$> genNameNE <*> arbitrary
"OP_NETWORK_SET_PARAMS" ->
OpCodes.OpNetworkSetParams <$> genNameNE <*> arbitrary <*>
getMaybe genIp4Addr <*> pure Nothing <*> pure Nothing <*>
getMaybe genMacPrefix <*> getMaybe (listOf genIp4Addr) <*>
getMaybe (listOf genIp4Addr)
"OP_NETWORK_CONNECT" ->
OpCodes.OpNetworkConnect <$> genNameNE <*> genNameNE <*>
arbitrary <*> genNameNE <*> arbitrary
"OP_NETWORK_DISCONNECT" ->
OpCodes.OpNetworkDisconnect <$> genNameNE <*> genNameNE <*> arbitrary
"OP_NETWORK_QUERY" ->
OpCodes.OpNetworkQuery <$> genFieldsNE <*> genNamesNE
_ -> fail $ "Undefined arbitrary for opcode " ++ op_id
-- * Helper functions
......@@ -354,6 +374,28 @@ genNamesNE = resize maxNodes (listOf genNameNE)
genFieldsNE :: Gen [NonEmptyString]
genFieldsNE = getFields >>= mapM mkNonEmpty
-- | Generate an arbitrary IPv4 address in textual form.
genIp4Addr :: Gen NonEmptyString
genIp4Addr = do
a <- choose (1::Int, 255)
b <- choose (0::Int, 255)
c <- choose (0::Int, 255)
d <- choose (0::Int, 255)
mkNonEmpty $ intercalate "." (map show [a, b, c, d])
-- | Generate an arbitrary IPv4 network address in textual form.
genIp4Net :: Gen NonEmptyString
genIp4Net = do
netmask <- choose (8::Int, 30)
ip <- genIp4Addr
mkNonEmpty $ fromNonEmpty ip ++ "/" ++ show netmask
-- | Generate a 3-byte MAC prefix.
genMacPrefix :: Gen NonEmptyString
genMacPrefix = do
octets <- vectorOf 3 $ choose (0::Int, 255)
mkNonEmpty . intercalate ":" $ map (printf "%02x") octets
-- * Test cases
-- | Check that opcode serialization is idempotent.
......
......@@ -103,6 +103,8 @@ $(genArbitrary ''IAllocatorTestDir)
$(genArbitrary ''IAllocatorMode)
$(genArbitrary ''NetworkType)
$(genArbitrary ''NICMode)
-- * Properties
......@@ -233,6 +235,17 @@ case_IAllocatorMode_pyequiv = do
all_hs_codes = sort $ map Types.iAllocatorModeToRaw [minBound..maxBound]
assertEqual "for IAllocatorMode equivalence" all_py_codes all_hs_codes
-- | Test 'NetworkType' serialisation.
prop_NetworkType_serialisation :: NetworkType -> Property
prop_NetworkType_serialisation = testSerialisation
-- | Tests equivalence with Python, based on Constants.hs code.
case_NetworkType_pyequiv :: Assertion
case_NetworkType_pyequiv = do
let all_py_codes = sort C.networkValidTypes
all_hs_codes = sort $ map Types.networkTypeToRaw [minBound..maxBound]
assertEqual "for NetworkType equivalence" all_py_codes all_hs_codes
-- | Test 'NICMode' serialisation.
prop_NICMode_serialisation :: NICMode -> Property
prop_NICMode_serialisation = testSerialisation
......@@ -271,6 +284,8 @@ testSuite "Types"
, 'prop_IAllocatorTestDir_serialisation
, 'prop_IAllocatorMode_serialisation
, 'case_IAllocatorMode_pyequiv
, 'prop_NetworkType_serialisation
, 'case_NetworkType_pyequiv
, 'prop_NICMode_serialisation
, 'case_NICMode_pyequiv
]
......@@ -476,6 +476,47 @@ $(genOpCode "OpCode"
, pTestDummyFail
, pTestDummySubmitJobs
])
, ("OpNetworkAdd",
[ pNetworkName
, pNetworkType
, pNetworkAddress4
, pNetworkGateway4
, pNetworkAddress6
, pNetworkGateway6
, pNetworkMacPrefix
, pNetworkAddRsvdIps
, pInstTags
])
, ("OpNetworkRemove",
[ pNetworkName
, pForce
])
, ("OpNetworkSetParams",
[ pNetworkName
, pNetworkType
, pNetworkGateway4
, pNetworkAddress6
, pNetworkGateway6
, pNetworkMacPrefix
, pNetworkAddRsvdIps
, pNetworkRemoveRsvdIps
])
, ("OpNetworkConnect",
[ pGroupName
, pNetworkName
, pNetworkMode
, pNetworkLink
, pIpConflictsCheck
])
, ("OpNetworkDisconnect",
[ pGroupName
, pNetworkName
, pIpConflictsCheck
])
, ("OpNetworkQuery",
[ pOutputFields
, pNames
])
])
-- | Returns the OP_ID for a given opcode value.
......
......@@ -209,6 +209,17 @@ module Ganeti.OpParams
, pTestDummyMessages
, pTestDummyFail
, pTestDummySubmitJobs
, pNetworkName
, pNetworkType
, pNetworkAddress4
, pNetworkGateway4
, pNetworkAddress6
, pNetworkGateway6
, pNetworkMacPrefix
, pNetworkAddRsvdIps
, pNetworkRemoveRsvdIps
, pNetworkMode
, pNetworkLink
) where
import Control.Monad (liftM)
......@@ -1262,3 +1273,65 @@ pTestDummySubmitJobs :: Field
pTestDummySubmitJobs =
renameField "TestDummySubmitJobs" $
simpleField "submit_jobs" [t| UncheckedValue |]
-- * Network parameters
-- | Network name.
pNetworkName :: Field
pNetworkName = simpleField "network_name" [t| NonEmptyString |]
-- | Network type field.
pNetworkType :: Field
pNetworkType = optionalField $ simpleField "network_type" [t| NetworkType |]
-- | Network address (IPv4 subnet). FIXME: no real type for this.
pNetworkAddress4 :: Field
pNetworkAddress4 =
renameField "NetworkAddress4" $
simpleField "network" [t| NonEmptyString |]
-- | Network gateway (IPv4 address). FIXME: no real type for this.
pNetworkGateway4 :: Field
pNetworkGateway4 =
renameField "NetworkGateway4" $
optionalNEStringField "gateway"
-- | Network address (IPv6 subnet). FIXME: no real type for this.
pNetworkAddress6 :: Field
pNetworkAddress6 =
renameField "NetworkAddress6" $
optionalNEStringField "network6"
-- | Network gateway (IPv6 address). FIXME: no real type for this.
pNetworkGateway6 :: Field
pNetworkGateway6 =
renameField "NetworkGateway6" $
optionalNEStringField "gateway6"
-- | Network specific mac prefix (that overrides the cluster one).
pNetworkMacPrefix :: Field
pNetworkMacPrefix =
renameField "NetMacPrefix" $
optionalNEStringField "mac_prefix"
-- | Network add reserved IPs.
pNetworkAddRsvdIps :: Field
pNetworkAddRsvdIps =
renameField "NetworkAddRsvdIps" .
optionalField $
simpleField "add_reserved_ips" [t| [NonEmptyString] |]
-- | Network remove reserved IPs.
pNetworkRemoveRsvdIps :: Field
pNetworkRemoveRsvdIps =
renameField "NetworkRemoveRsvdIps" .
optionalField $
simpleField "remove_reserved_ips" [t| [NonEmptyString] |]
-- | Network mode when connecting to a group.
pNetworkMode :: Field
pNetworkMode = simpleField "network_mode" [t| NICMode |]
-- | Network link when connecting to a group.
pNetworkLink :: Field
pNetworkLink = simpleField "network_link" [t| NonEmptyString |]
......@@ -67,6 +67,8 @@ module Ganeti.Types
, IAllocatorTestDir(..)
, IAllocatorMode(..)
, iAllocatorModeToRaw
, NetworkType(..)
, networkTypeToRaw
, NICMode(..)
, nICModeToRaw
) where
......@@ -317,6 +319,13 @@ $(THH.declareSADT "IAllocatorMode"
])
$(THH.makeJSONInstance ''IAllocatorMode)
-- | Network type.
$(THH.declareSADT "NetworkType"
[ ("PrivateNetwork", 'C.networkTypePrivate)
, ("PublicNetwork", 'C.networkTypePublic)
])
$(THH.makeJSONInstance ''NetworkType)
-- | Netork mode.
$(THH.declareSADT "NICMode"
[ ("NMBridged", 'C.nicModeBridged)
......
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