module Test.Ganeti.Kvmd (testKvmd) where
import Control.Concurrent
import Control.Exception (try)
import qualified Network.Socket as Socket
import System.Directory
import System.FilePath
import System.IO
import qualified Ganeti.Kvmd as Kvmd
import qualified Ganeti.UDSServer as UDSServer
import Test.HUnit as HUnit
import qualified Test.Ganeti.TestHelper as TestHelper (testSuite)
import qualified Test.Ganeti.TestCommon as TestCommon (getTempFileName)
import qualified Ganeti.Logging as Logging
startKvmd :: FilePath -> IO ThreadId
startKvmd dir =
forkIO (do Logging.setupLogging Nothing "ganeti-kvmd" False False
False Logging.SyslogNo
Kvmd.startWith dir)
stopKvmd :: ThreadId -> IO ()
stopKvmd = killThread
delayKvmd :: IO ()
delayKvmd = threadDelay 1000000
detectShutdown :: (Handle -> IO ()) -> IO Bool
detectShutdown putFn =
do monitorDir <- TestCommon.getTempFileName "ganeti"
let monitor = "instance" <.> Kvmd.monitorExtension
monitorFile = monitorDir </> monitor
shutdownFile = Kvmd.shutdownPath monitorFile
createDirectoryIfMissing True monitorDir
(try (removeFile shutdownFile) :: IO (Either IOError ())) >> return ()
threadId <- startKvmd monitorDir
threadDelay 1000
sock <- UDSServer.openServerSocket monitorFile
Socket.listen sock 1
handle <- UDSServer.acceptSocket sock
res <- try . hGetLine $ handle :: IO (Either IOError String)
case res of
Left err ->
assertFailure $ "Expecting " ++ show Kvmd.monitorGreeting ++
", received " ++ show err
Right str -> Kvmd.monitorGreeting @=? str
putFn handle
hFlush handle
UDSServer.closeClientSocket handle
UDSServer.closeServerSocket sock monitorFile
delayKvmd
stopKvmd threadId
doesFileExist shutdownFile
case_DetectAdminShutdown :: Assertion
case_DetectAdminShutdown =
do res <- detectShutdown putMessage
assertBool "Detected user shutdown instead of administrator shutdown" $
not res
where putMessage handle =
do hPrint handle "POWERDOWN"
hPrint handle "SHUTDOWN"
case_DetectUserShutdown :: Assertion
case_DetectUserShutdown =
do res <- detectShutdown putMessage
assertBool "Detected administrator shutdown instead of user shutdown" res
where putMessage handle =
hPrint handle "SHUTDOWN"
TestHelper.testSuite "Kvmd"
[ 'case_DetectAdminShutdown
, 'case_DetectUserShutdown
]