Skip to content

Commit 2790687

Browse files
committed
commands/.../test/*: make the tests work and improve output
1 parent becee99 commit 2790687

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package cmdtest
1616

1717
import (
18+
"bytes"
1819
"fmt"
1920
"os"
2021
"os/exec"
@@ -54,12 +55,22 @@ func NewTestClusterCmd() *cobra.Command {
5455
}
5556

5657
func testClusterFunc(cmd *cobra.Command, args []string) {
58+
if len(args) != 1 {
59+
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("operator-sdk test cluster requires exactly 1 argument"))
60+
}
5761
if globalManifestPathCluster != "" {
5862
globalCmd := exec.Command("kubectl", "create", "-f", globalManifestPathCluster)
5963
cmdOut, err := globalCmd.CombinedOutput()
6064
if err != nil {
61-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("could not create global resources: %v\nKubectl Output: %v", err, cmdOut))
65+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("could not create global resources: %v\nKubectl Output: %v", err, string(cmdOut)))
6266
}
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+
}()
6374
}
6475
testPod := &v1.Pod{
6576
ObjectMeta: metav1.ObjectMeta{
@@ -91,19 +102,33 @@ func testClusterFunc(cmd *cobra.Command, args []string) {
91102
if err != nil {
92103
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to create test pod: %v", err))
93104
}
105+
defer func() {
106+
err = kubeclient.CoreV1().Pods(testNamespace).Delete(testPod.Name, &metav1.DeleteOptions{})
107+
if err != nil {
108+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to delete test pod"))
109+
}
110+
}()
94111
for {
95-
testPod, err = kubeclient.CoreV1().Pods(testNamespace).Get("operator-test", metav1.GetOptions{})
112+
testPod, err = kubeclient.CoreV1().Pods(testNamespace).Get(testPod.Name, metav1.GetOptions{})
96113
if err != nil {
97114
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to get test pod: %v", err))
98115
}
99116
if testPod.Status.Phase != v1.PodSucceeded && testPod.Status.Phase != v1.PodFailed {
100117
time.Sleep(time.Second * 5)
101118
continue
102119
} else if testPod.Status.Phase == v1.PodSucceeded {
103-
fmt.Printf("Test Successfully Completed")
120+
fmt.Printf("Test Successfully Completed\n")
104121
return
105122
} else if testPod.Status.Phase == v1.PodFailed {
106-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("test Failed: %+v", kubeclient.CoreV1().Pods(testNamespace).GetLogs("operator-test", &v1.PodLogOptions{})))
123+
req := kubeclient.CoreV1().Pods(testNamespace).GetLogs(testPod.Name, &v1.PodLogOptions{})
124+
readCloser, err := req.Stream()
125+
if err != nil {
126+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("test failed and failed to get error logs"))
127+
}
128+
defer readCloser.Close()
129+
buf := new(bytes.Buffer)
130+
buf.ReadFrom(readCloser)
131+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("test failed:\n%+v", buf.String()))
107132
}
108133
}
109134
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ func NewTestLocalCmd() *cobra.Command {
5555
}
5656

5757
func testLocalFunc(cmd *cobra.Command, args []string) {
58+
if len(args) != 1 {
59+
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("operator-sdk test local requires exactly 1 argument"))
60+
}
5861
// if no namespaced manifest path is given, combine deploy/rbac.yaml and deploy/operator.yaml
5962
if namespacedManifestPath == "" {
6063
os.Mkdir("deploy/test", os.FileMode(int(0775)))

0 commit comments

Comments
 (0)