-
Notifications
You must be signed in to change notification settings - Fork 1.6k
✨ (go/v4): Add optional kubectl context locking for e2e tests #5336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -51,6 +51,7 @@ import ( | |||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||
| "os/exec" | ||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||
| "testing" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| . "github.com/onsi/ginkgo/v2" | ||||||||||||||||||||||||||
|
|
@@ -69,14 +70,26 @@ var ( | |||||||||||||||||||||||||
| // TestE2E runs the e2e test suite to validate the solution in an isolated environment. | ||||||||||||||||||||||||||
| // The default setup requires Kind and CertManager. | ||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||
| // To skip CertManager installation, set: CERT_MANAGER_INSTALL_SKIP=true | ||||||||||||||||||||||||||
| // Environment variables (see Makefile target 'test-e2e' for examples): | ||||||||||||||||||||||||||
| // - KIND_CLUSTER: Name of the Kind cluster (default: kind) | ||||||||||||||||||||||||||
| // - KUBE_CONTEXT: Kubectl context to use (default: current-context) | ||||||||||||||||||||||||||
| // - CERT_MANAGER_INSTALL_SKIP=true: Skip CertManager installation | ||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||
| // Note: When KIND_CLUSTER=my-cluster, the kubectl context will be "kind-my-cluster" | ||||||||||||||||||||||||||
|
Comment on lines
+73
to
+78
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be the exact same as in the Makefile?
Suggested change
|
||||||||||||||||||||||||||
| func TestE2E(t *testing.T) { | ||||||||||||||||||||||||||
| RegisterFailHandler(Fail) | ||||||||||||||||||||||||||
| _, _ = fmt.Fprintf(GinkgoWriter, "Starting {{ .ProjectName }} e2e test suite\n") | ||||||||||||||||||||||||||
| RunSpecs(t, "e2e suite") | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| var _ = BeforeSuite(func() { | ||||||||||||||||||||||||||
| // Display kubectl context being used | ||||||||||||||||||||||||||
| if kubectx := os.Getenv("KUBE_CONTEXT"); kubectx != "" { | ||||||||||||||||||||||||||
| _, _ = fmt.Fprintf(GinkgoWriter, "Using context: %s\n", kubectx) | ||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ran |
||||||||||||||||||||||||||
| } else if currentCtx, err := getCurrentContext(); err == nil { | ||||||||||||||||||||||||||
| _, _ = fmt.Fprintf(GinkgoWriter, "Using context: %s\n", currentCtx) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| By("building the manager image") | ||||||||||||||||||||||||||
| cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", managerImage)) | ||||||||||||||||||||||||||
| _, err := utils.Run(cmd) | ||||||||||||||||||||||||||
|
|
@@ -127,4 +140,14 @@ func teardownCertManager() { | |||||||||||||||||||||||||
| By("uninstalling CertManager") | ||||||||||||||||||||||||||
| utils.UninstallCertManager() | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // getCurrentContext returns the current kubectl context name | ||||||||||||||||||||||||||
| func getCurrentContext() (string, error) { | ||||||||||||||||||||||||||
| cmd := exec.Command("kubectl", "config", "current-context") | ||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't we simply extract the context that is create by default by
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would make the test fail if I just create kind and run the tests via IDE, for example |
||||||||||||||||||||||||||
| output, err := cmd.Output() | ||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||
| return "", err | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| return strings.TrimSpace(string(output)), nil | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,11 +30,20 @@ type Kubectl struct { | |
| *CmdContext | ||
| Namespace string | ||
| ServiceAccount string | ||
| KubeContext string | ||
| } | ||
|
|
||
| // cmdOptionsWithContext prepends --context flag to kubectl commands if KubeContext is set | ||
| func (k *Kubectl) cmdOptionsWithContext(cmdOptions ...string) []string { | ||
| if k.KubeContext != "" { | ||
| return append([]string{"--context", k.KubeContext}, cmdOptions...) | ||
| } | ||
| return cmdOptions | ||
|
Comment on lines
+37
to
+41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One more thing: I created the new cluster ( |
||
| } | ||
|
|
||
| // Command is a general func to run kubectl commands | ||
| func (k *Kubectl) Command(cmdOptions ...string) (string, error) { | ||
| cmd := exec.Command("kubectl", cmdOptions...) | ||
| cmd := exec.Command("kubectl", k.cmdOptionsWithContext(cmdOptions...)...) | ||
| output, err := k.Run(cmd) | ||
| return string(output), err | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,6 +76,8 @@ func NewTestContext(binaryName string, env ...string) (*TestContext, error) { | |
| Namespace: fmt.Sprintf("e2e-%s-system", testSuffix), | ||
| ServiceAccount: fmt.Sprintf("e2e-%s-controller-manager", testSuffix), | ||
| CmdContext: cc, | ||
| // Optional context lock from env var | ||
| KubeContext: os.Getenv("KUBE_CONTEXT"), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created a new cluster ( Kind still checked for the default It also passed But then, before each test, it used the context that was passed, So it seems we should either fix the |
||
| } | ||
|
|
||
| // For test outside of cluster we do not need to have kubectl | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran
make test-e2ewithout passing theKUBE_CONTEXTenv var and got this:So I believe the default
KUBE_CONTEXTwould bekind-{{ .ProjectName }}-test-e2e, right?