|
| 1 | +package e2e_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + |
| 10 | + "k8s.io/apimachinery/pkg/types" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | + |
| 13 | + "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/common" |
| 14 | + "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/status" |
| 15 | + "github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/toptr" |
| 16 | + "github.com/mongodb/mongodb-atlas-kubernetes/test/e2e/actions" |
| 17 | + "github.com/mongodb/mongodb-atlas-kubernetes/test/e2e/actions/deploy" |
| 18 | + "github.com/mongodb/mongodb-atlas-kubernetes/test/e2e/api/atlas" |
| 19 | + "github.com/mongodb/mongodb-atlas-kubernetes/test/e2e/data" |
| 20 | + "github.com/mongodb/mongodb-atlas-kubernetes/test/e2e/model" |
| 21 | + "github.com/mongodb/mongodb-atlas-kubernetes/test/helper" |
| 22 | + |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + |
| 25 | + mdbv1 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + atlasIAMRoleName = "atlas-role" |
| 30 | + atlasBucketPolicyName = "atlas-bucket-export-policy" |
| 31 | + bucketName = "cloud-backup-snapshot" |
| 32 | +) |
| 33 | + |
| 34 | +var _ = FDescribe("Deployment Backup Configuration", Label("backup-config"), func() { |
| 35 | + var testData *model.TestDataProvider |
| 36 | + |
| 37 | + AfterEach(func() { |
| 38 | + GinkgoWriter.Write([]byte("\n")) |
| 39 | + GinkgoWriter.Write([]byte("===============================================\n")) |
| 40 | + GinkgoWriter.Write([]byte("Operator namespace: " + testData.Resources.Namespace + "\n")) |
| 41 | + GinkgoWriter.Write([]byte("===============================================\n")) |
| 42 | + if CurrentSpecReport().Failed() { |
| 43 | + Expect(actions.SaveProjectsToFile(testData.Context, testData.K8SClient, testData.Resources.Namespace)).Should(Succeed()) |
| 44 | + Expect(actions.SaveDeploymentsToFile(testData.Context, testData.K8SClient, testData.Resources.Namespace)).Should(Succeed()) |
| 45 | + Expect(actions.SaveUsersToFile(testData.Context, testData.K8SClient, testData.Resources.Namespace)).Should(Succeed()) |
| 46 | + } |
| 47 | + |
| 48 | + By("Should clean up created resources", func() { |
| 49 | + actions.DeleteTestDataDeployments(testData) |
| 50 | + actions.DeleteTestDataProject(testData) |
| 51 | + |
| 52 | + actions.AfterEachFinalCleanup([]model.TestDataProvider{*testData}) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + DescribeTable("Configure backup for a deployment", |
| 57 | + func(test *model.TestDataProvider) { |
| 58 | + testData = test |
| 59 | + |
| 60 | + bucket := fmt.Sprintf("%s-%s", bucketName, testData.Resources.TestID) |
| 61 | + bucketPolicy := fmt.Sprintf("%s-%s", atlasBucketPolicyName, testData.Resources.TestID) |
| 62 | + role := fmt.Sprintf("%s-%s", atlasIAMRoleName, testData.Resources.TestID) |
| 63 | + |
| 64 | + actions.CreateProjectWithCloudProviderAccess(testData, role) |
| 65 | + setupAWSResource(testData.AWSResourcesGenerator, bucket, bucketPolicy, role) |
| 66 | + deploy.CreateInitialDeployments(testData) |
| 67 | + |
| 68 | + backupConfigFlow(test, bucket) |
| 69 | + }, |
| 70 | + Entry( |
| 71 | + "Enable backup for a deployment", |
| 72 | + model.DataProvider( |
| 73 | + "deployment-backup-enabled", |
| 74 | + model.NewEmptyAtlasKeyType().UseDefaultFullAccess(), |
| 75 | + 30001, |
| 76 | + []func(*model.TestDataProvider){}, |
| 77 | + ). |
| 78 | + WithProject(data.DefaultProject()). |
| 79 | + WithInitialDeployments(data.CreateAdvancedDeployment("backup-deployment")), |
| 80 | + ), |
| 81 | + ) |
| 82 | +}) |
| 83 | + |
| 84 | +func backupConfigFlow(data *model.TestDataProvider, bucket string) { |
| 85 | + By("Enable backup for deployment", func() { |
| 86 | + Expect(data.K8SClient.Get(data.Context, client.ObjectKeyFromObject(data.InitialDeployments[0]), data.InitialDeployments[0])).To(Succeed()) |
| 87 | + data.InitialDeployments[0].Spec.AdvancedDeploymentSpec.BackupEnabled = toptr.MakePtr(true) |
| 88 | + Expect(data.K8SClient.Update(data.Context, data.InitialDeployments[0])).To(Succeed()) |
| 89 | + |
| 90 | + Eventually(func(g Gomega) bool { |
| 91 | + objectKey := types.NamespacedName{ |
| 92 | + Name: data.InitialDeployments[0].Name, |
| 93 | + Namespace: data.InitialDeployments[0].Namespace, |
| 94 | + } |
| 95 | + g.Expect(data.K8SClient.Get(data.Context, objectKey, data.InitialDeployments[0])).To(Succeed()) |
| 96 | + return data.InitialDeployments[0].Status.StateName == status.StateIDLE |
| 97 | + }).WithTimeout(30 * time.Minute).Should(BeTrue()) |
| 98 | + }) |
| 99 | + |
| 100 | + By("Configure backup schedule and policy for the deployment", func() { |
| 101 | + bkpPolicy := &mdbv1.AtlasBackupPolicy{ |
| 102 | + ObjectMeta: metav1.ObjectMeta{ |
| 103 | + Namespace: data.Project.Namespace, |
| 104 | + Name: fmt.Sprintf("%s-bkp-policy", data.Project.Name), |
| 105 | + }, |
| 106 | + Spec: mdbv1.AtlasBackupPolicySpec{ |
| 107 | + Items: []mdbv1.AtlasBackupPolicyItem{ |
| 108 | + { |
| 109 | + FrequencyInterval: 6, |
| 110 | + FrequencyType: "hourly", |
| 111 | + RetentionValue: 2, |
| 112 | + RetentionUnit: "days", |
| 113 | + }, |
| 114 | + { |
| 115 | + FrequencyInterval: 1, |
| 116 | + FrequencyType: "daily", |
| 117 | + RetentionValue: 7, |
| 118 | + RetentionUnit: "days", |
| 119 | + }, |
| 120 | + { |
| 121 | + FrequencyInterval: 1, |
| 122 | + FrequencyType: "weekly", |
| 123 | + RetentionValue: 4, |
| 124 | + RetentionUnit: "weeks", |
| 125 | + }, |
| 126 | + { |
| 127 | + FrequencyInterval: 1, |
| 128 | + FrequencyType: "monthly", |
| 129 | + RetentionValue: 12, |
| 130 | + RetentionUnit: "months", |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + } |
| 135 | + Expect(data.K8SClient.Create(data.Context, bkpPolicy)).To(Succeed()) |
| 136 | + |
| 137 | + bkpSchedule := &mdbv1.AtlasBackupSchedule{ |
| 138 | + ObjectMeta: metav1.ObjectMeta{ |
| 139 | + Namespace: data.Project.Namespace, |
| 140 | + Name: fmt.Sprintf("%s-bkp-schedule", data.Project.Name), |
| 141 | + }, |
| 142 | + Spec: mdbv1.AtlasBackupScheduleSpec{ |
| 143 | + PolicyRef: common.ResourceRefNamespaced{ |
| 144 | + Namespace: data.Project.Namespace, |
| 145 | + Name: fmt.Sprintf("%s-bkp-policy", data.Project.Name), |
| 146 | + }, |
| 147 | + ReferenceHourOfDay: 19, |
| 148 | + ReferenceMinuteOfHour: 2, |
| 149 | + RestoreWindowDays: 1, |
| 150 | + UseOrgAndGroupNamesInExportPrefix: true, |
| 151 | + }, |
| 152 | + } |
| 153 | + Expect(data.K8SClient.Create(data.Context, bkpSchedule)).To(Succeed()) |
| 154 | + |
| 155 | + Expect(data.K8SClient.Get(data.Context, client.ObjectKeyFromObject(data.InitialDeployments[0]), data.InitialDeployments[0])).To(Succeed()) |
| 156 | + data.InitialDeployments[0].Spec.BackupScheduleRef = common.ResourceRefNamespaced{ |
| 157 | + Namespace: data.Project.Namespace, |
| 158 | + Name: fmt.Sprintf("%s-bkp-schedule", data.Project.Name), |
| 159 | + } |
| 160 | + Expect(data.K8SClient.Update(data.Context, data.InitialDeployments[0])).To(Succeed()) |
| 161 | + |
| 162 | + Eventually(func(g Gomega) bool { |
| 163 | + g.Expect(data.K8SClient.Get(data.Context, types.NamespacedName{ |
| 164 | + Name: data.InitialDeployments[0].Name, |
| 165 | + Namespace: data.InitialDeployments[0].Namespace, |
| 166 | + }, data.InitialDeployments[0])).To(Succeed()) |
| 167 | + |
| 168 | + return data.InitialDeployments[0].Status.StateName == status.StateIDLE |
| 169 | + }).WithTimeout(30 * time.Minute).Should(BeTrue()) |
| 170 | + }) |
| 171 | + |
| 172 | + By("Configure auto export to AWS bucket", func() { |
| 173 | + aClient := atlas.GetClientOrFail() |
| 174 | + exportBucket, err := aClient.CreateExportBucket( |
| 175 | + data.Project.ID(), |
| 176 | + bucket, |
| 177 | + data.Project.Status.CloudProviderAccessRoles[0].RoleID, |
| 178 | + ) |
| 179 | + Expect(err).Should(BeNil()) |
| 180 | + Expect(exportBucket).ShouldNot(BeNil()) |
| 181 | + |
| 182 | + backupSchedule := &mdbv1.AtlasBackupSchedule{ |
| 183 | + ObjectMeta: metav1.ObjectMeta{ |
| 184 | + Namespace: data.Project.Namespace, |
| 185 | + Name: fmt.Sprintf("%s-bkp-schedule", data.Project.Name), |
| 186 | + }, |
| 187 | + } |
| 188 | + Expect(data.K8SClient.Get(data.Context, client.ObjectKeyFromObject(backupSchedule), backupSchedule)).To(Succeed()) |
| 189 | + |
| 190 | + backupSchedule.Spec.AutoExportEnabled = true |
| 191 | + backupSchedule.Spec.Export = &mdbv1.AtlasBackupExportSpec{ |
| 192 | + ExportBucketID: exportBucket.ID, |
| 193 | + FrequencyType: "monthly", |
| 194 | + } |
| 195 | + Expect(data.K8SClient.Update(data.Context, backupSchedule)).To(Succeed()) |
| 196 | + |
| 197 | + Eventually(func(g Gomega) bool { |
| 198 | + g.Expect(data.K8SClient.Get(data.Context, types.NamespacedName{ |
| 199 | + Name: data.InitialDeployments[0].Name, |
| 200 | + Namespace: data.InitialDeployments[0].Namespace, |
| 201 | + }, data.InitialDeployments[0])).To(Succeed()) |
| 202 | + |
| 203 | + return data.InitialDeployments[0].Status.StateName == status.StateIDLE |
| 204 | + }).WithTimeout(30 * time.Minute).Should(BeTrue()) |
| 205 | + }) |
| 206 | +} |
| 207 | + |
| 208 | +func setupAWSResource(gen *helper.AwsResourcesGenerator, bucket, bucketPolicy, role string) { |
| 209 | + Expect(gen.CreateBucket(bucket)).To(Succeed()) |
| 210 | + gen.Cleanup(func() { |
| 211 | + Expect(gen.EmptyBucket(bucket)).To(Succeed()) |
| 212 | + Expect(gen.DeleteBucket(bucket)).To(Succeed()) |
| 213 | + }) |
| 214 | + |
| 215 | + policyArn, err := gen.CreatePolicy(bucketPolicy, func() helper.IAMPolicy { |
| 216 | + return helper.BucketExportPolicy(bucket) |
| 217 | + }) |
| 218 | + Expect(err).Should(BeNil()) |
| 219 | + Expect(policyArn).ShouldNot(BeEmpty()) |
| 220 | + gen.Cleanup(func() { |
| 221 | + Expect(gen.DeletePolicy(policyArn)).To(Succeed()) |
| 222 | + }) |
| 223 | + |
| 224 | + Expect(gen.AttachRolePolicy(role, policyArn)).To(Succeed()) |
| 225 | + gen.Cleanup(func() { |
| 226 | + Expect(gen.DetachRolePolicy(role, policyArn)).To(Succeed()) |
| 227 | + }) |
| 228 | +} |
0 commit comments