Skip to content

Commit

Permalink
Return error for non-existing runner group. (#2215)
Browse files Browse the repository at this point in the history
  • Loading branch information
TingluoHuang authored Jan 26, 2023
1 parent 7ea60e4 commit b09e3a2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion github/actions/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (c *Client) GetRunnerGroupByName(ctx context.Context, runnerGroup string) (
}

if runnerGroupList.Count == 0 {
return nil, nil
return nil, fmt.Errorf("no runner group found with name '%s'", runnerGroup)
}

if runnerGroupList.Count > 1 {
Expand Down
44 changes: 44 additions & 0 deletions github/actions/client_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,47 @@ func TestDeleteRunner(t *testing.T) {
assert.Equalf(t, actualRetry, expectedRetry, "A retry was expected after the first request but got: %v", actualRetry)
})
}

func TestGetRunnerGroupByName(t *testing.T) {
ctx := context.Background()
auth := &actions.ActionsAuth{
Token: "token",
}

t.Run("Get RunnerGroup by Name", func(t *testing.T) {
var runnerGroupID int64 = 1
var runnerGroupName string = "test-runner-group"
want := &actions.RunnerGroup{
ID: runnerGroupID,
Name: runnerGroupName,
}
response := []byte(`{"count": 1, "value": [{"id": 1, "name": "test-runner-group"}]}`)

server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(response)
}))

client, err := actions.NewClient(ctx, server.configURLForOrg("my-org"), auth)
require.NoError(t, err)

got, err := client.GetRunnerGroupByName(ctx, runnerGroupName)
require.NoError(t, err)
assert.Equal(t, want, got)
})

t.Run("Get RunnerGroup by name with not exist runner group", func(t *testing.T) {
var runnerGroupName string = "test-runner-group"
response := []byte(`{"count": 0, "value": []}`)

server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(response)
}))

client, err := actions.NewClient(ctx, server.configURLForOrg("my-org"), auth)
require.NoError(t, err)

got, err := client.GetRunnerGroupByName(ctx, runnerGroupName)
assert.ErrorContains(t, err, "no runner group found with name")
assert.Nil(t, got)
})
}

0 comments on commit b09e3a2

Please sign in to comment.