test: convert tests to table drive tests

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

@ -269,60 +269,95 @@ func TestFindPlugins(t *testing.T) {
} }
} }
func TestLoadMetadataLegacy_CapitalNameField(t *testing.T) { func TestLoadMetadataLegacy(t *testing.T) {
yamlWithCapitalName := `Name: my-plugin testCases := map[string]struct {
yaml string
expectError bool
errorContains string
expectedName string
logNote string
}{
"capital name field": {
yaml: `Name: my-plugin
version: 1.0.0 version: 1.0.0
usage: test plugin usage: test plugin
description: test description description: test description
command: echo test` command: echo test`,
expectError: true,
_, err := loadMetadataLegacy([]byte(yamlWithCapitalName)) errorContains: `invalid plugin name "": must contain only a-z, A-Z, 0-9, _ and -`,
// Legacy plugins: No strict unmarshalling (backwards compatibility)
// Legacy plugins: No strict unmarshalling (backwards compatibility) // YAML decoder silently ignores "Name:", then validation catches empty name
// YAML decoder silently ignores "Name:", then validation catches empty name logNote: "NOTE: V1 plugins use strict unmarshalling and would get: yaml: field Name not found",
require.Error(t, err, "expected error for capital 'Name:' field") },
assert.Contains(t, err.Error(), "invalid plugin name \"\": must contain only a-z, A-Z, 0-9, _ and -") "correct name field": {
yaml: `name: my-plugin
t.Logf("Legacy error (validation catches empty name): %v", err)
t.Log("NOTE: V1 plugins use strict unmarshalling and would get: yaml: field Name not found")
}
func TestLoadMetadataLegacy_CorrectField(t *testing.T) {
yamlWithCorrectName := `name: my-plugin
version: 1.0.0 version: 1.0.0
usage: test plugin usage: test plugin
description: test description description: test description
command: echo test` command: echo test`,
expectError: false,
m, err := loadMetadataLegacy([]byte(yamlWithCorrectName)) expectedName: "my-plugin",
},
}
require.NoError(t, err, "expected success for correct 'name:' field") for name, tc := range testCases {
assert.Equal(t, "my-plugin", m.Name) t.Run(name, func(t *testing.T) {
m, err := loadMetadataLegacy([]byte(tc.yaml))
if tc.expectError {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.errorContains)
t.Logf("Legacy error (validation catches empty name): %v", err)
if tc.logNote != "" {
t.Log(tc.logNote)
}
} else {
require.NoError(t, err)
assert.Equal(t, tc.expectedName, m.Name)
}
})
}
} }
func TestLoadMetadataV1_CapitalNameField(t *testing.T) { func TestLoadMetadataV1(t *testing.T) {
yamlWithCapitalName := `apiVersion: v1 testCases := map[string]struct {
yaml string
expectError bool
errorContains string
expectedName string
}{
"capital name field": {
yaml: `apiVersion: v1
Name: my-plugin Name: my-plugin
type: cli/v1 type: cli/v1
runtime: subprocess runtime: subprocess
` `,
expectError: true,
_, err := loadMetadataV1([]byte(yamlWithCapitalName)) errorContains: "field Name not found in type plugin.MetadataV1",
},
require.Error(t, err, "expected error for capital 'Name:' field") "correct name field": {
assert.Contains(t, err.Error(), "field Name not found in type plugin.MetadataV1") yaml: `apiVersion: v1
t.Logf("V1 error (strict unmarshalling): %v", err)
}
func TestLoadMetadataV1_CorrectField(t *testing.T) {
yamlWithCorrectName := `apiVersion: v1
name: my-plugin name: my-plugin
type: cli/v1 type: cli/v1
runtime: subprocess runtime: subprocess
` `,
m, err := loadMetadataV1([]byte(yamlWithCorrectName)) expectError: false,
expectedName: "my-plugin",
},
}
require.NoError(t, err, "expected success for correct 'name:' field") for name, tc := range testCases {
assert.Equal(t, "my-plugin", m.Name) t.Run(name, func(t *testing.T) {
m, err := loadMetadataV1([]byte(tc.yaml))
if tc.expectError {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.errorContains)
t.Logf("V1 error (strict unmarshalling): %v", err)
} else {
require.NoError(t, err)
assert.Equal(t, tc.expectedName, m.Name)
}
})
}
} }

Loading…
Cancel
Save