Skip to content

Commit

Permalink
feat: add context switching in TestStep (#560)
Browse files Browse the repository at this point in the history
Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
  • Loading branch information
kumar-mallikarjuna committed Sep 11, 2024
1 parent 733ad16 commit 2cb230f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 21 deletions.
21 changes: 11 additions & 10 deletions docs/testing/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,18 @@ kubeconfig: foo.kubeconfig

Supported settings:

Field | Type | Description
---------|---------------------------|---------------------------------------------------------------------
apply | list of files | A list of files to apply as part of this step. Specified path is relative to that in which the step occurs.
assert | list of files | A list of files to assert as part of this step. See documentation for [asserts and errors](asserts-errors.md) for more information. Specified path is relative to that in which the step occurs.
error | list of files | A list of files to error as part of this step. See documentation for [asserts and errors](asserts-errors.md) for more information. Specified path is relative to that in which the step occurs.
delete | list of object references | A list of objects to delete, if they do not already exist, at the beginning of the test step. The test harness will wait for the objects to be successfully deleted before applying the objects in the step.
index | int | Override the test step's index.
Field | Type | Description
---------|-------------------------------|---------------------------------------------------------------------
apply | list of files | A list of files to apply as part of this step. Specified path is relative to that in which the step occurs.
assert | list of files | A list of files to assert as part of this step. See documentation for [asserts and errors](asserts-errors.md) for more information. Specified path is relative to that in which the step occurs.
error | list of files | A list of files to error as part of this step. See documentation for [asserts and errors](asserts-errors.md) for more information. Specified path is relative to that in which the step occurs.
delete | list of object references | A list of objects to delete, if they do not already exist, at the beginning of the test step. The test harness will wait for the objects to be successfully deleted before applying the objects in the step.
index | int | Override the test step's index.
commands | list of [Commands](#commands) | Commands to run prior at the beginning of the test step.
kubeconfig | string | The Kubeconfig file to use to run the included steps(s).
kubeconfigLoading | string | Specifies the mode for loading Kubeconfig and making a cluster connection: `Eager` (when loading the test definition) or `Lazy` (right before executing the step, makes it possible to generate the Kubeconfig in a preceding step). Defaults to `Eager`.
unitTest | bool | Indicates if the step is a unit test, safe to run without a real Kubernetes cluster.
kubeconfig | string | The Kubeconfig file to use to run the included steps(s).
kubeconfigLoading | string | Specifies the mode for loading Kubeconfig and making a cluster connection: `Eager` (when loading the test definition) or `Lazy` (right before executing the step, makes it possible to generate the Kubeconfig in a preceding step). Defaults to `Eager`.
context | string | Specifies the context to use from the Kubeconfig.
unitTest | bool | Indicates if the step is a unit test, safe to run without a real Kubernetes cluster.


Object Reference:
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/testharness/v1beta1/test_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ type TestStep struct {
// +kubebuilder:default=Eager
// +kubebuilder:validation:Enum=Eager;Lazy
KubeconfigLoading string `json:"kubeconfigLoading,omitempty"`

// Specifies the context to use from the Kubeconfig.
Context string `json:"context,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
12 changes: 12 additions & 0 deletions pkg/k8s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package k8s

import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

Expand All @@ -19,3 +20,14 @@ func GetConfig() (*rest.Config, error) {

return cfg, nil
}

func BuildConfigWithContext(kubeconfig, context string) (*rest.Config, error) {
if context == "" {
// Use default context
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}

return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
&clientcmd.ConfigOverrides{CurrentContext: context}).ClientConfig()
}
17 changes: 8 additions & 9 deletions pkg/test/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ import (
"k8s.io/client-go/discovery"
"sigs.k8s.io/controller-runtime/pkg/client"

"k8s.io/client-go/tools/clientcmd"

"github.com/kudobuilder/kuttl/pkg/apis/testharness/v1beta1"
"github.com/kudobuilder/kuttl/pkg/k8s"
"github.com/kudobuilder/kuttl/pkg/report"
testutils "github.com/kudobuilder/kuttl/pkg/test/utils"
)
Expand Down Expand Up @@ -338,7 +337,7 @@ func (t *Case) Run(test *testing.T, ts *report.Testsuite) {
continue
}

cl, err = newClient(testStep.Kubeconfig)(false)
cl, err = newClient(testStep.Kubeconfig, testStep.Context)(false)
if err != nil {
setupReport.Failure = report.NewFailure(err.Error(), nil)
ts.AddTestcase(setupReport)
Expand All @@ -363,11 +362,11 @@ func (t *Case) Run(test *testing.T, ts *report.Testsuite) {
tc := report.NewCase("step " + testStep.String())
testStep.Client = t.Client
if testStep.Kubeconfig != "" {
testStep.Client = newClient(testStep.Kubeconfig)
testStep.Client = newClient(testStep.Kubeconfig, testStep.Context)
}
testStep.DiscoveryClient = t.DiscoveryClient
if testStep.Kubeconfig != "" {
testStep.DiscoveryClient = newDiscoveryClient(testStep.Kubeconfig)
testStep.DiscoveryClient = newDiscoveryClient(testStep.Kubeconfig, testStep.Context)
}
testStep.Logger = t.Logger.WithPrefix(testStep.String())
tc.Assertions += len(testStep.Asserts)
Expand Down Expand Up @@ -531,9 +530,9 @@ func (t *Case) LoadTestSteps() error {
return nil
}

func newClient(kubeconfig string) func(bool) (client.Client, error) {
func newClient(kubeconfig, context string) func(bool) (client.Client, error) {
return func(bool) (client.Client, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
config, err := k8s.BuildConfigWithContext(kubeconfig, context)
if err != nil {
return nil, err
}
Expand All @@ -544,9 +543,9 @@ func newClient(kubeconfig string) func(bool) (client.Client, error) {
}
}

func newDiscoveryClient(kubeconfig string) func() (discovery.DiscoveryInterface, error) {
func newDiscoveryClient(kubeconfig, context string) func() (discovery.DiscoveryInterface, error) {
return func() (discovery.DiscoveryInterface, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
config, err := k8s.BuildConfigWithContext(kubeconfig, context)
if err != nil {
return nil, err
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ type Step struct {

Kubeconfig string
KubeconfigLoading string
Client func(forceNew bool) (client.Client, error)
DiscoveryClient func() (discovery.DiscoveryInterface, error)
Context string

Client func(forceNew bool) (client.Client, error)
DiscoveryClient func() (discovery.DiscoveryInterface, error)

Logger testutils.Logger
}
Expand Down Expand Up @@ -556,6 +558,7 @@ func (s *Step) LoadYAML(file string) error {
exKubeconfig := env.Expand(s.Step.Kubeconfig)
s.Kubeconfig = cleanPath(exKubeconfig, s.Dir)
}
s.Context = s.Step.Context

switch s.Step.KubeconfigLoading {
case "", harness.KubeconfigLoadingEager, harness.KubeconfigLoadingLazy:
Expand Down

0 comments on commit 2cb230f

Please sign in to comment.