Refactor actions.Client with options to help extensibility (#2193)
This commit is contained in:
@@ -3,23 +3,21 @@ package actions_test
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/actions/actions-runner-controller/github/actions"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/go-retryablehttp"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var tokenExpireAt = time.Now().Add(10 * time.Minute)
|
||||
|
||||
func TestGetRunner(t *testing.T) {
|
||||
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjI1MTYyMzkwMjJ9.tlrHslTmDkoqnc4Kk9ISoKoUNDfHo-kjlH-ByISBqzE"
|
||||
ctx := context.Background()
|
||||
auth := &actions.ActionsAuth{
|
||||
Token: "token",
|
||||
}
|
||||
|
||||
t.Run("Get Runner", func(t *testing.T) {
|
||||
name := "Get Runner"
|
||||
var runnerID int64 = 1
|
||||
want := &actions.RunnerReference{
|
||||
Id: int(runnerID),
|
||||
@@ -27,59 +25,45 @@ func TestGetRunner(t *testing.T) {
|
||||
}
|
||||
response := []byte(`{"id": 1, "name": "self-hosted-ubuntu"}`)
|
||||
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write(response)
|
||||
}))
|
||||
defer s.Close()
|
||||
|
||||
actionsClient := actions.Client{
|
||||
ActionsServiceURL: &s.URL,
|
||||
ActionsServiceAdminToken: &token,
|
||||
ActionsServiceAdminTokenExpiresAt: &tokenExpireAt,
|
||||
}
|
||||
client, err := actions.NewClient(ctx, server.configURLForOrg("my-org"), auth)
|
||||
require.NoError(t, err)
|
||||
|
||||
got, err := actionsClient.GetRunner(context.Background(), runnerID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetRunner got unexepected error, %v", err)
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(want, got); diff != "" {
|
||||
t.Errorf("GetRunner(%v) mismatch (-want +got):\n%s", name, diff)
|
||||
}
|
||||
got, err := client.GetRunner(ctx, runnerID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, want, got)
|
||||
})
|
||||
|
||||
t.Run("Default retries on server error", func(t *testing.T) {
|
||||
var runnerID int64 = 1
|
||||
retryClient := retryablehttp.NewClient()
|
||||
retryClient.RetryWaitMax = 1 * time.Millisecond
|
||||
retryClient.RetryMax = 1
|
||||
retryWaitMax := 1 * time.Millisecond
|
||||
retryMax := 1
|
||||
|
||||
actualRetry := 0
|
||||
expectedRetry := retryClient.RetryMax + 1
|
||||
expectedRetry := retryMax + 1
|
||||
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
actualRetry++
|
||||
}))
|
||||
defer s.Close()
|
||||
|
||||
httpClient := retryClient.StandardClient()
|
||||
|
||||
actionsClient := actions.Client{
|
||||
Client: httpClient,
|
||||
ActionsServiceURL: &s.URL,
|
||||
ActionsServiceAdminToken: &token,
|
||||
ActionsServiceAdminTokenExpiresAt: &tokenExpireAt,
|
||||
}
|
||||
|
||||
_, _ = actionsClient.GetRunner(context.Background(), runnerID)
|
||||
client, err := actions.NewClient(ctx, server.configURLForOrg("my-org"), auth, actions.WithRetryMax(retryMax), actions.WithRetryWaitMax(retryWaitMax))
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = client.GetRunner(ctx, runnerID)
|
||||
require.Error(t, err)
|
||||
assert.Equalf(t, actualRetry, expectedRetry, "A retry was expected after the first request but got: %v", actualRetry)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetRunnerByName(t *testing.T) {
|
||||
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjI1MTYyMzkwMjJ9.tlrHslTmDkoqnc4Kk9ISoKoUNDfHo-kjlH-ByISBqzE"
|
||||
ctx := context.Background()
|
||||
auth := &actions.ActionsAuth{
|
||||
Token: "token",
|
||||
}
|
||||
|
||||
t.Run("Get Runner by Name", func(t *testing.T) {
|
||||
var runnerID int64 = 1
|
||||
@@ -90,130 +74,102 @@ func TestGetRunnerByName(t *testing.T) {
|
||||
}
|
||||
response := []byte(`{"count": 1, "value": [{"id": 1, "name": "self-hosted-ubuntu"}]}`)
|
||||
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write(response)
|
||||
}))
|
||||
defer s.Close()
|
||||
|
||||
actionsClient := actions.Client{
|
||||
ActionsServiceURL: &s.URL,
|
||||
ActionsServiceAdminToken: &token,
|
||||
ActionsServiceAdminTokenExpiresAt: &tokenExpireAt,
|
||||
}
|
||||
client, err := actions.NewClient(ctx, server.configURLForOrg("my-org"), auth)
|
||||
require.NoError(t, err)
|
||||
|
||||
got, err := actionsClient.GetRunnerByName(context.Background(), runnerName)
|
||||
if err != nil {
|
||||
t.Fatalf("GetRunnerByName got unexepected error, %v", err)
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(want, got); diff != "" {
|
||||
t.Errorf("GetRunnerByName(%v) mismatch (-want +got):\n%s", runnerName, diff)
|
||||
}
|
||||
got, err := client.GetRunnerByName(ctx, runnerName)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, want, got)
|
||||
})
|
||||
|
||||
t.Run("Get Runner by name with not exist runner", func(t *testing.T) {
|
||||
var runnerName string = "self-hosted-ubuntu"
|
||||
response := []byte(`{"count": 0, "value": []}`)
|
||||
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write(response)
|
||||
}))
|
||||
defer s.Close()
|
||||
|
||||
actionsClient := actions.Client{
|
||||
ActionsServiceURL: &s.URL,
|
||||
ActionsServiceAdminToken: &token,
|
||||
ActionsServiceAdminTokenExpiresAt: &tokenExpireAt,
|
||||
}
|
||||
client, err := actions.NewClient(ctx, server.configURLForOrg("my-org"), auth)
|
||||
require.NoError(t, err)
|
||||
|
||||
got, err := actionsClient.GetRunnerByName(context.Background(), runnerName)
|
||||
if err != nil {
|
||||
t.Fatalf("GetRunnerByName got unexepected error, %v", err)
|
||||
}
|
||||
|
||||
if diff := cmp.Diff((*actions.RunnerReference)(nil), got); diff != "" {
|
||||
t.Errorf("GetRunnerByName(%v) mismatch (-want +got):\n%s", runnerName, diff)
|
||||
}
|
||||
got, err := client.GetRunnerByName(ctx, runnerName)
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, got)
|
||||
})
|
||||
|
||||
t.Run("Default retries on server error", func(t *testing.T) {
|
||||
var runnerName string = "self-hosted-ubuntu"
|
||||
retryClient := retryablehttp.NewClient()
|
||||
retryClient.RetryWaitMax = 1 * time.Millisecond
|
||||
retryClient.RetryMax = 1
|
||||
|
||||
retryWaitMax := 1 * time.Millisecond
|
||||
retryMax := 1
|
||||
|
||||
actualRetry := 0
|
||||
expectedRetry := retryClient.RetryMax + 1
|
||||
expectedRetry := retryMax + 1
|
||||
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
actualRetry++
|
||||
}))
|
||||
defer s.Close()
|
||||
|
||||
httpClient := retryClient.StandardClient()
|
||||
|
||||
actionsClient := actions.Client{
|
||||
Client: httpClient,
|
||||
ActionsServiceURL: &s.URL,
|
||||
ActionsServiceAdminToken: &token,
|
||||
ActionsServiceAdminTokenExpiresAt: &tokenExpireAt,
|
||||
}
|
||||
|
||||
_, _ = actionsClient.GetRunnerByName(context.Background(), runnerName)
|
||||
client, err := actions.NewClient(ctx, server.configURLForOrg("my-org"), auth, actions.WithRetryMax(retryMax), actions.WithRetryWaitMax(retryWaitMax))
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = client.GetRunnerByName(ctx, runnerName)
|
||||
require.Error(t, err)
|
||||
assert.Equalf(t, actualRetry, expectedRetry, "A retry was expected after the first request but got: %v", actualRetry)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeleteRunner(t *testing.T) {
|
||||
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjI1MTYyMzkwMjJ9.tlrHslTmDkoqnc4Kk9ISoKoUNDfHo-kjlH-ByISBqzE"
|
||||
ctx := context.Background()
|
||||
auth := &actions.ActionsAuth{
|
||||
Token: "token",
|
||||
}
|
||||
|
||||
t.Run("Delete Runner", func(t *testing.T) {
|
||||
var runnerID int64 = 1
|
||||
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
defer s.Close()
|
||||
|
||||
actionsClient := actions.Client{
|
||||
ActionsServiceURL: &s.URL,
|
||||
ActionsServiceAdminToken: &token,
|
||||
ActionsServiceAdminTokenExpiresAt: &tokenExpireAt,
|
||||
}
|
||||
client, err := actions.NewClient(ctx, server.configURLForOrg("my-org"), auth)
|
||||
require.NoError(t, err)
|
||||
|
||||
if err := actionsClient.RemoveRunner(context.Background(), runnerID); err != nil {
|
||||
t.Fatalf("RemoveRunner got unexepected error, %v", err)
|
||||
}
|
||||
err = client.RemoveRunner(ctx, runnerID)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Default retries on server error", func(t *testing.T) {
|
||||
var runnerID int64 = 1
|
||||
|
||||
retryClient := retryablehttp.NewClient()
|
||||
retryClient.RetryWaitMax = 1 * time.Millisecond
|
||||
retryClient.RetryMax = 1
|
||||
retryWaitMax := 1 * time.Millisecond
|
||||
retryMax := 1
|
||||
|
||||
actualRetry := 0
|
||||
expectedRetry := retryClient.RetryMax + 1
|
||||
expectedRetry := retryMax + 1
|
||||
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
actualRetry++
|
||||
}))
|
||||
defer s.Close()
|
||||
|
||||
httpClient := retryClient.StandardClient()
|
||||
actionsClient := actions.Client{
|
||||
Client: httpClient,
|
||||
ActionsServiceURL: &s.URL,
|
||||
ActionsServiceAdminToken: &token,
|
||||
ActionsServiceAdminTokenExpiresAt: &tokenExpireAt,
|
||||
}
|
||||
|
||||
_ = actionsClient.RemoveRunner(context.Background(), runnerID)
|
||||
client, err := actions.NewClient(
|
||||
ctx,
|
||||
server.configURLForOrg("my-org"),
|
||||
auth,
|
||||
actions.WithRetryMax(retryMax),
|
||||
actions.WithRetryWaitMax(retryWaitMax),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = client.RemoveRunner(ctx, runnerID)
|
||||
require.Error(t, err)
|
||||
assert.Equalf(t, actualRetry, expectedRetry, "A retry was expected after the first request but got: %v", actualRetry)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user