44 "bytes"
55 "os"
66 "os/exec"
7+ "strconv"
78 "strings"
89 "testing"
910)
@@ -13,18 +14,46 @@ type Utility struct {
1314 ID string // The resource ID created by a step, if needed
1415}
1516
16- type Step struct {
17- Name string
18- Run func ( * Utility )
19- Deferred bool // If true, run only at the end as cleanup
17+ type Step interface {
18+ Run ( u * Utility )
19+ Name () string
20+ Deferred () bool
2021}
2122
22- type TestCase struct {
23+ // CLITest uses []Step interface now
24+ type CLITest struct {
2325 Steps []Step
2426}
2527
28+ func (tc * CLITest ) Run (t * testing.T ) {
29+ util := & Utility {T : t }
30+ // Run non-deferred steps
31+ for i , step := range tc .Steps {
32+ if step .Deferred () {
33+ continue
34+ }
35+ t .Run (step .Name ()+ "_" + strconv .Itoa (i ), func (t * testing.T ) {
36+ util .T = t
37+ step .Run (util )
38+ })
39+ }
40+ // Run deferred steps
41+ for i , step := range tc .Steps {
42+ if ! step .Deferred () {
43+ continue
44+ }
45+ t .Run (step .Name ()+ "_" + strconv .Itoa (i ), func (t * testing.T ) {
46+ util .T = t
47+ step .Run (util )
48+ })
49+ }
50+ }
51+
2652// Run executes the CLI using 'go run main.go' from the ./src directory with the given arguments and optional stdin, returning combined output and error.
2753func (u * Utility ) Run (args string , stdin ... string ) (string , error ) {
54+ // TODO: need to allow using the pre-built binary
55+ //cmd := exec.Command("opslevel", strings.Split(args, " ")...)
56+
2857 cmd := exec .Command ("go" , append ([]string {"run" , "main.go" }, strings .Split (args , " " )... )... )
2958 cmd .Dir = ".."
3059 cmd .Env = os .Environ ()
@@ -39,97 +68,107 @@ func (u *Utility) Run(args string, stdin ...string) (string, error) {
3968 return out .String () + errBuf .String (), err
4069}
4170
42- func (tc * TestCase ) Run (t * testing.T ) {
43- util := & Utility {T : t }
44- // Run non-deferred steps
45- for _ , step := range tc .Steps {
46- if step .Deferred {
47- continue
48- }
49- t .Run (step .Name , func (t * testing.T ) {
50- util .T = t
51- step .Run (util )
52- })
71+ // Create step
72+ type Create struct {
73+ Cmd string
74+ Input string
75+ }
76+
77+ func (s Create ) Run (u * Utility ) {
78+ out , err := u .Run (s .Cmd + " -f -" , s .Input )
79+ if err != nil {
80+ panic ("create failed: " + err .Error () + "\n out: " + out )
5381 }
54- // Run deferred steps
55- for _ , step := range tc .Steps {
56- if ! step .Deferred {
57- continue
58- }
59- t .Run (step .Name , func (t * testing.T ) {
60- util .T = t
61- step .Run (util )
62- })
82+ u .ID = strings .TrimRight (strings .TrimLeft (strings .TrimSpace (out ), "\" " ), "\" " )
83+ if u .ID == "" {
84+ panic ("expected ID, got: " + out )
6385 }
6486}
6587
66- func Create ( cmd string , input string ) Step {
67- return Step {
68- Name : "Create" ,
69- Run : func ( u * Utility ) {
70- out , err := u . Run ( cmd , input )
71- if err != nil {
72- u . Fatalf ( "create failed: %v \n out: %s" , err , out )
73- }
74- u . ID = strings . TrimSpace ( out )
75- if u . ID == "" {
76- u . Fatalf ( "expected ID, got: %q" , out )
77- }
78- },
88+ func ( s Create ) Name () string { return "Create" }
89+ func ( s Create ) Deferred () bool { return false }
90+
91+ // Get step
92+ type Get struct {
93+ Cmd string
94+ Validate func ( u * Utility , out string )
95+ }
96+
97+ func ( s Get ) Run ( u * Utility ) {
98+ out , err := u . Run ( s . Cmd + " " + u . ID )
99+ if err != nil {
100+ u . Fatalf ( "get failed: %v \n out: %s" , err , out )
79101 }
102+ s .Validate (u , out )
103+ }
104+
105+ func (s Get ) Name () string { return "Get" }
106+ func (s Get ) Deferred () bool { return false }
107+
108+ // List step
109+ type List struct {
110+ Cmd string
111+ Validate func (u * Utility , out string )
80112}
81113
82- func Delete (cmd string ) Step {
83- return Step {
84- Name : "Delete" ,
85- Deferred : true ,
86- Run : func (u * Utility ) {
87- out , err := u .Run (cmd + " " + u .ID )
88- if err != nil {
89- u .Fatalf ("delete failed: %v\n out: %s" , err , out )
90- }
91- },
114+ func (s List ) Run (u * Utility ) {
115+ out , err := u .Run (s .Cmd )
116+ if err != nil {
117+ u .Fatalf ("list failed: %v\n out: %s" , err , out )
92118 }
119+ if s .Validate != nil {
120+ s .Validate (u , out )
121+ }
122+ }
123+
124+ func (s List ) Name () string { return "List" }
125+ func (s List ) Deferred () bool { return false }
126+
127+ // Update step
128+ type Update struct {
129+ Cmd string
130+ Input string
131+ Validate func (u * Utility , out string )
93132}
94133
95- // Get returns a Step that runs the get command and validates the output using the provided function.
96- func Get (cmd string , validate func (u * Utility , stdout string )) Step {
97- return Step {
98- Name : "Get" ,
99- Run : func (u * Utility ) {
100- out , err := u .Run (cmd + " " + u .ID )
101- if err != nil {
102- u .Fatalf ("get failed: %v\n out: %s" , err , out )
103- }
104- validate (u , out )
105- },
134+ func (s Update ) Run (u * Utility ) {
135+ out , err := u .Run (s .Cmd + " -f - " + u .ID , s .Input )
136+ if err != nil {
137+ u .Fatalf ("update failed: %v\n out: %s" , err , out )
138+ }
139+ if s .Validate != nil {
140+ s .Validate (u , out )
106141 }
107142}
108143
109- // List returns a Step that runs the list command and validates the output using the provided function.
110- func List ( cmd string , validate func ( u * Utility , stdout string )) Step {
111- return Step {
112- Name : "List" ,
113- Run : func ( u * Utility ) {
114- out , err := u . Run ( cmd )
115- if err != nil {
116- u . Fatalf ( "list failed: %v \n out: %s" , err , out )
117- }
118- validate ( u , out )
119- },
144+ func ( s Update ) Name () string { return "Update" }
145+ func ( s Update ) Deferred () bool { return false }
146+
147+ type Delete struct {
148+ Cmd string
149+ }
150+
151+ func ( s Delete ) Run ( u * Utility ) {
152+ out , err := u . Run ( s . Cmd + " " + u . ID )
153+ if err != nil {
154+ u . Fatalf ( "delete failed: %v \n out: %s" , err , out )
120155 }
121156}
122157
123- // Update returns a Step that runs the update command with input and validates the output using the provided function.
124- func Update (cmd string , input string , validate func (u * Utility , stdout string )) Step {
125- return Step {
126- Name : "Update" ,
127- Run : func (u * Utility ) {
128- out , err := u .Run (cmd + " " + u .ID + " -f -" , input )
129- if err != nil {
130- u .Fatalf ("update failed: %v\n out: %s" , err , out )
131- }
132- validate (u , out )
133- },
158+ func (s Delete ) Name () string { return "Delete" }
159+ func (s Delete ) Deferred () bool { return true }
160+
161+ type Missing struct {
162+ Cmd string
163+ }
164+
165+ func (s Missing ) Run (u * Utility ) {
166+ out , err := u .Run (s .Cmd + " " + u .ID )
167+ lower := strings .ToLower (out )
168+ if err == nil || ! (strings .Contains (lower , "not found" ) || strings .Contains (lower , "missing" ) || strings .Contains (lower , "does not exist on this account" )) {
169+ u .Fatalf ("expected get after delete to fail with not found, got: %v\n out: %s" , err , out )
134170 }
135171}
172+
173+ func (s Missing ) Name () string { return "Missing" }
174+ func (s Missing ) Deferred () bool { return true }
0 commit comments