Bidello/tests/UnitTest1.fs
2024-12-13 11:53:13 +01:00

142 lines
3.6 KiB
Forth

module tests
open NUnit.Framework
open Pentole
open Bidello.Datatypes
open Bidello
let now = NodaTime.SystemClock.Instance.GetCurrentInstant ()
[<Test>]
let string_prefix_active_pattern () =
match "@after job" with
| Prefix "@after" _ -> Assert.Pass ()
| _ -> Assert.Pass ()
match "@after job " with
| Prefix "@after" _ -> Assert.Pass ()
| _ -> Assert.Pass ()
let bj =
{ job_name = "j1"
hostname = "h1"
``when`` = "* * * * *"
executable = "echo"
workdir = "/"; user = "nobody"
args = [||]; environment = "";
done_at = None }
let run_function x =
let reduce (cjs: ChainOfJobs) =
cjs.jobs |> List.map (fun cj -> (cj.hostname, cj.job_name))
Cron.sort_jobs now x
|> Result.map (List.map reduce)
|> Result.Unsafe.get
[<Test>]
let job_deps_simple () =
let requirements = [bj]
let cjs = run_function requirements
let expected = [[("h1", "j1")]]
Assert.setEqual expected cjs
[<Test>]
let job_deps () =
let requirements = [
bj
{bj with job_name = "j2"}
{bj with hostname = "h2"}
{bj with job_name = "j1_after"; ``when``="@after j1"}
]
let cjs = run_function requirements
let expected = [[("h1", "j1"); ("h1", "j1_after")]; [("h2", "j1")]; [("h1", "j2")]]
Assert.setEqual expected cjs
[<Test>]
let job_deps2 () =
let requirements = [
bj
{bj with job_name = "j2"}
{bj with hostname = "h2"}
{bj with job_name = "j1_after"; ``when``="@after j1"}
{bj with job_name = "j2_after"; ``when``="@after j2"}
]
let cjs = run_function requirements
let expected = [[("h1", "j1"); ("h1", "j1_after")];
[("h1", "j2"); ("h1", "j2_after")];
[("h2", "j1")]]
Assert.setEqual expected cjs
[<Test>]
let should_fail_no_host () =
let requirements = [
bj
{bj with job_name = "j2"}
{bj with hostname = "h2"}
{bj with job_name = "j1_after"; ``when``="@after j1"; hostname="nope"}
{bj with job_name = "j2_after"; ``when``="@after j2"}
]
Cron.sort_jobs now requirements
|> Result.isError
|> Assert.isTrue
[<Test>]
let job_deps_chain0 () =
let requirements = [
{bj with job_name = "j1"}
{bj with job_name = "j2_after_j1"; ``when``="@after j1"}
{bj with job_name = "j3_after_j2"; ``when``="@after j2_after_j1"}
{bj with job_name = "j1"; hostname="h2"}
]
let cjs = run_function requirements
let expected = [[("h1", "j1"); ("h1", "j2_after_j1"); ("h1", "j3_after_j2")];
[("h2", "j1")]]
Assert.setEqual expected cjs
[<Test>]
let job_deps_chain1 () =
let requirements = [
{bj with job_name = "j1"}
{bj with job_name = "j2_after_j1"; ``when``="@after j1"}
{bj with job_name = "j3_after_j2"; ``when``="@after j2_after_j1"}
{bj with job_name = "j2'_after_j1"; ``when``="@after j1"}
{bj with job_name = "j3'_after_j2'"; ``when``="@after j2'_after_j1"}
{bj with job_name = "j1"; hostname="h2"}
]
let cjs = run_function requirements
printfn "GOT: %A" cjs
let expected = [[("h1", "j1"); ("h1", "j2'_after_j1"); ("h1", "j3'_after_j2'");
("h1", "j2_after_j1"); ("h1", "j3_after_j2")];
[("h2", "j1")]]
Assert.setEqual expected cjs
[<Test>]
let job_deps_chain_failure () =
let requirements = [
{bj with job_name = "j1"}
{bj with job_name = "should_fail"; ``when``="@after j3"}
]
Cron.sort_jobs now requirements
|> Result.isError
|> Assert.isTrue