...

Source file src/sigs.k8s.io/cli-utils/test/stress/stress_test.go

Documentation: sigs.k8s.io/cli-utils/test/stress

     1  // Copyright 2020 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package stress
     5  
     6  import (
     7  	"context"
     8  	"time"
     9  
    10  	. "github.com/onsi/ginkgo/v2"
    11  	. "github.com/onsi/gomega"
    12  	"github.com/onsi/gomega/format"
    13  	v1 "k8s.io/api/core/v1"
    14  	"k8s.io/klog/v2"
    15  	"k8s.io/kubectl/pkg/scheme"
    16  	"sigs.k8s.io/cli-utils/test/e2e/e2eutil"
    17  	"sigs.k8s.io/cli-utils/test/e2e/invconfig"
    18  	ctrl "sigs.k8s.io/controller-runtime"
    19  	"sigs.k8s.io/controller-runtime/pkg/client"
    20  	"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
    21  )
    22  
    23  // Parse optional logging flags
    24  // Ex: ginkgo ./test/e2e/... -- -v=5
    25  // Allow init for e2e test (not imported by external code)
    26  // nolint:gochecknoinits
    27  func init() {
    28  	klog.InitFlags(nil)
    29  	klog.SetOutput(GinkgoWriter)
    30  }
    31  
    32  var defaultTestTimeout = 1 * time.Hour
    33  var defaultBeforeTestTimeout = 30 * time.Second
    34  var defaultAfterTestTimeout = 30 * time.Second
    35  
    36  var c client.Client
    37  var invConfig invconfig.InventoryConfig
    38  
    39  var _ = BeforeSuite(func() {
    40  	// increase from 4000 to handle long event lists
    41  	format.MaxLength = 10000
    42  
    43  	cfg, err := ctrl.GetConfig()
    44  	Expect(err).NotTo(HaveOccurred())
    45  
    46  	cfg.UserAgent = e2eutil.UserAgent("test/stress")
    47  
    48  	if e2eutil.IsFlowControlEnabled(cfg) {
    49  		// Disable client-side throttling.
    50  		klog.V(3).Infof("Client-side throttling disabled")
    51  		cfg.QPS = -1
    52  		cfg.Burst = -1
    53  	}
    54  
    55  	invConfig = invconfig.NewCustomTypeInvConfig(cfg)
    56  
    57  	mapper, err := apiutil.NewDynamicRESTMapper(cfg)
    58  	Expect(err).NotTo(HaveOccurred())
    59  
    60  	c, err = client.New(cfg, client.Options{
    61  		Scheme: scheme.Scheme,
    62  		Mapper: mapper,
    63  	})
    64  	Expect(err).NotTo(HaveOccurred())
    65  
    66  	ctx, cancel := context.WithTimeout(context.Background(), defaultBeforeTestTimeout)
    67  	defer cancel()
    68  	e2eutil.CreateInventoryCRD(ctx, c)
    69  	Expect(ctx.Err()).To(BeNil(), "BeforeSuite context cancelled or timed out")
    70  })
    71  
    72  var _ = AfterSuite(func() {
    73  	ctx, cancel := context.WithTimeout(context.Background(), defaultAfterTestTimeout)
    74  	defer cancel()
    75  	if c != nil {
    76  		// If BeforeSuite() failed, c might be nil. Skip deletion to avoid red herring panic.
    77  		e2eutil.DeleteInventoryCRD(ctx, c)
    78  	}
    79  	Expect(ctx.Err()).To(BeNil(), "AfterSuite context cancelled or timed out")
    80  })
    81  
    82  var _ = Describe("Stress", func() {
    83  	var namespace *v1.Namespace
    84  	var inventoryName string
    85  	var ctx context.Context
    86  	var cancel context.CancelFunc
    87  
    88  	BeforeEach(func() {
    89  		ctx, cancel = context.WithTimeout(context.Background(), defaultTestTimeout)
    90  		inventoryName = e2eutil.RandomString("test-inv-")
    91  		namespace = e2eutil.CreateRandomNamespace(ctx, c)
    92  	})
    93  
    94  	AfterEach(func() {
    95  		Expect(ctx.Err()).To(BeNil(), "test context cancelled or timed out")
    96  		cancel()
    97  		ctx, cancel = context.WithTimeout(context.Background(), defaultAfterTestTimeout)
    98  		defer cancel()
    99  		// clean up resources created by the tests
   100  		e2eutil.DeleteNamespace(ctx, c, namespace)
   101  	})
   102  
   103  	It("ThousandDeployments", func() {
   104  		thousandDeploymentsTest(ctx, c, invConfig, inventoryName, namespace.GetName())
   105  	})
   106  
   107  	It("ThousandDeploymentsRetry", func() {
   108  		thousandDeploymentsRetryTest(ctx, c, invConfig, inventoryName, namespace.GetName())
   109  	})
   110  
   111  	It("ThousandNamespaces", func() {
   112  		thousandNamespacesTest(ctx, c, invConfig, inventoryName, namespace.GetName())
   113  	})
   114  })
   115  

View as plain text