From 3cbd5808d0ec20b506142985a2487fa9e2814763 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Mon, 4 Mar 2013 13:32:22 +0100
Subject: [PATCH] Fix Haskell compatibility tests with disabled file storage

When file storage is disabled at ./configure time, we shouldn't pass
opcodes containing DTFile/DTSharedFile/StorageFile to Python for
validation, as they will fail.

This patch implements this by simply tweaking the Arbitrary instances
for DiskTemplate and StorageType (which IMHO is a nice and clean
way!), and also fixing the generation of arbitrary IPolicies to use
the correct 'allDiskTemplates' list (otherwise we'd loop forever
trying to generate a list of all templates).

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michele Tartara <mtartara@google.com>
---
 test/hs/Test/Ganeti/HTools/Types.hs |  8 ++-----
 test/hs/Test/Ganeti/Types.hs        | 33 ++++++++++++++++++++++++++---
 2 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/test/hs/Test/Ganeti/HTools/Types.hs b/test/hs/Test/Ganeti/HTools/Types.hs
index 0cb187a21..da2172543 100644
--- a/test/hs/Test/Ganeti/HTools/Types.hs
+++ b/test/hs/Test/Ganeti/HTools/Types.hs
@@ -7,7 +7,7 @@
 
 {-
 
-Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
+Copyright (C) 2009, 2010, 2011, 2012, 2013 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
@@ -46,7 +46,7 @@ import Data.List (sort)
 import Test.Ganeti.TestHelper
 import Test.Ganeti.TestCommon
 import Test.Ganeti.TestHTools
-import Test.Ganeti.Types ()
+import Test.Ganeti.Types (allDiskTemplates)
 
 import Ganeti.BasicTypes
 import qualified Ganeti.Constants as C
@@ -56,10 +56,6 @@ import qualified Ganeti.HTools.Types as Types
 
 -- * Helpers
 
--- | All disk templates (used later)
-allDiskTemplates :: [Types.DiskTemplate]
-allDiskTemplates = [minBound..maxBound]
-
 -- * Arbitrary instance
 
 $(genArbitrary ''Types.FailMode)
diff --git a/test/hs/Test/Ganeti/Types.hs b/test/hs/Test/Ganeti/Types.hs
index 6246e6e0c..3eff2f0ce 100644
--- a/test/hs/Test/Ganeti/Types.hs
+++ b/test/hs/Test/Ganeti/Types.hs
@@ -30,13 +30,14 @@ module Test.Ganeti.Types
   ( testTypes
   , AllocPolicy(..)
   , DiskTemplate(..)
+  , allDiskTemplates
   , InstanceStatus(..)
   , NonEmpty(..)
   , Hypervisor(..)
   , JobId(..)
   ) where
 
-import Data.List (sort)
+import Data.List (delete, sort)
 import Test.QuickCheck as QuickCheck hiding (Result)
 import Test.HUnit
 import qualified Text.JSON as J
@@ -78,7 +79,22 @@ instance (Arbitrary a) => Arbitrary (Types.NonEmpty a) where
 
 $(genArbitrary ''AllocPolicy)
 
-$(genArbitrary ''DiskTemplate)
+-- | Valid disk templates (depending on configure options).
+allDiskTemplates :: [DiskTemplate]
+allDiskTemplates =
+  let all_vals = [minBound..maxBound]::[DiskTemplate]
+      sel1 = if C.enableFileStorage
+               then all_vals
+               else delete DTFile all_vals
+      sel2 = if C.enableSharedFileStorage
+               then sel1
+               else delete DTSharedFile sel1
+  in sel2
+
+-- | Custom 'Arbitrary' instance for 'DiskTemplate', which needs to
+-- handle the case of file storage being disabled at configure time.
+instance Arbitrary DiskTemplate where
+  arbitrary = elements allDiskTemplates
 
 $(genArbitrary ''InstanceStatus)
 
@@ -96,7 +112,18 @@ $(genArbitrary ''Hypervisor)
 
 $(genArbitrary ''OobCommand)
 
-$(genArbitrary ''StorageType)
+-- | Valid storage types.
+allStorageTypes :: [StorageType]
+allStorageTypes =
+  let all_vals = [minBound..maxBound]::[StorageType]
+  in if C.enableFileStorage
+       then all_vals
+       else delete StorageFile all_vals
+
+-- | Custom 'Arbitrary' instance for 'StorageType', which needs to
+-- handle the case of file storage being disabled at configure time.
+instance Arbitrary StorageType where
+  arbitrary = elements allStorageTypes
 
 $(genArbitrary ''NodeEvacMode)
 
-- 
GitLab