test: refactor TestMetadataLegacyValidate to be more generic

Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
pull/31491/head
Benoit Tigeot 10 months ago
parent acf331a005
commit c81a09b89a
No known key found for this signature in database
GPG Key ID: 8E6D4FC8AEBDA62C

@ -17,33 +17,110 @@ package plugin
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestMetadataLegacyValidate_PluginName(t *testing.T) { func TestMetadataLegacyValidate(t *testing.T) {
tests := []struct { testsValid := map[string]MetadataLegacy{
name string "valid metadata": {
pluginName string Name: "myplugin",
shouldPass bool },
}{ "valid with command": {
{"valid name", "my-plugin", true}, Name: "myplugin",
{"invalid with space", "my plugin", false}, Command: "echo hello",
},
"valid with platformCommand": {
Name: "myplugin",
PlatformCommand: []PlatformCommand{
{OperatingSystem: "linux", Architecture: "amd64", Command: "echo hello"},
},
},
"valid with hooks": {
Name: "myplugin",
Hooks: Hooks{
"install": "echo install",
},
},
"valid with platformHooks": {
Name: "myplugin",
PlatformHooks: PlatformHooks{
"install": []PlatformCommand{
{OperatingSystem: "linux", Architecture: "amd64", Command: "echo install"},
},
},
},
"valid with downloaders": {
Name: "myplugin",
Downloaders: []Downloaders{
{
Protocols: []string{"myproto"},
Command: "echo download",
},
},
},
}
for testName, metadata := range testsValid {
t.Run(testName, func(t *testing.T) {
assert.NoError(t, metadata.Validate())
})
}
testsInvalid := map[string]MetadataLegacy{
"invalid name": {
Name: "my plugin", // further tested in TestValidPluginName
},
"both command and platformCommand": {
Name: "myplugin",
Command: "echo hello",
PlatformCommand: []PlatformCommand{
{OperatingSystem: "linux", Architecture: "amd64", Command: "echo hello"},
},
},
"both hooks and platformHooks": {
Name: "myplugin",
Hooks: Hooks{
"install": "echo install",
},
PlatformHooks: PlatformHooks{
"install": []PlatformCommand{
{OperatingSystem: "linux", Architecture: "amd64", Command: "echo install"},
},
},
},
"downloader with empty command": {
Name: "myplugin",
Downloaders: []Downloaders{
{
Protocols: []string{"myproto"},
Command: "",
},
},
},
"downloader with no protocols": {
Name: "myplugin",
Downloaders: []Downloaders{
{
Protocols: []string{},
Command: "echo download",
},
},
},
"downloader with empty protocol": {
Name: "myplugin",
Downloaders: []Downloaders{
{
Protocols: []string{""},
Command: "echo download",
},
},
},
} }
for _, tt := range tests { for testName, metadata := range testsInvalid {
t.Run(tt.name, func(t *testing.T) { t.Run(testName, func(t *testing.T) {
metadata := MetadataLegacy{ assert.Error(t, metadata.Validate())
Name: tt.pluginName,
Version: "1.0.0",
}
err := metadata.Validate()
if tt.shouldPass && err != nil {
t.Errorf("expected %q to be valid, got error: %v", tt.pluginName, err)
}
if !tt.shouldPass && err == nil {
t.Errorf("expected %q to be invalid, but passed", tt.pluginName)
}
}) })
} }
} }

Loading…
Cancel
Save