Skip to content

Commit 49fc1c1

Browse files
committed
commands/.../test/cluster.go: return errors instead of cmderror
The cluster test has defers that need to run on error, which would not run if cmdError is used due to its use of os.Exit(). Instead, we can just return an error, which allows the defer functions to run.
1 parent 2790687 commit 49fc1c1

File tree

1 file changed

+13
-32
lines changed

1 file changed

+13
-32
lines changed

commands/operator-sdk/cmd/test/cluster.go

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ import (
1818
"bytes"
1919
"fmt"
2020
"os"
21-
"os/exec"
2221
"time"
2322

24-
cmdError "github.com/operator-framework/operator-sdk/commands/operator-sdk/error"
25-
2623
"github.com/spf13/cobra"
2724
v1 "k8s.io/api/core/v1"
2825
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -31,16 +28,15 @@ import (
3128
)
3229

3330
var (
34-
testNamespace string
35-
kubeconfigCluster string
36-
globalManifestPathCluster string
31+
testNamespace string
32+
kubeconfigCluster string
3733
)
3834

3935
func NewTestClusterCmd() *cobra.Command {
4036
testCmd := &cobra.Command{
4137
Use: "cluster <image name> [flags]",
4238
Short: "Run End-To-End tests using image with embedded test binary",
43-
Run: testClusterFunc,
39+
RunE: testClusterFunc,
4440
}
4541
defaultKubeConfig := ""
4642
homedir, ok := os.LookupEnv("HOME")
@@ -49,28 +45,13 @@ func NewTestClusterCmd() *cobra.Command {
4945
}
5046
testCmd.Flags().StringVarP(&testNamespace, "namespace", "n", "default", "Namespace to run tests in")
5147
testCmd.Flags().StringVarP(&kubeconfigCluster, "kubeconfig", "k", defaultKubeConfig, "Kubeconfig path")
52-
testCmd.Flags().StringVarP(&globalManifestPathCluster, "global-init", "g", "", "Path to manifest for Global resources (e.g. CRD manifest)")
5348

5449
return testCmd
5550
}
5651

57-
func testClusterFunc(cmd *cobra.Command, args []string) {
52+
func testClusterFunc(cmd *cobra.Command, args []string) error {
5853
if len(args) != 1 {
59-
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("operator-sdk test cluster requires exactly 1 argument"))
60-
}
61-
if globalManifestPathCluster != "" {
62-
globalCmd := exec.Command("kubectl", "create", "-f", globalManifestPathCluster)
63-
cmdOut, err := globalCmd.CombinedOutput()
64-
if err != nil {
65-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("could not create global resources: %v\nKubectl Output: %v", err, string(cmdOut)))
66-
}
67-
defer func() {
68-
globalCmd := exec.Command("kubectl", "delete", "-f", globalManifestPathCluster)
69-
cmdOut, err := globalCmd.CombinedOutput()
70-
if err != nil {
71-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("could not delete global resources: %v\nKubectl Output: %v", err, string(cmdOut)))
72-
}
73-
}()
54+
return fmt.Errorf("operator-sdk test cluster requires exactly 1 argument")
7455
}
7556
testPod := &v1.Pod{
7657
ObjectMeta: metav1.ObjectMeta{
@@ -92,43 +73,43 @@ func testClusterFunc(cmd *cobra.Command, args []string) {
9273
}
9374
kubeconfig, err := clientcmd.BuildConfigFromFlags("", kubeconfigCluster)
9475
if err != nil {
95-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to get kubeconfig: %v", err))
76+
return fmt.Errorf("failed to get kubeconfig: %v", err)
9677
}
9778
kubeclient, err := kubernetes.NewForConfig(kubeconfig)
9879
if err != nil {
99-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to create kubeclient: %v", err))
80+
return fmt.Errorf("failed to create kubeclient: %v", err)
10081
}
10182
testPod, err = kubeclient.CoreV1().Pods(testNamespace).Create(testPod)
10283
if err != nil {
103-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to create test pod: %v", err))
84+
return fmt.Errorf("failed to create test pod: %v", err)
10485
}
10586
defer func() {
10687
err = kubeclient.CoreV1().Pods(testNamespace).Delete(testPod.Name, &metav1.DeleteOptions{})
10788
if err != nil {
108-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to delete test pod"))
89+
fmt.Printf("Warning: failed to delete test pod")
10990
}
11091
}()
11192
for {
11293
testPod, err = kubeclient.CoreV1().Pods(testNamespace).Get(testPod.Name, metav1.GetOptions{})
11394
if err != nil {
114-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to get test pod: %v", err))
95+
return fmt.Errorf("failed to get test pod: %v", err)
11596
}
11697
if testPod.Status.Phase != v1.PodSucceeded && testPod.Status.Phase != v1.PodFailed {
11798
time.Sleep(time.Second * 5)
11899
continue
119100
} else if testPod.Status.Phase == v1.PodSucceeded {
120101
fmt.Printf("Test Successfully Completed\n")
121-
return
102+
return nil
122103
} else if testPod.Status.Phase == v1.PodFailed {
123104
req := kubeclient.CoreV1().Pods(testNamespace).GetLogs(testPod.Name, &v1.PodLogOptions{})
124105
readCloser, err := req.Stream()
125106
if err != nil {
126-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("test failed and failed to get error logs"))
107+
return fmt.Errorf("test failed and failed to get error logs")
127108
}
128109
defer readCloser.Close()
129110
buf := new(bytes.Buffer)
130111
buf.ReadFrom(readCloser)
131-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("test failed:\n%+v", buf.String()))
112+
return fmt.Errorf("test failed:\n%+v", buf.String())
132113
}
133114
}
134115
}

0 commit comments

Comments
 (0)