mirror of
https://github.com/fluxcd/flagger.git
synced 2026-04-15 06:57:34 +00:00
webhook tests
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@@ -20,7 +24,7 @@ func TestCallWebhook(t *testing.T) {
|
||||
Metadata: &map[string]string{"key1": "val1"},
|
||||
}
|
||||
|
||||
err := CallWebhook("podinfo", "default", flaggerv1.CanaryPhaseProgressing, hook)
|
||||
err := CallWebhook("podinfo", v1.NamespaceDefault, flaggerv1.CanaryPhaseProgressing, hook)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
@@ -36,7 +40,91 @@ func TestCallWebhook_StatusCode(t *testing.T) {
|
||||
URL: ts.URL,
|
||||
}
|
||||
|
||||
err := CallWebhook("podinfo", "default", flaggerv1.CanaryPhaseProgressing, hook)
|
||||
err := CallWebhook("podinfo", v1.NamespaceDefault, flaggerv1.CanaryPhaseProgressing, hook)
|
||||
if err == nil {
|
||||
t.Errorf("Got no error wanted %v", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallEventWebhook(t *testing.T) {
|
||||
canaryName := "podinfo"
|
||||
canaryNamespace := v1.NamespaceDefault
|
||||
canaryMessage := fmt.Sprintf("Starting canary analysis for %s.%s", canaryName, canaryNamespace)
|
||||
canaryEventType := corev1.EventTypeNormal
|
||||
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
d := json.NewDecoder(r.Body)
|
||||
|
||||
var payload flaggerv1.CanaryWebhookPayload
|
||||
|
||||
err := d.Decode(&payload)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if payload.Metadata["eventMessage"] != canaryMessage {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if payload.Metadata["eventType"] != canaryEventType {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if payload.Name != canaryName {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if payload.Namespace != canaryNamespace {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
canary := &flaggerv1.Canary{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: canaryName,
|
||||
Namespace: canaryNamespace,
|
||||
},
|
||||
Status: flaggerv1.CanaryStatus{
|
||||
Phase: flaggerv1.CanaryPhaseProgressing,
|
||||
},
|
||||
}
|
||||
|
||||
err := CallEventWebhook(canary, ts.URL, canaryMessage, canaryEventType)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallEventWebhookStatusCode(t *testing.T) {
|
||||
canaryName := "podinfo"
|
||||
canaryNamespace := v1.NamespaceDefault
|
||||
canaryMessage := fmt.Sprintf("Starting canary analysis for %s.%s", canaryName, canaryNamespace)
|
||||
canaryEventType := corev1.EventTypeNormal
|
||||
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
canary := &flaggerv1.Canary{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: canaryName,
|
||||
Namespace: canaryNamespace,
|
||||
},
|
||||
Status: flaggerv1.CanaryStatus{
|
||||
Phase: flaggerv1.CanaryPhaseProgressing,
|
||||
},
|
||||
}
|
||||
|
||||
err := CallEventWebhook(canary, ts.URL, canaryMessage, canaryEventType)
|
||||
if err == nil {
|
||||
t.Errorf("Got no error wanted %v", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user