-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support execution of additional commands during ct lint (#283)
* support execution of additional commands for lint Given that helm unittest is installed locally or mounted into the chart testing container one could use this to run helm unittest for each chart. ``` additional-commands: - helm unittest --helm3 -f tests/*.yaml {{ .Path }} ``` The command is treated as a go template. Like this it's possible to get the path to the chart as in the example. It would also be open for further extension if needed. Signed-off-by: Torsten Walter <mail@torstenwalter.de> * Use sh to execute command Signed-off-by: Torsten Walter <mail@torstenwalter.de> Co-authored-by: Reinhard Nägele <unguiculus@gmail.com> * rename function to NewCmdTemplateExecutor Signed-off-by: Torsten Walter <mail@torstenwalter.de> * add error handling Signed-off-by: Torsten Walter <mail@torstenwalter.de> * add documentation Signed-off-by: Torsten Walter <mail@torstenwalter.de> * use go-shellwords to split rendered command Signed-off-by: Torsten Walter <mail@torstenwalter.de> * Update pkg/tool/cmdexecutor.go Signed-off-by: Torsten Walter <mail@torstenwalter.de> Co-authored-by: Reinhard Nägele <unguiculus@gmail.com> * add unit tests Signed-off-by: Torsten Walter <mail@torstenwalter.de> Co-authored-by: Reinhard Nägele <unguiculus@gmail.com>
- Loading branch information
1 parent
f5e0860
commit e4d7b78
Showing
9 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
FROM alpine:3.12 | ||
|
||
RUN apk --no-cache add \ | ||
bash \ | ||
curl \ | ||
git \ | ||
libc6-compat \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package tool | ||
|
||
import ( | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/mattn/go-shellwords" | ||
) | ||
|
||
type ProcessExecutor interface { | ||
RunProcess(executable string, execArgs ...interface{}) error | ||
} | ||
|
||
type CmdTemplateExecutor struct { | ||
exec ProcessExecutor | ||
} | ||
|
||
func NewCmdTemplateExecutor(exec ProcessExecutor) CmdTemplateExecutor { | ||
return CmdTemplateExecutor{ | ||
exec: exec, | ||
} | ||
} | ||
|
||
func (t CmdTemplateExecutor) RunCommand(cmdTemplate string, data interface{}) error { | ||
var template = template.Must(template.New("command").Parse(cmdTemplate)) | ||
var b strings.Builder | ||
if err := template.Execute(&b, data); err != nil { | ||
return err | ||
} | ||
rendered := b.String() | ||
|
||
words, err := shellwords.Parse(rendered) | ||
if err != nil { | ||
return err | ||
} | ||
name, args := words[0], words[1:] | ||
return t.exec.RunProcess(name, args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package tool | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type fakeProcessExecutor struct { | ||
mock.Mock | ||
} | ||
|
||
func (c *fakeProcessExecutor) RunProcess(executable string, execArgs ...interface{}) error { | ||
c.Called(executable, execArgs[0]) | ||
return nil | ||
} | ||
|
||
func TestCmdTemplateExecutor_RunCommand(t *testing.T) { | ||
type args struct { | ||
cmdTemplate string | ||
data interface{} | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
validate func(t *testing.T, executor *fakeProcessExecutor) | ||
}{ | ||
{ | ||
name: "command without arguments", | ||
args: args{ | ||
cmdTemplate: "echo", | ||
data: nil, | ||
}, | ||
validate: func(t *testing.T, executor *fakeProcessExecutor) { | ||
executor.AssertCalled(t, "RunProcess", "echo", []string{}) | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "command with args", | ||
args: args{ | ||
cmdTemplate: "echo hello world", | ||
}, | ||
validate: func(t *testing.T, executor *fakeProcessExecutor) { | ||
executor.AssertCalled(t, "RunProcess", "echo", []string{"hello", "world"}) | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "interpolate args", | ||
args: args{ | ||
cmdTemplate: "helm unittest --helm3 -f tests/*.yaml {{ .Path }}", | ||
data: map[string]string{"Path": "charts/my-chart"}, | ||
}, | ||
validate: func(t *testing.T, executor *fakeProcessExecutor) { | ||
executor.AssertCalled(t, "RunProcess", "helm", []string{"unittest", "--helm3", "-f", "tests/*.yaml", "charts/my-chart"}) | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
processExecutor := new(fakeProcessExecutor) | ||
processExecutor.On("RunProcess", mock.Anything, mock.Anything).Return(nil) | ||
templateExecutor := CmdTemplateExecutor{ | ||
exec: processExecutor, | ||
} | ||
if err := templateExecutor.RunCommand(tt.args.cmdTemplate, tt.args.data); (err != nil) != tt.wantErr { | ||
t.Errorf("RunCommand() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
tt.validate(t, processExecutor) | ||
|
||
}) | ||
} | ||
} |