From c5b4a1866c3933fe51bdadaf26b2d076443636c5 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Fri, 8 Jun 2012 11:40:43 +0200
Subject: [PATCH] Fix prefix bug in Haskell ssconf implementation

This patch fixes a (brown-bag) bug in the ssconf implementation where
the looked-for filenames were not prefixed with the ssconf file
prefix.

Additionally, a test for this is added (which fails without the fix).

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 htools/Ganeti/HTools/QC.hs | 15 +++++++++++++++
 htools/Ganeti/Ssconf.hs    |  9 ++++++++-
 htools/test.hs             |  1 +
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/htools/Ganeti/HTools/QC.hs b/htools/Ganeti/HTools/QC.hs
index 0c1ff5adb..78b88a638 100644
--- a/htools/Ganeti/HTools/QC.hs
+++ b/htools/Ganeti/HTools/QC.hs
@@ -41,6 +41,7 @@ module Ganeti.HTools.QC
   , testCLI
   , testJSON
   , testLUXI
+  , testSsconf
   ) where
 
 import Test.QuickCheck
@@ -58,6 +59,7 @@ import qualified Data.IntMap as IntMap
 import qualified Ganeti.OpCodes as OpCodes
 import qualified Ganeti.Jobs as Jobs
 import qualified Ganeti.Luxi as Luxi
+import qualified Ganeti.Ssconf as Ssconf
 import qualified Ganeti.HTools.CLI as CLI
 import qualified Ganeti.HTools.Cluster as Cluster
 import qualified Ganeti.HTools.Container as Container
@@ -1731,3 +1733,16 @@ prop_Luxi_CallEncoding op =
 testSuite "LUXI"
           [ 'prop_Luxi_CallEncoding
           ]
+
+-- * Ssconf tests
+
+instance Arbitrary Ssconf.SSKey where
+  arbitrary = elements [minBound..maxBound]
+
+prop_Ssconf_filename key =
+  printTestCase "Key doesn't start with correct prefix" $
+    Ssconf.sSFilePrefix `isPrefixOf` Ssconf.keyToFilename (Just "") key
+
+testSuite "Ssconf"
+  [ 'prop_Ssconf_filename
+  ]
diff --git a/htools/Ganeti/Ssconf.hs b/htools/Ganeti/Ssconf.hs
index 5910cd1a3..39a3d95df 100644
--- a/htools/Ganeti/Ssconf.hs
+++ b/htools/Ganeti/Ssconf.hs
@@ -30,6 +30,8 @@ module Ganeti.Ssconf
   , sSKeyToRaw
   , sSKeyFromRaw
   , getPrimaryIPFamily
+  , keyToFilename
+  , sSFilePrefix
   ) where
 
 import Ganeti.THH
@@ -51,6 +53,10 @@ import Ganeti.HTools.Utils
 maxFileSize :: Int
 maxFileSize = 131072
 
+-- | ssconf file prefix, re-exported from Constants.
+sSFilePrefix :: FilePath
+sSFilePrefix = C.ssconfFileprefix
+
 $(declareSADT "SSKey"
   [ ("SSClusterName",          'C.ssClusterName)
   , ("SSClusterTags",          'C.ssClusterTags)
@@ -80,7 +86,8 @@ $(declareSADT "SSKey"
 keyToFilename :: Maybe FilePath     -- ^ Optional config path override
               -> SSKey              -- ^ ssconf key
               -> FilePath
-keyToFilename optpath key = fromMaybe C.dataDir optpath </> sSKeyToRaw key
+keyToFilename optpath key = fromMaybe C.dataDir optpath </>
+                            sSFilePrefix ++ sSKeyToRaw key
 
 -- | Runs an IO action while transforming any error into 'Bad'
 -- values. It also accepts an optional value to use in case the error
diff --git a/htools/test.hs b/htools/test.hs
index eedefd35b..6e434274b 100644
--- a/htools/test.hs
+++ b/htools/test.hs
@@ -125,6 +125,7 @@ allTests =
   , (fast, testCLI)
   , (fast, testJSON)
   , (fast, testLUXI)
+  , (fast, testSsconf)
   , (slow, testCluster)
   ]
 
-- 
GitLab