This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
90 lines (82 loc) · 2.82 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
logger "github.com/sirupsen/logrus"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"time"
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
)
var serviceURLTests = []struct {
name string
event keptnv2.TestTriggeredEventData
url *url.URL
}{
{"local", deploymentFinishedEventInitHelper("sockshop", "carts", "dev", "https://carts.sockshop-dev.svc.cluster.local/test", "carts.sockshop-dev.mydomain.com"), encodeURL("https://carts.sockshop-dev.svc.cluster.local/test")},
{"public", deploymentFinishedEventInitHelper("sockshop", "carts", "dev", "", "http://carts.sockshop-dev.mydomain.com:8080/myendpoint"), encodeURL("http://carts.sockshop-dev.mydomain.com:8080/myendpoint")},
{"no path but valid uri", deploymentFinishedEventInitHelper("sockshop", "carts", "dev", "", "http://carts.sockshop-dev.mydomain.com:8080"), encodeURL("http://carts.sockshop-dev.mydomain.com:8080/")},
}
func encodeURL(urlString string) *url.URL {
parsedURL, err := url.Parse(urlString)
if err != nil {
logger.Fatal(err)
}
return parsedURL
}
func deploymentFinishedEventInitHelper(project, service, stage,
deploymentURILocal, deploymentURIPublic string) keptnv2.TestTriggeredEventData {
return keptnv2.TestTriggeredEventData{
EventData: keptnv2.EventData{
Project: project,
Service: service,
Stage: stage,
},
Deployment: keptnv2.TestTriggeredDeploymentDetails{
DeploymentURIsLocal: []string{deploymentURILocal},
DeploymentURIsPublic: []string{deploymentURIPublic},
},
}
}
func TestGetServiceURL(t *testing.T) {
for _, tt := range serviceURLTests {
t.Run(tt.name, func(t *testing.T) {
res, err := getServiceURL(tt.event)
if err != nil {
t.Errorf("unexpected error")
}
if !reflect.DeepEqual(*res, *tt.url) {
t.Errorf("got %v, want %v for %s", res, tt.url, tt.name)
}
})
}
}
func Test_checkEndpointAvailable(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer ts.Close()
reachableURL, _ := url.Parse(ts.URL)
nonReachableURL, _ := url.Parse("http://1.2.3.4:1234")
nonReachableURLwithoutPort, _ := url.Parse("http://1.2.3.4")
type args struct {
timeout time.Duration
serviceURL *url.URL
}
tests := []struct {
name string
args args
wantErr bool
}{
{"Url reachable", args{1 * time.Second, reachableURL}, false},
{"Url not reachable", args{1 * time.Nanosecond, nonReachableURL}, true},
{"nil", args{1 * time.Nanosecond, nil}, true},
{"Url without port not reachable", args{1 * time.Nanosecond, nonReachableURLwithoutPort}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := checkEndpointAvailable(tt.args.timeout, tt.args.serviceURL); (err != nil) != tt.wantErr {
t.Errorf("checkEndpointAvailable() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}