Skip to content

Commit

Permalink
feat: Add test for retryable calls, remove unusable code
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Paskal <[email protected]>
  • Loading branch information
maksim-paskal committed Dec 19, 2024
1 parent bf4a487 commit a35d5f4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
10 changes: 0 additions & 10 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ package webhook
import (
"bytes"
"context"
"fmt"
"net/http"
"os"

"github.com/hashicorp/go-retryablehttp"
Expand All @@ -28,8 +26,6 @@ import (

var client = &retryablehttp.Client{}

var errHTTPNotOK = errors.New("http result not OK")

func SetHTTPClient(c *retryablehttp.Client) {
client = c
}
Expand Down Expand Up @@ -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
}
40 changes: 39 additions & 1 deletion pkg/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
}
Expand Down Expand Up @@ -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
Expand All @@ -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{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
}

0 comments on commit a35d5f4

Please sign in to comment.