|
| 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 | +} |
0 commit comments