| 1 |
{-# LANGUAGE TemplateHaskell #-} |
| 2 |
{-| Unittests for the KVM daemon. |
| 3 |
|
| 4 |
-} |
| 5 |
|
| 6 |
{- |
| 7 |
|
| 8 |
Copyright (C) 2013 Google Inc. |
| 9 |
All rights reserved. |
| 10 |
|
| 11 |
Redistribution and use in source and binary forms, with or without |
| 12 |
modification, are permitted provided that the following conditions are |
| 13 |
met: |
| 14 |
|
| 15 |
1. Redistributions of source code must retain the above copyright notice, |
| 16 |
this list of conditions and the following disclaimer. |
| 17 |
|
| 18 |
2. Redistributions in binary form must reproduce the above copyright |
| 19 |
notice, this list of conditions and the following disclaimer in the |
| 20 |
documentation and/or other materials provided with the distribution. |
| 21 |
|
| 22 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 23 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 24 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 25 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 26 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 27 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 28 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 29 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 30 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 31 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 32 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
|
| 34 |
-} |
| 35 |
|
| 36 |
module Test.Ganeti.Kvmd (testKvmd) where |
| 37 |
|
| 38 |
import Control.Concurrent |
| 39 |
import Control.Exception (try) |
| 40 |
import qualified Network.Socket as Socket |
| 41 |
import System.Directory |
| 42 |
import System.FilePath |
| 43 |
import System.IO |
| 44 |
|
| 45 |
import qualified Ganeti.Kvmd as Kvmd |
| 46 |
import qualified Ganeti.UDSServer as UDSServer |
| 47 |
import Test.HUnit as HUnit |
| 48 |
|
| 49 |
import qualified Test.Ganeti.TestHelper as TestHelper (testSuite) |
| 50 |
import qualified Test.Ganeti.TestCommon as TestCommon (getTempFileName) |
| 51 |
|
| 52 |
import qualified Ganeti.Logging as Logging |
| 53 |
|
| 54 |
{-# ANN module "HLint: ignore Use camelCase" #-} |
| 55 |
|
| 56 |
startKvmd :: FilePath -> IO ThreadId |
| 57 |
startKvmd dir = |
| 58 |
forkIO (do Logging.setupLogging Nothing "ganeti-kvmd" False False |
| 59 |
False Logging.SyslogNo |
| 60 |
Kvmd.startWith dir) |
| 61 |
|
| 62 |
stopKvmd :: ThreadId -> IO () |
| 63 |
stopKvmd = killThread |
| 64 |
|
| 65 |
delayKvmd :: IO () |
| 66 |
delayKvmd = threadDelay 1000000 |
| 67 |
|
| 68 |
detectShutdown :: (Handle -> IO ()) -> IO Bool |
| 69 |
detectShutdown putFn = |
| 70 |
do monitorDir <- TestCommon.getTempFileName "ganeti" |
| 71 |
let monitor = "instance" <.> Kvmd.monitorExtension |
| 72 |
monitorFile = monitorDir </> monitor |
| 73 |
shutdownFile = Kvmd.shutdownPath monitorFile |
| 74 |
-- ensure the KVM directory exists |
| 75 |
createDirectoryIfMissing True monitorDir |
| 76 |
-- ensure the shutdown file does not exist |
| 77 |
(try (removeFile shutdownFile) :: IO (Either IOError ())) >> return () |
| 78 |
-- start KVM daemon |
| 79 |
threadId <- startKvmd monitorDir |
| 80 |
threadDelay 1000 |
| 81 |
-- create a Unix socket |
| 82 |
sock <- UDSServer.openServerSocket monitorFile |
| 83 |
Socket.listen sock 1 |
| 84 |
handle <- UDSServer.acceptSocket sock |
| 85 |
-- read 'qmp_capabilities' message |
| 86 |
res <- try . hGetLine $ handle :: IO (Either IOError String) |
| 87 |
case res of |
| 88 |
Left err -> |
| 89 |
assertFailure $ "Expecting " ++ show Kvmd.monitorGreeting ++ |
| 90 |
", received " ++ show err |
| 91 |
Right str -> Kvmd.monitorGreeting @=? str |
| 92 |
-- send Qmp messages |
| 93 |
putFn handle |
| 94 |
hFlush handle |
| 95 |
-- close the Unix socket |
| 96 |
UDSServer.closeClientSocket handle |
| 97 |
UDSServer.closeServerSocket sock monitorFile |
| 98 |
-- KVM needs time to create the shutdown file |
| 99 |
delayKvmd |
| 100 |
-- stop the KVM daemon |
| 101 |
stopKvmd threadId |
| 102 |
-- check for shutdown file |
| 103 |
doesFileExist shutdownFile |
| 104 |
|
| 105 |
case_DetectAdminShutdown :: Assertion |
| 106 |
case_DetectAdminShutdown = |
| 107 |
do res <- detectShutdown putMessage |
| 108 |
assertBool "Detected user shutdown instead of administrator shutdown" $ |
| 109 |
not res |
| 110 |
where putMessage handle = |
| 111 |
do hPrint handle "POWERDOWN" |
| 112 |
hPrint handle "SHUTDOWN" |
| 113 |
|
| 114 |
case_DetectUserShutdown :: Assertion |
| 115 |
case_DetectUserShutdown = |
| 116 |
do res <- detectShutdown putMessage |
| 117 |
assertBool "Detected administrator shutdown instead of user shutdown" res |
| 118 |
where putMessage handle = |
| 119 |
hPrint handle "SHUTDOWN" |
| 120 |
|
| 121 |
TestHelper.testSuite "Kvmd" |
| 122 |
[ 'case_DetectAdminShutdown |
| 123 |
, 'case_DetectUserShutdown |
| 124 |
] |