github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/testutil/k8s/deployment_util.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package testutil
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"github.com/onsi/gomega"
    26  	appsv1 "k8s.io/api/apps/v1"
    27  	corev1 "k8s.io/api/core/v1"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/apimachinery/pkg/types"
    30  	"sigs.k8s.io/controller-runtime/pkg/client"
    31  
    32  	"github.com/1aal/kubeblocks/pkg/constant"
    33  	"github.com/1aal/kubeblocks/pkg/testutil"
    34  )
    35  
    36  // MockDeploymentReady mocks deployment is ready
    37  func MockDeploymentReady(deploy *appsv1.Deployment, rsAvailableReason, rsName string) {
    38  	deploy.Status.AvailableReplicas = *deploy.Spec.Replicas
    39  	deploy.Status.ReadyReplicas = *deploy.Spec.Replicas
    40  	deploy.Status.Replicas = *deploy.Spec.Replicas
    41  	deploy.Status.ObservedGeneration = deploy.Generation
    42  	deploy.Status.UpdatedReplicas = *deploy.Spec.Replicas
    43  	deploy.Status.Conditions = []appsv1.DeploymentCondition{
    44  		{
    45  			Type:    appsv1.DeploymentProgressing,
    46  			Reason:  rsAvailableReason,
    47  			Status:  corev1.ConditionTrue,
    48  			Message: fmt.Sprintf(`ReplicaSet "%s" has successfully progressed.`, rsName),
    49  		},
    50  	}
    51  }
    52  
    53  // MockPodAvailable mocks pod is available
    54  func MockPodAvailable(pod *corev1.Pod, lastTransitionTime metav1.Time) {
    55  	pod.Status.Conditions = []corev1.PodCondition{
    56  		{
    57  			Type:               corev1.PodReady,
    58  			Status:             corev1.ConditionTrue,
    59  			LastTransitionTime: lastTransitionTime,
    60  		},
    61  	}
    62  }
    63  
    64  func ListAndCheckDeployment(testCtx *testutil.TestContext, key types.NamespacedName) *appsv1.DeploymentList {
    65  	deployList := &appsv1.DeploymentList{}
    66  	gomega.Eventually(func(g gomega.Gomega) {
    67  		g.Expect(testCtx.Cli.List(testCtx.Ctx, deployList, client.MatchingLabels{
    68  			constant.AppInstanceLabelKey: key.Name,
    69  		}, client.InNamespace(key.Namespace))).Should(gomega.Succeed())
    70  		g.Expect(deployList.Items).ShouldNot(gomega.BeNil())
    71  		g.Expect(deployList.Items).ShouldNot(gomega.BeEmpty())
    72  	}).Should(gomega.Succeed())
    73  	return deployList
    74  }