| 1 |
{-# LANGUAGE TemplateHaskell #-} |
| 2 |
|
| 3 |
{-| Unittests for the job queue functionality. |
| 4 |
|
| 5 |
-} |
| 6 |
|
| 7 |
{- |
| 8 |
|
| 9 |
Copyright (C) 2012, 2013 Google Inc. |
| 10 |
All rights reserved. |
| 11 |
|
| 12 |
Redistribution and use in source and binary forms, with or without |
| 13 |
modification, are permitted provided that the following conditions are |
| 14 |
met: |
| 15 |
|
| 16 |
1. Redistributions of source code must retain the above copyright notice, |
| 17 |
this list of conditions and the following disclaimer. |
| 18 |
|
| 19 |
2. Redistributions in binary form must reproduce the above copyright |
| 20 |
notice, this list of conditions and the following disclaimer in the |
| 21 |
documentation and/or other materials provided with the distribution. |
| 22 |
|
| 23 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 24 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 25 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 26 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 27 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 28 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 29 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 30 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 31 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 32 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 |
|
| 35 |
-} |
| 36 |
|
| 37 |
module Test.Ganeti.JQueue (testJQueue) where |
| 38 |
|
| 39 |
import Control.Monad (when) |
| 40 |
import Control.Monad.Fail (MonadFail) |
| 41 |
import Data.Char (isAscii) |
| 42 |
import Data.List (nub, sort) |
| 43 |
import System.Directory |
| 44 |
import System.FilePath |
| 45 |
import System.IO.Temp |
| 46 |
import System.Posix.Files |
| 47 |
import Test.HUnit |
| 48 |
import Test.QuickCheck as QuickCheck |
| 49 |
import Test.QuickCheck.Monadic |
| 50 |
import Text.JSON |
| 51 |
|
| 52 |
import Test.Ganeti.TestCommon |
| 53 |
import Test.Ganeti.TestHelper |
| 54 |
import Test.Ganeti.Types () |
| 55 |
|
| 56 |
import Ganeti.BasicTypes |
| 57 |
import qualified Ganeti.Constants as C |
| 58 |
import Ganeti.JQueue |
| 59 |
import Ganeti.OpCodes |
| 60 |
import Ganeti.Path |
| 61 |
import Ganeti.Types as Types |
| 62 |
import Test.Ganeti.JQueue.Objects (justNoTs, genQueuedOpCode, emptyJob, |
| 63 |
genJobId) |
| 64 |
|
| 65 |
{-# ANN module "HLint: ignore Use camelCase" #-} |
| 66 |
|
| 67 |
-- * Test cases |
| 68 |
|
| 69 |
-- | Tests default priority value. |
| 70 |
case_JobPriorityDef :: Assertion |
| 71 |
case_JobPriorityDef = do |
| 72 |
ej <- emptyJob |
| 73 |
assertEqual "for default priority" C.opPrioDefault $ calcJobPriority ej |
| 74 |
|
| 75 |
-- | Test arbitrary priorities. |
| 76 |
prop_JobPriority :: Property |
| 77 |
prop_JobPriority = |
| 78 |
forAll (listOf1 (genQueuedOpCode `suchThat` |
| 79 |
(not . opStatusFinalized . qoStatus))) |
| 80 |
$ \ops -> property $ do |
| 81 |
jid0 <- makeJobId 0 |
| 82 |
let job = QueuedJob jid0 ops justNoTs justNoTs justNoTs Nothing Nothing |
| 83 |
return $ calcJobPriority job ==? minimum (map qoPriority ops) :: Gen Property |
| 84 |
|
| 85 |
-- | Tests default job status. |
| 86 |
case_JobStatusDef :: Assertion |
| 87 |
case_JobStatusDef = do |
| 88 |
ej <- emptyJob |
| 89 |
assertEqual "for job status" JOB_STATUS_SUCCESS $ calcJobStatus ej |
| 90 |
|
| 91 |
-- | Test some job status properties. |
| 92 |
prop_JobStatus :: Property |
| 93 |
prop_JobStatus = |
| 94 |
forAll genJobId $ \jid -> |
| 95 |
forAll genQueuedOpCode $ \op -> |
| 96 |
let job1 = QueuedJob jid [op] justNoTs justNoTs justNoTs Nothing Nothing |
| 97 |
st1 = calcJobStatus job1 |
| 98 |
op_succ = op { qoStatus = OP_STATUS_SUCCESS } |
| 99 |
op_err = op { qoStatus = OP_STATUS_ERROR } |
| 100 |
op_cnl = op { qoStatus = OP_STATUS_CANCELING } |
| 101 |
op_cnd = op { qoStatus = OP_STATUS_CANCELED } |
| 102 |
-- computes status for a job with an added opcode before |
| 103 |
st_pre_op pop = calcJobStatus (job1 { qjOps = pop:qjOps job1 }) |
| 104 |
-- computes status for a job with an added opcode after |
| 105 |
st_post_op pop = calcJobStatus (job1 { qjOps = qjOps job1 ++ [pop] }) |
| 106 |
in conjoin |
| 107 |
[ counterexample "pre-success doesn't change status" |
| 108 |
(st_pre_op op_succ ==? st1) |
| 109 |
, counterexample "post-success doesn't change status" |
| 110 |
(st_post_op op_succ ==? st1) |
| 111 |
, counterexample "pre-error is error" |
| 112 |
(st_pre_op op_err ==? JOB_STATUS_ERROR) |
| 113 |
, counterexample "pre-canceling is canceling" |
| 114 |
(st_pre_op op_cnl ==? JOB_STATUS_CANCELING) |
| 115 |
, counterexample "pre-canceled is canceled" |
| 116 |
(st_pre_op op_cnd ==? JOB_STATUS_CANCELED) |
| 117 |
] |
| 118 |
|
| 119 |
-- | Tests job status equivalence with Python. Very similar to OpCodes test. |
| 120 |
case_JobStatusPri_py_equiv :: Assertion |
| 121 |
case_JobStatusPri_py_equiv = do |
| 122 |
let num_jobs = 2000::Int |
| 123 |
jobs <- genSample (vectorOf num_jobs $ do |
| 124 |
num_ops <- choose (1, 5) |
| 125 |
ops <- vectorOf num_ops genQueuedOpCode |
| 126 |
jid <- genJobId |
| 127 |
return $ QueuedJob jid ops justNoTs justNoTs justNoTs |
| 128 |
Nothing Nothing) |
| 129 |
let serialized = encode jobs |
| 130 |
-- check for non-ASCII fields, usually due to 'arbitrary :: String' |
| 131 |
mapM_ (\job -> when (any (not . isAscii) (encode job)) . |
| 132 |
assertFailure $ "Job has non-ASCII fields: " ++ show job |
| 133 |
) jobs |
| 134 |
py_stdout <- |
| 135 |
runPython "from ganeti import jqueue\n\ |
| 136 |
\from ganeti import serializer\n\ |
| 137 |
\import sys\n\ |
| 138 |
\job_data = serializer.Load(sys.stdin.read())\n\ |
| 139 |
\decoded = [jqueue._QueuedJob.Restore(None, o, False, False)\n\ |
| 140 |
\ for o in job_data]\n\ |
| 141 |
\encoded = [(job.CalcStatus(), job.CalcPriority())\n\ |
| 142 |
\ for job in decoded]\n\ |
| 143 |
\sys.stdout.buffer.write(serializer.Dump(encoded))" serialized |
| 144 |
>>= checkPythonResult |
| 145 |
let deserialised = decode py_stdout::Text.JSON.Result [(String, Int)] |
| 146 |
decoded <- case deserialised of |
| 147 |
Text.JSON.Ok jobs' -> return jobs' |
| 148 |
Error msg -> |
| 149 |
assertFailure ("Unable to decode jobs: " ++ msg) |
| 150 |
-- this already raised an exception, but we need it |
| 151 |
-- for proper types |
| 152 |
>> fail "Unable to decode jobs" |
| 153 |
assertEqual "Mismatch in number of returned jobs" |
| 154 |
(length decoded) (length jobs) |
| 155 |
mapM_ (\(py_sp, job) -> |
| 156 |
let hs_sp = (jobStatusToRaw $ calcJobStatus job, |
| 157 |
calcJobPriority job) |
| 158 |
in assertEqual ("Different result after encoding/decoding for " ++ |
| 159 |
show job) hs_sp py_sp |
| 160 |
) $ zip decoded jobs |
| 161 |
|
| 162 |
-- | Tests listing of Job ids. |
| 163 |
prop_ListJobIDs :: Property |
| 164 |
prop_ListJobIDs = monadicIO $ do |
| 165 |
let extractJobIDs :: (Show e, MonadFail m) => m (GenericResult e a) -> m a |
| 166 |
extractJobIDs = (>>= genericResult (fail . show) return) |
| 167 |
jobs <- pick $ resize 10 (listOf1 genJobId `suchThat` (\l -> l == nub l)) |
| 168 |
(e, f, g) <- |
| 169 |
run . withSystemTempDirectory "jqueue-test-ListJobIDs." $ \tempdir -> do |
| 170 |
empty_dir <- extractJobIDs $ getJobIDs [tempdir] |
| 171 |
mapM_ (\jid -> writeFile (tempdir </> jobFileName jid) "") jobs |
| 172 |
full_dir <- extractJobIDs $ getJobIDs [tempdir] |
| 173 |
invalid_dir <- getJobIDs [tempdir </> "no-such-dir"] |
| 174 |
return (empty_dir, sortJobIDs full_dir, invalid_dir) |
| 175 |
_ <- stop $ conjoin [ counterexample "empty directory" $ e ==? [] |
| 176 |
, counterexample "directory with valid names" $ |
| 177 |
f ==? sortJobIDs jobs |
| 178 |
, counterexample "invalid directory" $ isBad g |
| 179 |
] |
| 180 |
return () |
| 181 |
|
| 182 |
-- | Tests loading jobs from disk. |
| 183 |
prop_LoadJobs :: Property |
| 184 |
prop_LoadJobs = monadicIO $ do |
| 185 |
ops <- pick $ resize 5 (listOf1 genQueuedOpCode) |
| 186 |
jid <- pick genJobId |
| 187 |
let job = QueuedJob jid ops justNoTs justNoTs justNoTs Nothing Nothing |
| 188 |
job_s = encode job |
| 189 |
-- check that jobs in the right directories are parsed correctly |
| 190 |
(missing, current, archived, missing_current, broken) <- |
| 191 |
run . withSystemTempDirectory "jqueue-test-LoadJobs." $ \tempdir -> do |
| 192 |
let load a = loadJobFromDisk tempdir a jid |
| 193 |
live_path = liveJobFile tempdir jid |
| 194 |
arch_path = archivedJobFile tempdir jid |
| 195 |
createDirectory $ tempdir </> jobQueueArchiveSubDir |
| 196 |
createDirectory $ dropFileName arch_path |
| 197 |
-- missing job |
| 198 |
missing <- load True |
| 199 |
writeFile live_path job_s |
| 200 |
-- this should exist |
| 201 |
current <- load False |
| 202 |
removeFile live_path |
| 203 |
writeFile arch_path job_s |
| 204 |
-- this should exist (archived) |
| 205 |
archived <- load True |
| 206 |
-- this should be missing |
| 207 |
missing_current <- load False |
| 208 |
removeFile arch_path |
| 209 |
writeFile live_path "invalid job" |
| 210 |
broken <- load True |
| 211 |
return (missing, current, archived, missing_current, broken) |
| 212 |
_ <- stop $ conjoin [ missing ==? noSuchJob |
| 213 |
, current ==? Ganeti.BasicTypes.Ok (job, False) |
| 214 |
, archived ==? Ganeti.BasicTypes.Ok (job, True) |
| 215 |
, missing_current ==? noSuchJob |
| 216 |
, counterexample "broken job" (isBad broken) |
| 217 |
] |
| 218 |
return () |
| 219 |
|
| 220 |
-- | Tests computing job directories. Creates random directories, |
| 221 |
-- files and stale symlinks in a directory, and checks that we return |
| 222 |
-- \"the right thing\". |
| 223 |
prop_DetermineDirs :: Property |
| 224 |
prop_DetermineDirs = monadicIO $ do |
| 225 |
count <- pick $ choose (2, 10) |
| 226 |
nums <- pick $ genUniquesList count |
| 227 |
(arbitrary::Gen (QuickCheck.Positive Int)) |
| 228 |
let (valid, invalid) = splitAt (count `div` 2) $ |
| 229 |
map (\(QuickCheck.Positive i) -> show i) nums |
| 230 |
(tempdir, non_arch, with_arch, invalid_root) <- |
| 231 |
run . withSystemTempDirectory "jqueue-test-DetermineDirs." $ \tempdir -> do |
| 232 |
let arch_dir = tempdir </> jobQueueArchiveSubDir |
| 233 |
createDirectory arch_dir |
| 234 |
mapM_ (createDirectory . (arch_dir </>)) valid |
| 235 |
mapM_ (\p -> writeFile (arch_dir </> p) "") invalid |
| 236 |
mapM_ (\p -> createSymbolicLink "/dev/null/no/such/file" |
| 237 |
(arch_dir </> p <.> "missing")) invalid |
| 238 |
non_arch <- determineJobDirectories tempdir False |
| 239 |
with_arch <- determineJobDirectories tempdir True |
| 240 |
invalid_root <- determineJobDirectories (tempdir </> "no-such-subdir") True |
| 241 |
return (tempdir, non_arch, with_arch, invalid_root) |
| 242 |
let arch_dir = tempdir </> jobQueueArchiveSubDir |
| 243 |
_ <- stop $ conjoin [ non_arch ==? [tempdir] |
| 244 |
, sort with_arch ==? |
| 245 |
sort (tempdir:map (arch_dir </>) valid) |
| 246 |
, invalid_root ==? [tempdir </> "no-such-subdir"] |
| 247 |
] |
| 248 |
return () |
| 249 |
|
| 250 |
-- | Tests the JSON serialisation for 'InputOpCode'. |
| 251 |
prop_InputOpCode :: MetaOpCode -> Int -> Property |
| 252 |
prop_InputOpCode meta i = |
| 253 |
conjoin [ readJSON (showJSON valid) ==? Text.JSON.Ok valid |
| 254 |
, readJSON (showJSON invalid) ==? Text.JSON.Ok invalid |
| 255 |
] |
| 256 |
where valid = ValidOpCode meta |
| 257 |
invalid = InvalidOpCode (showJSON i) |
| 258 |
|
| 259 |
-- | Tests 'extractOpSummary'. |
| 260 |
prop_extractOpSummary :: MetaOpCode -> Int -> Property |
| 261 |
prop_extractOpSummary meta i = |
| 262 |
conjoin [ counterexample "valid opcode" $ |
| 263 |
extractOpSummary (ValidOpCode meta) ==? summary |
| 264 |
, counterexample "invalid opcode, correct object" $ |
| 265 |
extractOpSummary (InvalidOpCode jsobj) ==? summary |
| 266 |
, counterexample "invalid opcode, empty object" $ |
| 267 |
extractOpSummary (InvalidOpCode emptyo) ==? invalid |
| 268 |
, counterexample "invalid opcode, object with invalid OP_ID" $ |
| 269 |
extractOpSummary (InvalidOpCode invobj) ==? invalid |
| 270 |
, counterexample "invalid opcode, not jsobject" $ |
| 271 |
extractOpSummary (InvalidOpCode jsinval) ==? invalid |
| 272 |
] |
| 273 |
where summary = opSummary (metaOpCode meta) |
| 274 |
jsobj = showJSON $ toJSObject [("OP_ID", |
| 275 |
showJSON ("OP_" ++ summary))] |
| 276 |
emptyo = showJSON $ toJSObject ([]::[(String, JSValue)]) |
| 277 |
invobj = showJSON $ toJSObject [("OP_ID", showJSON False)] |
| 278 |
jsinval = showJSON i |
| 279 |
invalid = "INVALID_OP" |
| 280 |
|
| 281 |
testSuite "JQueue" |
| 282 |
[ 'case_JobPriorityDef |
| 283 |
, 'prop_JobPriority |
| 284 |
, 'case_JobStatusDef |
| 285 |
, 'prop_JobStatus |
| 286 |
, 'case_JobStatusPri_py_equiv |
| 287 |
, 'prop_ListJobIDs |
| 288 |
, 'prop_LoadJobs |
| 289 |
, 'prop_DetermineDirs |
| 290 |
, 'prop_InputOpCode |
| 291 |
, 'prop_extractOpSummary |
| 292 |
] |