This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cobra_call.go
73 lines (63 loc) · 1.79 KB
/
cobra_call.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package cliscenario
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/cloud-barista/poc-cicd-ladybug/src/grpc-api/cbadm/cmd"
"github.com/stretchr/testify/assert"
)
func LadybugCmdTest(t *testing.T, tc TestCases) (string, error) {
var (
res string = ""
err error = nil
)
t.Run(tc.Name, func(t *testing.T) {
ladybugCmd := cmd.NewRootCmd()
b := bytes.NewBufferString("")
e := bytes.NewBufferString("")
ladybugCmd.SetOut(b)
ladybugCmd.SetErr(e)
ladybugCmd.SetArgs(tc.CmdArgs)
ladybugCmd.Execute()
out, err := ioutil.ReadAll(e)
if assert.NoError(t, err) && string(out) == "" {
out, err = ioutil.ReadAll(b)
}
if assert.NoError(t, err) {
if strings.HasPrefix(string(out), "{") {
dst := new(bytes.Buffer)
err = json.Compact(dst, out)
assert.NoError(t, err)
res = dst.String()
} else {
res = string(out)
}
if tc.ExpectResStartsWith != "" {
if !assert.True(t, strings.HasPrefix(res, tc.ExpectResStartsWith)) {
fmt.Fprintf(os.Stderr, "\n Not Equal: \n"+
" Expected Start With: %s\n"+
" Actual : %s\n", tc.ExpectResStartsWith, res)
}
}
if tc.ExpectResContains != "" {
if !assert.True(t, strings.Contains(res, tc.ExpectResContains)) {
fmt.Fprintf(os.Stderr, "\n Not Equal: \n"+
" Expected Contains: %s\n"+
" Actual : %s\n", tc.ExpectResContains, res)
}
}
if tc.ExpectResStartsWith == "" && tc.ExpectResContains == "" {
if !assert.True(t, "" == res) {
fmt.Fprintf(os.Stderr, "\n Not Equal: \n"+
" Expected StartWith/Contains: %s\n"+
" Actual : %s\n", tc.ExpectResStartsWith, res)
}
}
}
})
return res, err
}