From 722d36a8cc6b8e124a377615d1ab3d238db80a70 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Wed, 26 Dec 2018 17:58:35 +0200 Subject: [PATCH] Add webhook tests --- pkg/controller/webhook.go | 1 - pkg/controller/webhook_test.go | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 pkg/controller/webhook_test.go diff --git a/pkg/controller/webhook.go b/pkg/controller/webhook.go index ef4b2900..bfba0ff7 100644 --- a/pkg/controller/webhook.go +++ b/pkg/controller/webhook.go @@ -16,7 +16,6 @@ import ( // CallWebhook does a HTTP POST to an external service and // returns an error if the response status code is non-2xx func CallWebhook(name string, namepace string, w flaggerv1.CanaryWebhook) error { - payload := flaggerv1.CanaryWebhookPayload{ Name: name, Namespace: namepace, diff --git a/pkg/controller/webhook_test.go b/pkg/controller/webhook_test.go new file mode 100644 index 00000000..2e96786d --- /dev/null +++ b/pkg/controller/webhook_test.go @@ -0,0 +1,42 @@ +package controller + +import ( + flaggerv1 "github.com/stefanprodan/flagger/pkg/apis/flagger/v1alpha2" + "net/http" + "net/http/httptest" + "testing" +) + +func TestCallWebhook(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusAccepted) + })) + defer ts.Close() + hook := flaggerv1.CanaryWebhook{ + Name: "validation", + URL: ts.URL, + Timeout: "10s", + Metadata: &map[string]string{"key1": "val1"}, + } + + err := CallWebhook("podinfo", "default", hook) + if err != nil { + t.Fatal(err.Error()) + } +} + +func TestCallWebhook_StatusCode(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + })) + defer ts.Close() + hook := flaggerv1.CanaryWebhook{ + Name: "validation", + URL: ts.URL, + } + + err := CallWebhook("podinfo", "default", hook) + if err == nil { + t.Errorf("Got no error wanted %v", http.StatusInternalServerError) + } +}