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

     1  //go:build e2e
     2  // +build e2e
     3  
     4  /*
     5  Copyright 2020 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  	"time"
    25  
    26  	"github.com/aws/aws-sdk-go/aws/client"
    27  	"github.com/aws/aws-sdk-go/service/iam"
    28  	"github.com/awslabs/goformation/v4/cloudformation"
    29  	"github.com/gofrs/flock"
    30  	corev1 "k8s.io/api/core/v1"
    31  	"k8s.io/apimachinery/pkg/runtime"
    32  
    33  	cfn_bootstrap "sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cloudformation/bootstrap"
    34  	clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
    35  	"sigs.k8s.io/cluster-api/test/framework"
    36  	"sigs.k8s.io/cluster-api/test/framework/bootstrap"
    37  	"sigs.k8s.io/cluster-api/test/framework/clusterctl"
    38  )
    39  
    40  // Option represents an option to use when creating a e2e context.
    41  type Option func(*E2EContext)
    42  
    43  func NewE2EContext(options ...Option) *E2EContext {
    44  	ctx := &E2EContext{
    45  		IsManaged: false,
    46  	}
    47  	ctx.Environment.Scheme = DefaultScheme()
    48  	ctx.Environment.Namespaces = map[*corev1.Namespace]context.CancelFunc{}
    49  
    50  	for _, opt := range options {
    51  		opt(ctx)
    52  	}
    53  
    54  	return ctx
    55  }
    56  
    57  // E2EContext represents the context of the e2e test.
    58  type E2EContext struct {
    59  	// Settings is the settings used for the test.
    60  	Settings Settings
    61  	// E2EConfig to be used for this test, read from configPath.
    62  	E2EConfig *clusterctl.E2EConfig
    63  	// Environment represents the runtime environment.
    64  	Environment RuntimeEnvironment
    65  	// AWSSession is the AWS session for the tests.
    66  	AWSSession client.ConfigProvider
    67  	// BootstrapUserAWSSession is the AWS session for the bootstrap user.
    68  	BootstrapUserAWSSession client.ConfigProvider
    69  	// IsManaged indicates that this is for the managed part of the provider.
    70  	IsManaged bool
    71  	// CloudFormationTemplate is the rendered template created for the test.
    72  	CloudFormationTemplate *cloudformation.Template
    73  	StartOfSuite           time.Time
    74  }
    75  
    76  // Settings represents the test settings.
    77  type Settings struct {
    78  	// ConfigPath is the path to the e2e config file.
    79  	ConfigPath string
    80  	// useExistingCluster instructs the test to use the current cluster instead of creating a new one (default discovery rules apply).
    81  	UseExistingCluster bool
    82  	// ArtifactFolder is the folder to store e2e test artifacts.
    83  	ArtifactFolder string
    84  	// DataFolder is the root folder for the data required by the tests.
    85  	DataFolder string
    86  	// SkipCleanup prevents cleanup of test resources e.g. for debug purposes.
    87  	SkipCleanup bool
    88  	// SkipCloudFormationCreation will skip the cloudformation execution - useful for debugging e2e tests.
    89  	SkipCloudFormationCreation bool
    90  	// SkipCloudFormationDeletion prevents the deletion of the AWS CloudFormation stack.
    91  	SkipCloudFormationDeletion bool
    92  	// number of ginkgo nodes to use for kubetest.
    93  	GinkgoNodes int
    94  	// time in s before kubetest spec is marked as slow.
    95  	GinkgoSlowSpecThreshold int
    96  	// kubetestConfigFilePath is the path to the kubetest configuration file.
    97  	KubetestConfigFilePath string
    98  	// useCIArtifacts specifies whether or not to use the latest build from the main branch of the Kubernetes repository.
    99  	UseCIArtifacts bool
   100  	// SourceTemplate specifies which source template to use.
   101  	SourceTemplate string
   102  	// FileLock is the lock to be used to read the resource quotas file.
   103  	FileLock *flock.Flock
   104  	// InstanceVcpu is the number of vCPUs needed for the AWS instance type used for workers and control plane.
   105  	InstanceVCPU int
   106  }
   107  
   108  // RuntimeEnvironment represents the runtime environment of the test.
   109  type RuntimeEnvironment struct {
   110  	// BootstrapClusterProvider manages provisioning of the the bootstrap cluster to be used for the e2e tests.
   111  	// Please note that provisioning will be skipped if use-existing-cluster is provided.
   112  	BootstrapClusterProvider bootstrap.ClusterProvider
   113  	// BootstrapClusterProxy allows to interact with the bootstrap cluster to be used for the e2e tests.
   114  	BootstrapClusterProxy framework.ClusterProxy
   115  	// BootstrapTemplate is the clusterawsadm bootstrap template for this run.
   116  	BootstrapTemplate *cfn_bootstrap.Template
   117  	// BootstrapAccessKey is the bootstrap user access key.
   118  	BootstrapAccessKey *iam.AccessKey
   119  	// ResourceTicker for dumping resources.
   120  	ResourceTicker *time.Ticker
   121  	// ResourceTickerDone to stop ticking.
   122  	ResourceTickerDone chan bool
   123  	// MachineTicker for dumping resources.
   124  	MachineTicker *time.Ticker
   125  	// MachineTickerDone to stop ticking.
   126  	MachineTickerDone chan bool
   127  	// Namespaces holds the namespaces used in the tests.
   128  	Namespaces map[*corev1.Namespace]context.CancelFunc
   129  	// ClusterctlConfigPath to be used for this test, created by generating a clusterctl local repository
   130  	// with the providers specified in the configPath.
   131  	ClusterctlConfigPath string
   132  	// Scheme is the GVK scheme to use for the tests.
   133  	Scheme *runtime.Scheme
   134  }
   135  
   136  // InitSchemeFunc is a function that will create a scheme.
   137  type InitSchemeFunc func() *runtime.Scheme
   138  
   139  // WithSchemeInit will set a different function to initialize the scheme.
   140  func WithSchemeInit(fn InitSchemeFunc) Option {
   141  	return func(ctx *E2EContext) {
   142  		ctx.Environment.Scheme = fn()
   143  	}
   144  }
   145  
   146  // WithManaged will set a different function to initialize the scheme.
   147  func WithManaged() Option {
   148  	return func(ctx *E2EContext) {
   149  		ctx.IsManaged = true
   150  	}
   151  }
   152  
   153  func (c *E2EContext) InfrastructureProviders() []string {
   154  	InfraProviders := []string{}
   155  	for _, provider := range c.E2EConfig.Providers {
   156  		if provider.Type == string(clusterctlv1.InfrastructureProviderType) {
   157  			InfraProviders = append(InfraProviders, provider.Name)
   158  		}
   159  	}
   160  	return InfraProviders
   161  }
   162  
   163  func (c *E2EContext) BootstrapProviders() []string {
   164  	BootstrapProviders := []string{}
   165  	for _, provider := range c.E2EConfig.Providers {
   166  		if provider.Type == string(clusterctlv1.BootstrapProviderType) {
   167  			BootstrapProviders = append(BootstrapProviders, provider.Name)
   168  		}
   169  	}
   170  	return BootstrapProviders
   171  }
   172  
   173  func (c *E2EContext) ControlPlaneProviders() []string {
   174  	ControlPlaneProviders := []string{}
   175  	for _, provider := range c.E2EConfig.Providers {
   176  		if provider.Type == string(clusterctlv1.ControlPlaneProviderType) {
   177  			ControlPlaneProviders = append(ControlPlaneProviders, provider.Name)
   178  		}
   179  	}
   180  	return ControlPlaneProviders
   181  }