Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ test-testdata: ## Run the tests of the testdata directory
.PHONY: test-e2e-local
test-e2e-local: ## Run the end-to-end tests locally
## To keep the same kind cluster between test runs, use `SKIP_KIND_CLEANUP=1 make test-e2e-local`
## To lock to a specific kubectl context, use `KUBE_CONTEXT=kind-test make test-e2e-local`
./test/e2e/local.sh

.PHONY: test-e2e-ci
Expand Down
7 changes: 5 additions & 2 deletions docs/book/src/cronjob-tutorial/testdata/project/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ test: manifests generate fmt vet setup-envtest ## Run tests.

# TODO(user): To use a different vendor for e2e tests, modify the setup under 'tests/e2e'.
# The default setup assumes Kind is pre-installed and builds/loads the Manager Docker image locally.
# CertManager is installed by default; skip with:
# - CERT_MANAGER_INSTALL_SKIP=true
# Environment variables:
# - KIND_CLUSTER: Name of the Kind cluster (default: project-test-e2e)
# - 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"
KIND_CLUSTER ?= project-test-e2e

.PHONY: setup-test-e2e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -43,7 +44,12 @@ 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"
func TestE2E(t *testing.T) {
RegisterFailHandler(Fail)
_, _ = fmt.Fprintf(GinkgoWriter, "Starting project e2e test suite\n")
Expand All @@ -54,6 +60,13 @@ var _ = BeforeSuite(func() {
By("Ensure that Prometheus is enabled")
_ = utils.UncommentCode("config/default/kustomization.yaml", "#- ../prometheus", "#")

// Display kubectl context being used
if kubectx := os.Getenv("KUBE_CONTEXT"); kubectx != "" {
_, _ = fmt.Fprintf(GinkgoWriter, "Using context: %s\n", kubectx)
} 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)
Expand Down Expand Up @@ -119,3 +132,13 @@ 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")
output, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
}
7 changes: 5 additions & 2 deletions docs/book/src/getting-started/testdata/project/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ test: manifests generate fmt vet setup-envtest ## Run tests.

# TODO(user): To use a different vendor for e2e tests, modify the setup under 'tests/e2e'.
# The default setup assumes Kind is pre-installed and builds/loads the Manager Docker image locally.
# CertManager is installed by default; skip with:
# - CERT_MANAGER_INSTALL_SKIP=true
# Environment variables:
# - KIND_CLUSTER: Name of the Kind cluster (default: project-test-e2e)
# - 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"
KIND_CLUSTER ?= project-test-e2e

.PHONY: setup-test-e2e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -41,14 +42,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"
func TestE2E(t *testing.T) {
RegisterFailHandler(Fail)
_, _ = fmt.Fprintf(GinkgoWriter, "Starting project 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)
} 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)
Expand Down Expand Up @@ -99,3 +112,13 @@ 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")
output, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
}
7 changes: 5 additions & 2 deletions docs/book/src/multiversion-tutorial/testdata/project/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ test: manifests generate fmt vet setup-envtest ## Run tests.

# TODO(user): To use a different vendor for e2e tests, modify the setup under 'tests/e2e'.
# The default setup assumes Kind is pre-installed and builds/loads the Manager Docker image locally.
# CertManager is installed by default; skip with:
# - CERT_MANAGER_INSTALL_SKIP=true
# Environment variables:
# - KIND_CLUSTER: Name of the Kind cluster (default: project-test-e2e)
# - 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"
KIND_CLUSTER ?= project-test-e2e

.PHONY: setup-test-e2e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -43,7 +44,12 @@ 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"
func TestE2E(t *testing.T) {
RegisterFailHandler(Fail)
_, _ = fmt.Fprintf(GinkgoWriter, "Starting project e2e test suite\n")
Expand All @@ -54,6 +60,13 @@ var _ = BeforeSuite(func() {
By("Ensure that Prometheus is enabled")
_ = utils.UncommentCode("config/default/kustomization.yaml", "#- ../prometheus", "#")

// Display kubectl context being used
if kubectx := os.Getenv("KUBE_CONTEXT"); kubectx != "" {
_, _ = fmt.Fprintf(GinkgoWriter, "Using context: %s\n", kubectx)
} 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)
Expand Down Expand Up @@ -119,3 +132,13 @@ 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")
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
Expand Up @@ -142,8 +142,11 @@ test: manifests generate fmt vet setup-envtest ## Run tests.

# TODO(user): To use a different vendor for e2e tests, modify the setup under 'tests/e2e'.
# The default setup assumes Kind is pre-installed and builds/loads the Manager Docker image locally.
# CertManager is installed by default; skip with:
# - CERT_MANAGER_INSTALL_SKIP=true
# Environment variables:
# - KIND_CLUSTER: Name of the Kind cluster (default: {{ .ProjectName }}-test-e2e)
# - 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"
KIND_CLUSTER ?= {{ .ProjectName }}-test-e2e

.PHONY: setup-test-e2e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -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"
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)
} 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)
Expand Down Expand Up @@ -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")
output, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
}
`
11 changes: 10 additions & 1 deletion test/e2e/utils/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

// 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
}
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/utils/test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}

// For test outside of cluster we do not need to have kubectl
Expand Down
7 changes: 5 additions & 2 deletions testdata/project-v4-multigroup/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ test: manifests generate fmt vet setup-envtest ## Run tests.

# TODO(user): To use a different vendor for e2e tests, modify the setup under 'tests/e2e'.
# The default setup assumes Kind is pre-installed and builds/loads the Manager Docker image locally.
# CertManager is installed by default; skip with:
# - CERT_MANAGER_INSTALL_SKIP=true
# Environment variables:
# - KIND_CLUSTER: Name of the Kind cluster (default: project-v4-multigroup-test-e2e)
# - 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"
KIND_CLUSTER ?= project-v4-multigroup-test-e2e

.PHONY: setup-test-e2e
Expand Down
25 changes: 24 additions & 1 deletion testdata/project-v4-multigroup/test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -41,14 +42,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"
func TestE2E(t *testing.T) {
RegisterFailHandler(Fail)
_, _ = fmt.Fprintf(GinkgoWriter, "Starting project-v4-multigroup 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)
} 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)
Expand Down Expand Up @@ -99,3 +112,13 @@ 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")
output, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
}
7 changes: 5 additions & 2 deletions testdata/project-v4-with-plugins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ test: manifests generate fmt vet setup-envtest ## Run tests.

# TODO(user): To use a different vendor for e2e tests, modify the setup under 'tests/e2e'.
# The default setup assumes Kind is pre-installed and builds/loads the Manager Docker image locally.
# CertManager is installed by default; skip with:
# - CERT_MANAGER_INSTALL_SKIP=true
# Environment variables:
# - KIND_CLUSTER: Name of the Kind cluster (default: project-v4-with-plugins-test-e2e)
# - 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"
KIND_CLUSTER ?= project-v4-with-plugins-test-e2e

.PHONY: setup-test-e2e
Expand Down
Loading
Loading