Skip to content

Commit

Permalink
chore: add more repo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eliecharra committed Jun 18, 2024
1 parent 47593e5 commit 9c47005
Show file tree
Hide file tree
Showing 9 changed files with 1,170 additions and 62 deletions.
28 changes: 16 additions & 12 deletions internal/spacelift/repository/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ func NewContextRepository(client client.Client) *contextRepository {
return &contextRepository{client: client}
}

type contextCreateMutation struct {
ContextCreate struct {
Id string `graphql:"id"`
} `graphql:"contextCreateV2(input: $input)"`
}

func (r *contextRepository) Create(ctx context.Context, context *v1beta1.Context) (*models.Context, error) {
c, err := spaceliftclient.GetSpaceliftClient(ctx, r.client, context.Namespace)
c, err := spaceliftclient.DefaultClient(ctx, r.client, context.Namespace)
if err != nil {
return nil, errors.Wrap(err, "unable to fetch spacelift client while creating context")
}

var createMutation struct {
ContextCreate struct {
Id string `graphql:"id"`
} `graphql:"contextCreateV2(input: $input)"`
}
var createMutation contextCreateMutation

contextInput, err := structs.FromContextSpec(context)
if err != nil {
Expand All @@ -61,17 +63,19 @@ func (r *contextRepository) Create(ctx context.Context, context *v1beta1.Context
}, nil
}

type contextUpdateMutation struct {
ContextUpdate struct {
Id string `graphql:"id"`
} `graphql:"contextUpdateV2(id: $id, input: $input, replaceConfigElements: $replaceConfigElements)"`
}

func (r *contextRepository) Update(ctx context.Context, context *v1beta1.Context) (*models.Context, error) {
c, err := spaceliftclient.GetSpaceliftClient(ctx, r.client, context.Namespace)
c, err := spaceliftclient.DefaultClient(ctx, r.client, context.Namespace)
if err != nil {
return nil, errors.Wrap(err, "unable to fetch spacelift client while creating context")
}

var updateMutation struct {
ContextUpdate struct {
Id string `graphql:"id"`
} `graphql:"contextUpdateV2(id: $id, input: $input, replaceConfigElements: $replaceConfigElements)"`
}
var updateMutation contextUpdateMutation

contextInput, err := structs.FromContextSpec(context)
if err != nil {
Expand Down
185 changes: 185 additions & 0 deletions internal/spacelift/repository/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package repository

import (
"context"
"testing"

"github.com/shurcooL/graphql"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/spacelift-io/spacelift-operator/api/v1beta1"
spaceliftclient "github.com/spacelift-io/spacelift-operator/internal/spacelift/client"
"github.com/spacelift-io/spacelift-operator/internal/spacelift/client/mocks"
"github.com/spacelift-io/spacelift-operator/internal/spacelift/repository/structs"
"github.com/spacelift-io/spacelift-operator/internal/utils"
)

func Test_contextRepository_Create(t *testing.T) {
testCases := []struct {
name string
context v1beta1.Context
assertPayload func(*testing.T, structs.ContextInput)
}{
{
name: "basic context",
context: v1beta1.Context{
ObjectMeta: v1.ObjectMeta{
Name: "name",
},
Spec: v1beta1.ContextSpec{
SpaceId: utils.AddressOf("test-space-id"),
Description: utils.AddressOf("test description"),
Labels: []string{"label1", "label2"},
Attachments: []v1beta1.Attachment{
{
StackId: utils.AddressOf("test-attached-stack-id"),
},
},
Hooks: v1beta1.Hooks{
AfterApply: []string{"after", "apply"},
},
Environment: []v1beta1.Environment{
{
Id: "id",
Value: utils.AddressOf("secret"),
Secret: utils.AddressOf(true),
Description: utils.AddressOf("test description"),
},
},
MountedFiles: []v1beta1.MountedFile{
{
Id: "id-file",
Value: utils.AddressOf("secret file"),
Secret: utils.AddressOf(true),
Description: utils.AddressOf("test description"),
},
},
},
},
assertPayload: func(t *testing.T, input structs.ContextInput) {
assert.Equal(t, "name", input.Name)
assert.Equal(t, "test description", *input.Description)
assert.Equal(t, []string{"label1", "label2"}, input.Labels)
assert.Equal(t, []string{"after", "apply"}, input.Hooks.AfterApply)
assert.Equal(t, []structs.StackAttachment{
{
Stack: "test-attached-stack-id",
},
}, input.StackAttachments)
assert.Equal(t, []structs.ConfigAttachments{
{
Description: utils.AddressOf("test description"),
Id: "id",
Type: "ENVIRONMENT_VARIABLE",
Value: "secret",
WriteOnly: true,
},
{
Description: utils.AddressOf("test description"),
Id: "id-file",
Type: "FILE_MOUNT",
Value: "secret file",
WriteOnly: true,
},
}, input.ConfigAttachments)
},
},
{
name: "basic context with name",
context: v1beta1.Context{
ObjectMeta: v1.ObjectMeta{
Name: "name",
},
Spec: v1beta1.ContextSpec{
SpaceId: utils.AddressOf("test-space-id"),
Name: utils.AddressOf("test name override"),
},
},
assertPayload: func(t *testing.T, input structs.ContextInput) {
assert.Equal(t, "test name override", input.Name)
},
},
}

originalClient := spaceliftclient.DefaultClient
defer func() { spaceliftclient.DefaultClient = originalClient }()
var fakeClient *mocks.Client
spaceliftclient.DefaultClient = func(_ context.Context, _ client.Client, _ string) (spaceliftclient.Client, error) {
return fakeClient, nil
}
repo := NewContextRepository(nil)

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
fakeClient = mocks.NewClient(t)
var actualVars = map[string]any{}
fakeClient.EXPECT().
Mutate(mock.Anything, mock.AnythingOfType("*repository.contextCreateMutation"), mock.Anything).
Run(func(_ context.Context, _ interface{}, vars map[string]interface{}, _a3 ...graphql.RequestOption) {
actualVars = vars
}).Return(nil)
_, err := repo.Create(context.Background(), &testCase.context)
require.NoError(t, err)
testCase.assertPayload(t, actualVars["input"].(structs.ContextInput))
})
}

}

