sigs.k8s.io/cluster-api-provider-aws@v1.5.5/test/e2e/shared/workload.go (about)

     1  //go:build e2e
     2  // +build e2e
     3  
     4  /*
     5  Copyright 2022 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package shared
    21  
    22  import (
    23  	"context"
    24  	"fmt"
    25  	"strings"
    26  
    27  	. "github.com/onsi/ginkgo"
    28  	. "github.com/onsi/gomega"
    29  	appsv1 "k8s.io/api/apps/v1"
    30  	corev1 "k8s.io/api/core/v1"
    31  	"sigs.k8s.io/controller-runtime/pkg/client"
    32  
    33  	"sigs.k8s.io/cluster-api/test/framework"
    34  )
    35  
    36  // WaitForDeploymentsAvailableInput is the input for WaitForDeploymentsAvailable.
    37  type WaitForDeploymentsAvailableInput struct {
    38  	Getter    framework.Getter
    39  	Name      string
    40  	Namespace string
    41  }
    42  
    43  // WaitForDeploymentsAvailable will wait for the specified intervel for a Deployment to have status.Available = True.
    44  // NOTE: this is based on a version from the Cluster API test framework.
    45  func WaitForDeploymentsAvailable(ctx context.Context, input WaitForDeploymentsAvailableInput, intervals ...interface{}) {
    46  	By(fmt.Sprintf("Waiting for deployment %s/%s to be available", input.Namespace, input.Name))
    47  	deployment := &appsv1.Deployment{}
    48  	Eventually(func() bool {
    49  		key := client.ObjectKey{
    50  			Namespace: input.Namespace,
    51  			Name:      input.Name,
    52  		}
    53  		if err := input.Getter.Get(ctx, key, deployment); err != nil {
    54  			return false
    55  		}
    56  		for _, c := range deployment.Status.Conditions {
    57  			if c.Type == appsv1.DeploymentAvailable && c.Status == corev1.ConditionTrue {
    58  				return true
    59  			}
    60  		}
    61  		return false
    62  	}, intervals...).Should(BeTrue(), func() string { return DescribeFailedDeployment(input, deployment) })
    63  }
    64  
    65  // DescribeFailedDeployment returns detailed output to help debug a deployment failure in e2e.
    66  func DescribeFailedDeployment(input WaitForDeploymentsAvailableInput, deployment *appsv1.Deployment) string {
    67  	b := strings.Builder{}
    68  	b.WriteString(fmt.Sprintf("Deployment %s/%s failed to get status.Available = True condition",
    69  		input.Namespace, input.Name))
    70  	if deployment == nil {
    71  		b.WriteString("\nDeployment: nil\n")
    72  	} else {
    73  		b.WriteString(fmt.Sprintf("\nDeployment:\n%s\n", framework.PrettyPrint(deployment)))
    74  	}
    75  	return b.String()
    76  }