github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/test/integration/backup_mysql_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     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  
    17  package appstest
    18  
    19  import (
    20  	. "github.com/onsi/ginkgo/v2"
    21  	. "github.com/onsi/gomega"
    22  
    23  	corev1 "k8s.io/api/core/v1"
    24  	"k8s.io/apimachinery/pkg/types"
    25  	"sigs.k8s.io/controller-runtime/pkg/client"
    26  
    27  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    28  	dpv1alpha1 "github.com/1aal/kubeblocks/apis/dataprotection/v1alpha1"
    29  	"github.com/1aal/kubeblocks/pkg/constant"
    30  	"github.com/1aal/kubeblocks/pkg/controller/component"
    31  	intctrlutil "github.com/1aal/kubeblocks/pkg/generics"
    32  	testapps "github.com/1aal/kubeblocks/pkg/testutil/apps"
    33  	testdp "github.com/1aal/kubeblocks/pkg/testutil/dataprotection"
    34  )
    35  
    36  var _ = Describe("MySQL data protection function", func() {
    37  	const clusterDefName = "test-clusterdef"
    38  	const clusterVersionName = "test-clusterversion"
    39  	const clusterNamePrefix = "test-cluster"
    40  	const scriptConfigName = "test-cluster-mysql-scripts"
    41  	const mysqlCompDefName = "replicasets"
    42  	const mysqlCompName = "mysql"
    43  	const backupPolicyTemplateName = "test-backup-policy-template"
    44  	const backupPolicyName = "test-backup-policy"
    45  	const backupRemotePVCName = "backup-remote-pvc"
    46  	const backupName = "test-backup-job"
    47  
    48  	// Cleanups
    49  
    50  	cleanAll := func() {
    51  		// must wait until resources deleted and no longer exist before the testcases start,
    52  		// otherwise if later it needs to create some new resource objects with the same name,
    53  		// in race conditions, it will find the existence of old objects, resulting failure to
    54  		// create the new objects.
    55  		By("clean resources")
    56  
    57  		// delete cluster(and all dependent sub-resources), clusterversion and clusterdef
    58  		testapps.ClearClusterResources(&testCtx)
    59  
    60  		inNS := client.InNamespace(testCtx.DefaultNamespace)
    61  		ml := client.HasLabels{testCtx.TestObjLabelKey}
    62  		// namespaced
    63  		testapps.ClearResources(&testCtx, intctrlutil.OpsRequestSignature, inNS, ml)
    64  		testapps.ClearResources(&testCtx, intctrlutil.ConfigMapSignature, inNS, ml)
    65  		testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, intctrlutil.BackupSignature, true, inNS)
    66  		testapps.ClearResources(&testCtx, intctrlutil.BackupPolicySignature, inNS, ml)
    67  		testapps.ClearResources(&testCtx, intctrlutil.ActionSetSignature, inNS, ml)
    68  		testapps.ClearResources(&testCtx, intctrlutil.RestoreSignature, inNS, ml)
    69  
    70  	}
    71  
    72  	BeforeEach(cleanAll)
    73  
    74  	AfterEach(cleanAll)
    75  
    76  	// Testcases
    77  
    78  	var (
    79  		clusterDefObj     *appsv1alpha1.ClusterDefinition
    80  		clusterVersionObj *appsv1alpha1.ClusterVersion
    81  		clusterObj        *appsv1alpha1.Cluster
    82  		clusterKey        types.NamespacedName
    83  		backupKey         types.NamespacedName
    84  	)
    85  
    86  	createClusterObj := func() {
    87  		By("Create configmap")
    88  		_ = testapps.CreateCustomizedObj(&testCtx, "resources/mysql-scripts.yaml", &corev1.ConfigMap{},
    89  			testapps.WithName(scriptConfigName), testCtx.UseDefaultNamespace())
    90  
    91  		By("Create a clusterDef obj")
    92  		mode := int32(0755)
    93  		clusterDefObj = testapps.NewClusterDefFactory(clusterDefName).
    94  			AddComponentDef(testapps.ConsensusMySQLComponent, mysqlCompDefName).
    95  			AddScriptTemplate(scriptConfigName, scriptConfigName, testCtx.DefaultNamespace, testapps.ScriptsVolumeName, &mode).
    96  			Create(&testCtx).GetObject()
    97  
    98  		By("Create a clusterVersion obj")
    99  		clusterVersionObj = testapps.NewClusterVersionFactory(clusterVersionName, clusterDefObj.GetName()).
   100  			AddComponentVersion(mysqlCompDefName).AddContainerShort(testapps.DefaultMySQLContainerName, testapps.ApeCloudMySQLImage).
   101  			Create(&testCtx).GetObject()
   102  
   103  		By("Create a cluster obj")
   104  		pvcSpec := testapps.NewPVCSpec("1Gi")
   105  		clusterObj = testapps.NewClusterFactory(testCtx.DefaultNamespace, clusterNamePrefix,
   106  			clusterDefObj.Name, clusterVersionObj.Name).WithRandomName().
   107  			AddComponent(mysqlCompName, mysqlCompDefName).
   108  			SetReplicas(1).
   109  			AddVolumeClaimTemplate(testapps.DataVolumeName, pvcSpec).
   110  			Create(&testCtx).GetObject()
   111  		clusterKey = client.ObjectKeyFromObject(clusterObj)
   112  		Eventually(testapps.GetClusterObservedGeneration(&testCtx, clusterKey)).Should(BeEquivalentTo(1))
   113  
   114  		By("check cluster running")
   115  		Eventually(testapps.CheckObj(&testCtx, clusterKey, func(g Gomega, cluster *appsv1alpha1.Cluster) {
   116  			g.Expect(cluster.Status.Phase).To(Equal(appsv1alpha1.RunningClusterPhase))
   117  		})).Should(Succeed())
   118  	}
   119  
   120  	createBackupObj := func() {
   121  		By("By creating a actionSet")
   122  		actionSet := testapps.CreateCustomizedObj(&testCtx, "backup/actionset.yaml",
   123  			&dpv1alpha1.ActionSet{}, testapps.RandomizedObjName())
   124  
   125  		By("By creating a backupPolicy from backupPolicyTemplate: " + backupPolicyTemplateName)
   126  		backupPolicyObj := testdp.NewBackupPolicyFactory(testCtx.DefaultNamespace, backupPolicyName).
   127  			WithRandomName().
   128  			SetTarget(constant.AppInstanceLabelKey, clusterKey.Name).
   129  			SetTargetConnectionCredential(component.GenerateConnCredential(clusterKey.Name)).
   130  			AddBackupMethod(testdp.BackupMethodName, false, actionSet.Name).
   131  			SetBackupMethodVolumeMounts(testapps.DataVolumeName, "/data").
   132  			Create(&testCtx).GetObject()
   133  		backupPolicyKey := client.ObjectKeyFromObject(backupPolicyObj)
   134  
   135  		By("By create remove pvc")
   136  		testapps.NewPersistentVolumeClaimFactory(testCtx.DefaultNamespace, backupRemotePVCName, clusterKey.Name,
   137  			"none", "remote-volume").
   138  			SetAnnotations(map[string]string{}).
   139  			SetStorage("1Gi").
   140  			Create(&testCtx)
   141  
   142  		By("By check backupPolicy available")
   143  		Eventually(testapps.CheckObj(&testCtx, backupPolicyKey, func(g Gomega, backupPolicy *dpv1alpha1.BackupPolicy) {
   144  			g.Expect(backupPolicy.Status.Phase).To(Equal(dpv1alpha1.BackupPolicyAvailable))
   145  		})).Should(Succeed())
   146  
   147  		By("By creating a backup from backupPolicy: " + backupPolicyKey.Name)
   148  		backup := testdp.NewBackupFactory(testCtx.DefaultNamespace, backupName).
   149  			WithRandomName().
   150  			SetBackupPolicyName(backupPolicyKey.Name).
   151  			SetBackupMethod(testdp.BackupMethodName).
   152  			Create(&testCtx).GetObject()
   153  		backupKey = client.ObjectKeyFromObject(backup)
   154  	}
   155  
   156  	Context("with MySQL full backup", func() {
   157  		BeforeEach(func() {
   158  			createClusterObj()
   159  			createBackupObj()
   160  		})
   161  
   162  		It("should be completed", func() {
   163  			Eventually(testapps.CheckObj(&testCtx, backupKey, func(g Gomega, backup *dpv1alpha1.Backup) {
   164  				g.Expect(backup.Status.Phase).To(Equal(dpv1alpha1.BackupPhaseCompleted))
   165  			})).Should(Succeed())
   166  		})
   167  	})
   168  })