0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-01 11:02:59 +00:00

fix(vm): MAC address validator should allow lowercase hex (#660)

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
This commit is contained in:
Pavel Boldyrev 2023-10-29 14:17:28 -04:00 committed by GitHub
parent f6f05a56e4
commit 7867e66d53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 4 deletions

View File

@ -16,23 +16,23 @@ import (
// MACAddress is a schema validation function for MAC address.
func MACAddress() schema.SchemaValidateDiagFunc {
return validation.ToDiagFunc(func(i interface{}, k string) ([]string, []error) {
return validation.ToDiagFunc(func(i interface{}, path string) ([]string, []error) {
v, ok := i.(string)
var ws []string
var es []error
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
es = append(es, fmt.Errorf("expected type of %q to be string", path))
return ws, es
}
if v != "" {
r := regexp.MustCompile(`^[A-Z\d]{2}(:[A-Z\d]{2}){5}$`)
r := regexp.MustCompile(`^[A-Fa-f0-9]{2}(:[A-Fa-f0-9]{2}){5}$`)
ok := r.MatchString(v)
if !ok {
es = append(es, fmt.Errorf("expected %s to be a valid MAC address (A0:B1:C2:D3:E4:F5), got %s", k, v))
es = append(es, fmt.Errorf("expected %q to be a valid MAC address (A0:B1:C2:D3:E4:F5), got %q", path, v))
return ws, es
}
}

View File

@ -0,0 +1,46 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package validator
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestMACAddress(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value string
valid bool
}{
{"empty", "", true},
{"invalid", "invalid", false},
{"invalid: no dashes", "38-f9-d3-4b-f5-51", false},
{"valid", "38:f9:d3:4b:f5:51", true},
{"valid", "38:F9:D3:4B:F5:51", true},
{"valid", "00:15:5d:00:09:03", true},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := MACAddress()
res := f(tt.value, nil)
if tt.valid {
require.Empty(t, res, "validate: '%s'", tt.value)
} else {
require.NotEmpty(t, res, "validate: '%s'", tt.value)
}
})
}
}