2021-10-20 09:10:43 +00:00
|
|
|
let ( >>= ) = Result.bind
|
|
|
|
let ( >>| ) x f = Result.map f x
|
2021-03-16 12:09:31 +00:00
|
|
|
|
|
|
|
module type CONN = Caqti_blocking.CONNECTION
|
|
|
|
|
|
|
|
let () = Mirage_crypto_rng_unix.initialize ()
|
|
|
|
|
|
|
|
let iter f xs = List.fold_left (fun r x -> r >>= fun () -> f x) (Ok ()) xs
|
2021-03-22 09:01:54 +00:00
|
|
|
let get_opt message = function
|
|
|
|
| Some x -> x
|
|
|
|
| None -> Alcotest.fail message
|
|
|
|
|
2021-03-16 12:09:31 +00:00
|
|
|
let or_fail x =
|
|
|
|
match x with
|
|
|
|
| Ok x -> x
|
2021-06-09 09:48:51 +00:00
|
|
|
| Error (`Msg msg) ->
|
|
|
|
Alcotest.failf "Error: %s" msg
|
2021-03-16 12:09:31 +00:00
|
|
|
| Error (#Caqti_error.t as e) ->
|
|
|
|
Alcotest.failf "DB error: %a" Caqti_error.pp e
|
|
|
|
|
2021-03-22 09:01:54 +00:00
|
|
|
module Testable = struct
|
|
|
|
let builder_web_auth =
|
|
|
|
let equal (x : _ Builder_web_auth.user_info) (y : _ Builder_web_auth.user_info) =
|
|
|
|
x.username = y.username &&
|
2021-06-08 14:54:23 +00:00
|
|
|
x.restricted = y.restricted &&
|
2021-03-22 09:01:54 +00:00
|
|
|
match x.password_hash, y.password_hash with
|
|
|
|
| `Scrypt (hash, salt, params), `Scrypt (hash', salt', params') ->
|
|
|
|
Cstruct.equal hash hash' &&
|
|
|
|
Cstruct.equal salt salt' &&
|
|
|
|
params = params'
|
|
|
|
in
|
2021-06-08 14:54:23 +00:00
|
|
|
let pp ppf { Builder_web_auth.username; password_hash; restricted } =
|
2021-03-22 09:01:54 +00:00
|
|
|
match password_hash with
|
|
|
|
| `Scrypt (hash, salt, { Builder_web_auth.scrypt_n; scrypt_r; scrypt_p }) ->
|
2021-06-08 14:54:23 +00:00
|
|
|
Format.fprintf ppf "user:%s;(%d,%d,%d);%B;%a;%a" username
|
|
|
|
scrypt_n scrypt_r scrypt_p restricted
|
2021-03-22 09:01:54 +00:00
|
|
|
Cstruct.hexdump_pp hash Cstruct.hexdump_pp salt
|
|
|
|
in
|
|
|
|
Alcotest.testable
|
|
|
|
pp
|
|
|
|
equal
|
|
|
|
|
|
|
|
let file =
|
|
|
|
let equal (x : Builder_db.Rep.file) (y : Builder_db.Rep.file) =
|
|
|
|
Fpath.equal x.filepath y.filepath &&
|
|
|
|
Fpath.equal x.localpath y.localpath &&
|
|
|
|
Cstruct.equal x.sha256 y.sha256 &&
|
|
|
|
x.size = y.size
|
|
|
|
in
|
|
|
|
let pp ppf { Builder_db.Rep.filepath; localpath; sha256; size } =
|
|
|
|
Format.fprintf ppf "{@[<v 1>@;<1 0>Builder_db.Rep.filepath = %a;@;<1 0>\
|
|
|
|
localpath = %a;@;<1 0>\
|
|
|
|
sha256 = %a;@;<1 0>\
|
|
|
|
size = %d;@;<1 0>\
|
|
|
|
@]@,}"
|
|
|
|
Fpath.pp filepath Fpath.pp localpath
|
|
|
|
Cstruct.hexdump_pp sha256 size
|
|
|
|
in
|
|
|
|
Alcotest.testable pp equal
|
|
|
|
|
|
|
|
let uuid = Alcotest.testable Uuidm.pp Uuidm.equal
|
|
|
|
end
|
|
|
|
|
2021-03-16 12:09:31 +00:00
|
|
|
let setup_db () =
|
|
|
|
Caqti_blocking.connect
|
|
|
|
(Uri.make ~scheme:"sqlite3" ~path:":memory:" ~query:["create", ["true"]] ())
|
|
|
|
>>= fun ((module Db) as conn) ->
|
|
|
|
iter (fun migrate -> Db.exec migrate ()) Builder_db.migrate >>= fun () ->
|
|
|
|
Ok conn
|
|
|
|
|
|
|
|
let scrypt_params = Builder_web_auth.scrypt_params
|
|
|
|
~scrypt_n:1024 ~scrypt_r:1 ()
|
|
|
|
let username = "test" and password = "testtest"
|
2021-06-08 14:54:23 +00:00
|
|
|
let restricted = false
|
2021-03-16 12:09:31 +00:00
|
|
|
(* Bad, but fast *)
|
2021-06-08 14:54:23 +00:00
|
|
|
let auth = Builder_web_auth.hash ~scrypt_params ~username ~password ~restricted ()
|
2021-03-16 12:09:31 +00:00
|
|
|
|
|
|
|
let add_test_user (module Db : CONN) =
|
2021-06-09 11:54:24 +00:00
|
|
|
Db.exec Builder_db.User.add auth >>= fun () ->
|
|
|
|
Db.find Builder_db.last_insert_rowid ()
|
2021-03-16 12:09:31 +00:00
|
|
|
|
|
|
|
let with_user_db f () =
|
|
|
|
or_fail
|
|
|
|
(setup_db () >>= fun conn ->
|
2021-06-09 11:54:24 +00:00
|
|
|
add_test_user conn >>= fun _id ->
|
2021-03-16 12:09:31 +00:00
|
|
|
f conn)
|
|
|
|
|
|
|
|
let test_user_get_all (module Db : CONN) =
|
|
|
|
Db.collect_list Builder_db.User.get_all () >>| fun users ->
|
|
|
|
Alcotest.(check int) "one user" (List.length users) 1
|
|
|
|
|
|
|
|
let test_user_get_user (module Db : CONN) =
|
|
|
|
Db.find_opt Builder_db.User.get_user username >>| fun res ->
|
|
|
|
let auth_opt = Option.map snd res in
|
2021-03-22 09:01:54 +00:00
|
|
|
Alcotest.(check (option Testable.builder_web_auth)) "test user" auth_opt (Some auth)
|
2021-03-16 12:09:31 +00:00
|
|
|
|
|
|
|
let test_user_remove_user (module Db : CONN) =
|
|
|
|
Db.exec Builder_db.User.remove_user username >>= fun () ->
|
|
|
|
Db.find_opt Builder_db.User.get_user username >>| fun res ->
|
|
|
|
let auth_opt = Option.map snd res in
|
2021-03-22 09:01:54 +00:00
|
|
|
Alcotest.(check (option Testable.builder_web_auth)) "remove user" auth_opt None
|
2021-03-16 12:09:31 +00:00
|
|
|
|
|
|
|
let test_user_update (module Db : CONN) =
|
|
|
|
let auth' = Builder_web_auth.hash ~scrypt_params ~username
|
2021-06-08 14:54:23 +00:00
|
|
|
~password:"differentpassword" ~restricted () in
|
2021-03-16 12:09:31 +00:00
|
|
|
Db.exec Builder_db.User.update_user auth' >>= fun () ->
|
|
|
|
Db.find_opt Builder_db.User.get_user username >>| fun res ->
|
|
|
|
let auth_opt = Option.map snd res in
|
2021-03-22 09:01:54 +00:00
|
|
|
Alcotest.(check (option Testable.builder_web_auth)) "update user" auth_opt (Some auth')
|
2021-03-16 12:09:31 +00:00
|
|
|
|
|
|
|
let test_user_auth (module Db : CONN) =
|
|
|
|
Db.find_opt Builder_db.User.get_user username >>| function
|
|
|
|
| None ->
|
|
|
|
Alcotest.fail "user not found"
|
|
|
|
| Some (_id, auth') ->
|
|
|
|
Alcotest.(check bool) "authorized"
|
|
|
|
(Builder_web_auth.verify_password password auth') true
|
|
|
|
|
|
|
|
let test_user_unauth (module Db : CONN) =
|
|
|
|
Db.find_opt Builder_db.User.get_user username >>| function
|
|
|
|
| None ->
|
|
|
|
Alcotest.fail "user not found"
|
|
|
|
| Some (_id, auth') ->
|
|
|
|
Alcotest.(check bool) "unauthorized"
|
|
|
|
(Builder_web_auth.verify_password "wrong" auth') false
|
|
|
|
|
2021-03-22 09:01:54 +00:00
|
|
|
let job_name = "test-job"
|
2021-08-31 11:59:45 +00:00
|
|
|
let script = Fpath.v "/dev/null"
|
2022-03-16 10:44:23 +00:00
|
|
|
let uuid = Uuidm.v `V4
|
2021-08-31 11:59:45 +00:00
|
|
|
let console = Fpath.v "/dev/null"
|
2021-03-22 09:01:54 +00:00
|
|
|
let start = Option.get (Ptime.of_float_s 0.)
|
|
|
|
let finish = Option.get (Ptime.of_float_s 1.)
|
|
|
|
let result = Builder.Exited 0
|
|
|
|
let main_binary =
|
|
|
|
let filepath = Result.get_ok (Fpath.of_string "bin/hello.sh") in
|
|
|
|
let localpath = Result.get_ok (Fpath.of_string "/dev/null") in
|
|
|
|
let data = "#!/bin/sh\necho Hello, World\n" in
|
|
|
|
let sha256 = Mirage_crypto.Hash.SHA256.digest (Cstruct.of_string data) in
|
|
|
|
let size = String.length data in
|
|
|
|
{ Builder_db.Rep.filepath; localpath; sha256; size }
|
2021-11-17 15:28:15 +00:00
|
|
|
let main_binary2 =
|
|
|
|
let data = "#!/bin/sh\necho Hello, World 2\n" in
|
|
|
|
let sha256 = Mirage_crypto.Hash.SHA256.digest (Cstruct.of_string data) in
|
|
|
|
let size = String.length data in
|
|
|
|
{ main_binary with sha256 ; size }
|
2021-11-05 10:45:26 +00:00
|
|
|
let platform = "exotic-os"
|
2021-03-22 09:01:54 +00:00
|
|
|
|
2021-06-09 11:54:24 +00:00
|
|
|
let fail_if_none a =
|
|
|
|
Option.to_result ~none:(`Msg "Failed to retrieve") a
|
2021-06-09 09:48:51 +00:00
|
|
|
|
2021-06-09 11:54:24 +00:00
|
|
|
let add_test_build user_id (module Db : CONN) =
|
2021-03-22 09:01:54 +00:00
|
|
|
let r =
|
|
|
|
let open Builder_db in
|
|
|
|
Db.start () >>= fun () ->
|
|
|
|
Db.exec Job.try_add job_name >>= fun () ->
|
2021-06-09 09:48:51 +00:00
|
|
|
Db.find_opt Job.get_id_by_name job_name >>= fail_if_none >>= fun job_id ->
|
2021-11-05 10:45:26 +00:00
|
|
|
Db.exec Build.add { Build.uuid; start; finish; result; console; script; platform;
|
2021-07-08 11:33:27 +00:00
|
|
|
main_binary = None; input_id = None; user_id; job_id } >>= fun () ->
|
2021-03-22 09:01:54 +00:00
|
|
|
Db.find last_insert_rowid () >>= fun id ->
|
|
|
|
Db.exec Build_artifact.add (main_binary, id) >>= fun () ->
|
2021-06-02 10:29:08 +00:00
|
|
|
Db.find last_insert_rowid () >>= fun main_binary_id ->
|
|
|
|
Db.exec Build.set_main_binary (id, main_binary_id) >>= fun () ->
|
2021-03-22 09:01:54 +00:00
|
|
|
Db.commit ()
|
|
|
|
in
|
2021-10-20 09:10:43 +00:00
|
|
|
Result.fold ~ok:Result.ok ~error:(fun _ -> Db.rollback ())
|
2021-03-22 09:01:54 +00:00
|
|
|
r
|
|
|
|
|
|
|
|
let with_build_db f () =
|
|
|
|
or_fail
|
|
|
|
(setup_db () >>= fun conn ->
|
2021-06-09 11:54:24 +00:00
|
|
|
add_test_user conn >>= fun user_id ->
|
|
|
|
add_test_build user_id conn >>= fun () ->
|
2021-03-22 09:01:54 +00:00
|
|
|
f conn)
|
|
|
|
|
|
|
|
let test_job_get_id_by_name (module Db : CONN) =
|
2021-06-09 09:48:51 +00:00
|
|
|
Db.find_opt Builder_db.Job.get_id_by_name job_name >>= fail_if_none >>| fun _id ->
|
2021-03-22 09:01:54 +00:00
|
|
|
()
|
|
|
|
|
|
|
|
let test_job_get (module Db : CONN) =
|
2021-06-09 09:48:51 +00:00
|
|
|
Db.find_opt Builder_db.Job.get_id_by_name job_name >>= fail_if_none >>= fun job_id ->
|
2021-03-22 09:01:54 +00:00
|
|
|
Db.find_opt Builder_db.Job.get job_id >>| fun job_name' ->
|
|
|
|
Alcotest.(check (option string)) "job equal" job_name' (Some job_name)
|
|
|
|
|
|
|
|
let test_job_remove () =
|
|
|
|
let r =
|
|
|
|
setup_db () >>= fun (module Db : CONN) ->
|
|
|
|
Db.exec Builder_db.Job.try_add "test-job" >>= fun () ->
|
2021-06-09 09:48:51 +00:00
|
|
|
Db.find_opt Builder_db.Job.get_id_by_name "test-job" >>= fail_if_none >>= fun id ->
|
2021-03-22 09:01:54 +00:00
|
|
|
Db.exec Builder_db.Job.remove id >>= fun () ->
|
2021-11-05 12:49:16 +00:00
|
|
|
match Db.find Builder_db.Job.get id with
|
|
|
|
| Error #Caqti_error.call_or_retrieve -> Ok ()
|
|
|
|
| Ok _ -> Alcotest.fail "expected no job"
|
2021-03-22 09:01:54 +00:00
|
|
|
in
|
|
|
|
or_fail r
|
|
|
|
|
|
|
|
let test_build_get_by_uuid (module Db : CONN) =
|
|
|
|
Db.find_opt Builder_db.Build.get_by_uuid uuid
|
|
|
|
>>| get_opt "no build" >>| fun (_id, build) ->
|
|
|
|
Alcotest.(check Testable.uuid) "same uuid" build.uuid uuid
|
|
|
|
|
|
|
|
let test_build_get_all (module Db : CONN) =
|
2021-06-09 09:48:51 +00:00
|
|
|
Db.find_opt Builder_db.Job.get_id_by_name job_name >>= fail_if_none >>= fun job_id ->
|
2021-03-22 09:01:54 +00:00
|
|
|
Db.collect_list Builder_db.Build.get_all job_id >>| fun builds ->
|
|
|
|
Alcotest.(check int) "one build" (List.length builds) 1
|
|
|
|
|
2022-03-16 10:44:23 +00:00
|
|
|
let uuid' = Uuidm.v `V4
|
2021-03-22 09:01:54 +00:00
|
|
|
let start' = Option.get (Ptime.of_float_s 3600.)
|
|
|
|
let finish' = Option.get (Ptime.of_float_s 3601.)
|
|
|
|
|
|
|
|
let add_second_build (module Db : CONN) =
|
|
|
|
let uuid = uuid' and start = start' and finish = finish' in
|
|
|
|
let open Builder_db in
|
2021-06-09 11:54:24 +00:00
|
|
|
Db.find_opt User.get_user username >>= fail_if_none >>= fun (user_id, _) ->
|
2021-03-22 09:01:54 +00:00
|
|
|
Db.start () >>= fun () ->
|
2021-06-09 09:48:51 +00:00
|
|
|
Db.find_opt Job.get_id_by_name job_name >>= fail_if_none >>= fun job_id ->
|
2021-11-05 10:45:26 +00:00
|
|
|
Db.exec Build.add { Build.uuid; start; finish; result; console; script; platform;
|
2021-07-08 11:33:27 +00:00
|
|
|
main_binary = None; input_id = None; user_id; job_id; } >>= fun () ->
|
2021-03-22 09:01:54 +00:00
|
|
|
Db.find last_insert_rowid () >>= fun id ->
|
2021-11-17 15:28:15 +00:00
|
|
|
Db.exec Build_artifact.add (main_binary2, id) >>= fun () ->
|
2021-06-02 10:29:08 +00:00
|
|
|
Db.find last_insert_rowid () >>= fun main_binary_id ->
|
|
|
|
Db.exec Build.set_main_binary (id, main_binary_id) >>= fun () ->
|
2021-03-22 09:01:54 +00:00
|
|
|
Db.commit ()
|
|
|
|
|
|
|
|
let test_build_get_latest (module Db : CONN) =
|
|
|
|
add_second_build (module Db) >>= fun () ->
|
|
|
|
(* Test *)
|
2021-06-09 09:48:51 +00:00
|
|
|
Db.find_opt Builder_db.Job.get_id_by_name job_name >>= fail_if_none >>= fun job_id ->
|
2021-11-17 16:39:49 +00:00
|
|
|
Db.find_opt Builder_db.Build.get_latest_successful_with_binary (job_id, platform)
|
2021-03-22 09:01:54 +00:00
|
|
|
>>| get_opt "no latest build" >>| fun (_id, meta, main_binary') ->
|
2021-11-17 15:28:15 +00:00
|
|
|
Alcotest.(check (option Testable.file)) "same main binary" main_binary' (Some main_binary2);
|
2021-03-22 09:01:54 +00:00
|
|
|
Alcotest.(check Testable.uuid) "same uuid" meta.uuid uuid'
|
|
|
|
|
2021-04-23 10:06:39 +00:00
|
|
|
let test_build_get_previous (module Db : CONN) =
|
|
|
|
add_second_build (module Db) >>= fun () ->
|
|
|
|
Db.find_opt Builder_db.Build.get_by_uuid uuid'
|
|
|
|
>>| get_opt "no build" >>= fun (id, _build) ->
|
2021-11-17 15:28:15 +00:00
|
|
|
Db.find_opt Builder_db.Build.get_previous_successful_different_output id
|
|
|
|
>>| get_opt "no previous build" >>| fun build ->
|
|
|
|
Alcotest.(check Testable.uuid) "same uuid" build.Builder_db.Build.uuid uuid
|
2021-04-23 10:06:39 +00:00
|
|
|
|
|
|
|
let test_build_get_previous_none (module Db : CONN) =
|
|
|
|
Db.find_opt Builder_db.Build.get_by_uuid uuid
|
|
|
|
>>| get_opt "no build" >>= fun (id, _build) ->
|
2021-11-17 15:28:15 +00:00
|
|
|
Db.find_opt Builder_db.Build.get_previous_successful_different_output id >>| function
|
2021-04-23 10:06:39 +00:00
|
|
|
| None -> ()
|
2021-11-17 15:28:15 +00:00
|
|
|
| Some build ->
|
|
|
|
Alcotest.failf "Got unexpected result %a" Uuidm.pp build.Builder_db.Build.uuid
|
2021-04-23 10:06:39 +00:00
|
|
|
|
2021-08-31 11:59:45 +00:00
|
|
|
let test_build_get_with_jobname_by_hash (module Db : CONN) =
|
2021-03-22 09:01:54 +00:00
|
|
|
add_second_build (module Db) >>= fun () ->
|
2021-08-31 11:59:45 +00:00
|
|
|
Db.find_opt Builder_db.Build.get_with_jobname_by_hash main_binary.sha256
|
2021-11-17 15:28:15 +00:00
|
|
|
>>| get_opt "no build" >>= fun (job_name', build) ->
|
|
|
|
Alcotest.(check string) "same job" job_name' job_name;
|
|
|
|
Alcotest.(check Testable.uuid) "same uuid" build.uuid uuid;
|
|
|
|
Db.find_opt Builder_db.Build.get_with_jobname_by_hash main_binary2.sha256
|
2021-03-22 09:01:54 +00:00
|
|
|
>>| get_opt "no build" >>| fun (job_name', build) ->
|
|
|
|
Alcotest.(check string) "same job" job_name' job_name;
|
|
|
|
Alcotest.(check Testable.uuid) "same uuid" build.uuid uuid'
|
|
|
|
|
|
|
|
let test_artifact_get_all_by_build (module Db : CONN) =
|
|
|
|
Db.find_opt Builder_db.Build.get_by_uuid uuid >>|
|
|
|
|
get_opt "no build" >>= fun (id, _build) ->
|
|
|
|
Db.collect_list Builder_db.Build_artifact.get_all_by_build id >>| fun build_artifacts ->
|
|
|
|
Alcotest.(check int) "one build artifact" (List.length build_artifacts) 1
|
|
|
|
|
|
|
|
let test_artifact_get_by_build_uuid (module Db : CONN) =
|
|
|
|
Db.find_opt Builder_db.Build_artifact.get_by_build_uuid
|
|
|
|
(uuid, main_binary.Builder_db.Rep.filepath) >>|
|
|
|
|
get_opt "no build" >>| fun (_id, file) ->
|
|
|
|
Alcotest.(check Testable.file) "same file" file main_binary
|
|
|
|
|
|
|
|
(* XXX: This test should fail because main_binary on the corresponding build
|
|
|
|
* references its main_binary. This is not the case now due to foreign key. *)
|
|
|
|
let test_artifact_remove_by_build (module Db : CONN) =
|
|
|
|
Db.find_opt Builder_db.Build.get_by_uuid uuid >>|
|
|
|
|
get_opt "no build" >>= fun (id, _build) ->
|
|
|
|
Db.exec Builder_db.Build_artifact.remove_by_build id
|
|
|
|
|
2021-03-16 12:09:31 +00:00
|
|
|
let () =
|
|
|
|
let open Alcotest in
|
|
|
|
Alcotest.run "Builder_db" [
|
|
|
|
"user", [
|
|
|
|
test_case "One user" `Quick (with_user_db test_user_get_all);
|
|
|
|
test_case "Get user" `Quick (with_user_db test_user_get_user);
|
|
|
|
test_case "Remove user by name" `Quick (with_user_db test_user_remove_user);
|
|
|
|
test_case "Update user" `Quick (with_user_db test_user_update);
|
|
|
|
];
|
|
|
|
"user-auth", [
|
|
|
|
test_case "User auth success" `Quick (with_user_db test_user_auth);
|
|
|
|
test_case "User auth fail" `Quick (with_user_db test_user_unauth);
|
|
|
|
];
|
2021-03-22 09:01:54 +00:00
|
|
|
"job", [
|
|
|
|
test_case "Add build" `Quick (with_build_db (fun _ -> Ok ()));
|
|
|
|
test_case "Get job id" `Quick (with_build_db test_job_get_id_by_name);
|
|
|
|
test_case "Get job" `Quick (with_build_db test_job_get);
|
|
|
|
test_case "Remove job" `Quick test_job_remove;
|
|
|
|
];
|
|
|
|
"build", [
|
|
|
|
test_case "Get build" `Quick (with_build_db test_build_get_by_uuid);
|
|
|
|
test_case "One build" `Quick (with_build_db test_build_get_all);
|
|
|
|
test_case "Get latest build" `Quick (with_build_db test_build_get_latest);
|
2021-08-31 11:59:45 +00:00
|
|
|
test_case "Get build by hash" `Quick (with_build_db test_build_get_with_jobname_by_hash);
|
2021-04-23 10:06:39 +00:00
|
|
|
test_case "Get previous build" `Quick (with_build_db test_build_get_previous);
|
|
|
|
test_case "Get previous build when first" `Quick (with_build_db test_build_get_previous_none);
|
2021-03-22 09:01:54 +00:00
|
|
|
];
|
|
|
|
"build-artifact", [
|
|
|
|
test_case "Get all by build" `Quick (with_build_db test_artifact_get_all_by_build);
|
|
|
|
test_case "Get by build uuid" `Quick (with_build_db test_artifact_get_by_build_uuid);
|
|
|
|
test_case "Remove by build" `Quick (with_build_db test_artifact_remove_by_build);
|
|
|
|
];
|
2021-03-16 12:09:31 +00:00
|
|
|
]
|