|
| 1 | +/* |
| 2 | +Copyright 2023. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
1 | 17 | package path_controller_test |
2 | 18 |
|
3 | 19 | import ( |
@@ -55,3 +71,86 @@ func TestHandleCreate(t *testing.T) { |
55 | 71 | assert.NoError(t, err) |
56 | 72 | }) |
57 | 73 | } |
| 74 | + |
| 75 | +func TestHandleUpdate(t *testing.T) { |
| 76 | + // Set up a logger before running tests |
| 77 | + log.SetLogger(zap.New(zap.UseDevMode(true))) |
| 78 | + |
| 79 | + // Create a fake client with a sample CR |
| 80 | + pathResource := &s3v1alpha1.Path{ |
| 81 | + ObjectMeta: metav1.ObjectMeta{ |
| 82 | + Name: "existing-path", |
| 83 | + Namespace: "default", |
| 84 | + Generation: 1, |
| 85 | + Finalizers: []string{"s3.onyxia.sh/finalizer"}, |
| 86 | + }, |
| 87 | + Spec: s3v1alpha1.PathSpec{ |
| 88 | + BucketName: "existing-bucket", |
| 89 | + S3InstanceRef: "s3-operator/default", |
| 90 | + Paths: []string{"example"}, |
| 91 | + }, |
| 92 | + } |
| 93 | + |
| 94 | + // Create a fake client with a sample CR |
| 95 | + pathInvalidResource := &s3v1alpha1.Path{ |
| 96 | + ObjectMeta: metav1.ObjectMeta{ |
| 97 | + Name: "existing-invalid-paths", |
| 98 | + Namespace: "default", |
| 99 | + Generation: 1, |
| 100 | + Finalizers: []string{"s3.onyxia.sh/finalizer"}, |
| 101 | + }, |
| 102 | + Spec: s3v1alpha1.PathSpec{ |
| 103 | + BucketName: "existing-invalid-bucket", |
| 104 | + S3InstanceRef: "s3-operator/default", |
| 105 | + Paths: []string{"example", "non-existing"}, |
| 106 | + }, |
| 107 | + } |
| 108 | + |
| 109 | + pathInvalidResource2 := &s3v1alpha1.Path{ |
| 110 | + ObjectMeta: metav1.ObjectMeta{ |
| 111 | + Name: "existing-invalid-paths2", |
| 112 | + Namespace: "default", |
| 113 | + Generation: 1, |
| 114 | + Finalizers: []string{"s3.onyxia.sh/finalizer"}, |
| 115 | + }, |
| 116 | + Spec: s3v1alpha1.PathSpec{ |
| 117 | + BucketName: "non-existing-bucket", |
| 118 | + S3InstanceRef: "s3-operator/default", |
| 119 | + Paths: []string{"example", "non-existing"}, |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + // Add mock for s3Factory and client |
| 124 | + testUtils := TestUtils.NewTestUtils() |
| 125 | + testUtils.SetupMockedS3FactoryAndClient() |
| 126 | + s3instanceResource, secretResource := testUtils.GenerateBasicS3InstanceAndSecret() |
| 127 | + testUtils.SetupClient([]client.Object{s3instanceResource, secretResource, pathResource, pathInvalidResource, pathInvalidResource2}) |
| 128 | + |
| 129 | + // Create the reconciler |
| 130 | + reconciler := &path_controller.PathReconciler{ |
| 131 | + Client: testUtils.Client, |
| 132 | + Scheme: testUtils.Client.Scheme(), |
| 133 | + S3factory: testUtils.S3Factory, |
| 134 | + } |
| 135 | + |
| 136 | + t.Run("no error", func(t *testing.T) { |
| 137 | + // Call Reconcile function |
| 138 | + req := ctrl.Request{NamespacedName: types.NamespacedName{Name: pathResource.Name, Namespace: pathResource.Namespace}} |
| 139 | + _, err := reconciler.Reconcile(context.TODO(), req) |
| 140 | + assert.NoError(t, err) |
| 141 | + }) |
| 142 | + |
| 143 | + t.Run("no error on invalid resource", func(t *testing.T) { |
| 144 | + // Call Reconcile function |
| 145 | + req := ctrl.Request{NamespacedName: types.NamespacedName{Name: pathInvalidResource.Name, Namespace: pathInvalidResource.Namespace}} |
| 146 | + _, err := reconciler.Reconcile(context.TODO(), req) |
| 147 | + assert.NoError(t, err) |
| 148 | + }) |
| 149 | + |
| 150 | + t.Run("failed create path on non existing bucket", func(t *testing.T) { |
| 151 | + // Call Reconcile function |
| 152 | + req := ctrl.Request{NamespacedName: types.NamespacedName{Name: pathInvalidResource2.Name, Namespace: pathInvalidResource2.Namespace}} |
| 153 | + _, err := reconciler.Reconcile(context.TODO(), req) |
| 154 | + assert.NoError(t, err) |
| 155 | + }) |
| 156 | +} |
0 commit comments