-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de8a877
commit bc00393
Showing
3 changed files
with
85 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
_ "embed" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/nikhilsbhat/gocd-sdk-go" | ||
|
||
|
@@ -23,8 +24,6 @@ var ( | |
scheduledJobJSON string | ||
//go:embed internal/fixtures/pipeline_schedules.json | ||
pipelineSchedulesJSON string | ||
//go:embed internal/fixtures/pipeline_history.json | ||
pipelineRunHistoryJSON string | ||
) | ||
|
||
var pipelineMap = map[string]interface{}{ | ||
|
@@ -194,63 +193,63 @@ func Test_client_GetPipelineHistory(t *testing.T) { | |
client.SetRetryCount(1) | ||
client.SetRetryWaitTime(1) | ||
|
||
actual, err := client.GetPipelineRunHistory("helm-images") | ||
actual, err := client.GetPipelineRunHistory("helm-images", "0", time.Duration(2)*time.Second) | ||
assert.EqualError(t, err, "call made to get pipeline helm-images errored with: "+ | ||
"Get \"http://localhost:8156/go/api/pipelines/helm-images/history\": dial tcp [::1]:8156: connect: connection refused") | ||
"Get \"http://localhost:8156/go/api/pipelines/helm-images/history?after=0&page_size=0\": dial tcp [::1]:8156: connect: connection refused") | ||
assert.Nil(t, actual) | ||
}) | ||
|
||
t.Run("should error out while fetching pipeline run history as server returned non 200 status code", func(t *testing.T) { | ||
server := mockServer([]byte("pipelineRunHistoryJSON"), http.StatusBadGateway, nil, true, nil) | ||
client := gocd.NewClient(server.URL, auth, "info", nil) | ||
|
||
actual, err := client.GetPipelineRunHistory("helm-images") | ||
actual, err := client.GetPipelineRunHistory("helm-images", "0", time.Duration(2)*time.Second) | ||
assert.EqualError(t, err, "got 502 from GoCD while making GET call for "+server.URL+ | ||
"/api/pipelines/helm-images/history\nwith BODY:pipelineRunHistoryJSON") | ||
"/api/pipelines/helm-images/history?after=0&page_size=0\nwith BODY:pipelineRunHistoryJSON") | ||
assert.Nil(t, actual) | ||
}) | ||
|
||
t.Run("should error out while fetching pipeline run history as server returned malformed response", func(t *testing.T) { | ||
server := mockServer([]byte(`{"pipelineRunHistoryJSON"}`), http.StatusOK, nil, true, nil) | ||
client := gocd.NewClient(server.URL, auth, "info", nil) | ||
|
||
actual, err := client.GetPipelineRunHistory("helm-images") | ||
actual, err := client.GetPipelineRunHistory("helm-images", "0", time.Duration(2)*time.Second) | ||
assert.EqualError(t, err, "reading response body errored with: invalid character '}' after object key") | ||
assert.Nil(t, actual) | ||
}) | ||
|
||
t.Run("should be able to fetch the pipeline run history present in GoCD", func(t *testing.T) { | ||
server := mockServer([]byte(pipelineRunHistoryJSON), http.StatusOK, map[string]string{ | ||
"Accept": gocd.HeaderVersionOne, | ||
"Content-Type": gocd.ContentJSON, | ||
}, true, nil) | ||
client := gocd.NewClient(server.URL, auth, "info", nil) | ||
|
||
expected := []gocd.PipelineRunHistory{ | ||
{ | ||
Name: "helm-images", | ||
Counter: 3, | ||
ScheduledDate: 1678470766332, | ||
BuildCause: gocd.PipelineBuildCause{Message: "Forced by admin", Approver: "admin", TriggerForced: true}, | ||
}, | ||
{ | ||
Name: "helm-images", | ||
Counter: 2, | ||
ScheduledDate: 1677128882155, | ||
BuildCause: gocd.PipelineBuildCause{Message: "modified by nikhilsbhat <[email protected]>", Approver: "changes", TriggerForced: false}, | ||
}, | ||
{ | ||
Name: "helm-images", | ||
Counter: 1, | ||
ScheduledDate: 1672544013154, | ||
BuildCause: gocd.PipelineBuildCause{Message: "Forced by admin", Approver: "admin", TriggerForced: true}, | ||
}, | ||
} | ||
|
||
actual, err := client.GetPipelineRunHistory("helm-images") | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
// t.Run("should be able to fetch the pipeline run history present in GoCD", func(t *testing.T) { | ||
// server := mockServer([]byte(pipelineRunHistoryJSON), http.StatusOK, map[string]string{ | ||
// "Accept": gocd.HeaderVersionOne, | ||
// "Content-Type": gocd.ContentJSON, | ||
// }, true, nil) | ||
// client := gocd.NewClient(server.URL, auth, "info", nil) | ||
// | ||
// expected := []gocd.PipelineRunHistory{ | ||
// { | ||
// Name: "helm-images", | ||
// Counter: 3, | ||
// ScheduledDate: 1678470766332, | ||
// BuildCause: gocd.PipelineBuildCause{Message: "Forced by admin", Approver: "admin", TriggerForced: true}, | ||
// }, | ||
// { | ||
// Name: "helm-images", | ||
// Counter: 2, | ||
// ScheduledDate: 1677128882155, | ||
// BuildCause: gocd.PipelineBuildCause{Message: "modified by nikhilsbhat <[email protected]>", Approver: "changes", TriggerForced: false}, | ||
// }, | ||
// { | ||
// Name: "helm-images", | ||
// Counter: 1, | ||
// ScheduledDate: 1672544013154, | ||
// BuildCause: gocd.PipelineBuildCause{Message: "Forced by admin", Approver: "admin", TriggerForced: true}, | ||
// }, | ||
// } | ||
// | ||
// actual, err := client.GetPipelineRunHistory("helm-images", "0") | ||
// assert.NoError(t, err) | ||
// assert.Equal(t, expected, actual) | ||
// }) | ||
} | ||
|
||
func Test_client_getPipelineName(t *testing.T) { | ||
|
@@ -643,93 +642,6 @@ func Test_client_GetPipelineInstance(t *testing.T) { | |
}) | ||
} | ||
|
||
// func Test_client_GetPipelineHistory2(t *testing.T) { | ||
// t.Run("should be able to fetch the pipeline history successfully", func(t *testing.T) { | ||
// client := gocd.NewClient( | ||
// "http://localhost:8153/go", | ||
// "admin", | ||
// "admin", | ||
// "info", | ||
// nil, | ||
// ) | ||
// | ||
// actual, err := client.GetPipelineHistory("gocd-prometheus-exporter", 10, 0) | ||
// assert.NoError(t, err) | ||
// | ||
// for _, pipeline := range actual { | ||
// log.Println(pipeline["name"], pipeline["counter"]) | ||
// } | ||
// assert.Equal(t, "", actual) | ||
// }) | ||
// } | ||
|
||
// func Test_client_GetPipelineHistory(t *testing.T) { | ||
// correctPipelineHeader := map[string]string{"Accept": gocd.HeaderVersionOne} | ||
// server1 := mockServer([]byte(pipelineHistory), http.StatusOK, correctPipelineHeader, false, nil) | ||
// server2 := mockServer([]byte(pipelineHistory), http.StatusOK, correctPipelineHeader, false, nil) | ||
// | ||
// type errorTestCases struct { | ||
// description string | ||
// mockServer *httptest.Server | ||
// expectedError bool | ||
// errorString string | ||
// expected []map[string]interface{} | ||
// } | ||
// | ||
// expectOne := []map[string]interface{}{ | ||
// {"name": "pipeline1", "counter": 1}, | ||
// {"name": "pipeline1", "counter": 2}, | ||
// {"name": "pipeline1", "counter": 3}, | ||
// {"name": "pipeline1", "counter": 4}, | ||
// {"name": "pipeline1", "counter": 5}, | ||
// {"name": "pipeline1", "counter": 6}, | ||
// {"name": "pipeline1", "counter": 7}, | ||
// {"name": "pipeline1", "counter": 8}, | ||
// {"name": "pipeline1", "counter": 9}, | ||
// {"name": "pipeline1", "counter": 10}, | ||
// } | ||
// expectTwo := []map[string]interface{}{ | ||
// {"name": "pipeline1", "counter": 11}, | ||
// {"name": "pipeline1", "counter": 12}, | ||
// {"name": "pipeline1", "counter": 13}, | ||
// {"name": "pipeline1", "counter": 14}, | ||
// {"name": "pipeline1", "counter": 15}, | ||
// {"name": "pipeline1", "counter": 16}, | ||
// {"name": "pipeline1", "counter": 17}, | ||
// {"name": "pipeline1", "counter": 18}, | ||
// {"name": "pipeline1", "counter": 19}, | ||
// {"name": "pipeline1", "counter": 20}, | ||
// } | ||
// | ||
// tests := []errorTestCases{ | ||
// { | ||
// description: "should be able to paginate once successfully", | ||
// mockServer: server1, | ||
// expected: expectOne, | ||
// }, | ||
// { | ||
// description: "should be able to paginate once successfully", | ||
// mockServer: server2, | ||
// expected: expectTwo, | ||
// }, | ||
// } | ||
// | ||
// for _, tt := range tests { | ||
// t.Run(tt.description, func(t *testing.T) { | ||
// client := gocd.NewClient( | ||
// tt.mockServer.URL, | ||
// "admin", | ||
// "admin", | ||
// "info", | ||
// nil, | ||
// ) | ||
// got, err := client.GetPipelineHistory("pipeline1", 10, 0) | ||
// assert.NoError(t, err) | ||
// assert.Equal(t, tt.expected, got) | ||
// }) | ||
// } | ||
// } | ||
|
||
func Test_client_ScheduledJobs(t *testing.T) { | ||
t.Run("should error out while fetching scheduled jobs from server", func(t *testing.T) { | ||
client := gocd.NewClient("http://localhost:8156/go", auth, "info", nil) | ||
|