From 7674f907c45b7b10d2b191681eefc084f301dd67 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Thu, 13 Oct 2022 10:42:46 +0200 Subject: [PATCH] replace log.Fatal with t.Fatal (#1805) This is required to run deferred teardown functions --- pkg/apiclient/auth_service_test.go | 16 ++++++++-------- pkg/apiclient/auth_test.go | 8 ++++---- pkg/apiclient/client_http_test.go | 5 ++--- pkg/apiclient/client_test.go | 6 +++--- pkg/apiclient/decisions_service_test.go | 20 ++++++++++---------- 5 files changed, 27 insertions(+), 28 deletions(-) diff --git a/pkg/apiclient/auth_service_test.go b/pkg/apiclient/auth_service_test.go index 4cee22d92..1eb5138e3 100644 --- a/pkg/apiclient/auth_service_test.go +++ b/pkg/apiclient/auth_service_test.go @@ -42,7 +42,7 @@ func TestWatcherAuth(t *testing.T) { log.Printf("URL is %s", urlx) apiURL, err := url.Parse(urlx + "/") if err != nil { - log.Fatalf("parsing api url: %s", apiURL) + t.Fatalf("parsing api url: %s", apiURL) } //ok auth @@ -57,7 +57,7 @@ func TestWatcherAuth(t *testing.T) { client, err := NewClient(mycfg) if err != nil { - log.Fatalf("new api client: %s", err) + t.Fatalf("new api client: %s", err) } _, err = client.Auth.AuthenticateWatcher(context.Background(), models.WatcherAuthRequest{ @@ -81,7 +81,7 @@ func TestWatcherAuth(t *testing.T) { client, err = NewClient(mycfg) if err != nil { - log.Fatalf("new api client: %s", err) + t.Fatalf("new api client: %s", err) } _, err = client.Auth.AuthenticateWatcher(context.Background(), models.WatcherAuthRequest{ @@ -112,7 +112,7 @@ func TestWatcherRegister(t *testing.T) { log.Printf("URL is %s", urlx) apiURL, err := url.Parse(urlx + "/") if err != nil { - log.Fatalf("parsing api url: %s", apiURL) + t.Fatalf("parsing api url: %s", apiURL) } client, err := RegisterClient(&Config{ MachineID: "test_login", @@ -158,7 +158,7 @@ func TestWatcherUnregister(t *testing.T) { log.Printf("URL is %s", urlx) apiURL, err := url.Parse(urlx + "/") if err != nil { - log.Fatalf("parsing api url: %s", apiURL) + t.Fatalf("parsing api url: %s", apiURL) } mycfg := &Config{ MachineID: "test_login", @@ -171,7 +171,7 @@ func TestWatcherUnregister(t *testing.T) { client, err := NewClient(mycfg) if err != nil { - log.Fatalf("new api client: %s", err) + t.Fatalf("new api client: %s", err) } _, err = client.Auth.UnregisterWatcher(context.Background()) if err != nil { @@ -211,7 +211,7 @@ func TestWatcherEnroll(t *testing.T) { log.Printf("URL is %s", urlx) apiURL, err := url.Parse(urlx + "/") if err != nil { - log.Fatalf("parsing api url: %s", apiURL) + t.Fatalf("parsing api url: %s", apiURL) } mycfg := &Config{ @@ -225,7 +225,7 @@ func TestWatcherEnroll(t *testing.T) { client, err := NewClient(mycfg) if err != nil { - log.Fatalf("new api client: %s", err) + t.Fatalf("new api client: %s", err) } _, err = client.Auth.EnrollWatcher(context.Background(), "goodkey", "", []string{}, false) diff --git a/pkg/apiclient/auth_test.go b/pkg/apiclient/auth_test.go index a14cbf89f..58247d8b7 100644 --- a/pkg/apiclient/auth_test.go +++ b/pkg/apiclient/auth_test.go @@ -29,7 +29,7 @@ func TestApiAuth(t *testing.T) { log.Printf("URL is %s", urlx) apiURL, err := url.Parse(urlx + "/") if err != nil { - log.Fatalf("parsing api url: %s", apiURL) + t.Fatalf("parsing api url: %s", apiURL) } defer teardown() @@ -41,7 +41,7 @@ func TestApiAuth(t *testing.T) { newcli, err := NewDefaultClient(apiURL, "v1", "toto", auth.Client()) if err != nil { - log.Fatalf("new api client: %s", err) + t.Fatalf("new api client: %s", err) } alert := DecisionsListOpts{IPEquals: new(string)} @@ -60,7 +60,7 @@ func TestApiAuth(t *testing.T) { newcli, err = NewDefaultClient(apiURL, "v1", "toto", auth.Client()) if err != nil { - log.Fatalf("new api client: %s", err) + t.Fatalf("new api client: %s", err) } _, resp, err = newcli.Decisions.List(context.Background(), alert) @@ -74,7 +74,7 @@ func TestApiAuth(t *testing.T) { auth = &APIKeyTransport{} newcli, err = NewDefaultClient(apiURL, "v1", "toto", auth.Client()) if err != nil { - log.Fatalf("new api client: %s", err) + t.Fatalf("new api client: %s", err) } _, resp, err = newcli.Decisions.List(context.Background(), alert) diff --git a/pkg/apiclient/client_http_test.go b/pkg/apiclient/client_http_test.go index c89325510..f9ad57210 100644 --- a/pkg/apiclient/client_http_test.go +++ b/pkg/apiclient/client_http_test.go @@ -9,7 +9,6 @@ import ( "time" "github.com/crowdsecurity/crowdsec/pkg/cwversion" - log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" ) @@ -19,7 +18,7 @@ func TestNewRequestInvalid(t *testing.T) { //missing slash in uri apiURL, err := url.Parse(urlx) if err != nil { - log.Fatalf("parsing api url: %s", apiURL) + t.Fatalf("parsing api url: %s", apiURL) } client, err := NewClient(&Config{ MachineID: "test_login", @@ -52,7 +51,7 @@ func TestNewRequestTimeout(t *testing.T) { //missing slash in uri apiURL, err := url.Parse(urlx + "/") if err != nil { - log.Fatalf("parsing api url: %s", apiURL) + t.Fatalf("parsing api url: %s", apiURL) } client, err := NewClient(&Config{ MachineID: "test_login", diff --git a/pkg/apiclient/client_test.go b/pkg/apiclient/client_test.go index 38c46f691..7bbc08d1b 100644 --- a/pkg/apiclient/client_test.go +++ b/pkg/apiclient/client_test.go @@ -46,7 +46,7 @@ func TestNewClientOk(t *testing.T) { defer teardown() apiURL, err := url.Parse(urlx + "/") if err != nil { - log.Fatalf("parsing api url: %s", apiURL) + t.Fatalf("parsing api url: %s", apiURL) } client, err := NewClient(&Config{ MachineID: "test_login", @@ -83,7 +83,7 @@ func TestNewClientKo(t *testing.T) { defer teardown() apiURL, err := url.Parse(urlx + "/") if err != nil { - log.Fatalf("parsing api url: %s", apiURL) + t.Fatalf("parsing api url: %s", apiURL) } client, err := NewClient(&Config{ MachineID: "test_login", @@ -116,7 +116,7 @@ func TestNewDefaultClient(t *testing.T) { defer teardown() apiURL, err := url.Parse(urlx + "/") if err != nil { - log.Fatalf("parsing api url: %s", apiURL) + t.Fatalf("parsing api url: %s", apiURL) } client, err := NewDefaultClient(apiURL, "/v1", "", nil) if err != nil { diff --git a/pkg/apiclient/decisions_service_test.go b/pkg/apiclient/decisions_service_test.go index 57e911e4c..8ea5e39de 100644 --- a/pkg/apiclient/decisions_service_test.go +++ b/pkg/apiclient/decisions_service_test.go @@ -36,7 +36,7 @@ func TestDecisionsList(t *testing.T) { }) apiURL, err := url.Parse(urlx + "/") if err != nil { - log.Fatalf("parsing api url: %s", apiURL) + t.Fatalf("parsing api url: %s", apiURL) } //ok answer @@ -46,7 +46,7 @@ func TestDecisionsList(t *testing.T) { newcli, err := NewDefaultClient(apiURL, "v1", "toto", auth.Client()) if err != nil { - log.Fatalf("new api client: %s", err) + t.Fatalf("new api client: %s", err) } tduration := "3h59m55.756182786s" @@ -77,7 +77,7 @@ func TestDecisionsList(t *testing.T) { } if err != nil { - log.Fatalf("new api client: %s", err) + t.Fatalf("new api client: %s", err) } if !reflect.DeepEqual(*decisions, *expected) { t.Fatalf("returned %+v, want %+v", resp, expected) @@ -126,7 +126,7 @@ func TestDecisionsStream(t *testing.T) { apiURL, err := url.Parse(urlx + "/") if err != nil { - log.Fatalf("parsing api url: %s", apiURL) + t.Fatalf("parsing api url: %s", apiURL) } //ok answer @@ -136,7 +136,7 @@ func TestDecisionsStream(t *testing.T) { newcli, err := NewDefaultClient(apiURL, "v1", "toto", auth.Client()) if err != nil { - log.Fatalf("new api client: %s", err) + t.Fatalf("new api client: %s", err) } tduration := "3h59m55.756182786s" @@ -167,7 +167,7 @@ func TestDecisionsStream(t *testing.T) { } if err != nil { - log.Fatalf("new api client: %s", err) + t.Fatalf("new api client: %s", err) } if !reflect.DeepEqual(*decisions, *expected) { t.Fatalf("returned %+v, want %+v", resp, expected) @@ -208,7 +208,7 @@ func TestDeleteDecisions(t *testing.T) { log.Printf("URL is %s", urlx) apiURL, err := url.Parse(urlx + "/") if err != nil { - log.Fatalf("parsing api url: %s", apiURL) + t.Fatalf("parsing api url: %s", apiURL) } client, err := NewClient(&Config{ MachineID: "test_login", @@ -219,7 +219,7 @@ func TestDeleteDecisions(t *testing.T) { }) if err != nil { - log.Fatalf("new api client: %s", err) + t.Fatalf("new api client: %s", err) } filters := DecisionsDeleteOpts{IPEquals: new(string)} @@ -315,7 +315,7 @@ func TestDecisionsStreamOpts_addQueryParamsToURL(t *testing.T) { // log.Printf("URL is %s", urlx) // apiURL, err := url.Parse(urlx + "/") // if err != nil { -// log.Fatalf("parsing api url: %s", apiURL) +// t.Fatalf("parsing api url: %s", apiURL) // } // client, err := NewClient(&Config{ // MachineID: "test_login", @@ -326,7 +326,7 @@ func TestDecisionsStreamOpts_addQueryParamsToURL(t *testing.T) { // }) // if err != nil { -// log.Fatalf("new api client: %s", err.Error()) +// t.Fatalf("new api client: %s", err.Error()) // } // filters := DecisionsDeleteOpts{IPEquals: new(string)}