Fix url encoding of job names in jenkins input plugin (#7211)
This commit is contained in:
parent
1d697dd323
commit
a907edc1f3
|
@ -442,12 +442,20 @@ func (jr jobRequest) combined() []string {
|
||||||
return append(jr.parents, jr.name)
|
return append(jr.parents, jr.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (jr jobRequest) combinedEscaped() []string {
|
||||||
|
jobs := jr.combined()
|
||||||
|
for index, job := range jobs {
|
||||||
|
jobs[index] = url.PathEscape(job)
|
||||||
|
}
|
||||||
|
return jobs
|
||||||
|
}
|
||||||
|
|
||||||
func (jr jobRequest) URL() string {
|
func (jr jobRequest) URL() string {
|
||||||
return "/job/" + strings.Join(jr.combined(), "/job/") + jobPath
|
return "/job/" + strings.Join(jr.combinedEscaped(), "/job/") + jobPath
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jr jobRequest) buildURL(number int64) string {
|
func (jr jobRequest) buildURL(number int64) string {
|
||||||
return "/job/" + strings.Join(jr.combined(), "/job/") + "/" + strconv.Itoa(int(number)) + jobPath
|
return "/job/" + strings.Join(jr.combinedEscaped(), "/job/") + "/" + strconv.Itoa(int(number)) + jobPath
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jr jobRequest) hierarchyName() string {
|
func (jr jobRequest) hierarchyName() string {
|
||||||
|
|
|
@ -17,11 +17,13 @@ import (
|
||||||
func TestJobRequest(t *testing.T) {
|
func TestJobRequest(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
input jobRequest
|
input jobRequest
|
||||||
output string
|
hierarchyName string
|
||||||
|
URL string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
jobRequest{},
|
jobRequest{},
|
||||||
"",
|
"",
|
||||||
|
"",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
jobRequest{
|
jobRequest{
|
||||||
|
@ -29,12 +31,26 @@ func TestJobRequest(t *testing.T) {
|
||||||
parents: []string{"3", "2"},
|
parents: []string{"3", "2"},
|
||||||
},
|
},
|
||||||
"3/2/1",
|
"3/2/1",
|
||||||
|
"/job/3/job/2/job/1/api/json",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
jobRequest{
|
||||||
|
name: "job 3",
|
||||||
|
parents: []string{"job 1", "job 2"},
|
||||||
|
},
|
||||||
|
"job 1/job 2/job 3",
|
||||||
|
"/job/job%201/job/job%202/job/job%203/api/json",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
output := test.input.hierarchyName()
|
hierarchyName := test.input.hierarchyName()
|
||||||
if output != test.output {
|
URL := test.input.URL()
|
||||||
t.Errorf("Expected %s, got %s\n", test.output, output)
|
if hierarchyName != test.hierarchyName {
|
||||||
|
t.Errorf("Expected %s, got %s\n", test.hierarchyName, hierarchyName)
|
||||||
|
}
|
||||||
|
|
||||||
|
if test.URL != "" && URL != test.URL {
|
||||||
|
t.Errorf("Expected %s, got %s\n", test.URL, URL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,7 +82,7 @@ type mockHandler struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
o, ok := h.responseMap[r.URL.Path]
|
o, ok := h.responseMap[r.URL.RequestURI()]
|
||||||
if !ok {
|
if !ok {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
return
|
return
|
||||||
|
@ -549,6 +565,43 @@ func TestGatherJobs(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "gather metrics for jobs with space",
|
||||||
|
input: mockHandler{
|
||||||
|
responseMap: map[string]interface{}{
|
||||||
|
"/api/json": &jobResponse{
|
||||||
|
Jobs: []innerJob{
|
||||||
|
{Name: "job 1"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"/job/job%201/api/json": &jobResponse{
|
||||||
|
LastBuild: jobBuild{
|
||||||
|
Number: 3,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"/job/job%201/3/api/json": &buildResponse{
|
||||||
|
Building: false,
|
||||||
|
Result: "SUCCESS",
|
||||||
|
Duration: 25558,
|
||||||
|
Timestamp: (time.Now().Unix() - int64(time.Minute.Seconds())) * 1000,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
output: &testutil.Accumulator{
|
||||||
|
Metrics: []*testutil.Metric{
|
||||||
|
{
|
||||||
|
Tags: map[string]string{
|
||||||
|
"name": "job 1",
|
||||||
|
"result": "SUCCESS",
|
||||||
|
},
|
||||||
|
Fields: map[string]interface{}{
|
||||||
|
"duration": int64(25558),
|
||||||
|
"result_code": 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "gather sub jobs, jobs filter",
|
name: "gather sub jobs, jobs filter",
|
||||||
input: mockHandler{
|
input: mockHandler{
|
||||||
|
@ -582,6 +635,8 @@ func TestGatherJobs(t *testing.T) {
|
||||||
{Name: "PR-100"},
|
{Name: "PR-100"},
|
||||||
{Name: "PR-101"},
|
{Name: "PR-101"},
|
||||||
{Name: "PR-ignore2"},
|
{Name: "PR-ignore2"},
|
||||||
|
{Name: "PR 1"},
|
||||||
|
{Name: "PR ignore"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"/job/apps/job/k8s-cloud/job/PR-100/api/json": &jobResponse{
|
"/job/apps/job/k8s-cloud/job/PR-100/api/json": &jobResponse{
|
||||||
|
@ -594,6 +649,11 @@ func TestGatherJobs(t *testing.T) {
|
||||||
Number: 4,
|
Number: 4,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
"/job/apps/job/k8s-cloud/job/PR%201/api/json": &jobResponse{
|
||||||
|
LastBuild: jobBuild{
|
||||||
|
Number: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
"/job/apps/job/chronograf/1/api/json": &buildResponse{
|
"/job/apps/job/chronograf/1/api/json": &buildResponse{
|
||||||
Building: false,
|
Building: false,
|
||||||
Result: "FAILURE",
|
Result: "FAILURE",
|
||||||
|
@ -612,10 +672,27 @@ func TestGatherJobs(t *testing.T) {
|
||||||
Duration: 91558,
|
Duration: 91558,
|
||||||
Timestamp: (time.Now().Unix() - int64(time.Minute.Seconds())) * 1000,
|
Timestamp: (time.Now().Unix() - int64(time.Minute.Seconds())) * 1000,
|
||||||
},
|
},
|
||||||
|
"/job/apps/job/k8s-cloud/job/PR%201/1/api/json": &buildResponse{
|
||||||
|
Building: false,
|
||||||
|
Result: "SUCCESS",
|
||||||
|
Duration: 87832,
|
||||||
|
Timestamp: (time.Now().Unix() - int64(time.Minute.Seconds())) * 1000,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
output: &testutil.Accumulator{
|
output: &testutil.Accumulator{
|
||||||
Metrics: []*testutil.Metric{
|
Metrics: []*testutil.Metric{
|
||||||
|
{
|
||||||
|
Tags: map[string]string{
|
||||||
|
"name": "PR 1",
|
||||||
|
"parents": "apps/k8s-cloud",
|
||||||
|
"result": "SUCCESS",
|
||||||
|
},
|
||||||
|
Fields: map[string]interface{}{
|
||||||
|
"duration": int64(87832),
|
||||||
|
"result_code": 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Tags: map[string]string{
|
Tags: map[string]string{
|
||||||
"name": "PR-100",
|
"name": "PR-100",
|
||||||
|
@ -666,6 +743,7 @@ func TestGatherJobs(t *testing.T) {
|
||||||
"ignore-1",
|
"ignore-1",
|
||||||
"apps/ignore-all/*",
|
"apps/ignore-all/*",
|
||||||
"apps/k8s-cloud/PR-ignore2",
|
"apps/k8s-cloud/PR-ignore2",
|
||||||
|
"apps/k8s-cloud/PR ignore",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
te := j.initialize(&http.Client{Transport: &http.Transport{}})
|
te := j.initialize(&http.Client{Transport: &http.Transport{}})
|
||||||
|
|
Loading…
Reference in New Issue