From 5a36814ebe77d5db43d40b026f77eaa422019581 Mon Sep 17 00:00:00 2001 From: Thibault Le Ouay Date: Thu, 7 Nov 2024 15:49:00 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20flaky=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/monitors/monitor_trigger.go | 59 ++--------------------- internal/monitors/monitor_trigger_test.go | 6 +-- internal/monitors/monitors.go | 4 +- internal/run/run.go | 53 ++++++++++++-------- internal/run/run_test.go | 37 ++++++++++++++ 5 files changed, 80 insertions(+), 79 deletions(-) diff --git a/internal/monitors/monitor_trigger.go b/internal/monitors/monitor_trigger.go index 8972c3d..45ddf84 100644 --- a/internal/monitors/monitor_trigger.go +++ b/internal/monitors/monitor_trigger.go @@ -7,8 +7,6 @@ import ( "io" "net/http" - "github.com/fatih/color" - "github.com/rodaine/table" "github.com/urfave/cli/v3" ) @@ -24,7 +22,7 @@ func MonitorTrigger(httpClient *http.Client, apiKey string, monitorId string) er } fmt.Println("Waiting for the result...") - url := fmt.Sprintf("https://api.openstatus.dev/v1/monitor/%s/run", monitorId) + url := fmt.Sprintf("https://api.openstatus.dev/v1/monitor/%s/trigger", monitorId) req, err := http.NewRequest("POST", url, nil) if err != nil { @@ -43,55 +41,14 @@ func MonitorTrigger(httpClient *http.Client, apiKey string, monitorId string) er defer res.Body.Close() body, _ := io.ReadAll(res.Body) - var result []RunResult - err = json.Unmarshal(body, &result) + var r MonitorTriggerResponse + err = json.Unmarshal(body, &r) if err != nil { - return err - } - - headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc() - columnFmt := color.New(color.FgYellow).SprintfFunc() - - tbl := table.New("Region", "Latency (ms)", "Status") - tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt) - - var inError bool - for _, r := range result { - if r.JobType == "tcp" { - var result TCPRunResult - if err := json.Unmarshal(r.Message, &result); err != nil { - return fmt.Errorf("unable to unmarshal : %w", err) - } - if result.ErrorMessge != "" { - inError = true - tbl.AddRow(r.Region, r.Latency, color.RedString("❌")) - continue - } - - } - if r.JobType == "http" { - var result HTTPRunResult - if err := json.Unmarshal(r.Message, &result); err != nil { - return fmt.Errorf("unable to unmarshal : %w", err) - } - if result.Error != "" { - inError = true - tbl.AddRow(r.Region, r.Latency, color.RedString("❌")) - continue - } - } - tbl.AddRow(r.Region, r.Latency, color.GreenString("✔")) + return err } - tbl.Print() - - if inError { - fmt.Println(color.RedString("Some regions failed")) + fmt.Printf("Result ID: %d\n", r.ResultId) - return fmt.Errorf("Some regions failed") - } else { - fmt.Println(color.GreenString("All regions passed")) - } return nil } @@ -100,12 +57,6 @@ func GetMonitorsTriggerCmd() *cli.Command { Name: "trigger", Usage: "Trigger a monitor test", Flags: []cli.Flag{ - - &cli.BoolFlag{ - Name: "no-result", - Usage: "Do not return the result of the test, return the result ID", - Destination: &noResult, - }, &cli.StringFlag{ Name: "access-token", Usage: "OpenStatus API Access Token", diff --git a/internal/monitors/monitor_trigger_test.go b/internal/monitors/monitor_trigger_test.go index 1e22e5d..469b919 100644 --- a/internal/monitors/monitor_trigger_test.go +++ b/internal/monitors/monitor_trigger_test.go @@ -53,9 +53,9 @@ func Test_getMonitorTrigger(t *testing.T) { t.Cleanup(func() { log.SetOutput(os.Stdout) }) - err := monitors.MonitorTrigger(interceptor.GetHTTPClient(), "", "") - if err == nil { - t.Errorf("Expected log output, got nothing") + err := monitors.MonitorTrigger(interceptor.GetHTTPClient(), "", "1") + if err != nil { + t.Errorf("Expected no output, got error") } }) t.Run("No 200 throw error", func(t *testing.T) { diff --git a/internal/monitors/monitors.go b/internal/monitors/monitors.go index 3caa76d..ee241d0 100644 --- a/internal/monitors/monitors.go +++ b/internal/monitors/monitors.go @@ -64,8 +64,8 @@ type HTTPRunResult struct { } type TCPRunResult struct { - ErrorMessge string `json:"errorMessage"` - Timing struct { + ErrorMessage string `json:"errorMessage"` + Timing struct { TCPStart int64 `json:"tcpStart"` TCPDone int64 `json:"tcpDone"` } `json:"timing"` diff --git a/internal/run/run.go b/internal/run/run.go index ef68891..067fc6c 100644 --- a/internal/run/run.go +++ b/internal/run/run.go @@ -40,7 +40,7 @@ func MonitorTrigger(httpClient *http.Client, apiKey string, monitorId string) er defer res.Body.Close() body, _ := io.ReadAll(res.Body) - var result []monitors.RunResult + var result []json.RawMessage err = json.Unmarshal(body, &result) if err != nil { return err @@ -54,31 +54,44 @@ func MonitorTrigger(httpClient *http.Client, apiKey string, monitorId string) er var inError bool for _, r := range result { - if r.JobType == "tcp" { - var result monitors.TCPRunResult - if err := json.Unmarshal(r.Message, &result); err != nil { - return fmt.Errorf("unable to unmarshal : %w", err) - } - if result.ErrorMessge != "" { - inError = true - tbl.AddRow(r.Region, r.Latency, color.RedString("❌")) - continue - } + result := monitors.RunResult{} + if err := json.Unmarshal(r, &result); err != nil { + + return fmt.Errorf("unable to unmarshal : %w", err) } - if r.JobType == "http" { - var result monitors.HTTPRunResult - if err := json.Unmarshal(r.Message, &result); err != nil { - return fmt.Errorf("unable to unmarshal : %w", err) + switch result.JobType { + case "tcp": + { + var tcp monitors.TCPRunResult + if err := json.Unmarshal(r, &result); err != nil { + return fmt.Errorf("unable to unmarshal : %w", err) + } + if tcp.ErrorMessage != "" { + inError = true + tbl.AddRow(result.Region, result.Latency, color.RedString("❌")) + continue + } + } - if result.Error != "" { - inError = true - tbl.AddRow(r.Region, r.Latency, color.RedString("❌")) - continue + case "http": + { + var http monitors.HTTPRunResult + if err := json.Unmarshal(r, &http); err != nil { + fmt.Println("Error", err) + return fmt.Errorf("unable to unmarshal : %w", err) + } + if http.Error != "" { + inError = true + tbl.AddRow(result.Region, result.Latency, color.RedString("❌")) + continue + } } + default: + return fmt.Errorf("Unknown job type") } - tbl.AddRow(r.Region, r.Latency, color.GreenString("✔")) + tbl.AddRow(result.Region, result.Latency, color.GreenString("✔")) } tbl.Print() diff --git a/internal/run/run_test.go b/internal/run/run_test.go index 21f05f2..e815e94 100644 --- a/internal/run/run_test.go +++ b/internal/run/run_test.go @@ -59,6 +59,7 @@ func Test_run(t *testing.T) { t.Run("Successfully run http reponse", func(t *testing.T) { body := `[ { + "jobType": "http", "status": 200, "latency": 318, "region": "iad", @@ -88,6 +89,42 @@ func Test_run(t *testing.T) { }, } + var bf bytes.Buffer + log.SetOutput(&bf) + t.Cleanup(func() { + log.SetOutput(os.Stdout) + }) + err := run.MonitorTrigger(interceptor.GetHTTPClient(), "", "1") + if err != nil { + t.Error(err) + t.Errorf("Monitor Trigger should return error") + } + }) + t.Run("Successfully run tcp reponse", func(t *testing.T) { + body := `[ + { + "jobType": "tcp", + "latency": 3, + "region": "ams", + "timestamp": 1730990324626, + "timing": { + "tcpStart": 1730990324626, + "tcpDone": 1730990324629 + }, + "errorMessage": "" + }]` + + r := io.NopCloser(bytes.NewReader([]byte(body))) + + interceptor := &interceptorHTTPClient{ + f: func(req *http.Request) (*http.Response, error) { + return &http.Response{ + StatusCode: http.StatusOK, + Body: r, + }, nil + }, + } + var bf bytes.Buffer log.SetOutput(&bf) t.Cleanup(func() { -- 2.51.2