Refactor actions.Client with options to help extensibility (#2193)

This commit is contained in:
Francesco Renzi
2023-01-23 11:50:14 +00:00
committed by GitHub
parent 282f2dd09c
commit 3327f620fb
11 changed files with 642 additions and 1285 deletions

View File

@@ -4,19 +4,22 @@ import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/actions/actions-runner-controller/github/actions"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCreateMessageSession(t *testing.T) {
ctx := context.Background()
auth := &actions.ActionsAuth{
Token: "token",
}
t.Run("CreateMessageSession unmarshals correctly", func(t *testing.T) {
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjI1MTYyMzkwMjJ9.tlrHslTmDkoqnc4Kk9ISoKoUNDfHo-kjlH-ByISBqzE"
owner := "foo"
runnerScaleSet := actions.RunnerScaleSet{
Id: 1,
@@ -35,7 +38,7 @@ func TestCreateMessageSession(t *testing.T) {
MessageQueueAccessToken: "fake.jwt.here",
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
resp := []byte(`{
"ownerName": "foo",
"runnerScaleSet": {
@@ -47,31 +50,16 @@ func TestCreateMessageSession(t *testing.T) {
}`)
w.Write(resp)
}))
defer srv.Close()
retryMax := 1
retryWaitMax := 1 * time.Microsecond
client, err := actions.NewClient(ctx, server.configURLForOrg("my-org"), auth)
require.NoError(t, err)
actionsClient := actions.Client{
ActionsServiceURL: &srv.URL,
ActionsServiceAdminToken: &token,
ActionsServiceAdminTokenExpiresAt: &tokenExpireAt,
RetryMax: &retryMax,
RetryWaitMax: &retryWaitMax,
}
got, err := actionsClient.CreateMessageSession(context.Background(), runnerScaleSet.Id, owner)
if err != nil {
t.Fatalf("CreateMessageSession got unexpected error: %v", err)
}
if diff := cmp.Diff(got, want); diff != "" {
t.Fatalf("CreateMessageSession got unexpected diff: -want +got: %v", diff)
}
got, err := client.CreateMessageSession(ctx, runnerScaleSet.Id, owner)
require.NoError(t, err)
assert.Equal(t, want, got)
})
t.Run("CreateMessageSession unmarshals errors into ActionsError", func(t *testing.T) {
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjI1MTYyMzkwMjJ9.tlrHslTmDkoqnc4Kk9ISoKoUNDfHo-kjlH-ByISBqzE"
owner := "foo"
runnerScaleSet := actions.RunnerScaleSet{
Id: 1,
@@ -86,44 +74,32 @@ func TestCreateMessageSession(t *testing.T) {
StatusCode: http.StatusBadRequest,
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
resp := []byte(`{"typeName": "CSharpExceptionNameHere","message": "could not do something"}`)
w.Write(resp)
}))
defer srv.Close()
retryMax := 1
retryWaitMax := 1 * time.Microsecond
client, err := actions.NewClient(ctx, server.configURLForOrg("my-org"), auth)
require.NoError(t, err)
actionsClient := actions.Client{
ActionsServiceURL: &srv.URL,
ActionsServiceAdminToken: &token,
ActionsServiceAdminTokenExpiresAt: &tokenExpireAt,
RetryMax: &retryMax,
RetryWaitMax: &retryWaitMax,
}
got, err := actionsClient.CreateMessageSession(context.Background(), runnerScaleSet.Id, owner)
if err == nil {
t.Fatalf("CreateMessageSession did not get expected error: %v", got)
}
_, err = client.CreateMessageSession(ctx, runnerScaleSet.Id, owner)
require.NotNil(t, err)
errorTypeForComparison := &actions.ActionsError{}
if isActionsError := errors.As(err, &errorTypeForComparison); !isActionsError {
t.Fatalf("CreateMessageSession expected to be able to parse the error into ActionsError type: %v", err)
}
assert.True(
t,
errors.As(err, &errorTypeForComparison),
"CreateMessageSession expected to be able to parse the error into ActionsError type: %v",
err,
)
gotErr := err.(*actions.ActionsError)
if diff := cmp.Diff(want, gotErr); diff != "" {
t.Fatalf("CreateMessageSession got unexpected diff: -want +got: %v", diff)
}
assert.Equal(t, want, gotErr)
})
t.Run("CreateMessageSession call is retried the correct amount of times", func(t *testing.T) {
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjI1MTYyMzkwMjJ9.tlrHslTmDkoqnc4Kk9ISoKoUNDfHo-kjlH-ByISBqzE"
owner := "foo"
runnerScaleSet := actions.RunnerScaleSet{
Id: 1,
@@ -133,37 +109,38 @@ func TestCreateMessageSession(t *testing.T) {
}
gotRetries := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
gotRetries++
}))
defer srv.Close()
retryMax := 3
retryWaitMax, err := time.ParseDuration("1µs")
if err != nil {
t.Fatalf("%v", err)
}
retryWaitMax := 1 * time.Microsecond
wantRetries := retryMax + 1
actionsClient := actions.Client{
ActionsServiceURL: &srv.URL,
ActionsServiceAdminToken: &token,
ActionsServiceAdminTokenExpiresAt: &tokenExpireAt,
RetryMax: &retryMax,
RetryWaitMax: &retryWaitMax,
}
_, _ = actionsClient.CreateMessageSession(context.Background(), runnerScaleSet.Id, owner)
client, err := actions.NewClient(
ctx,
server.configURLForOrg("my-org"),
auth,
actions.WithRetryMax(retryMax),
actions.WithRetryWaitMax(retryWaitMax),
)
require.NoError(t, err)
_, err = client.CreateMessageSession(ctx, runnerScaleSet.Id, owner)
assert.NotNil(t, err)
assert.Equalf(t, gotRetries, wantRetries, "CreateMessageSession got unexpected retry count: got=%v, want=%v", gotRetries, wantRetries)
})
}
func TestDeleteMessageSession(t *testing.T) {
ctx := context.Background()
auth := &actions.ActionsAuth{
Token: "token",
}
t.Run("DeleteMessageSession call is retried the correct amount of times", func(t *testing.T) {
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjI1MTYyMzkwMjJ9.tlrHslTmDkoqnc4Kk9ISoKoUNDfHo-kjlH-ByISBqzE"
runnerScaleSet := actions.RunnerScaleSet{
Id: 1,
Name: "ScaleSet",
@@ -172,39 +149,40 @@ func TestDeleteMessageSession(t *testing.T) {
}
gotRetries := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
gotRetries++
}))
defer srv.Close()
retryMax := 3
retryWaitMax, err := time.ParseDuration("1µs")
if err != nil {
t.Fatalf("%v", err)
}
retryWaitMax := 1 * time.Microsecond
wantRetries := retryMax + 1
actionsClient := actions.Client{
ActionsServiceURL: &srv.URL,
ActionsServiceAdminToken: &token,
ActionsServiceAdminTokenExpiresAt: &tokenExpireAt,
RetryMax: &retryMax,
RetryWaitMax: &retryWaitMax,
}
client, err := actions.NewClient(
ctx,
server.configURLForOrg("my-org"),
auth,
actions.WithRetryMax(retryMax),
actions.WithRetryWaitMax(retryWaitMax),
)
require.NoError(t, err)
sessionId := uuid.New()
_ = actionsClient.DeleteMessageSession(context.Background(), runnerScaleSet.Id, &sessionId)
err = client.DeleteMessageSession(ctx, runnerScaleSet.Id, &sessionId)
assert.NotNil(t, err)
assert.Equalf(t, gotRetries, wantRetries, "CreateMessageSession got unexpected retry count: got=%v, want=%v", gotRetries, wantRetries)
})
}
func TestRefreshMessageSession(t *testing.T) {
ctx := context.Background()
auth := &actions.ActionsAuth{
Token: "token",
}
t.Run("RefreshMessageSession call is retried the correct amount of times", func(t *testing.T) {
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjI1MTYyMzkwMjJ9.tlrHslTmDkoqnc4Kk9ISoKoUNDfHo-kjlH-ByISBqzE"
runnerScaleSet := actions.RunnerScaleSet{
Id: 1,
Name: "ScaleSet",
@@ -213,32 +191,29 @@ func TestRefreshMessageSession(t *testing.T) {
}
gotRetries := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
gotRetries++
}))
defer srv.Close()
retryMax := 3
retryWaitMax, err := time.ParseDuration("1µs")
if err != nil {
t.Fatalf("%v", err)
}
retryWaitMax := 1 * time.Microsecond
wantRetries := retryMax + 1
actionsClient := actions.Client{
ActionsServiceURL: &srv.URL,
ActionsServiceAdminToken: &token,
ActionsServiceAdminTokenExpiresAt: &tokenExpireAt,
RetryMax: &retryMax,
RetryWaitMax: &retryWaitMax,
}
client, err := actions.NewClient(
ctx,
server.configURLForOrg("my-org"),
auth,
actions.WithRetryMax(retryMax),
actions.WithRetryWaitMax(retryWaitMax),
)
require.NoError(t, err)
sessionId := uuid.New()
_, _ = actionsClient.RefreshMessageSession(context.Background(), runnerScaleSet.Id, &sessionId)
_, err = client.RefreshMessageSession(context.Background(), runnerScaleSet.Id, &sessionId)
assert.NotNil(t, err)
assert.Equalf(t, gotRetries, wantRetries, "CreateMessageSession got unexpected retry count: got=%v, want=%v", gotRetries, wantRetries)
})
}