-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatcher-exec.go
More file actions
147 lines (128 loc) · 4.06 KB
/
matcher-exec.go
File metadata and controls
147 lines (128 loc) · 4.06 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package arp
import (
"encoding/json"
"errors"
"fmt"
"os/exec"
"reflect"
"strings"
)
type ExecutableMatcher struct {
ReturnCode *int
Cmd string
BinPath string
PrgmArgs []string
FieldMatcherProps
}
func (m *ExecutableMatcher) Parse(parentNode interface{}, node map[interface{}]interface{}) error {
// expected return value of the programs execution
if code, ok := node[TEST_EXEC_KEY_RETURN_CODE]; ok {
if codeInt, cOk := code.(int); cOk {
m.ReturnCode = &codeInt
} else {
return errors.New(ObjectPrintf(fmt.Sprintf(MalformedDefinitionFmt, TEST_EXEC_KEY_RETURN_CODE, TYPE_INT), parentNode))
}
}
// One-liner command (same as with dynamic inputs)
if cmdStr, ok := node[TEST_EXEC_KEY_CMD]; ok {
if s, sOk := cmdStr.(string); sOk {
m.Cmd = s
fmt.Printf("Got command: %v\n", m.Cmd)
} else {
return errors.New(ObjectPrintf(fmt.Sprintf(MalformedDefinitionFmt, TEST_EXEC_KEY_CMD, TYPE_STR), parentNode))
}
} else {
//Otherwise, if no cmd string is provided, fall back to the split binary argument syntax
// path of the program to execute
if binPath, ok := node[TEST_EXEC_KEY_BIN_PATH]; ok {
if p, pOk := binPath.(string); pOk {
m.BinPath = p
} else {
return errors.New(ObjectPrintf(fmt.Sprintf(MalformedDefinitionFmt, TEST_EXEC_KEY_BIN_PATH, TYPE_STR), parentNode))
}
}
// collect the arguments to run
if prgmArgs, ok := node[TEST_EXEC_KEY_ARGS]; ok {
if args, aOk := prgmArgs.([]interface{}); aOk {
for _, a := range args {
if curArg, cAOk := a.(string); cAOk {
m.PrgmArgs = append(m.PrgmArgs, curArg)
} else {
return errors.New(ObjectPrintf(fmt.Sprintf(MalformedDefinitionFmt, TEST_EXEC_KEY_ARGS, TYPE_STR), parentNode))
}
}
} else {
return errors.New(ObjectPrintf(fmt.Sprintf(MalformedDefinitionFmt, TEST_EXEC_KEY_ARGS, TYPE_ARRAY), parentNode))
}
}
}
return m.ParseProps(node)
}
func (m *ExecutableMatcher) Match(responseValue interface{}, datastore *DataStore) (bool, DataStore, error) {
store := NewDataStore()
m.ErrorStr = ""
// immediately store value into datastore so it can be resolved as a variable for program inputs
// When we fetch the values from data store, they will be left as an object or converted to string depending
// on the context (is it being embedded in a string vs standalone)
if m.DSName != "" {
if err := (*datastore).PutVariable(m.DSName, responseValue); err != nil {
return false, store, err
}
}
var status bool
if m.Cmd == "" {
resolvedBinPath, err := datastore.ExpandVariable(m.BinPath)
if err != nil {
return false, store, fmt.Errorf(BadVarMatcherFmt, m.BinPath)
}
// resolve variables in the program
resolvedArgs, argErr := datastore.RecursiveResolveVariables(m.PrgmArgs)
if argErr != nil {
return false, store, fmt.Errorf(BadVarMatcherFmt, m.PrgmArgs)
}
argArray, aOk := resolvedArgs.([]interface{})
if !aOk {
m.ErrorStr = fmt.Sprintf(MismatchedMatcher, TYPE_ARRAY, reflect.TypeOf(resolvedArgs))
return false, store, nil
}
var argStrings []string
for _, aA := range argArray {
if s, isStr := aA.(string); isStr {
argStrings = append(argStrings, s)
} else {
b, _ := json.Marshal(aA)
argStrings = append(argStrings, string(b))
}
}
status := true
cmd := exec.Command(resolvedBinPath.(string), argStrings...)
result, err := cmd.CombinedOutput()
sanitizedResult := string(result)
if m.ReturnCode != nil {
status = *m.ReturnCode == cmd.ProcessState.ExitCode()
}
if !status && err != nil {
m.ErrorStr = fmt.Sprintf("[%v]\n %v", err.Error(), sanitizedResult)
status = false
} else {
m.ErrorStr = sanitizedResult
}
} else {
resolvedCmd, err := datastore.ExpandVariable(m.Cmd)
if err != nil {
return false, store, fmt.Errorf(BadVarMatcherFmt, m.Cmd)
}
status = true
result, err := ExecuteCommand(resolvedCmd.(string))
if err != nil {
status = false
m.ErrorStr = fmt.Sprintf("[%v]\n %v", err, result)
} else {
m.ErrorStr = strings.TrimSpace(result.(string))
if m.ErrorStr == "" {
m.ErrorStr = "[status 0]"
}
}
}
return status, store, nil
}