Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
feat: Added unit tests for webhook-service and improved error messages (
Browse files Browse the repository at this point in the history
#6064)

* Added unit tests for webhook-config and added checks for required parameters

Signed-off-by: odubajDT <[email protected]>

* Added unit tests for template_engine

Signed-off-by: odubajDT <[email protected]>

* Improved error message of webhook-service

Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT authored Dec 1, 2021
1 parent adebbbb commit 5b4516e
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 6 deletions.
7 changes: 4 additions & 3 deletions webhook-service/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package handler
import (
"errors"
"fmt"
"strings"

"github.com/keptn/go-utils/pkg/api/models"
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
"github.com/keptn/keptn/go-sdk/pkg/sdk"
"github.com/keptn/keptn/webhook-service/lib"
logger "github.com/sirupsen/logrus"
"strings"
)

const webhookConfigFileName = "webhook/webhook.yaml"
Expand Down Expand Up @@ -174,9 +175,9 @@ func (th *TaskHandler) onStartedWebhookExecution(keptnHandler sdk.IKeptn, event
}
} else {
// if sendFinished is set to false, we need to send a .started event for each webhook request to be executed
for range webhook.Requests {
for _, req := range webhook.Requests {
if err := keptnHandler.SendStartedEvent(event); err != nil {
return sdkError(fmt.Sprintf("could not send .started event: %s", err.Error()), err)
return sdkError(fmt.Sprintf("could not send .started event: request '%s': %s", req, err.Error()), err)
}
}
}
Expand Down
38 changes: 37 additions & 1 deletion webhook-service/lib/template_engine_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package lib_test

import (
"github.com/keptn/keptn/webhook-service/lib"
"strings"
"testing"

"github.com/keptn/keptn/webhook-service/lib"
)

func TestTemplateEngine_ParseTemplate(t1 *testing.T) {
Expand All @@ -15,6 +17,7 @@ func TestTemplateEngine_ParseTemplate(t1 *testing.T) {
args args
want string
wantErr bool
errMsg string
}{
{
name: "parse template",
Expand All @@ -28,6 +31,7 @@ func TestTemplateEngine_ParseTemplate(t1 *testing.T) {
},
want: "foo bar",
wantErr: false,
errMsg: "",
},
{
name: "wrong template syntax",
Expand All @@ -41,6 +45,35 @@ func TestTemplateEngine_ParseTemplate(t1 *testing.T) {
},
want: "",
wantErr: true,
errMsg: "unexpected",
},
{
name: "non-existing key",
args: args{
data: map[string]interface{}{
"env": map[string]interface{}{
"bar": "foo",
},
},
templateStr: "foo {{.env.foo}}",
},
want: "",
wantErr: true,
errMsg: ".env.foo",
},
{
name: "empty value",
args: args{
data: map[string]interface{}{
"env": map[string]interface{}{
"bar": "",
},
},
templateStr: "foo {{.env.barz}}",
},
want: "",
wantErr: true,
errMsg: ".env.barz",
},
}
for _, tt := range tests {
Expand All @@ -54,6 +87,9 @@ func TestTemplateEngine_ParseTemplate(t1 *testing.T) {
if got != tt.want {
t1.Errorf("ParseTemplate() got = %v, want %v", got, tt.want)
}
if err != nil && !strings.Contains(err.Error(), tt.errMsg) {
t1.Errorf("ParseTemplate() errMsg = %v, want %v", err.Error(), tt.errMsg)
}
})
}
}
25 changes: 24 additions & 1 deletion webhook-service/lib/webhook_config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package lib

import "gopkg.in/yaml.v3"
import (
"errors"

"gopkg.in/yaml.v3"
)

type WebHookConfig struct {
ApiVersion string `yaml:"apiVersion"`
Expand Down Expand Up @@ -44,5 +48,24 @@ func DecodeWebHookConfigYAML(webhookConfigYaml []byte) (*WebHookConfig, error) {
if err := yaml.Unmarshal(webhookConfigYaml, webHookConfig); err != nil {
return nil, err
}

if len(webHookConfig.Spec.Webhooks) == 0 {
return nil, errors.New("Webhook configuration invalid: missing 'webhooks[]' part")
}

for _, webhook := range webHookConfig.Spec.Webhooks {
if webhook.Type == "" {
return nil, errors.New("Webhook configuration invalid: missing 'webhooks[].Type' part")
}

if webhook.SubscriptionID == "" {
return nil, errors.New("Webhook configuration invalid: missing 'webhooks[].SubscriptionID' part")
}

if len(webhook.Requests) == 0 {
return nil, errors.New("Webhook configuration invalid: missing 'webhooks[].Requests[]' part")
}
}

return webHookConfig, nil
}
62 changes: 61 additions & 1 deletion webhook-service/lib/webhook_config_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package lib

import (
"github.com/stretchr/testify/require"
"testing"

"github.com/stretchr/testify/require"
)

func TestDecodeWebHookConfigYAML(t *testing.T) {
Expand Down Expand Up @@ -65,6 +66,65 @@ spec:
want: nil,
wantErr: true,
},
{
name: "bad padding invalid input",
args: args{
webhookConfigYaml: []byte(`apiVersion: webhookconfig.keptn.sh/v1alpha1
kind: WebhookConfig
metadata:
name: webhook-configuration
spec:
webhooks:
- type: "sh.keptn.event.webhook.triggered"
subscriptionID: "my-subscription-id"
envFrom:
- secretRef:
name: mysecret
requests:
- "curl http://localhost:8080 {{.data.project}} {{.env.mysecret}}"`),
},
want: nil,
wantErr: true,
},
{
name: "misspeling keyworkds invalid input",
args: args{
webhookConfigYaml: []byte(`apiVersion: webhookconfig.keptn.sh/v1alpha1
kind: WebhookConfig
metadata:
name: webhook-configuration
spec:
webhooks:
- type: "sh.keptn.event.webhook.triggered"
subscriptionIDs: "my-subscription-id"
envFrom:
- secretRef:
name: mysecret
requests:
- "curl http://localhost:8080 {{.data.project}} {{.env.mysecret}}"`),
},
want: nil,
wantErr: true,
},
{
name: "missing requests invalid input",
args: args{
webhookConfigYaml: []byte(`apiVersion: webhookconfig.keptn.sh/v1alpha1
kind: WebhookConfig
metadata:
name: webhook-configuration
spec:
webhooks:
- type: "sh.keptn.event.webhook.triggered"
subscriptionIDs: "my-subscription-id"
envFrom:
- secretRef:
name: mysecret
requests:`),
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 5b4516e

Please sign in to comment.