Add webhook tests

This commit is contained in:
stefanprodan
2018-12-26 17:58:35 +02:00
parent e86c02d600
commit 722d36a8cc
2 changed files with 42 additions and 1 deletions

View File

@@ -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,

View File

@@ -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)
}
}