From e8127e662ab617558494c8bb1f07a86c81babe75 Mon Sep 17 00:00:00 2001 From: Petr Pudlak Date: Fri, 4 Jul 2014 11:14:20 +0200 Subject: [PATCH] Add basic serialization tests for TempRes's data types In particular, test serialization of IPv4Reservation, which is used on the Python part as well. Signed-off-by: Petr Pudlak Reviewed-by: Klaus Aehlig --- Makefile.am | 5 +- src/Ganeti/WConfd/TempRes.hs | 8 ++- test/hs/Test/Ganeti/WConfd/TempRes.hs | 76 +++++++++++++++++++++++++++ test/hs/htest.hs | 2 + 4 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 test/hs/Test/Ganeti/WConfd/TempRes.hs diff --git a/Makefile.am b/Makefile.am index 6daa8c8fc..a061f71b9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -160,7 +160,8 @@ HS_DIRS = \ test/hs/Test/Ganeti/Objects \ test/hs/Test/Ganeti/Query \ test/hs/Test/Ganeti/THH \ - test/hs/Test/Ganeti/Utils + test/hs/Test/Ganeti/Utils \ + test/hs/Test/Ganeti/WConfd # Haskell directories without the roots (src, test/hs) HS_DIRS_NOROOT = $(filter-out src,$(filter-out test/hs,$(HS_DIRS))) @@ -944,7 +945,7 @@ HS_TEST_SRCS = \ test/hs/Test/Ganeti/Types.hs \ test/hs/Test/Ganeti/Utils.hs \ test/hs/Test/Ganeti/Utils/MultiMap.hs \ - test/hs/Test/Ganeti/Utils/Statistics.hs + test/hs/Test/Ganeti/WConfd/TempRes.hs HS_LIBTEST_SRCS = $(HS_LIB_SRCS) $(HS_TEST_SRCS) diff --git a/src/Ganeti/WConfd/TempRes.hs b/src/Ganeti/WConfd/TempRes.hs index 9a487e57a..4d68f3509 100644 --- a/src/Ganeti/WConfd/TempRes.hs +++ b/src/Ganeti/WConfd/TempRes.hs @@ -29,7 +29,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -} module Ganeti.WConfd.TempRes - ( TempResState(..) + ( TempRes + , mkTempRes + , TempResState(..) , emptyTempResState , NodeUUID , InstanceUUID @@ -152,6 +154,10 @@ instance (J.JSON j, Ord j, J.JSON a, Ord a) => J.JSON (TempRes j a) where showJSON = J.showJSON . getTempRes readJSON = liftM TempRes . J.readJSON +-- | Create a temporary reservations from a given multi-map. +mkTempRes :: MM.MultiMap j a -> TempRes j a +mkTempRes = TempRes + -- | The state of the temporary reservations $(buildObject "TempResState" "trs" [ simpleField "dRBD" [t| DRBDMap |] diff --git a/test/hs/Test/Ganeti/WConfd/TempRes.hs b/test/hs/Test/Ganeti/WConfd/TempRes.hs new file mode 100644 index 000000000..51f050eca --- /dev/null +++ b/test/hs/Test/Ganeti/WConfd/TempRes.hs @@ -0,0 +1,76 @@ +{-# LANGUAGE TemplateHaskell #-} +{-# OPTIONS_GHC -fno-warn-orphans #-} + +{-| Tests for temporary configuration resources allocation + +-} + +{- + +Copyright (C) 2014 Google Inc. + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301, USA. + +-} + +module Test.Ganeti.WConfd.TempRes (testWConfd_TempRes) where + +import Control.Applicative + +import Test.QuickCheck + +import Test.Ganeti.Objects () +import Test.Ganeti.TestCommon +import Test.Ganeti.TestHelper + +import Test.Ganeti.Locking.Locks () -- the JSON ClientId instance +import Test.Ganeti.Utils.MultiMap () + +import Ganeti.WConfd.TempRes + +-- * Instances + +instance Arbitrary IPv4ResAction where + arbitrary = elements [minBound..maxBound] + +instance Arbitrary IPv4Reservation where + arbitrary = IPv4Res <$> arbitrary <*> arbitrary <*> arbitrary + +instance (Arbitrary k, Ord k, Arbitrary v, Ord v) + => Arbitrary (TempRes k v) where + arbitrary = mkTempRes <$> arbitrary + +instance Arbitrary TempResState where + arbitrary = TempResState <$> genMap arbitrary (genMap arbitrary arbitrary) + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + +-- * Tests + +prop_IPv4Reservation_serialisation :: IPv4Reservation -> Property +prop_IPv4Reservation_serialisation = testSerialisation + +prop_TempRes_serialisation :: TempRes Int Int -> Property +prop_TempRes_serialisation = testSerialisation + +-- * The tests combined + +testSuite "WConfd/TempRes" + [ 'prop_IPv4Reservation_serialisation + , 'prop_TempRes_serialisation + ] diff --git a/test/hs/htest.hs b/test/hs/htest.hs index 05d1707e4..043d07da4 100644 --- a/test/hs/htest.hs +++ b/test/hs/htest.hs @@ -83,6 +83,7 @@ import Test.Ganeti.Types import Test.Ganeti.Utils import Test.Ganeti.Utils.MultiMap import Test.Ganeti.Utils.Statistics +import Test.Ganeti.WConfd.TempRes -- | Our default test options, overring the built-in test-framework -- ones (but not the supplied command line parameters). @@ -151,6 +152,7 @@ allTests = , testUtils , testUtils_MultiMap , testUtils_Statistics + , testWConfd_TempRes ] -- | Main function. Note we don't use defaultMain since we want to -- GitLab