diff --git a/pkg/webhook/webhook.go b/pkg/webhook/webhook.go index 9cfa849..c05ef76 100644 --- a/pkg/webhook/webhook.go +++ b/pkg/webhook/webhook.go @@ -15,8 +15,6 @@ package webhook import ( "bytes" "context" - "fmt" - "net/http" "os" "github.com/hashicorp/go-retryablehttp" @@ -28,8 +26,6 @@ import ( var client = &retryablehttp.Client{} -var errHTTPNotOK = errors.New("http result not OK") - func SetHTTPClient(c *retryablehttp.Client) { client = c } @@ -84,11 +80,5 @@ func SendWebHook(ctx context.Context, obj *template.MessageType) error { } defer resp.Body.Close() - log.Infof("response status: %s", resp.Status) - - if resp.StatusCode != http.StatusOK { - return errors.Wrap(errHTTPNotOK, fmt.Sprintf("StatusCode=%d", resp.StatusCode)) - } - return nil } diff --git a/pkg/webhook/webhook_test.go b/pkg/webhook/webhook_test.go index f607f14..bb19627 100644 --- a/pkg/webhook/webhook_test.go +++ b/pkg/webhook/webhook_test.go @@ -31,7 +31,22 @@ import ( "github.com/stretchr/testify/require" ) +var retryableRequstCount = 0 + var ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.RequestURI == "/test-retryable" { + retryableRequstCount++ + + // return 500 for first 2 requests + if retryableRequstCount < 3 { + w.WriteHeader(http.StatusInternalServerError) + } else { + _, _ = w.Write([]byte("OK")) + } + + return + } + if err := testWebhookRequest(r); err != nil { log.WithError(err).Error() w.WriteHeader(http.StatusInternalServerError) @@ -40,6 +55,10 @@ var ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http } })) +func getWebhookRetryableURL() string { + return ts.URL + "/test-retryable" +} + func getWebhookURL() string { return ts.URL + "/metrics/job/aks-node-termination-handler" } @@ -77,6 +96,14 @@ func TestWebHook(t *testing.T) { //nolint:funlen,tparallel InstrumentedRoundTripper() retryClientProxy.RetryMax = 0 + // retryable client with default retry settings + retryClientDefault := retryablehttp.NewClient() + retryClientDefault.HTTPClient.Transport = metrics.NewInstrumenter("TestWebHookWithDefaultSettings"). + WithProxy(""). + WithInsecureSkipVerify(true). + InstrumentedRoundTripper() + retryClientDefault.RetryMax = 3 + type Test struct { Name string Args map[string]string @@ -87,6 +114,13 @@ func TestWebHook(t *testing.T) { //nolint:funlen,tparallel } tests := []Test{ + { + Name: "TestRetryable", + Args: map[string]string{ + "webhook.url": getWebhookRetryableURL(), + }, + HTTPClient: retryClientDefault, + }, { Name: "ValidHookAndTemplate", Args: map[string]string{ @@ -123,7 +157,8 @@ func TestWebHook(t *testing.T) { //nolint:funlen,tparallel "webhook.url": ts.URL, "webhook.template": `{{ .NodeName }}`, }, - Error: true, + Error: true, + ErrorMessage: "giving up after 1 attempt", }, { Name: "InvalidMethod", @@ -208,4 +243,7 @@ func TestWebHook(t *testing.T) { //nolint:funlen,tparallel } }) } + + // Check retryable request counter, 3 requests should be made + require.Equal(t, 3, retryableRequstCount) }