Simplify Go Endpoints + Add Tests (#120)
This MR represents a major refactor of the Go codebase, as well as introducing tests for the handlers. The MR also introduces an endpoint to shutdown or restart the Go server, which may be useful for clients who want to refresh the state of the plugin after checking out branches. Finally, this MR adds a contributing document for users who want to make feature changes.
This commit is contained in:
committed by
GitHub
parent
10b0b596ae
commit
93fe3e8bd6
86
cmd/pipeline_test.go
Normal file
86
cmd/pipeline_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
func listPipelineJobs(pid interface{}, pipelineID int, opts *gitlab.ListJobsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Job, *gitlab.Response, error) {
|
||||
return []*gitlab.Job{}, makeResponse(http.StatusOK), nil
|
||||
}
|
||||
|
||||
func listPipelineJobsErr(pid interface{}, pipelineID int, opts *gitlab.ListJobsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Job, *gitlab.Response, error) {
|
||||
return nil, nil, errors.New("Some error from Gitlab")
|
||||
}
|
||||
|
||||
func listPipelineJobsNon200(pid interface{}, pipelineID int, opts *gitlab.ListJobsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Job, *gitlab.Response, error) {
|
||||
return nil, makeResponse(http.StatusSeeOther), nil
|
||||
}
|
||||
|
||||
func retryPipelineBuild(pid interface{}, pipeline int, options ...gitlab.RequestOptionFunc) (*gitlab.Pipeline, *gitlab.Response, error) {
|
||||
return &gitlab.Pipeline{}, makeResponse(http.StatusOK), nil
|
||||
}
|
||||
|
||||
func retryPipelineBuildErr(pid interface{}, pipeline int, options ...gitlab.RequestOptionFunc) (*gitlab.Pipeline, *gitlab.Response, error) {
|
||||
return nil, nil, errors.New("Some error from Gitlab")
|
||||
}
|
||||
|
||||
func retryPipelineBuildNon200(pid interface{}, pipeline int, options ...gitlab.RequestOptionFunc) (*gitlab.Pipeline, *gitlab.Response, error) {
|
||||
return nil, makeResponse(http.StatusSeeOther), nil
|
||||
}
|
||||
|
||||
func TestPipelineHandler(t *testing.T) {
|
||||
t.Run("Gets all pipeline jobs", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodGet, "/pipeline/1", nil)
|
||||
server, _ := createRouterAndApi(fakeClient{listPipelineJobs: listPipelineJobs})
|
||||
data := serveRequest(t, server, request, GetJobsResponse{})
|
||||
assert(t, data.SuccessResponse.Message, "Pipeline jobs retrieved")
|
||||
assert(t, data.SuccessResponse.Status, http.StatusOK)
|
||||
})
|
||||
|
||||
t.Run("Disallows non-GET, non-POST methods", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodPatch, "/pipeline/1", nil)
|
||||
server, _ := createRouterAndApi(fakeClient{listPipelineJobs: listPipelineJobs})
|
||||
data := serveRequest(t, server, request, ErrorResponse{})
|
||||
checkBadMethod(t, *data, http.MethodGet, http.MethodPost)
|
||||
})
|
||||
|
||||
t.Run("Handles errors from Gitlab client", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodGet, "/pipeline/1", nil)
|
||||
server, _ := createRouterAndApi(fakeClient{listPipelineJobs: listPipelineJobsErr})
|
||||
data := serveRequest(t, server, request, ErrorResponse{})
|
||||
checkErrorFromGitlab(t, *data, "Could not get pipeline jobs")
|
||||
})
|
||||
|
||||
t.Run("Handles non-200s from Gitlab client", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodGet, "/pipeline/1", nil)
|
||||
server, _ := createRouterAndApi(fakeClient{listPipelineJobs: listPipelineJobsNon200})
|
||||
data := serveRequest(t, server, request, ErrorResponse{})
|
||||
checkNon200(t, *data, "Could not get pipeline jobs", "/pipeline")
|
||||
})
|
||||
|
||||
t.Run("Retriggers pipeline", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodPost, "/pipeline/1", nil)
|
||||
server, _ := createRouterAndApi(fakeClient{retryPipelineBuild: retryPipelineBuild})
|
||||
data := serveRequest(t, server, request, GetJobsResponse{})
|
||||
assert(t, data.SuccessResponse.Message, "Pipeline retriggered")
|
||||
assert(t, data.SuccessResponse.Status, http.StatusOK)
|
||||
})
|
||||
|
||||
t.Run("Handles errors from Gitlab client", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodPost, "/pipeline/1", nil)
|
||||
server, _ := createRouterAndApi(fakeClient{retryPipelineBuild: retryPipelineBuildErr})
|
||||
data := serveRequest(t, server, request, ErrorResponse{})
|
||||
checkErrorFromGitlab(t, *data, "Could not retrigger pipeline")
|
||||
})
|
||||
|
||||
t.Run("Handles non-200s from Gitlab client on retrigger", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodPost, "/pipeline/1", nil)
|
||||
server, _ := createRouterAndApi(fakeClient{retryPipelineBuild: retryPipelineBuildNon200})
|
||||
data := serveRequest(t, server, request, ErrorResponse{})
|
||||
checkNon200(t, *data, "Could not retrigger pipeline", "/pipeline")
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user