sigs.k8s.io/cluster-api-provider-aws@v1.5.5/exp/instancestate/helpers_test.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     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 instancestate
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	. "github.com/onsi/ginkgo"
    24  	. "github.com/onsi/gomega"
    25  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    26  	"k8s.io/apimachinery/pkg/api/meta"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/types"
    29  	"sigs.k8s.io/controller-runtime/pkg/client"
    30  
    31  	infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1"
    32  )
    33  
    34  func createAWSCluster(name string) *infrav1.AWSCluster {
    35  	return &infrav1.AWSCluster{
    36  		ObjectMeta: metav1.ObjectMeta{
    37  			Name:      name,
    38  			Namespace: "default",
    39  		},
    40  	}
    41  }
    42  
    43  func persistObject(g *WithT, o client.Object) {
    44  	ctx := context.TODO()
    45  	g.Expect(k8sClient.Create(ctx, o)).Should(Succeed())
    46  	metaObj, err := meta.Accessor(o)
    47  	g.Expect(err).NotTo(HaveOccurred())
    48  	lookupKey := types.NamespacedName{Name: metaObj.GetName(), Namespace: metaObj.GetNamespace()}
    49  
    50  	g.Eventually(func() bool {
    51  		err := k8sClient.Get(ctx, lookupKey, o)
    52  		return err == nil
    53  	}, time.Second*10).Should(BeTrue())
    54  }
    55  
    56  func deleteAWSCluster(g *WithT, name string) {
    57  	ctx := context.TODO()
    58  	awsLookupKey := types.NamespacedName{Name: name, Namespace: "default"}
    59  	awsCluster := &infrav1.AWSCluster{}
    60  	err := k8sClient.Get(ctx, awsLookupKey, awsCluster)
    61  	if err != nil {
    62  		if apierrors.IsNotFound(err) {
    63  			// already deleted
    64  			return
    65  		}
    66  		Fail("Unexpected error when fetching cluster")
    67  	}
    68  	g.Expect(k8sClient.Delete(ctx, awsCluster)).To(Succeed())
    69  }