mirror of
https://github.com/actions-runner-controller/actions-runner-controller.git
synced 2026-10-21 09:08:32 +02:00
Refactor actions.Client with options to help extensibility (#2193)
This commit is contained in:
@@ -3,22 +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"
|
||||
)
|
||||
|
||||
func TestAcquireJobs(t *testing.T) {
|
||||
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjI1MTYyMzkwMjJ9.tlrHslTmDkoqnc4Kk9ISoKoUNDfHo-kjlH-ByISBqzE"
|
||||
ctx := context.Background()
|
||||
auth := &actions.ActionsAuth{
|
||||
Token: "token",
|
||||
}
|
||||
|
||||
t.Run("Acquire Job", func(t *testing.T) {
|
||||
name := "Acquire Job"
|
||||
|
||||
want := []int64{1}
|
||||
response := []byte(`{"value": [1]}`)
|
||||
|
||||
@@ -28,24 +27,16 @@ func TestAcquireJobs(t *testing.T) {
|
||||
}
|
||||
requestIDs := want
|
||||
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Write(response)
|
||||
}))
|
||||
defer s.Close()
|
||||
|
||||
actionsClient := actions.Client{
|
||||
ActionsServiceURL: &s.URL,
|
||||
ActionsServiceAdminToken: &token,
|
||||
}
|
||||
client, err := actions.NewClient(ctx, server.configURLForOrg("my-org"), auth)
|
||||
require.NoError(t, err)
|
||||
|
||||
got, err := actionsClient.AcquireJobs(context.Background(), session.RunnerScaleSet.Id, session.MessageQueueAccessToken, requestIDs)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateRunnerScaleSet got unexepected error, %v", err)
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(want, got); diff != "" {
|
||||
t.Errorf("GetRunnerScaleSet(%v) mismatch (-want +got):\n%s", name, diff)
|
||||
}
|
||||
got, err := client.AcquireJobs(ctx, session.RunnerScaleSet.Id, session.MessageQueueAccessToken, requestIDs)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, want, got)
|
||||
})
|
||||
|
||||
t.Run("Default retries on server error", func(t *testing.T) {
|
||||
@@ -55,90 +46,78 @@ func TestAcquireJobs(t *testing.T) {
|
||||
}
|
||||
var requestIDs []int64 = []int64{1}
|
||||
|
||||
retryClient := retryablehttp.NewClient()
|
||||
retryClient.RetryWaitMax = 1 * time.Millisecond
|
||||
retryClient.RetryMax = 1
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
_, _ = actionsClient.AcquireJobs(context.Background(), session.RunnerScaleSet.Id, session.MessageQueueAccessToken, requestIDs)
|
||||
client, err := actions.NewClient(
|
||||
ctx,
|
||||
server.configURLForOrg("my-org"),
|
||||
auth,
|
||||
actions.WithRetryMax(retryMax),
|
||||
actions.WithRetryWaitMax(1*time.Millisecond),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = client.AcquireJobs(context.Background(), session.RunnerScaleSet.Id, session.MessageQueueAccessToken, requestIDs)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equalf(t, actualRetry, expectedRetry, "A retry was expected after the first request but got: %v", actualRetry)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetAcquirableJobs(t *testing.T) {
|
||||
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjI1MTYyMzkwMjJ9.tlrHslTmDkoqnc4Kk9ISoKoUNDfHo-kjlH-ByISBqzE"
|
||||
ctx := context.Background()
|
||||
auth := &actions.ActionsAuth{
|
||||
Token: "token",
|
||||
}
|
||||
|
||||
t.Run("Acquire Job", func(t *testing.T) {
|
||||
name := "Acquire Job"
|
||||
|
||||
want := &actions.AcquirableJobList{}
|
||||
response := []byte(`{"count": 0}`)
|
||||
|
||||
runnerScaleSet := &actions.RunnerScaleSet{Id: 1}
|
||||
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *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.GetAcquirableJobs(context.Background(), runnerScaleSet.Id)
|
||||
if err != nil {
|
||||
t.Fatalf("GetAcquirableJobs got unexepected error, %v", err)
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(want, got); diff != "" {
|
||||
t.Errorf("GetAcquirableJobs(%v) mismatch (-want +got):\n%s", name, diff)
|
||||
}
|
||||
got, err := client.GetAcquirableJobs(context.Background(), runnerScaleSet.Id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, want, got)
|
||||
})
|
||||
|
||||
t.Run("Default retries on server error", func(t *testing.T) {
|
||||
runnerScaleSet := &actions.RunnerScaleSet{Id: 1}
|
||||
|
||||
retryClient := retryablehttp.NewClient()
|
||||
retryClient.RetryWaitMax = 1 * time.Millisecond
|
||||
retryClient.RetryMax = 1
|
||||
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.GetAcquirableJobs(context.Background(), runnerScaleSet.Id)
|
||||
client, err := actions.NewClient(
|
||||
context.Background(),
|
||||
server.configURLForOrg("my-org"),
|
||||
auth,
|
||||
actions.WithRetryMax(retryMax),
|
||||
actions.WithRetryWaitMax(1*time.Millisecond),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = client.GetAcquirableJobs(context.Background(), runnerScaleSet.Id)
|
||||
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