(ut) add tests for bad filed values checking; refactor some funcs

This commit is contained in:
Paco Xu
2022-03-07 18:17:28 +08:00
parent 3fbfd7eb1b
commit db147b7d67
11 changed files with 445 additions and 96 deletions

View File

@@ -1,5 +1,5 @@
/*
Copyright 2018 The Kubernetes Authors.
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -17,11 +17,14 @@ limitations under the License.
package persistentvolume
import (
"context"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
utilfeature "k8s.io/apiserver/pkg/util/feature"
featuregatetesting "k8s.io/component-base/featuregate/testing"
api "k8s.io/kubernetes/pkg/apis/core"
@@ -108,3 +111,74 @@ func specWithCSISecrets(secret *api.SecretReference) *api.PersistentVolumeSpec {
}
return pvSpec
}
func TestWarnings(t *testing.T) {
testcases := []struct {
name string
template *api.PersistentVolume
expected []string
}{
{
name: "null",
template: nil,
expected: nil,
},
{
name: "no warning",
template: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Status: api.PersistentVolumeStatus{
Phase: api.VolumeBound,
},
},
expected: nil,
},
{
name: "warning",
template: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Spec: api.PersistentVolumeSpec{
NodeAffinity: &api.VolumeNodeAffinity{
Required: &api.NodeSelector{
NodeSelectorTerms: []api.NodeSelectorTerm{
{
MatchExpressions: []api.NodeSelectorRequirement{
{
Key: "beta.kubernetes.io/os",
Operator: "Equal",
Values: []string{"windows"},
},
},
},
},
},
},
},
Status: api.PersistentVolumeStatus{
Phase: api.VolumeBound,
},
},
expected: []string{
`spec.nodeAffinity.required.nodeSelectorTerms[0].matchExpressions[0].key: beta.kubernetes.io/os is deprecated since v1.14; use "kubernetes.io/os" instead`,
},
},
}
for _, tc := range testcases {
t.Run("podspec_"+tc.name, func(t *testing.T) {
actual := sets.NewString(GetWarningsForPersistentVolume(context.TODO(), tc.template)...)
expected := sets.NewString(tc.expected...)
for _, missing := range expected.Difference(actual).List() {
t.Errorf("missing: %s", missing)
}
for _, extra := range actual.Difference(expected).List() {
t.Errorf("extra: %s", extra)
}
})
}
}