...

Source file src/sigs.k8s.io/cli-utils/test/e2e/invconfig/configmap.go

Documentation: sigs.k8s.io/cli-utils/test/e2e/invconfig

     1  // Copyright 2021 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package invconfig
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/onsi/gomega"
    10  	v1 "k8s.io/api/core/v1"
    11  	"k8s.io/client-go/rest"
    12  	"sigs.k8s.io/cli-utils/pkg/apply"
    13  	"sigs.k8s.io/cli-utils/pkg/common"
    14  	"sigs.k8s.io/cli-utils/pkg/inventory"
    15  	"sigs.k8s.io/controller-runtime/pkg/client"
    16  )
    17  
    18  func NewConfigMapTypeInvConfig(cfg *rest.Config) InventoryConfig {
    19  	return InventoryConfig{
    20  		ClientConfig:         cfg,
    21  		Strategy:             inventory.LabelStrategy,
    22  		FactoryFunc:          cmInventoryManifest,
    23  		InvWrapperFunc:       inventory.WrapInventoryInfoObj,
    24  		ApplierFactoryFunc:   newDefaultInvApplierFactory(cfg),
    25  		DestroyerFactoryFunc: newDefaultInvDestroyerFactory(cfg),
    26  		InvSizeVerifyFunc:    defaultInvSizeVerifyFunc,
    27  		InvCountVerifyFunc:   defaultInvCountVerifyFunc,
    28  		InvNotExistsFunc:     defaultInvNotExistsFunc,
    29  	}
    30  }
    31  
    32  func newDefaultInvApplierFactory(cfg *rest.Config) applierFactoryFunc {
    33  	cfgPtrCopy := cfg
    34  	return func() *apply.Applier {
    35  		return newApplier(inventory.ClusterClientFactory{
    36  			StatusPolicy: inventory.StatusPolicyAll,
    37  		}, cfgPtrCopy)
    38  	}
    39  }
    40  
    41  func newDefaultInvDestroyerFactory(cfg *rest.Config) destroyerFactoryFunc {
    42  	cfgPtrCopy := cfg
    43  	return func() *apply.Destroyer {
    44  		return newDestroyer(inventory.ClusterClientFactory{
    45  			StatusPolicy: inventory.StatusPolicyAll,
    46  		}, cfgPtrCopy)
    47  	}
    48  }
    49  
    50  func defaultInvNotExistsFunc(ctx context.Context, c client.Client, name, namespace, id string) {
    51  	var cmList v1.ConfigMapList
    52  	err := c.List(ctx, &cmList,
    53  		client.MatchingLabels(map[string]string{common.InventoryLabel: id}),
    54  		client.InNamespace(namespace))
    55  	gomega.Expect(err).ToNot(gomega.HaveOccurred())
    56  	gomega.Expect(cmList.Items).To(gomega.HaveLen(0), "expected inventory list to be empty")
    57  }
    58  
    59  func defaultInvSizeVerifyFunc(ctx context.Context, c client.Client, name, namespace, id string, specCount, _ int) {
    60  	var cmList v1.ConfigMapList
    61  	err := c.List(ctx, &cmList,
    62  		client.MatchingLabels(map[string]string{common.InventoryLabel: id}),
    63  		client.InNamespace(namespace))
    64  	gomega.Expect(err).WithOffset(1).ToNot(gomega.HaveOccurred(), "listing ConfigMap inventory from cluster")
    65  
    66  	gomega.Expect(len(cmList.Items)).WithOffset(1).To(gomega.Equal(1), "number of inventory objects by label")
    67  
    68  	data := cmList.Items[0].Data
    69  	gomega.Expect(len(data)).WithOffset(1).To(gomega.Equal(specCount), "inventory spec.data length")
    70  
    71  	// Don't validate status size.
    72  	// ConfigMap provider uses inventory.StatusPolicyNone.
    73  }
    74  
    75  func defaultInvCountVerifyFunc(ctx context.Context, c client.Client, namespace string, count int) {
    76  	var cmList v1.ConfigMapList
    77  	err := c.List(ctx, &cmList, client.InNamespace(namespace), client.HasLabels{common.InventoryLabel})
    78  	gomega.Expect(err).NotTo(gomega.HaveOccurred())
    79  	gomega.Expect(len(cmList.Items)).To(gomega.Equal(count))
    80  }
    81  

View as plain text