2023-07-20 11:25:36 -04:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gotest.tools/v3/assert"
|
2024-05-17 04:59:54 -04:00
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2023-07-20 11:25:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetNaiveFlags(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
expectedFlags map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{"docker"},
|
|
|
|
expectedFlags: map[string]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"docker", "build", "-q", "--file", "test.Dockerfile", "."},
|
|
|
|
expectedFlags: map[string]string{
|
|
|
|
"q": "",
|
|
|
|
"file": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"docker", "--context", "a-context", "pull", "-q", "--progress", "auto", "alpine"},
|
|
|
|
expectedFlags: map[string]string{
|
|
|
|
"context": "",
|
|
|
|
"q": "",
|
|
|
|
"progress": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
assert.DeepEqual(t, getNaiveFlags(tc.args), tc.expectedFlags)
|
|
|
|
}
|
|
|
|
}
|
hooks: include full configured command
Before, for plugin commands, only the plugin name (such as `buildx`)
would be both included as `RootCmd` when passed to the hook plugin,
which isn't enough information for a plugin to decide whether to execute
a hook or not since plugins implement multiple varied commands (`buildx
build`, `buildx prune`, etc.).
This commit changes the hook logic to account for this situation, so
that the the entire configured hook is passed, i.e., if a user has a
hook configured for `buildx imagetools inspect` and the command
`docker buildx imagetools inspect alpine` is called, then the plugin
hooks will be passed `buildx imagetools inspect`.
This logic works for aliased commands too, so whether `docker build ...`
or `docker buildx build` is executed (unless Buildx is disabled) the
hook will be invoked with `buildx build`.
Signed-off-by: Laura Brehm <laurabrehm@hey.com>
hooks: include full match when invoking plugins
Signed-off-by: Laura Brehm <laurabrehm@hey.com>
2024-04-19 07:32:18 -04:00
|
|
|
|
|
|
|
func TestPluginMatch(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
commandString string
|
|
|
|
pluginConfig map[string]string
|
|
|
|
expectedMatch string
|
|
|
|
expectedOk bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
commandString: "image ls",
|
|
|
|
pluginConfig: map[string]string{
|
|
|
|
"hooks": "image",
|
|
|
|
},
|
|
|
|
expectedMatch: "image",
|
|
|
|
expectedOk: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
commandString: "context ls",
|
|
|
|
pluginConfig: map[string]string{
|
|
|
|
"hooks": "build",
|
|
|
|
},
|
|
|
|
expectedMatch: "",
|
|
|
|
expectedOk: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
commandString: "context ls",
|
|
|
|
pluginConfig: map[string]string{
|
|
|
|
"hooks": "context ls",
|
|
|
|
},
|
|
|
|
expectedMatch: "context ls",
|
|
|
|
expectedOk: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
commandString: "image ls",
|
|
|
|
pluginConfig: map[string]string{
|
|
|
|
"hooks": "image ls,image",
|
|
|
|
},
|
|
|
|
expectedMatch: "image ls",
|
|
|
|
expectedOk: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
commandString: "image ls",
|
|
|
|
pluginConfig: map[string]string{
|
|
|
|
"hooks": "",
|
|
|
|
},
|
|
|
|
expectedMatch: "",
|
|
|
|
expectedOk: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
commandString: "image inspect",
|
|
|
|
pluginConfig: map[string]string{
|
|
|
|
"hooks": "image i",
|
|
|
|
},
|
|
|
|
expectedMatch: "",
|
|
|
|
expectedOk: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
commandString: "image inspect",
|
|
|
|
pluginConfig: map[string]string{
|
|
|
|
"hooks": "image",
|
|
|
|
},
|
|
|
|
expectedMatch: "image",
|
|
|
|
expectedOk: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
match, ok := pluginMatch(tc.pluginConfig, tc.commandString)
|
|
|
|
assert.Equal(t, ok, tc.expectedOk)
|
|
|
|
assert.Equal(t, match, tc.expectedMatch)
|
|
|
|
}
|
|
|
|
}
|
2024-05-17 04:59:54 -04:00
|
|
|
|
|
|
|
func TestAppendNextSteps(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
processed []string
|
|
|
|
expectedOut []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
processed: []string{},
|
|
|
|
expectedOut: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
processed: []string{"", ""},
|
|
|
|
expectedOut: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
processed: []string{"Some hint", "", "Some other hint"},
|
|
|
|
expectedOut: []string{"Some hint", "", "Some other hint"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
processed: []string{"Hint 1", "Hint 2"},
|
|
|
|
expectedOut: []string{"Hint 1", "Hint 2"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
got, appended := appendNextSteps([]string{}, tc.processed)
|
|
|
|
assert.Check(t, is.DeepEqual(got, tc.expectedOut))
|
|
|
|
assert.Check(t, is.Equal(appended, len(got) > 0))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|