|
1 | 1 | package services
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "fmt"
|
| 6 | + v1 "k8s.io/api/core/v1" |
| 7 | + "k8s.io/apimachinery/pkg/api/errors" |
| 8 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 9 | + "k8s.io/apimachinery/pkg/types" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/client" |
5 | 11 | "testing"
|
6 | 12 |
|
7 | 13 | "github.com/linode/linodego"
|
@@ -204,3 +210,104 @@ func TestEnsureObjectStorageBucket(t *testing.T) {
|
204 | 210 | })
|
205 | 211 | }
|
206 | 212 | }
|
| 213 | + |
| 214 | +func TestCreateS3ClientWithAccessKey(t *testing.T) { |
| 215 | + t.Parallel() |
| 216 | + |
| 217 | + tests := []struct { |
| 218 | + name string |
| 219 | + bScope *scope.ObjectStorageBucketScope |
| 220 | + expectedError error |
| 221 | + expects func(client *mock.MockK8sClient) |
| 222 | + }{ |
| 223 | + { |
| 224 | + name: "Success - Successfully create client", |
| 225 | + bScope: &scope.ObjectStorageBucketScope{ |
| 226 | + Bucket: &infrav1alpha2.LinodeObjectStorageBucket{ |
| 227 | + ObjectMeta: metav1.ObjectMeta{ |
| 228 | + Name: "test-bucket", |
| 229 | + }, |
| 230 | + Spec: infrav1alpha2.LinodeObjectStorageBucketSpec{ |
| 231 | + Region: "test-region", |
| 232 | + AccessKeyRef: &v1.ObjectReference{ |
| 233 | + Name: "test", |
| 234 | + }, |
| 235 | + }, |
| 236 | + }, |
| 237 | + }, |
| 238 | + expects: func(k8s *mock.MockK8sClient) { |
| 239 | + k8s.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, name types.NamespacedName, obj *v1.Secret, opts ...client.GetOption) error { |
| 240 | + secret := v1.Secret{ |
| 241 | + ObjectMeta: metav1.ObjectMeta{ |
| 242 | + Name: "test-bucket-obj-key", |
| 243 | + }, |
| 244 | + Data: map[string][]byte{ |
| 245 | + "access": []byte("test-access-key"), |
| 246 | + "secret": []byte("test-secret-key"), |
| 247 | + "bucket": []byte("test-bucket"), |
| 248 | + }, |
| 249 | + } |
| 250 | + *obj = secret |
| 251 | + return nil |
| 252 | + }) |
| 253 | + }, |
| 254 | + }, |
| 255 | + { |
| 256 | + name: "Error - failed to get access key", |
| 257 | + bScope: &scope.ObjectStorageBucketScope{ |
| 258 | + Bucket: &infrav1alpha2.LinodeObjectStorageBucket{ |
| 259 | + ObjectMeta: metav1.ObjectMeta{ |
| 260 | + Name: "test-bucket", |
| 261 | + }, |
| 262 | + Spec: infrav1alpha2.LinodeObjectStorageBucketSpec{ |
| 263 | + Region: "test-region", |
| 264 | + AccessKeyRef: &v1.ObjectReference{ |
| 265 | + Name: "test", |
| 266 | + }, |
| 267 | + }, |
| 268 | + }, |
| 269 | + }, |
| 270 | + expects: func(k8s *mock.MockK8sClient) { |
| 271 | + k8s.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.NewNotFound(schema.GroupResource{}, "")) |
| 272 | + }, |
| 273 | + expectedError: fmt.Errorf("failed to get bucket secret"), |
| 274 | + }, |
| 275 | + { |
| 276 | + name: "Error - access key is nil", |
| 277 | + bScope: &scope.ObjectStorageBucketScope{ |
| 278 | + Bucket: &infrav1alpha2.LinodeObjectStorageBucket{ |
| 279 | + ObjectMeta: metav1.ObjectMeta{ |
| 280 | + Name: "test-bucket", |
| 281 | + }, |
| 282 | + Spec: infrav1alpha2.LinodeObjectStorageBucketSpec{ |
| 283 | + Region: "test-region", |
| 284 | + }, |
| 285 | + }, |
| 286 | + }, |
| 287 | + expects: func(k8s *mock.MockK8sClient) {}, |
| 288 | + expectedError: fmt.Errorf("accessKeyRef is nil"), |
| 289 | + }, |
| 290 | + } |
| 291 | + for _, tt := range tests { |
| 292 | + testcase := tt |
| 293 | + t.Run(testcase.name, func(t *testing.T) { |
| 294 | + t.Parallel() |
| 295 | + |
| 296 | + ctrl := gomock.NewController(t) |
| 297 | + defer ctrl.Finish() |
| 298 | + |
| 299 | + mockClient := mock.NewMockK8sClient(ctrl) |
| 300 | + |
| 301 | + testcase.bScope.Client = mockClient |
| 302 | + |
| 303 | + testcase.expects(mockClient) |
| 304 | + |
| 305 | + s3Client, err := createS3ClientWithAccessKey(t.Context(), testcase.bScope) |
| 306 | + if testcase.expectedError != nil { |
| 307 | + assert.ErrorContains(t, err, testcase.expectedError.Error()) |
| 308 | + } else { |
| 309 | + assert.NotNil(t, s3Client) |
| 310 | + } |
| 311 | + }) |
| 312 | + } |
| 313 | +} |
0 commit comments