mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2025-03-15 04:57:56 +00:00
Merge pull request #1590 from marquiz/devel/validation-assert
apis/nfd/validate: use testify/assert for checking test results
This commit is contained in:
commit
ebd19fe692
2 changed files with 27 additions and 47 deletions
|
@ -115,7 +115,7 @@ func Label(key, value string) error {
|
||||||
|
|
||||||
// Validate label value
|
// Validate label value
|
||||||
if err := k8svalidation.IsValidLabelValue(value); len(err) > 0 {
|
if err := k8svalidation.IsValidLabelValue(value); len(err) > 0 {
|
||||||
return fmt.Errorf("invalid labelvalue %q: %s", value, strings.Join(err, "; "))
|
return fmt.Errorf("invalid value %q: %s", value, strings.Join(err, "; "))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -156,7 +156,7 @@ func Annotation(key, value string) error {
|
||||||
|
|
||||||
// Validate annotation value
|
// Validate annotation value
|
||||||
if errs := k8svalidation.IsValidLabelValue(value); len(errs) > 0 {
|
if errs := k8svalidation.IsValidLabelValue(value); len(errs) > 0 {
|
||||||
return fmt.Errorf("invalid annotation value %q: %s", value, strings.Join(errs, "; "))
|
return fmt.Errorf("invalid value %q: %s", value, strings.Join(errs, "; "))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -236,10 +236,10 @@ func ExtendedResource(key, value string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Static Value (Pre-Defined at the NodeFeatureRule)
|
// Validate extended resource value
|
||||||
_, err := k8sQuantity.ParseQuantity(value)
|
_, err := k8sQuantity.ParseQuantity(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("invalid value %s (from %s): %w", value, value, err)
|
return fmt.Errorf("invalid value %q: %w", value, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package validate
|
package validate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,8 +12,7 @@ func TestAnnotation(t *testing.T) {
|
||||||
name string
|
name string
|
||||||
key string
|
key string
|
||||||
value string
|
value string
|
||||||
want error
|
want interface{}
|
||||||
fail bool
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Valid annotation",
|
name: "Valid annotation",
|
||||||
|
@ -31,26 +30,23 @@ func TestAnnotation(t *testing.T) {
|
||||||
name: "Invalid annotation value",
|
name: "Invalid annotation value",
|
||||||
key: "feature.node.kubernetes.io/feature",
|
key: "feature.node.kubernetes.io/feature",
|
||||||
value: "invalid value",
|
value: "invalid value",
|
||||||
want: fmt.Errorf("invalid value \"invalid value\": value must be a valid label value"),
|
want: "invalid value \"invalid value\": ",
|
||||||
fail: true,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Denied annotation key",
|
name: "Denied annotation key",
|
||||||
key: "kubernetes.io/denied",
|
key: "kubernetes.io/denied",
|
||||||
value: "true",
|
value: "true",
|
||||||
want: ErrNSNotAllowed,
|
want: ErrNSNotAllowed,
|
||||||
fail: true,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
got := Annotation(tt.key, tt.value)
|
err := Annotation(tt.key, tt.value)
|
||||||
if got != tt.want {
|
if str, ok := tt.want.(string); ok {
|
||||||
if tt.fail {
|
assert.ErrorContains(t, err, str)
|
||||||
return
|
} else {
|
||||||
}
|
assert.Equal(t, tt.want, err)
|
||||||
t.Errorf("Annotation() = %v, want %v", got, tt.want)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -112,9 +108,7 @@ func TestTaint(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
got := Taint(tt.taint)
|
got := Taint(tt.taint)
|
||||||
if got != tt.want {
|
assert.Equal(t, tt.want, got)
|
||||||
t.Errorf("Taint() = %v, want %v", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,61 +118,53 @@ func TestLabel(t *testing.T) {
|
||||||
name string
|
name string
|
||||||
key string
|
key string
|
||||||
value string
|
value string
|
||||||
want error
|
want interface{}
|
||||||
fail bool
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Valid label",
|
name: "Valid label",
|
||||||
key: "feature.node.kubernetes.io/label",
|
key: "feature.node.kubernetes.io/label",
|
||||||
value: "true",
|
value: "true",
|
||||||
want: nil,
|
want: nil,
|
||||||
fail: false,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Valid vendor label",
|
name: "Valid vendor label",
|
||||||
key: "vendor.io/label",
|
key: "vendor.io/label",
|
||||||
value: "true",
|
value: "true",
|
||||||
want: nil,
|
want: nil,
|
||||||
fail: false,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Denied label with prefix",
|
name: "Denied label with prefix",
|
||||||
key: "kubernetes.io/label",
|
key: "kubernetes.io/label",
|
||||||
value: "true",
|
value: "true",
|
||||||
want: ErrNSNotAllowed,
|
want: ErrNSNotAllowed,
|
||||||
fail: true,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Invalid label key",
|
name: "Invalid label key",
|
||||||
key: "invalid-key",
|
key: "invalid-key",
|
||||||
value: "true",
|
value: "true",
|
||||||
want: ErrNSNotAllowed,
|
want: ErrUnprefixedKeysNotAllowed,
|
||||||
fail: true,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Invalid label value",
|
name: "Invalid label value",
|
||||||
key: "feature.node.kubernetes.io/label",
|
key: "feature.node.kubernetes.io/label",
|
||||||
value: "invalid value",
|
value: "invalid value",
|
||||||
want: fmt.Errorf("invalid value \"invalid value\": value must be a valid label value"),
|
want: "invalid value \"invalid value\": ",
|
||||||
fail: true,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Valid value label",
|
name: "Valid value label",
|
||||||
key: "feature.node.kubernetes.io/label",
|
key: "feature.node.kubernetes.io/label",
|
||||||
value: "true",
|
value: "true",
|
||||||
want: nil,
|
want: nil,
|
||||||
fail: false,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
err := Label(tt.key, tt.value)
|
err := Label(tt.key, tt.value)
|
||||||
if err != tt.want {
|
if str, ok := tt.want.(string); ok {
|
||||||
if tt.fail {
|
assert.ErrorContains(t, err, str)
|
||||||
return
|
} else {
|
||||||
}
|
assert.Equal(t, tt.want, err)
|
||||||
t.Errorf("Label() = %v, want %v", err, tt.want)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -189,47 +175,41 @@ func TestExtendedResource(t *testing.T) {
|
||||||
name string
|
name string
|
||||||
key string
|
key string
|
||||||
value string
|
value string
|
||||||
want error
|
want interface{}
|
||||||
fail bool
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Valid extended resource",
|
name: "Valid extended resource",
|
||||||
key: "feature.node.kubernetes.io/extended-resource",
|
key: "feature.node.kubernetes.io/extended-resource",
|
||||||
value: "123",
|
value: "123",
|
||||||
want: nil,
|
want: nil,
|
||||||
fail: false,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Invalid extended resource key",
|
name: "Invalid extended resource key",
|
||||||
key: "invalid-key",
|
key: "invalid-key",
|
||||||
value: "123",
|
value: "123",
|
||||||
want: ErrNSNotAllowed,
|
want: ErrUnprefixedKeysNotAllowed,
|
||||||
fail: true,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Invalid extended resource value",
|
name: "Invalid extended resource value",
|
||||||
key: "feature.node.kubernetes.io/extended-resource",
|
key: "feature.node.kubernetes.io/extended-resource",
|
||||||
value: "invalid value",
|
value: "invalid value",
|
||||||
want: fmt.Errorf("invalid value \"invalid value\": value must be a valid label value"),
|
want: "invalid value \"invalid value\": ",
|
||||||
fail: true,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Denied extended resource key",
|
name: "Denied extended resource key",
|
||||||
key: "kubernetes.io/extended-resource",
|
key: "kubernetes.io/extended-resource",
|
||||||
value: "123",
|
value: "123",
|
||||||
want: ErrNSNotAllowed,
|
want: ErrNSNotAllowed,
|
||||||
fail: true,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
err := ExtendedResource(tt.key, tt.value)
|
err := ExtendedResource(tt.key, tt.value)
|
||||||
if err != tt.want {
|
if str, ok := tt.want.(string); ok {
|
||||||
if tt.fail {
|
assert.ErrorContains(t, err, str)
|
||||||
return
|
} else {
|
||||||
}
|
assert.Equal(t, tt.want, err)
|
||||||
t.Errorf("ExtendedResource() = %v, want %v", err, tt.want)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue