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
/
jmeterUtils_test.go
198 lines (186 loc) · 4.92 KB
/
jmeterUtils_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os/exec"
"strings"
"testing"
apimodels "github.com/keptn/go-utils/pkg/api/models"
keptncommon "github.com/keptn/go-utils/pkg/lib/keptn"
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
)
func Test_executeJMeter(t *testing.T) {
localTmpDir := t.TempDir()
var returnedStatus int
var returnedResources apimodels.Resources
myurl, _ := url.Parse("http://keptn.sh/test/")
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
if strings.HasSuffix(r.URL.Path, "/resource") || strings.HasSuffix(r.URL.Path, "/resource/") {
marshal, _ := json.Marshal(returnedResources)
w.Write(marshal)
return
}
w.WriteHeader(returnedStatus)
w.Write([]byte(`{
"code": ` + fmt.Sprintf("%d", returnedStatus) + `,
"message": ""
}`))
}),
)
defer ts.Close()
checkJmeter()
t.Setenv("RESOURCE_SERVICE", ts.URL)
t.Setenv("env", "production")
type args struct {
testInfo TestInfo
workload *Workload
resultsDir string
url *url.URL
LTN string
funcValidation bool
logger *keptncommon.Logger
}
tests := []struct {
name string
args args
returnedStatus int
returnedResources []string
want bool
wantErr bool
}{
{
name: "Skip tests if 404 is returned by configuration service and mark as success",
args: args{
testInfo: TestInfo{
Project: "sockshop",
Stage: "dev",
Service: "carts",
TestStrategy: "functional",
Context: localTmpDir,
},
workload: &Workload{
Script: "test.jmx",
},
},
want: true,
wantErr: false,
returnedStatus: 404,
},
{
name: "Skip tests if error code is returned by configuration service and return error",
args: args{
testInfo: TestInfo{
Project: "sockshop",
Stage: "dev",
Service: "carts",
TestStrategy: "functional",
Context: localTmpDir,
},
workload: &Workload{
Script: "test.jmx",
},
},
want: false,
wantErr: true,
returnedStatus: 500,
},
{
name: "Jmeter returns unparsable result",
args: args{
testInfo: TestInfo{
Project: "sockshop",
Stage: "dev",
Service: "carts",
TestStrategy: "functional",
Context: localTmpDir,
TriggeredID: "",
CommitID: "",
TestTriggeredData: keptnv2.TestTriggeredEventData{},
ServiceURL: nil,
},
workload: &Workload{
Script: "test.jmx",
VUser: 1,
LoopCount: 1,
ThinkTime: 10,
},
resultsDir: localTmpDir,
url: myurl,
LTN: "",
funcValidation: false,
logger: nil,
},
want: false,
wantErr: checkJmeter(),
returnedResources: []string{`{
"metadata": {
"branch": "dev",
"version": "de2037b85919406ea949bdfc3aa4bbbe6b0e1e61"
},
"resourceContent": "cHJvamVjdG5hbWU6IHBvdGF0bwpjcmVhdGlvbnRpbWVzdGFtcDogMjAyMi0wMy0zMCAxNToyMjo0MS4zMTIzNDE2NzYgKzAwMDAgVVRDIG09KzcxOTIuNDk5OTkxODIxCg==",
"resourceURI": "test.jmx"
}`,
},
returnedStatus: 200,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
returnedStatus = tt.returnedStatus
returnedResources = apimodels.Resources{
Resources: []*apimodels.Resource{},
}
for _, res := range tt.returnedResources {
returnedResources.Resources = append(returnedResources.Resources, &apimodels.Resource{
ResourceURI: &res,
})
}
tmpDir := t.TempDir()
got, err := executeJMeter(tt.args.testInfo, tt.args.workload, tmpDir, tt.args.url, tt.args.LTN, tt.args.funcValidation)
if (err != nil) != tt.wantErr {
t.Errorf("executeJMeter() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("executeJMeter() got = %v, want %v", got, tt.want)
}
})
}
}
func checkJmeter() bool {
//check if jmeter command exists
cmd := exec.Command("jmeter")
_, err := cmd.CombinedOutput()
if err != nil {
return true
}
return false
}
func Test_derivePort(t *testing.T) {
tests := []struct {
name string
url string
want string
}{
{"HTTP Address without port", "http://1.2.3.4", "80"},
{"HTTP Address with port", "http://1.2.3.4:80", "80"},
{"HTTP Address with different port", "http://1.2.3.4:1234", "1234"},
{"HTTPS Address without port", "https://1.2.3.4", "443"},
{"HTTPS Address with port", "https://1.2.3.4:443", "443"},
{"HTTPS Address with different port", "http://1.2.3.4:1234", "1234"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parsedURL, _ := url.Parse(tt.url)
got := derivePort(parsedURL)
if got != tt.want {
t.Errorf("derivePort(%v) got = %v, want %v", tt.url, got, tt.want)
}
})
}
}