@@ -18,11 +18,8 @@ import (
18
18
"bytes"
19
19
"fmt"
20
20
"os"
21
- "os/exec"
22
21
"time"
23
22
24
- cmdError "github.com/operator-framework/operator-sdk/commands/operator-sdk/error"
25
-
26
23
"github.com/spf13/cobra"
27
24
v1 "k8s.io/api/core/v1"
28
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -31,16 +28,15 @@ import (
31
28
)
32
29
33
30
var (
34
- testNamespace string
35
- kubeconfigCluster string
36
- globalManifestPathCluster string
31
+ testNamespace string
32
+ kubeconfigCluster string
37
33
)
38
34
39
35
func NewTestClusterCmd () * cobra.Command {
40
36
testCmd := & cobra.Command {
41
37
Use : "cluster <image name> [flags]" ,
42
38
Short : "Run End-To-End tests using image with embedded test binary" ,
43
- Run : testClusterFunc ,
39
+ RunE : testClusterFunc ,
44
40
}
45
41
defaultKubeConfig := ""
46
42
homedir , ok := os .LookupEnv ("HOME" )
@@ -49,28 +45,13 @@ func NewTestClusterCmd() *cobra.Command {
49
45
}
50
46
testCmd .Flags ().StringVarP (& testNamespace , "namespace" , "n" , "default" , "Namespace to run tests in" )
51
47
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)" )
53
48
54
49
return testCmd
55
50
}
56
51
57
- func testClusterFunc (cmd * cobra.Command , args []string ) {
52
+ func testClusterFunc (cmd * cobra.Command , args []string ) error {
58
53
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\n Kubectl 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\n Kubectl Output: %v" , err , string (cmdOut )))
72
- }
73
- }()
54
+ return fmt .Errorf ("operator-sdk test cluster requires exactly 1 argument" )
74
55
}
75
56
testPod := & v1.Pod {
76
57
ObjectMeta : metav1.ObjectMeta {
@@ -92,43 +73,43 @@ func testClusterFunc(cmd *cobra.Command, args []string) {
92
73
}
93
74
kubeconfig , err := clientcmd .BuildConfigFromFlags ("" , kubeconfigCluster )
94
75
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 )
96
77
}
97
78
kubeclient , err := kubernetes .NewForConfig (kubeconfig )
98
79
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 )
100
81
}
101
82
testPod , err = kubeclient .CoreV1 ().Pods (testNamespace ).Create (testPod )
102
83
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 )
104
85
}
105
86
defer func () {
106
87
err = kubeclient .CoreV1 ().Pods (testNamespace ).Delete (testPod .Name , & metav1.DeleteOptions {})
107
88
if err != nil {
108
- cmdError . ExitWithError ( cmdError . ExitError , fmt .Errorf ( " failed to delete test pod") )
89
+ fmt .Printf ( "Warning: failed to delete test pod" )
109
90
}
110
91
}()
111
92
for {
112
93
testPod , err = kubeclient .CoreV1 ().Pods (testNamespace ).Get (testPod .Name , metav1.GetOptions {})
113
94
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 )
115
96
}
116
97
if testPod .Status .Phase != v1 .PodSucceeded && testPod .Status .Phase != v1 .PodFailed {
117
98
time .Sleep (time .Second * 5 )
118
99
continue
119
100
} else if testPod .Status .Phase == v1 .PodSucceeded {
120
101
fmt .Printf ("Test Successfully Completed\n " )
121
- return
102
+ return nil
122
103
} else if testPod .Status .Phase == v1 .PodFailed {
123
104
req := kubeclient .CoreV1 ().Pods (testNamespace ).GetLogs (testPod .Name , & v1.PodLogOptions {})
124
105
readCloser , err := req .Stream ()
125
106
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" )
127
108
}
128
109
defer readCloser .Close ()
129
110
buf := new (bytes.Buffer )
130
111
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 ())
132
113
}
133
114
}
134
115
}
0 commit comments