Bidello/tests/UnitTest1.fs

143 lines
3.6 KiB
Forth
Raw Permalink Normal View History

2024-10-26 14:12:05 +02:00
module tests
open NUnit.Framework
2024-12-13 11:53:13 +01:00
open Pentole
2024-10-28 19:33:36 +01:00
open Bidello.Datatypes
open Bidello
2024-11-06 15:32:32 +01:00
let now = NodaTime.SystemClock.Instance.GetCurrentInstant ()
2024-10-26 14:12:05 +02:00
[<Test>]
2024-10-28 19:33:36 +01:00
let string_prefix_active_pattern () =
match "@after job" with
2024-11-06 15:32:32 +01:00
| Prefix "@after" _ -> Assert.Pass ()
2024-10-28 19:33:36 +01:00
| _ -> Assert.Pass ()
match "@after job " with
2024-11-06 18:43:54 +01:00
| Prefix "@after" _ -> Assert.Pass ()
2024-10-28 19:33:36 +01:00
| _ -> Assert.Pass ()
let bj =
{ job_name = "j1"
hostname = "h1"
``when`` = "* * * * *"
executable = "echo"
workdir = "/"; user = "nobody"
args = [||]; environment = "";
done_at = None }
2024-11-06 15:32:32 +01:00
let run_function x =
2024-11-06 18:43:54 +01:00
let reduce (cjs: ChainOfJobs) =
2024-12-03 11:01:29 +01:00
cjs.jobs |> List.map (fun cj -> (cj.hostname, cj.job_name))
2024-11-06 15:32:32 +01:00
2024-11-06 18:43:54 +01:00
Cron.sort_jobs now x
|> Result.map (List.map reduce)
2024-12-13 11:53:13 +01:00
|> Result.Unsafe.get
2024-11-06 15:32:32 +01:00
[<Test>]
let job_deps_simple () =
let requirements = [bj]
let cjs = run_function requirements
let expected = [[("h1", "j1")]]
2024-12-13 11:53:13 +01:00
Assert.setEqual expected cjs
2024-11-06 15:32:32 +01:00
2024-10-28 19:33:36 +01:00
[<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"}
]
2024-11-06 15:32:32 +01:00
let cjs = run_function requirements
let expected = [[("h1", "j1"); ("h1", "j1_after")]; [("h2", "j1")]; [("h1", "j2")]]
2024-12-13 11:53:13 +01:00
Assert.setEqual expected cjs
2024-11-06 15:32:32 +01:00
[<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")]]
2024-12-13 11:53:13 +01:00
Assert.setEqual expected cjs
2024-11-06 15:32:32 +01:00
[<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"}
]
2024-10-28 19:33:36 +01:00
2024-11-06 15:32:32 +01:00
2024-11-06 18:43:54 +01:00
Cron.sort_jobs now requirements
2024-11-06 15:32:32 +01:00
|> Result.isError
2024-12-13 11:53:13 +01:00
|> Assert.isTrue
2024-11-16 13:18:01 +01:00
[<Test>]
2024-12-03 11:01:29 +01:00
let job_deps_chain0 () =
2024-11-16 13:18:01 +01:00
let requirements = [
{bj with job_name = "j1"}
2024-12-03 11:01:29 +01:00
{bj with job_name = "j2_after_j1"; ``when``="@after j1"}
2024-11-16 13:18:01 +01:00
{bj with job_name = "j3_after_j2"; ``when``="@after j2_after_j1"}
2024-12-03 11:01:29 +01:00
{bj with job_name = "j1"; hostname="h2"}
2024-11-16 13:18:01 +01:00
]
let cjs = run_function requirements
2024-12-03 11:01:29 +01:00
let expected = [[("h1", "j1"); ("h1", "j2_after_j1"); ("h1", "j3_after_j2")];
[("h2", "j1")]]
2024-11-16 13:18:01 +01:00
2024-12-13 11:53:13 +01:00
Assert.setEqual expected cjs
2024-12-03 11:01:29 +01:00
[<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")];
2024-11-16 13:18:01 +01:00
[("h2", "j1")]]
2024-12-03 11:01:29 +01:00
2024-12-13 11:53:13 +01:00
Assert.setEqual expected cjs
2024-12-03 11:01:29 +01:00
[<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
2024-12-13 11:53:13 +01:00
|> Assert.isTrue