/
core_test_common.go
119 lines (106 loc) · 4.44 KB
/
core_test_common.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
package core
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/jenkins-zh/jenkins-client/pkg/mock/mhttp"
)
// PrepareRestart only for test
func PrepareRestart(roundTripper *mhttp.MockRoundTripper, rootURL, user, password string, statusCode int) {
request, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/safeRestart", rootURL), nil)
response := PrepareCommonPost(request, "", roundTripper, user, password, rootURL)
response.StatusCode = statusCode
}
// PrepareRestartDirectly only for test
func PrepareRestartDirectly(roundTripper *mhttp.MockRoundTripper, rootURL, user, password string, statusCode int) {
request, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/restart", rootURL), nil)
response := PrepareCommonPost(request, "", roundTripper, user, password, rootURL)
response.StatusCode = statusCode
}
// PrepareForShutdown only for test
func PrepareForShutdown(roundTripper *mhttp.MockRoundTripper, rootURL, user, password string, safe bool) {
var request *http.Request
if safe {
request, _ = http.NewRequest(http.MethodPost, fmt.Sprintf("%s/safeExit", rootURL), nil)
} else {
request, _ = http.NewRequest(http.MethodPost, fmt.Sprintf("%s/exit", rootURL), nil)
}
PrepareCommonPost(request, "", roundTripper, user, password, rootURL)
}
// PrepareForCancelShutdown only for test
func PrepareForCancelShutdown(roundTripper *mhttp.MockRoundTripper, rootURL, user, password string, cancel bool) {
var request *http.Request
if cancel {
request, _ = http.NewRequest(http.MethodPost, fmt.Sprintf("%s/cancelQuietDown", rootURL), nil)
} else {
request, _ = http.NewRequest(http.MethodPost, fmt.Sprintf("%s/quietDown", rootURL), nil)
}
PrepareCommonPost(request, "", roundTripper, user, password, rootURL)
}
// PrepareForToJSON only for test
func PrepareForToJSON(roundTripper *mhttp.MockRoundTripper, rootURL, user, password string) {
payloadData := url.Values{"jenkinsfile": {"jenkinsfile"}}
payload := strings.NewReader(payloadData.Encode())
request, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/pipeline-model-converter/toJson", rootURL), payload)
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
PrepareCommonPost(request, `{"status":"ok","data":{"result":"success","json":{"a":"b"}}}`, roundTripper, user, password, rootURL)
}
// PrepareForToJenkinsfile only for test
func PrepareForToJenkinsfile(roundTripper *mhttp.MockRoundTripper, rootURL, user, password string) {
payloadData := url.Values{"json": {"json"}}
payload := strings.NewReader(payloadData.Encode())
request, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/pipeline-model-converter/toJenkinsfile", rootURL), payload)
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
PrepareCommonPost(request, `{"status":"ok","data":{"result":"success","jenkinsfile":"jenkinsfile"}}`, roundTripper, user, password, rootURL)
}
// PrepareForToGetLabels only for test
func PrepareForToGetLabels(roundTripper *mhttp.MockRoundTripper, rootURL, user, password string) {
request, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/labelsdashboard/labelsData", rootURL), nil)
if user != "" && password != "" {
request.SetBasicAuth(user, password)
}
response := &http.Response{
StatusCode: http.StatusOK,
Request: request,
Body: ioutil.NopCloser(bytes.NewBufferString(`{
"status": "ok",
"data": [
{
"cloudsCount": 0,
"description": "",
"hasMoreThanOneJob": false,
"jobs": [],
"jobsCount": 0,
"jobsWithLabelDefaultValue": [],
"jobsWithLabelDefaultValueCount": 0,
"label": "java",
"labelURL": "label/java/",
"nodesCount": 1,
"pluginActiveForLabel": false,
"triggeredJobs": [],
"triggeredJobsCount": 0
}
]
}`)),
}
roundTripper.EXPECT().
RoundTrip(NewVerboseRequestMatcher(request).WithBody().WithQuery()).Return(response, nil).AnyTimes()
}
// PrepareForGetIdentity only for test
func PrepareForGetIdentity(roundTripper *mhttp.MockRoundTripper, rootURL, user, password string) {
request, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/instance", rootURL), nil)
response := &http.Response{
StatusCode: 200,
Request: request,
Body: ioutil.NopCloser(bytes.NewBufferString(`
{"fingerprint":"fingerprint","publicKey":"publicKey","systemMessage":"systemMessage"}`)),
}
roundTripper.EXPECT().
RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && password != "" {
request.SetBasicAuth(user, password)
}
}