Skip to content

Commit

Permalink
tests: load secrets and config files per test case
Browse files Browse the repository at this point in the history
We can now configure secrets, config, state, and incoming payload
separately, which lets us test multi-repo behavior more easily.
  • Loading branch information
yasunariw committed Dec 25, 2020
1 parent 4670f0e commit 6adbf96
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 53 deletions.
4 changes: 3 additions & 1 deletion lib/api_local.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ let cwd = Caml.Sys.getcwd ()
let cache_dir = Caml.Filename.concat cwd "github-api-cache"

module Github : Api.Github = struct
let mock_config_dir = Caml.Filename.concat Caml.Filename.parent_dir_name "mock_config"

let get_config ~(ctx : Context.t) ~repo:_ =
let url = Caml.Filename.concat cwd ctx.config_filename in
let url = Caml.Filename.concat mock_config_dir ctx.config_filename in
match get_local_file url with
| Error e -> Lwt.return @@ fmt_error "error while getting local file: %s\nfailed to get config %s" e url
| Ok file -> Lwt.return @@ Ok (Config_j.config_of_string file)
Expand Down
File renamed without changes.
File renamed without changes.
16 changes: 9 additions & 7 deletions mock_states/status.state_hide_success_test.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

{
"pipeline_statuses": {
"default": {
"master": "failure"
},
"buildkite/notabot-test": {
"master": "success"
"https://git.ahrefs.com/ahrefs/notabot_test": {
"pipeline_statuses": {
"default": {
"master": "failure"
},
"buildkite/notabot-test": {
"master": "success"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"pipeline_statuses": {
"buildkite/notabot-test": {
"master": "failure"
"https://git.ahrefs.com/ahrefs/notabot_test": {
"pipeline_statuses": {
"buildkite/notabot-test": {
"master": "failure"
}
}
}
}
}
6 changes: 3 additions & 3 deletions test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
(deps
(source_tree ../mock_states)
(source_tree ../mock_payloads)
(source_tree github-api-cache)
notabot.json
secrets.json)
(source_tree ../mock_config)
(source_tree ../mock_secrets)
(source_tree github-api-cache))
(action
(with-stdout-to
slack_payloads.out
Expand Down
59 changes: 21 additions & 38 deletions test/test.ml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
open Base
open Lib
open Common

let log = Devkit.Log.from "test"

let () = Devkit.Log.set_loglevels "error"

let mock_payload_dir = Caml.Filename.concat Caml.Filename.parent_dir_name "mock_payloads"

let mock_state_dir = Caml.Filename.concat Caml.Filename.parent_dir_name "mock_states"

let mock_secrets_dir = Caml.Filename.concat Caml.Filename.parent_dir_name "mock_secrets"

module Action_local = Action.Action (Api_local.Github) (Api_local.Slack)

let get_mock_payloads () =
Expand All @@ -19,51 +24,29 @@ let get_mock_payloads () =
let state_path = Caml.Filename.concat mock_state_dir fn in
if Caml.Sys.file_exists state_path then kind, payload_path, Some state_path else kind, payload_path, None)

let process ~(secrets : Config_t.secrets) ~config (kind, path, state_path) =
let headers = [ "x-github-event", kind ] in
let make_test_context event =
let repo = Github.repo_of_notification @@ Github.parse_exn headers event in
let ctx = Context.make () in
ctx.secrets <- Some secrets;
ignore (State.find_or_add_repo ctx.state repo.url);
match state_path with
| None ->
State.set_repo_config ctx.state ~config ~repo_url:repo.url;
Lwt.return ctx
| Some state_path ->
match Common.get_local_file state_path with
| Error e ->
log#error "failed to read %s: %s" state_path e;
Lwt.return ctx
| Ok file ->
let repo_state = State_j.repo_state_of_string file in
Hashtbl.set ctx.state ~key:repo.url ~data:repo_state;
State.set_repo_config ctx.state ~repo_url:repo.url ~config;
Lwt.return ctx
let process (kind, path, state_filepath) =
let make_test_context () =
let config_filename = Context.default.config_filename in
let secrets_filepath = Caml.Filename.concat mock_secrets_dir Context.default.secrets_filepath in
let ctx = Context.make ~config_filename ~secrets_filepath ?state_filepath ~verbose:false () in
match Context.refresh_state ctx with
| Error e -> fmt_error "failed to read state: %s" e
| Ok ctx ->
match Context.refresh_secrets ctx with
| Error e -> fmt_error "failed to read secrets: %s" e
| Ok ctx -> Ok ctx
in
Stdio.printf "===== file %s =====\n" path;
let headers = [ "x-github-event", kind ] in
match Common.get_local_file path with
match get_local_file path with
| Error e -> Lwt.return @@ log#error "failed to read %s: %s" path e
| Ok event ->
let%lwt ctx = make_test_context event in
match make_test_context () with
| Error e -> Lwt.return @@ log#error "%s" e
| Ok ctx ->
let%lwt _ctx = Action_local.process_github_notification ctx headers event in
Lwt.return_unit

let () =
let payloads = get_mock_payloads () in
let repo : Github_t.repository = { name = ""; full_name = ""; url = ""; commits_url = ""; contents_url = "" } in
let ctx = Context.make ~state_filepath:"state.json" () in
Lwt_main.run
( match%lwt Api_local.Github.get_config ~ctx ~repo with
| Error e ->
log#error "%s" e;
Lwt.return_unit
| Ok config ->
match Context.refresh_secrets ctx with
| Ok ctx -> Lwt_list.iter_s (process ~secrets:(Option.value_exn ctx.secrets) ~config) payloads
| Error e ->
log#error "failed to read secrets:";
log#error "%s" e;
Lwt.return_unit
)
Lwt_main.run (Lwt_list.iter_s process payloads)

0 comments on commit 6adbf96

Please sign in to comment.