prop_LoadJobs :: Property
prop_LoadJobs = monadicIO $ do
  ops <- pick $ resize 5 (listOf1 genQueuedOpCode)
  jid <- pick genJobId
  let job = QueuedJob jid ops justNoTs justNoTs justNoTs Nothing Nothing
      job_s = encode job
  -- check that jobs in the right directories are parsed correctly
  (missing, current, archived, missing_current, broken) <-
    run  . withSystemTempDirectory "jqueue-test-LoadJobs." $ \tempdir -> do
    let load a = loadJobFromDisk tempdir a jid
        live_path = liveJobFile tempdir jid
        arch_path = archivedJobFile tempdir jid
    createDirectory $ tempdir </> jobQueueArchiveSubDir
    createDirectory $ dropFileName arch_path
    -- missing job
    missing <- load True
    writeFile live_path job_s
    -- this should exist
    current <- load False
    removeFile live_path
    writeFile arch_path job_s
    -- this should exist (archived)
    archived <- load True
    -- this should be missing
    missing_current <- load False
    removeFile arch_path
    writeFile live_path "invalid job"
    broken <- load True
    return (missing, current, archived, missing_current, broken)
  _ <- stop $ conjoin [ missing ==? noSuchJob
                      , current ==? Ganeti.BasicTypes.Ok (job, False)
                      , archived ==? Ganeti.BasicTypes.Ok (job, True)
                      , missing_current ==? noSuchJob
                      , counterexample "broken job" (isBad broken)
                      ]
  return ()

-- | Tests computing job directories. Creates random directories,
-- files and stale symlinks in a directory, and checks that we return
-- \"the right thing\".
prop_LoadJobs :: Property
prop_LoadJobs = monadicIO $ do
  ops <- pick $ resize 5 (listOf1 genQueuedOpCode)
  jid <- pick genJobId
  let job = QueuedJob jid ops justNoTs justNoTs justNoTs Nothing Nothing
      job_s = encode job
  -- check that jobs in the right directories are parsed correctly
  (missing, current, archived, missing_current, broken) <-
    run  . withSystemTempDirectory "jqueue-test-LoadJobs." $ \tempdir -> do
    let load a = loadJobFromDisk tempdir a jid
        live_path = liveJobFile tempdir jid
        arch_path = archivedJobFile tempdir jid
    createDirectory $ tempdir </> jobQueueArchiveSubDir
    createDirectory $ dropFileName arch_path
    -- missing job
    missing <- load True
    writeFile live_path job_s
    -- this should exist
    current <- load False
    removeFile live_path
    writeFile arch_path job_s
    -- this should exist (archived)
    archived <- load True
    -- this should be missing
    missing_current <- load False
    removeFile arch_path
    writeFile live_path "invalid job"
    broken <- load True
    return (missing, current, archived, missing_current, broken)
  _ <- stop $ conjoin [ missing ==? noSuchJob
                      , current ==? Ganeti.BasicTypes.Ok (job, False)
                      , archived ==? Ganeti.BasicTypes.Ok (job, True)
                      , missing_current ==? noSuchJob
                      , counterexample "broken job" (isBad broken)
                      ]
  return ()

-- | Tests computing job directories. Creates random directories,
-- files and stale symlinks in a directory, and checks that we return
-- \"the right thing\".
prop_LoadJobs = monadicIO $ do
  ops <- pick $ resize 5 (listOf1 genQueuedOpCode)
  jid <- pick genJobId
  let job = QueuedJob jid ops justNoTs justNoTs justNoTs Nothing Nothing
      job_s = encode job
  -- check that jobs in the right directories are parsed correctly
  (missing, current, archived, missing_current, broken) <-
    run  . withSystemTempDirectory "jqueue-test-LoadJobs." $ \tempdir -> do
    let load a = loadJobFromDisk tempdir a jid
        live_path = liveJobFile tempdir jid
        arch_path = archivedJobFile tempdir jid
    createDirectory $ tempdir </> jobQueueArchiveSubDir
    createDirectory $ dropFileName arch_path
    -- missing job
    missing <- load True
    writeFile live_path job_s
    -- this should exist
    current <- load False
    removeFile live_path
    writeFile arch_path job_s
    -- this should exist (archived)
    archived <- load True
    -- this should be missing
    missing_current <- load False
    removeFile arch_path
    writeFile live_path "invalid job"
    broken <- load True
    return (missing, current, archived, missing_current, broken)
  _ <- stop $ conjoin [ missing ==? noSuchJob
                      , current ==? Ganeti.BasicTypes.Ok (job, False)
                      , archived ==? Ganeti.BasicTypes.Ok (job, True)
                      , missing_current ==? noSuchJob
                      , counterexample "broken job" (isBad broken)
                      ]
  return ()

-- | Tests computing job directories. Creates random directories,
-- files and stale symlinks in a directory, and checks that we return
-- \"the right thing\".