From 60f7f6a4e69a6f346e59a8a50a23c7a5473e29b7 Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Mon, 20 Aug 2012 00:41:26 +0200 Subject: [PATCH] Add infrastructure for reading Python command output This will be used in the future to make tests/assertions for Python/Haskell codebase equivalence (e.g. checking that the same opcodes are defined, etc.). (Side-note: this might not work in VPATH builds; I'll see how it goes) Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: Agata Murawska <agatamurawska@google.com> --- htools/Ganeti/HTools/QC.hs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/htools/Ganeti/HTools/QC.hs b/htools/Ganeti/HTools/QC.hs index ebbff9503..118a795c2 100644 --- a/htools/Ganeti/HTools/QC.hs +++ b/htools/Ganeti/HTools/QC.hs @@ -52,6 +52,7 @@ module Ganeti.HTools.QC , testQlang ) where +import qualified Test.HUnit as HUnit import Test.QuickCheck import Test.QuickCheck.Monadic (assert, monadicIO, run, stop) import Text.Printf (printf) @@ -67,8 +68,11 @@ import qualified Data.IntMap as IntMap import Control.Concurrent (forkIO) import Control.Exception (bracket, catchJust) import System.Directory (getTemporaryDirectory, removeFile) +import System.Environment (getEnv) +import System.Exit (ExitCode(..)) import System.IO (hClose, openTempFile) -import System.IO.Error (isEOFErrorType, ioeGetErrorType) +import System.IO.Error (isEOFErrorType, ioeGetErrorType, isDoesNotExistError) +import System.Process (readProcessWithExitCode) import qualified Ganeti.Confd as Confd import qualified Ganeti.Config as Config @@ -207,6 +211,27 @@ infix 3 ==? failTest :: String -> Property failTest msg = printTestCase msg False +-- | Return the python binary to use. If the PYTHON environment +-- variable is defined, use its value, otherwise use just \"python\". +pythonCmd :: IO String +pythonCmd = catchJust (guard . isDoesNotExistError) + (getEnv "PYTHON") (const (return "python")) + +-- | Run Python with an expression, returning the exit code, standard +-- output and error. +runPython :: String -> String -> IO (ExitCode, String, String) +runPython expr stdin = do + py_binary <- pythonCmd + readProcessWithExitCode py_binary ["-c", expr] stdin + +-- | Check python exit code, and fail via HUnit assertions if +-- non-zero. Otherwise, return the standard output. +checkPythonResult :: (ExitCode, String, String) -> IO String +checkPythonResult (py_code, py_stdout, py_stderr) = do + HUnit.assertEqual ("python exited with error: " ++ py_stderr) + ExitSuccess py_code + return py_stdout + -- | Update an instance to be smaller than a node. setInstanceSmallerThanNode :: Node.Node -> Instance.Instance -> Instance.Instance -- GitLab