...

Source file src/edge-infra.dev/test/f2/x/ktest/options.go

Documentation: edge-infra.dev/test/f2/x/ktest

     1  package ktest
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"k8s.io/apimachinery/pkg/runtime"
     7  	"sigs.k8s.io/controller-runtime/pkg/manager"
     8  
     9  	"edge-infra.dev/pkg/k8s/runtime/controller"
    10  	"edge-infra.dev/test/f2/x/ktest/envtest"
    11  )
    12  
    13  // options for creating a ktest framework
    14  type options struct {
    15  	konfigkonnector       bool
    16  	certmgr               bool
    17  	skipNamespaceCreation bool
    18  	skipNamespaceDeletion bool
    19  	metricsAddress        string
    20  	mgrCreator            func(...controller.Option) (manager.Manager, error)
    21  	gracefulTimeout       string
    22  	envtestOpts           []envtest.Option
    23  	clientScheme          *runtime.Scheme
    24  }
    25  
    26  // Option is a ktest framework extension option. The behavior of individual
    27  // options may be modified by providing test flags exposed by this package.
    28  type Option func(*options)
    29  
    30  // SkipNamespaceCreation will make the framework skip creation of a namespace
    31  // for each test case.  Useful for tests that don't operate against any
    32  // Namespaced resources.
    33  func SkipNamespaceCreation() Option {
    34  	return func(o *options) {
    35  		o.skipNamespaceCreation = true
    36  	}
    37  }
    38  
    39  // SkipNamespaceDeletion will make the framework skip deletion of a namespace
    40  // for each test case.
    41  func SkipNamespaceDeletion() Option {
    42  	return func(o *options) {
    43  		o.skipNamespaceDeletion = true
    44  	}
    45  }
    46  
    47  // WithKonfigKonnector controls whether or not K8s config connector is installed
    48  // and configured for the test run.
    49  func WithKonfigKonnector() Option {
    50  	return func(o *options) {
    51  		o.konfigkonnector = true
    52  	}
    53  }
    54  
    55  // WithCertManager ensures that cert-manager is installed for the test run.
    56  func WithCertManager() Option {
    57  	return func(o *options) {
    58  		o.certmgr = true
    59  	}
    60  }
    61  
    62  // WithCtrlManager sets up the framework for testing a controller-runtime Manager
    63  func WithCtrlManager(fn func(...controller.Option) (manager.Manager, error)) Option {
    64  	return func(o *options) {
    65  		o.mgrCreator = fn
    66  	}
    67  }
    68  
    69  // WithGracefulTimeout allows an optional timeout duration to be passed during ktest setup.
    70  // The desired timeout is passed as a string and parsed using time.ParseDuration.
    71  // e.g. WithGracefulTimeout("1m30s")
    72  // Passing "0" disables graceful shutdown while passing a negative value
    73  // allows the graceful shutdown to complete with no timeout.
    74  // Defaults to defaultGracefulShutdownPeriod (30s)
    75  func WithGracefulTimeout(timeout string) Option {
    76  	return func(o *options) {
    77  		o.gracefulTimeout = timeout
    78  	}
    79  }
    80  
    81  func WithEnvtestOptions(opts ...envtest.Option) Option {
    82  	return func(o *options) {
    83  		o.envtestOpts = opts
    84  	}
    85  }
    86  
    87  // WithMetricsAddress enables the manager's metrics server.
    88  // The passed port will be used if available.
    89  // Otherwise, a random available port will be used.
    90  // Passing 0 will allow a random available port to be used by default.
    91  func WithMetricsAddress(addr int) Option {
    92  	return func(o *options) {
    93  		o.metricsAddress = fmt.Sprintf(":%d", addr)
    94  	}
    95  }
    96  
    97  // WithScheme sets the Scheme that will be used when creating the
    98  // [K8s.Client]. If it is not set, the Scheme provided by [K8s.Manager]
    99  // will be used. Ie, WithScheme takes precedence over [WithCtrlManager] when
   100  // setting the [client.Client] Scheme
   101  func WithScheme(scheme *runtime.Scheme) Option {
   102  	return func(o *options) {
   103  		o.clientScheme = scheme
   104  	}
   105  }
   106  
   107  func makeOptions(opts ...Option) *options {
   108  	options := &options{}
   109  	for _, o := range opts {
   110  		o(options)
   111  	}
   112  	return options
   113  }
   114  

View as plain text