From eff1b24cf31d920fbce698094bbacb6ce7f3678e Mon Sep 17 00:00:00 2001 From: Kui Wang Date: Thu, 9 Oct 2025 09:42:49 +0800 Subject: [PATCH] UPSTREAM: : check kubeconfig only run-test and run-suite --- openshift/tests-extension/cmd/main.go | 25 ++++++++++++++++++- .../tests-extension/test/qe/util/framework.go | 7 ------ .../tests-extension/test/qe/util/init.go | 17 +++++++++++++ 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/openshift/tests-extension/cmd/main.go b/openshift/tests-extension/cmd/main.go index 421610928..1b89b4870 100644 --- a/openshift/tests-extension/cmd/main.go +++ b/openshift/tests-extension/cmd/main.go @@ -289,7 +289,30 @@ func main() { Long: "OLMv1 Tests Extension", } - root.AddCommand(cmd.DefaultExtensionCommands(registry)...) + // Get all default commands from the extension framework + allCommands := cmd.DefaultExtensionCommands(registry) + + // Add KUBECONFIG check to run-suite and run-test commands only. + // Other commands (list, info, images, update, completion, help) don't need KUBECONFIG. + for _, command := range allCommands { + // Identify run-suite and run-test commands by their Use field + if command.Use == "run-suite NAME" || command.Use == "run-test [-n NAME...] [NAME]" { + // Save the original RunE function + originalRunE := command.RunE + + // Wrap it with KUBECONFIG check + command.RunE = func(cmd *cobra.Command, args []string) error { + // Check KUBECONFIG before running the test + if err := exutil.CheckKubeconfigSet(); err != nil { + return err + } + // Call the original RunE function + return originalRunE(cmd, args) + } + } + } + + root.AddCommand(allCommands...) if err := func() error { return root.Execute() diff --git a/openshift/tests-extension/test/qe/util/framework.go b/openshift/tests-extension/test/qe/util/framework.go index 2593ccd86..d7c72ccfb 100644 --- a/openshift/tests-extension/test/qe/util/framework.go +++ b/openshift/tests-extension/test/qe/util/framework.go @@ -22,13 +22,6 @@ import ( testdata "github.com/openshift/operator-framework-operator-controller/openshift/tests-extension/pkg/bindata/qe" ) -func init() { - if KubeConfigPath() == "" { - fmt.Fprintf(os.Stderr, "Please set KUBECONFIG first!\n") - os.Exit(0) - } -} - // WaitForServiceAccount waits until the named service account gets fully // provisioned func WaitForServiceAccount(c corev1client.ServiceAccountInterface, name string, checkSecret bool) error { diff --git a/openshift/tests-extension/test/qe/util/init.go b/openshift/tests-extension/test/qe/util/init.go index 70d0254c4..8fb846555 100644 --- a/openshift/tests-extension/test/qe/util/init.go +++ b/openshift/tests-extension/test/qe/util/init.go @@ -9,6 +9,23 @@ import ( e2e "k8s.io/kubernetes/test/e2e/framework" ) +// CheckKubeconfigSet verifies that the KUBECONFIG environment variable is set and points to a valid file. +// This check is only required for commands that interact with a Kubernetes cluster (run-suite and run-test). +// Other commands (list, info, images, update, completion, help) do not require KUBECONFIG. +func CheckKubeconfigSet() error { + kubeconfig := os.Getenv("KUBECONFIG") + if kubeconfig == "" { + return fmt.Errorf("KUBECONFIG environment variable is not set.\nPlease set KUBECONFIG to point to your cluster configuration file.\nExample: export KUBECONFIG=/path/to/kubeconfig") + } + + // Check if kubeconfig file exists + if _, err := os.Stat(kubeconfig); err != nil { + return fmt.Errorf("KUBECONFIG file does not exist: %s (error: %v)", kubeconfig, err) + } + + return nil +} + // InitClusterEnv initializes the cluster environment for testing by setting up test framework and configuration // This function will panic if initialization fails func InitClusterEnv() {