func Test_contextRepository_Update(t *testing.T) {
testCases := []struct {
name string
context v1beta1.Context
assertPayload func(*testing.T, map[string]any)
}{
{
name: "basic context",
context: v1beta1.Context{
ObjectMeta: v1.ObjectMeta{
Name: "name",
},
Spec: v1beta1.ContextSpec{
SpaceId: utils.AddressOf("test-space-id"),
},
Status: v1beta1.ContextStatus{
Id: "test-context-id",
},
},
assertPayload: func(t *testing.T, input map[string]any) {
assert.Equal(t, "test-context-id", input["id"])
assert.Equal(t, graphql.Boolean(true), input["replaceConfigElements"])
// No need to assert on input details since we use the same code than the Create function
// and this is already covered in Test_contextRepository_Create
assert.IsType(t, structs.ContextInput{}, input["input"])
},
},
}

originalClient := spaceliftclient.DefaultClient
defer func() { spaceliftclient.DefaultClient = originalClient }()
var fakeClient *mocks.Client
spaceliftclient.DefaultClient = func(_ context.Context, _ client.Client, _ string) (spaceliftclient.Client, error) {
return fakeClient, nil
}
repo := NewContextRepository(nil)

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
fakeClient = mocks.NewClient(t)
var actualVars = map[string]any{}
fakeClient.EXPECT().
Mutate(mock.Anything, mock.AnythingOfType("*repository.contextUpdateMutation"), mock.Anything).
Run(func(_ context.Context, _ interface{}, vars map[string]interface{}, _a3 ...graphql.RequestOption) {
actualVars = vars
}).Return(nil)
_, err := repo.Update(context.Background(), &testCase.context)
require.NoError(t, err)
testCase.assertPayload(t, actualVars)
})
}

}
16 changes: 9 additions & 7 deletions internal/spacelift/repository/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ func NewRunRepository(client client.Client) *runRepository {
type CreateRunQuery struct {
}

type createRunMutation struct {
RunTrigger struct {
ID string `graphql:"id"`
State string `graphql:"state"`
} `graphql:"runTrigger(stack: $stack)"`
}

func (r *runRepository) Create(ctx context.Context, stack *v1beta1.Stack) (*models.Run, error) {
c, err := spaceliftclient.GetSpaceliftClient(ctx, r.client, stack.Namespace)
c, err := spaceliftclient.DefaultClient(ctx, r.client, stack.Namespace)
if err != nil {
return nil, errors.Wrap(err, "unable to fetch spacelift client while creating run")
}
var mutation struct {
RunTrigger struct {
ID string `graphql:"id"`
State string `graphql:"state"`
} `graphql:"runTrigger(stack: $stack)"`
}
var mutation createRunMutation
vars := map[string]any{
"stack": graphql.ID(stack.Status.Id),
}
Expand Down
51 changes: 51 additions & 0 deletions internal/spacelift/repository/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package repository

import (
"context"
"testing"

"github.com/shurcooL/graphql"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/spacelift-io/spacelift-operator/api/v1beta1"
spaceliftclient "github.com/spacelift-io/spacelift-operator/internal/spacelift/client"
"github.com/spacelift-io/spacelift-operator/internal/spacelift/client/mocks"
)

func Test_runRepository_Create(t *testing.T) {
originalClient := spaceliftclient.DefaultClient
defer func() { spaceliftclient.DefaultClient = originalClient }()
var fakeClient *mocks.Client
spaceliftclient.DefaultClient = func(_ context.Context, _ client.Client, _ string) (spaceliftclient.Client, error) {
return fakeClient, nil
}

var actualVars map[string]any
fakeClient = mocks.NewClient(t)
fakeClient.EXPECT().
Mutate(mock.Anything, mock.AnythingOfType("*repository.createRunMutation"), mock.Anything).
Run(func(_ context.Context, mutation any, vars map[string]interface{}, _ ...graphql.RequestOption) {
actualVars = vars
runMutation := mutation.(*createRunMutation)
runMutation.RunTrigger.ID = "run-id"
runMutation.RunTrigger.State = "QUEUED"
}).Return(nil)
fakeClient.EXPECT().URL("/stack/%s/run/%s", "stack-id", "run-id").Return("run-url")

fakeStack := &v1beta1.Stack{
Status: v1beta1.StackStatus{
Id: "stack-id",
},
}
repo := NewRunRepository(nil)
run, err := repo.Create(context.Background(), fakeStack)
assert.NoError(t, err)

assert.Equal(t, "stack-id", actualVars["stack"])
assert.Equal(t, "run-id", run.Id)
assert.Equal(t, "QUEUED", run.State)
assert.Equal(t, "run-url", run.Url)
assert.Equal(t, "stack-id", run.StackId)
}
28 changes: 16 additions & 12 deletions internal/spacelift/repository/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ func NewSpaceRepository(client client.Client) *spaceRepository {
return &spaceRepository{client: client}
}

type spaceCreateMutation struct {
SpaceCreate struct {
ID string `graphql:"id"`
} `graphql:"spaceCreate(input: $input)"`
}

func (r *spaceRepository) Create(ctx context.Context, space *v1beta1.Space) (*models.Space, error) {
c, err := spaceliftclient.GetSpaceliftClient(ctx, r.client, space.Namespace)
c, err := spaceliftclient.DefaultClient(ctx, r.client, space.Namespace)
if err != nil {
return nil, errors.Wrap(err, "unable to fetch spacelift client while creating run")
}

var mutation struct {
SpaceCreate struct {
ID string `graphql:"id"`
} `graphql:"spaceCreate(input: $input)"`
}
var mutation spaceCreateMutation

spaceCreationVars := map[string]any{"input": structs.FromSpaceSpec(space)}

Expand All @@ -54,17 +56,19 @@ func (r *spaceRepository) Create(ctx context.Context, space *v1beta1.Space) (*mo
}, nil
}

type spaceUpdateMutation struct {
SpaceUpdate struct {
ID string `graphql:"id"`
} `graphql:"spaceUpdate(space: $space, input: $input)"`
}

func (r *spaceRepository) Update(ctx context.Context, space *v1beta1.Space) (*models.Space, error) {
c, err := spaceliftclient.GetSpaceliftClient(ctx, r.client, space.Namespace)
c, err := spaceliftclient.DefaultClient(ctx, r.client, space.Namespace)
if err != nil {
return nil, errors.Wrap(err, "unable to fetch spacelift client while updating space")
}

var spaceUpdateMutation struct {
SpaceUpdate struct {
ID string `graphql:"id"`
} `graphql:"spaceUpdate(space: $space, input: $input)"`
}
var spaceUpdateMutation spaceUpdateMutation

spaceUpdateVars := map[string]any{
"space": graphql.ID(space.Status.Id),
Expand Down
Loading

0 comments on commit 9c47005

Please sign in to comment.