From 3e77a36c184533041a9c541d24d20aafe8a3d4f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dato=20Sim=C3=B3?= <dato@google.com> Date: Fri, 16 Nov 2012 22:16:10 +0000 Subject: [PATCH] Add initial constants and Haskell ADTs for auto repair MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In this commit, the AutoRepairType and AutoRepairResult types are defined, with the possible values specified in doc/design-autorepair.rst. Signed-off-by: Dato SimΓ³ <dato@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- lib/constants.py | 29 +++++++++++++++++++++++++++++ src/Ganeti/HTools/Types.hs | 22 ++++++++++++++++++++++ test/hs/Test/Ganeti/HTools/Types.hs | 26 ++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/lib/constants.py b/lib/constants.py index 508d6088e..b24c055b8 100644 --- a/lib/constants.py +++ b/lib/constants.py @@ -2280,5 +2280,34 @@ RANDOM_UUID_FILE = "/proc/sys/kernel/random/uuid" # Regex string for verifying a UUID UUID_REGEX = "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$" +# Auto-repair tag prefixes +AUTO_REPAIR_TAG_PREFIX = "ganeti:watcher:autorepair:" +AUTO_REPAIR_TAG_ENABLED = AUTO_REPAIR_TAG_PREFIX +AUTO_REPAIR_TAG_SUSPENDED = AUTO_REPAIR_TAG_ENABLED + "suspend:" +AUTO_REPAIR_TAG_PENDING = AUTO_REPAIR_TAG_PREFIX + "pending:" +AUTO_REPAIR_TAG_RESULT = AUTO_REPAIR_TAG_PREFIX + "result:" + +# Auto-repair levels +AUTO_REPAIR_FIX_STORAGE = "fix-storage" +AUTO_REPAIR_MIGRATE = "migrate" +AUTO_REPAIR_FAILOVER = "failover" +AUTO_REPAIR_REINSTALL = "reinstall" +AUTO_REPAIR_ALL_TYPES = [ + AUTO_REPAIR_FIX_STORAGE, + AUTO_REPAIR_MIGRATE, + AUTO_REPAIR_FAILOVER, + AUTO_REPAIR_REINSTALL, +] + +# Auto-repair results +AUTO_REPAIR_SUCCESS = "success" +AUTO_REPAIR_FAILURE = "failure" +AUTO_REPAIR_ENOPERM = "enoperm" +AUTO_REPAIR_ALL_RESULTS = frozenset([ + AUTO_REPAIR_SUCCESS, + AUTO_REPAIR_FAILURE, + AUTO_REPAIR_ENOPERM, +]) + # Do not re-export imported modules del re, _vcsversion, _autoconf, socket, pathutils, compat diff --git a/src/Ganeti/HTools/Types.hs b/src/Ganeti/HTools/Types.hs index 160473b13..b006dde79 100644 --- a/src/Ganeti/HTools/Types.hs +++ b/src/Ganeti/HTools/Types.hs @@ -72,6 +72,12 @@ module Ganeti.HTools.Types , IPolicy(..) , defIPolicy , rspecFromISpec + , AutoRepairType(..) + , autoRepairTypeToRaw + , autoRepairTypeFromRaw + , AutoRepairResult(..) + , autoRepairResultToRaw + , autoRepairResultFromRaw ) where import qualified Data.Map as M @@ -347,3 +353,19 @@ $(THH.declareSADT "EvacMode" , ("ChangeAll", 'C.iallocatorNevacAll) ]) $(THH.makeJSONInstance ''EvacMode) + +-- | The repair modes for the auto-repair tool. +$(THH.declareSADT "AutoRepairType" + -- Order is important here: from least destructive to most. + [ ("ArFixStorage", 'C.autoRepairFixStorage) + , ("ArMigrate", 'C.autoRepairMigrate) + , ("ArFailover", 'C.autoRepairFailover) + , ("ArReinstall", 'C.autoRepairReinstall) + ]) + +-- | The possible auto-repair results. +$(THH.declareSADT "AutoRepairResult" + [ ("ArSuccess", 'C.autoRepairSuccess) + , ("ArFailure", 'C.autoRepairFailure) + , ("ArEnoperm", 'C.autoRepairEnoperm) + ]) diff --git a/test/hs/Test/Ganeti/HTools/Types.hs b/test/hs/Test/Ganeti/HTools/Types.hs index 5dbcbe715..7c12c4185 100644 --- a/test/hs/Test/Ganeti/HTools/Types.hs +++ b/test/hs/Test/Ganeti/HTools/Types.hs @@ -38,8 +38,10 @@ module Test.Ganeti.HTools.Types ) where import Test.QuickCheck hiding (Result) +import Test.HUnit import Control.Applicative +import Data.List (sort) import Test.Ganeti.TestHelper import Test.Ganeti.TestCommon @@ -47,6 +49,7 @@ import Test.Ganeti.TestHTools import Test.Ganeti.Types () import Ganeti.BasicTypes +import qualified Ganeti.Constants as C import qualified Ganeti.HTools.Types as Types -- * Helpers @@ -146,10 +149,33 @@ prop_eitherToResult ei = Ok v' -> v == v' where r = eitherToResult ei +-- | Test 'AutoRepairType' ordering is as expected and consistent with Python +-- codebase. +case_AutoRepairType_sort :: Assertion +case_AutoRepairType_sort = do + let expected = [ Types.ArFixStorage + , Types.ArMigrate + , Types.ArFailover + , Types.ArReinstall + ] + all_hs_raw = map Types.autoRepairTypeToRaw [minBound..maxBound] + assertEqual "Haskell order" expected [minBound..maxBound] + assertEqual "consistent with Python" C.autoRepairAllTypes all_hs_raw + +-- | Test 'AutoRepairResult' type is equivalent with Python codebase. +case_AutoRepairResult_pyequiv :: Assertion +case_AutoRepairResult_pyequiv = do + let all_py_results = sort C.autoRepairAllResults + all_hs_results = sort $ + map Types.autoRepairResultToRaw [minBound..maxBound] + assertEqual "for AutoRepairResult equivalence" all_py_results all_hs_results + testSuite "HTools/Types" [ 'prop_ISpec_serialisation , 'prop_IPolicy_serialisation , 'prop_EvacMode_serialisation , 'prop_opToResult , 'prop_eitherToResult + , 'case_AutoRepairType_sort + , 'case_AutoRepairResult_pyequiv ] -- GitLab