Skip to content

Commit 56bd7cf

Browse files
Merge pull request #507 from kuiwang02/enhancekubecheck
NO-ISSUE: UPSTREAM: <carry>: check kubeconfig only run-test and run-suite
2 parents 25e39ce + eff1b24 commit 56bd7cf

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

openshift/tests-extension/cmd/main.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,30 @@ func main() {
289289
Long: "OLMv1 Tests Extension",
290290
}
291291

292-
root.AddCommand(cmd.DefaultExtensionCommands(registry)...)
292+
// Get all default commands from the extension framework
293+
allCommands := cmd.DefaultExtensionCommands(registry)
294+
295+
// Add KUBECONFIG check to run-suite and run-test commands only.
296+
// Other commands (list, info, images, update, completion, help) don't need KUBECONFIG.
297+
for _, command := range allCommands {
298+
// Identify run-suite and run-test commands by their Use field
299+
if command.Use == "run-suite NAME" || command.Use == "run-test [-n NAME...] [NAME]" {
300+
// Save the original RunE function
301+
originalRunE := command.RunE
302+
303+
// Wrap it with KUBECONFIG check
304+
command.RunE = func(cmd *cobra.Command, args []string) error {
305+
// Check KUBECONFIG before running the test
306+
if err := exutil.CheckKubeconfigSet(); err != nil {
307+
return err
308+
}
309+
// Call the original RunE function
310+
return originalRunE(cmd, args)
311+
}
312+
}
313+
}
314+
315+
root.AddCommand(allCommands...)
293316

294317
if err := func() error {
295318
return root.Execute()

openshift/tests-extension/test/qe/util/framework.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ import (
2222
testdata "github.com/openshift/operator-framework-operator-controller/openshift/tests-extension/pkg/bindata/qe"
2323
)
2424

25-
func init() {
26-
if KubeConfigPath() == "" {
27-
fmt.Fprintf(os.Stderr, "Please set KUBECONFIG first!\n")
28-
os.Exit(0)
29-
}
30-
}
31-
3225
// WaitForServiceAccount waits until the named service account gets fully
3326
// provisioned
3427
func WaitForServiceAccount(c corev1client.ServiceAccountInterface, name string, checkSecret bool) error {

openshift/tests-extension/test/qe/util/init.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ import (
99
e2e "k8s.io/kubernetes/test/e2e/framework"
1010
)
1111

12+
// CheckKubeconfigSet verifies that the KUBECONFIG environment variable is set and points to a valid file.
13+
// This check is only required for commands that interact with a Kubernetes cluster (run-suite and run-test).
14+
// Other commands (list, info, images, update, completion, help) do not require KUBECONFIG.
15+
func CheckKubeconfigSet() error {
16+
kubeconfig := os.Getenv("KUBECONFIG")
17+
if kubeconfig == "" {
18+
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")
19+
}
20+
21+
// Check if kubeconfig file exists
22+
if _, err := os.Stat(kubeconfig); err != nil {
23+
return fmt.Errorf("KUBECONFIG file does not exist: %s (error: %v)", kubeconfig, err)
24+
}
25+
26+
return nil
27+
}
28+
1229
// InitClusterEnv initializes the cluster environment for testing by setting up test framework and configuration
1330
// This function will panic if initialization fails
1431
func InitClusterEnv() {

0 commit comments

Comments
 (0)