From 9990c0687a0024db42f297a3a60feca6f819052d Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Mon, 20 Aug 2012 00:42:58 +0200
Subject: [PATCH] Add test case for OpCode list equivalence

For now, we only test that we don't define extra opcodes in the
Haskell code; once we have parity, we can enable the (for now)
commented-out lines that check the reverse.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Agata Murawska <agatamurawska@google.com>
---
 htools/Ganeti/HTools/QC.hs | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/htools/Ganeti/HTools/QC.hs b/htools/Ganeti/HTools/QC.hs
index 118a795c2..6df169231 100644
--- a/htools/Ganeti/HTools/QC.hs
+++ b/htools/Ganeti/HTools/QC.hs
@@ -56,7 +56,7 @@ import qualified Test.HUnit as HUnit
 import Test.QuickCheck
 import Test.QuickCheck.Monadic (assert, monadicIO, run, stop)
 import Text.Printf (printf)
-import Data.List (intercalate, nub, isPrefixOf)
+import Data.List (intercalate, nub, isPrefixOf, sort, (\\))
 import Data.Maybe
 import qualified Data.Set as Set
 import Control.Monad
@@ -1617,8 +1617,26 @@ prop_OpCodes_serialization op =
     J.Error e -> failTest $ "Cannot deserialise: " ++ e
     J.Ok op' -> op ==? op'
 
+-- | Check that Python and Haskell defined the same opcode list.
+case_OpCodes_AllDefined :: HUnit.Assertion
+case_OpCodes_AllDefined = do
+  py_stdout <- runPython "from ganeti import opcodes\n\
+                         \print '\\n'.join(opcodes.OP_MAPPING.keys())" "" >>=
+               checkPythonResult
+  let py_ops = sort $ lines py_stdout
+      hs_ops = OpCodes.allOpIDs
+      -- extra_py = py_ops \\ hs_ops
+      extra_hs = hs_ops \\ py_ops
+  -- FIXME: uncomment when we have parity
+  -- HUnit.assertBool ("OpCodes missing from Haskell code:\n" ++
+  --                  unlines extra_py) (null extra_py)
+  HUnit.assertBool ("Extra OpCodes in the Haskell code code:\n" ++
+                    unlines extra_hs) (null extra_hs)
+
 testSuite "OpCodes"
-            [ 'prop_OpCodes_serialization ]
+            [ 'prop_OpCodes_serialization
+            , 'case_OpCodes_AllDefined
+            ]
 
 -- ** Jobs tests
 
-- 
GitLab