Skip to content

Commit fc47afb

Browse files
committed
Fix execute action and command parsing
1 parent 13eb9cf commit fc47afb

4 files changed

Lines changed: 89 additions & 4 deletions

File tree

conf/parser/factory/actions.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ func (f *NativeActionsFactory) parseAction(
147147
benchAction := &types.BenchAction{Service: meta.serviceName}
148148
err = f.structParser(data, benchAction, path)
149149
action = benchAction
150+
case "execute":
151+
executeAction := &types.ExecuteAction{Service: meta.serviceName}
152+
err = f.structParser(data, executeAction, path)
153+
action = executeAction
150154
case "expect":
151155
customNameAllowed = true
152156
action, err = f.parseExpectationAction(meta, data, path)

conf/parser/factory/actions_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,27 @@ func TestNativeActionsFactory_ParseActions(t *testing.T) {
9191
},
9292
wantErr: false,
9393
},
94+
{
95+
name: "Valid execute action",
96+
actions: []interface{}{
97+
"execute/serviceName",
98+
},
99+
mockParseCalls: []struct {
100+
data map[string]interface{}
101+
path string
102+
err error
103+
}{
104+
{
105+
data: map[string]interface{}{},
106+
path: staticPath,
107+
err: nil,
108+
},
109+
},
110+
want: []types.Action{
111+
&types.ExecuteAction{Service: "serviceName"},
112+
},
113+
wantErr: false,
114+
},
94115
{
95116
name: "Invalid action format - too many elements",
96117
actions: []interface{}{

conf/parser/factory/functions.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,20 @@ func (f *FuncProvider) createCommand(data interface{}, fieldValue reflect.Value,
110110
var command types.Command
111111
switch v := data.(type) {
112112
case string:
113-
command = types.ShellCommand{
113+
command = &types.ShellCommand{
114114
Command: v,
115115
}
116-
case []string:
117-
command = types.ArgsCommand{
118-
Args: v,
116+
case []interface{}:
117+
args := make([]string, 0, len(v))
118+
for _, item := range v {
119+
if str, ok := item.(string); ok {
120+
args = append(args, str)
121+
} else {
122+
return errors.Errorf("invalid command data at %s", f.loc.String())
123+
}
124+
}
125+
command = &types.ArgsCommand{
126+
Args: args,
119127
}
120128
default:
121129
return errors.Errorf("unsupported type for command data at %s", f.loc.String())

conf/parser/factory/functions_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,58 @@ func TestFuncProvider_GetFactoryFunc(t *testing.T) {
158158
wantErr: true,
159159
errMsg: "data must be an array, got int",
160160
},
161+
// Command
162+
{
163+
name: "createCommand with string shell command",
164+
funcName: "createCommand",
165+
data: "echo hello world",
166+
expectedValue: &types.ShellCommand{
167+
Command: "echo hello world",
168+
},
169+
wantErr: false,
170+
},
171+
{
172+
name: "createCommand with args array",
173+
funcName: "createCommand",
174+
data: []interface{}{"ls", "-la", "/tmp"},
175+
expectedValue: &types.ArgsCommand{
176+
Args: []string{"ls", "-la", "/tmp"},
177+
},
178+
wantErr: false,
179+
},
180+
{
181+
name: "createCommand with args array containing non-string element",
182+
funcName: "createCommand",
183+
data: []interface{}{"ls", 123, "/tmp"},
184+
expectedValue: &types.ShellCommand{}, // Use a concrete type that implements Command
185+
wantErr: true,
186+
errMsg: "invalid command data",
187+
},
188+
{
189+
name: "createCommand with invalid data type",
190+
funcName: "createCommand",
191+
data: 123, // Invalid type for command data
192+
expectedValue: &types.ArgsCommand{}, // Use a concrete type that implements Command
193+
wantErr: true,
194+
errMsg: "unsupported type for command data",
195+
},
196+
{
197+
name: "createCommand with empty args array",
198+
funcName: "createCommand",
199+
data: []interface{}{},
200+
expectedValue: &types.ArgsCommand{
201+
Args: []string{},
202+
},
203+
wantErr: false,
204+
},
205+
{
206+
name: "createCommand with map data",
207+
funcName: "createCommand",
208+
data: map[string]interface{}{"command": "echo hello"},
209+
expectedValue: &types.ShellCommand{}, // Use a concrete type that implements Command
210+
wantErr: true,
211+
errMsg: "unsupported type for command data",
212+
},
161213
// Container image
162214
{
163215
name: "createContainerImage with name and tag",

0 commit comments

Comments
 (0)