Skip to content

Commit 97cdbe7

Browse files
committed
chore: add more repo tests
1 parent 3403b03 commit 97cdbe7

File tree

9 files changed

+1076
-50
lines changed

9 files changed

+1076
-50
lines changed

internal/spacelift/repository/context.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,19 @@ func NewContextRepository(client client.Client) *contextRepository {
3232
return &contextRepository{client: client}
3333
}
3434

35+
type contextCreateMutation struct {
36+
ContextCreate struct {
37+
Id string `graphql:"id"`
38+
} `graphql:"contextCreateV2(input: $input)"`
39+
}
40+
3541
func (r *contextRepository) Create(ctx context.Context, context *v1beta1.Context) (*models.Context, error) {
36-
c, err := spaceliftclient.GetSpaceliftClient(ctx, r.client, context.Namespace)
42+
c, err := spaceliftclient.DefaultClient(ctx, r.client, context.Namespace)
3743
if err != nil {
3844
return nil, errors.Wrap(err, "unable to fetch spacelift client while creating context")
3945
}
4046

41-
var createMutation struct {
42-
ContextCreate struct {
43-
Id string `graphql:"id"`
44-
} `graphql:"contextCreateV2(input: $input)"`
45-
}
47+
var createMutation contextCreateMutation
4648

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

66+
type contextUpdateMutation struct {
67+
ContextUpdate struct {
68+
Id string `graphql:"id"`
69+
} `graphql:"contextUpdateV2(id: $id, input: $input, replaceConfigElements: $replaceConfigElements)"`
70+
}
71+
6472
func (r *contextRepository) Update(ctx context.Context, context *v1beta1.Context) (*models.Context, error) {
65-
c, err := spaceliftclient.GetSpaceliftClient(ctx, r.client, context.Namespace)
73+
c, err := spaceliftclient.DefaultClient(ctx, r.client, context.Namespace)
6674
if err != nil {
6775
return nil, errors.Wrap(err, "unable to fetch spacelift client while creating context")
6876
}
6977

70-
var updateMutation struct {
71-
ContextUpdate struct {
72-
Id string `graphql:"id"`
73-
} `graphql:"contextUpdateV2(id: $id, input: $input, replaceConfigElements: $replaceConfigElements)"`
74-
}
78+
var updateMutation contextUpdateMutation
7579

7680
contextInput, err := structs.FromContextSpec(context)
7781
if err != nil {
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package repository
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/shurcooL/graphql"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/mock"
10+
"github.com/stretchr/testify/require"
11+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"sigs.k8s.io/controller-runtime/pkg/client"
13+
14+
"github.com/spacelift-io/spacelift-operator/api/v1beta1"
15+
spaceliftclient "github.com/spacelift-io/spacelift-operator/internal/spacelift/client"
16+
"github.com/spacelift-io/spacelift-operator/internal/spacelift/client/mocks"
17+
"github.com/spacelift-io/spacelift-operator/internal/spacelift/repository/structs"
18+
"github.com/spacelift-io/spacelift-operator/internal/utils"
19+
)
20+
21+
func Test_contextRepository_Create(t *testing.T) {
22+
testCases := []struct {
23+
name string
24+
context v1beta1.Context
25+
assertPayload func(*testing.T, structs.ContextInput)
26+
}{
27+
{
28+
name: "basic context",
29+
context: v1beta1.Context{
30+
ObjectMeta: v1.ObjectMeta{
31+
Name: "name",
32+
},
33+
Spec: v1beta1.ContextSpec{
34+
SpaceId: utils.AddressOf("test-space-id"),
35+
Description: utils.AddressOf("test description"),
36+
Labels: []string{"label1", "label2"},
37+
Attachments: []v1beta1.Attachment{
38+
{
39+
StackId: utils.AddressOf("test-attached-stack-id"),
40+
},
41+
},
42+
Hooks: v1beta1.Hooks{
43+
AfterApply: []string{"after", "apply"},
44+
},
45+
Environment: []v1beta1.Environment{
46+
{
47+
Id: "id",
48+
Value: utils.AddressOf("secret"),
49+
Secret: utils.AddressOf(true),
50+
Description: utils.AddressOf("test description"),
51+
},
52+
},
53+
MountedFiles: []v1beta1.MountedFile{
54+
{
55+
Id: "id-file",
56+
Value: utils.AddressOf("secret file"),
57+
Secret: utils.AddressOf(true),
58+
Description: utils.AddressOf("test description"),
59+
},
60+
},
61+
},
62+
},
63+
assertPayload: func(t *testing.T, input structs.ContextInput) {
64+
assert.Equal(t, "name", input.Name)
65+
assert.Equal(t, "test description", *input.Description)
66+
assert.Equal(t, []string{"label1", "label2"}, input.Labels)
67+
assert.Equal(t, []string{"after", "apply"}, input.Hooks.AfterApply)
68+
assert.Equal(t, []structs.StackAttachment{
69+
{
70+
Stack: "test-attached-stack-id",
71+
},
72+
}, input.StackAttachments)
73+
assert.Equal(t, []structs.ConfigAttachments{
74+
{
75+
Description: utils.AddressOf("test description"),
76+
Id: "id",
77+
Type: "ENVIRONMENT_VARIABLE",
78+
Value: "secret",
79+
WriteOnly: true,
80+
},
81+
{
82+
Description: utils.AddressOf("test description"),
83+
Id: "id-file",
84+
Type: "FILE_MOUNT",
85+
Value: "secret file",
86+
WriteOnly: true,
87+
},
88+
}, input.ConfigAttachments)
89+
},
90+
},
91+
{
92+
name: "basic context with name",
93+
context: v1beta1.Context{
94+
ObjectMeta: v1.ObjectMeta{
95+
Name: "name",
96+
},
97+
Spec: v1beta1.ContextSpec{
98+
SpaceId: utils.AddressOf("test-space-id"),
99+
Name: utils.AddressOf("test name override"),
100+
},
101+
},
102+
assertPayload: func(t *testing.T, input structs.ContextInput) {
103+
assert.Equal(t, "test name override", input.Name)
104+
},
105+
},
106+
}
107+
108+
originalClient := spaceliftclient.DefaultClient
109+
defer func() { spaceliftclient.DefaultClient = originalClient }()
110+
var fakeClient *mocks.Client
111+
spaceliftclient.DefaultClient = func(_ context.Context, _ client.Client, _ string) (spaceliftclient.Client, error) {
112+
return fakeClient, nil
113+
}
114+
repo := NewContextRepository(nil)
115+
116+
for _, testCase := range testCases {
117+
t.Run(testCase.name, func(t *testing.T) {
118+
fakeClient = mocks.NewClient(t)
119+
var actualVars = map[string]any{}
120+
fakeClient.EXPECT().
121+
Mutate(mock.Anything, mock.AnythingOfType("*repository.contextCreateMutation"), mock.Anything).
122+
Run(func(_ context.Context, _ interface{}, vars map[string]interface{}, _a3 ...graphql.RequestOption) {
123+
actualVars = vars
124+
}).Return(nil)
125+
_, err := repo.Create(context.Background(), &testCase.context)
126+
require.NoError(t, err)
127+
testCase.assertPayload(t, actualVars["input"].(structs.ContextInput))
128+
})
129+
}
130+
131+
}
132+
133+
func Test_contextRepository_Update(t *testing.T) {
134+
testCases := []struct {
135+
name string
136+
context v1beta1.Context
137+
assertPayload func(*testing.T, map[string]any)
138+
}{
139+
{
140+
name: "basic context",
141+
context: v1beta1.Context{
142+
ObjectMeta: v1.ObjectMeta{
143+
Name: "name",
144+
},
145+
Spec: v1beta1.ContextSpec{
146+
SpaceId: utils.AddressOf("test-space-id"),
147+
},
148+
Status: v1beta1.ContextStatus{
149+
Id: "test-context-id",
150+
},
151+
},
152+
assertPayload: func(t *testing.T, input map[string]any) {
153+
assert.Equal(t, "test-context-id", input["id"])
154+
assert.Equal(t, graphql.Boolean(true), input["replaceConfigElements"])
155+
// No need to assert on input details since we use the same code than the Create function
156+
// and this is already covered in Test_contextRepository_Create
157+
assert.IsType(t, structs.ContextInput{}, input["input"])
158+
},
159+
},
160+
}
161+
162+
originalClient := spaceliftclient.DefaultClient
163+
defer func() { spaceliftclient.DefaultClient = originalClient }()
164+
var fakeClient *mocks.Client
165+
spaceliftclient.DefaultClient = func(_ context.Context, _ client.Client, _ string) (spaceliftclient.Client, error) {
166+
return fakeClient, nil
167+
}
168+
repo := NewContextRepository(nil)
169+
170+
for _, testCase := range testCases {
171+
t.Run(testCase.name, func(t *testing.T) {
172+
fakeClient = mocks.NewClient(t)
173+
var actualVars = map[string]any{}
174+
fakeClient.EXPECT().
175+
Mutate(mock.Anything, mock.AnythingOfType("*repository.contextUpdateMutation"), mock.Anything).
176+
Run(func(_ context.Context, _ interface{}, vars map[string]interface{}, _a3 ...graphql.RequestOption) {
177+
actualVars = vars
178+
}).Return(nil)
179+
_, err := repo.Update(context.Background(), &testCase.context)
180+
require.NoError(t, err)
181+
testCase.assertPayload(t, actualVars)
182+
})
183+
}
184+
185+
}

internal/spacelift/repository/run.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,19 @@ func NewRunRepository(client client.Client) *runRepository {
2929
type CreateRunQuery struct {
3030
}
3131

32+
type createRunMutation struct {
33+
RunTrigger struct {
34+
ID string `graphql:"id"`
35+
State string `graphql:"state"`
36+
} `graphql:"runTrigger(stack: $stack)"`
37+
}
38+
3239
func (r *runRepository) Create(ctx context.Context, stack *v1beta1.Stack) (*models.Run, error) {
33-
c, err := spaceliftclient.GetSpaceliftClient(ctx, r.client, stack.Namespace)
40+
c, err := spaceliftclient.DefaultClient(ctx, r.client, stack.Namespace)
3441
if err != nil {
3542
return nil, errors.Wrap(err, "unable to fetch spacelift client while creating run")
3643
}
37-
var mutation struct {
38-
RunTrigger struct {
39-
ID string `graphql:"id"`
40-
State string `graphql:"state"`
41-
} `graphql:"runTrigger(stack: $stack)"`
42-
}
44+
var mutation createRunMutation
4345
vars := map[string]any{
4446
"stack": graphql.ID(stack.Status.Id),
4547
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package repository
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/shurcooL/graphql"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/mock"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
11+
12+
"github.com/spacelift-io/spacelift-operator/api/v1beta1"
13+
spaceliftclient "github.com/spacelift-io/spacelift-operator/internal/spacelift/client"
14+
"github.com/spacelift-io/spacelift-operator/internal/spacelift/client/mocks"
15+
)
16+
17+
func Test_runRepository_Create(t *testing.T) {
18+
originalClient := spaceliftclient.DefaultClient
19+
defer func() { spaceliftclient.DefaultClient = originalClient }()
20+
var fakeClient *mocks.Client
21+
spaceliftclient.DefaultClient = func(_ context.Context, _ client.Client, _ string) (spaceliftclient.Client, error) {
22+
return fakeClient, nil
23+
}
24+
25+
var actualVars map[string]any
26+
fakeClient = mocks.NewClient(t)
27+
fakeClient.EXPECT().
28+
Mutate(mock.Anything, mock.AnythingOfType("*repository.createRunMutation"), mock.Anything).
29+
Run(func(_ context.Context, mutation any, vars map[string]interface{}, _ ...graphql.RequestOption) {
30+
actualVars = vars
31+
runMutation := mutation.(*createRunMutation)
32+
runMutation.RunTrigger.ID = "run-id"
33+
runMutation.RunTrigger.State = "QUEUED"
34+
}).Return(nil)
35+
fakeClient.EXPECT().URL("/stack/%s/run/%s", "stack-id", "run-id").Return("run-url")
36+
37+
fakeStack := &v1beta1.Stack{
38+
Status: v1beta1.StackStatus{
39+
Id: "stack-id",
40+
},
41+
}
42+
repo := NewRunRepository(nil)
43+
run, err := repo.Create(context.Background(), fakeStack)
44+
assert.NoError(t, err)
45+
46+
assert.Equal(t, "stack-id", actualVars["stack"])
47+
assert.Equal(t, "run-id", run.Id)
48+
assert.Equal(t, "QUEUED", run.State)
49+
assert.Equal(t, "run-url", run.Url)
50+
assert.Equal(t, "stack-id", run.StackId)
51+
}

internal/spacelift/repository/space.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,19 @@ func NewSpaceRepository(client client.Client) *spaceRepository {
3030
return &spaceRepository{client: client}
3131
}
3232

33+
type spaceCreateMutation struct {
34+
SpaceCreate struct {
35+
ID string `graphql:"id"`
36+
} `graphql:"spaceCreate(input: $input)"`
37+
}
38+
3339
func (r *spaceRepository) Create(ctx context.Context, space *v1beta1.Space) (*models.Space, error) {
34-
c, err := spaceliftclient.GetSpaceliftClient(ctx, r.client, space.Namespace)
40+
c, err := spaceliftclient.DefaultClient(ctx, r.client, space.Namespace)
3541
if err != nil {
3642
return nil, errors.Wrap(err, "unable to fetch spacelift client while creating run")
3743
}
3844

39-
var mutation struct {
40-
SpaceCreate struct {
41-
ID string `graphql:"id"`
42-
} `graphql:"spaceCreate(input: $input)"`
43-
}
45+
var mutation spaceCreateMutation
4446

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

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

59+
type spaceUpdateMutation struct {
60+
SpaceUpdate struct {
61+
ID string `graphql:"id"`
62+
} `graphql:"spaceUpdate(space: $space, input: $input)"`
63+
}
64+
5765
func (r *spaceRepository) Update(ctx context.Context, space *v1beta1.Space) (*models.Space, error) {
58-
c, err := spaceliftclient.GetSpaceliftClient(ctx, r.client, space.Namespace)
66+
c, err := spaceliftclient.DefaultClient(ctx, r.client, space.Namespace)
5967
if err != nil {
6068
return nil, errors.Wrap(err, "unable to fetch spacelift client while updating space")
6169
}
6270

63-
var spaceUpdateMutation struct {
64-
SpaceUpdate struct {
65-
ID string `graphql:"id"`
66-
} `graphql:"spaceUpdate(space: $space, input: $input)"`
67-
}
71+
var spaceUpdateMutation spaceUpdateMutation
6872

6973
spaceUpdateVars := map[string]any{
7074
"space": graphql.ID(space.Status.Id),

0 commit comments

Comments
 (0)