mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-02 16:29:21 +00:00
Beef up validation tests
This commit is contained in:
parent
8b42534f77
commit
1ec3457fc9
@ -338,7 +338,22 @@ func TestValidateRestartPolicy(t *testing.T) {
|
|||||||
if noPolicySpecified.Always == nil {
|
if noPolicySpecified.Always == nil {
|
||||||
t.Errorf("expected Always policy specified")
|
t.Errorf("expected Always policy specified")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateDNSPolicy(t *testing.T) {
|
||||||
|
successCases := []api.DNSPolicy{api.DNSClusterFirst, api.DNSDefault, api.DNSPolicy("")}
|
||||||
|
for _, policy := range successCases {
|
||||||
|
if errs := validateDNSPolicy(&policy); len(errs) != 0 {
|
||||||
|
t.Errorf("expected success: %v", errs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
errorCases := []api.DNSPolicy{api.DNSPolicy("invalid")}
|
||||||
|
for _, policy := range errorCases {
|
||||||
|
if errs := validateDNSPolicy(&policy); len(errs) == 0 {
|
||||||
|
t.Errorf("expected failure for %v", policy)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidateManifest(t *testing.T) {
|
func TestValidateManifest(t *testing.T) {
|
||||||
@ -404,59 +419,109 @@ func TestValidateManifest(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidatePod(t *testing.T) {
|
func TestValidatePodSpec(t *testing.T) {
|
||||||
errs := ValidatePod(&api.Pod{
|
successCases := []api.PodSpec{
|
||||||
ObjectMeta: api.ObjectMeta{
|
{}, // empty is valid, if not very useful */
|
||||||
Name: "foo", Namespace: api.NamespaceDefault,
|
{ // Populate basic fields, leave defaults for most.
|
||||||
Labels: map[string]string{
|
Volumes: []api.Volume{{Name: "vol"}},
|
||||||
"foo": "bar",
|
Containers: []api.Container{{Name: "ctr", Image: "image"}},
|
||||||
},
|
},
|
||||||
|
{ // Populate all fields.
|
||||||
|
Volumes: []api.Volume{
|
||||||
|
{Name: "vol"},
|
||||||
},
|
},
|
||||||
Spec: api.PodSpec{
|
Containers: []api.Container{{Name: "ctr", Image: "image"}},
|
||||||
RestartPolicy: api.RestartPolicy{
|
RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{}},
|
||||||
Always: &api.RestartPolicyAlways{},
|
DNSPolicy: api.DNSClusterFirst,
|
||||||
|
NodeSelector: map[string]string{
|
||||||
|
"key": "value",
|
||||||
},
|
},
|
||||||
|
Host: "foobar",
|
||||||
},
|
},
|
||||||
})
|
|
||||||
if len(errs) != 0 {
|
|
||||||
t.Errorf("Unexpected non-zero error list: %#v", errs)
|
|
||||||
}
|
}
|
||||||
errs = ValidatePod(&api.Pod{
|
for i := range successCases {
|
||||||
ObjectMeta: api.ObjectMeta{
|
if errs := ValidatePodSpec(&successCases[i]); len(errs) != 0 {
|
||||||
Name: "foo",
|
t.Errorf("expected success: %v", errs)
|
||||||
Namespace: api.NamespaceDefault,
|
}
|
||||||
Labels: map[string]string{
|
|
||||||
"foo": "bar",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Spec: api.PodSpec{},
|
|
||||||
})
|
|
||||||
if len(errs) != 0 {
|
|
||||||
t.Errorf("Unexpected non-zero error list: %#v", errs)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errs = ValidatePodSpec(&api.PodSpec{
|
failureCases := map[string]api.PodSpec{
|
||||||
RestartPolicy: api.RestartPolicy{
|
"bad volume": {
|
||||||
Always: &api.RestartPolicyAlways{},
|
Volumes: []api.Volume{{}},
|
||||||
Never: &api.RestartPolicyNever{},
|
},
|
||||||
|
"bad container": {
|
||||||
|
Containers: []api.Container{{}},
|
||||||
|
},
|
||||||
|
"bad DNS policy": {
|
||||||
|
DNSPolicy: api.DNSPolicy("invalid"),
|
||||||
},
|
},
|
||||||
})
|
|
||||||
if len(errs) != 1 {
|
|
||||||
t.Errorf("Unexpected error list: %#v", errs)
|
|
||||||
}
|
}
|
||||||
errs = ValidatePod(&api.Pod{
|
for k, v := range failureCases {
|
||||||
ObjectMeta: api.ObjectMeta{
|
if errs := ValidatePodSpec(&v); len(errs) == 0 {
|
||||||
Name: "foo",
|
t.Errorf("expected failure for %q", k)
|
||||||
Namespace: api.NamespaceDefault,
|
}
|
||||||
Labels: map[string]string{
|
}
|
||||||
"1/2/3/4/5": "bar", // invalid
|
|
||||||
"valid": "bar", // good
|
defaultPod := api.PodSpec{} // all empty fields
|
||||||
|
if errs := ValidatePodSpec(&defaultPod); len(errs) != 0 {
|
||||||
|
t.Errorf("expected success: %v", errs)
|
||||||
|
}
|
||||||
|
if util.AllPtrFieldsNil(defaultPod.RestartPolicy) {
|
||||||
|
t.Errorf("expected a default RestartPolicy")
|
||||||
|
}
|
||||||
|
if defaultPod.DNSPolicy == "" {
|
||||||
|
t.Errorf("expected a default DNSPolicy")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidatePod(t *testing.T) {
|
||||||
|
successCases := []api.Pod{
|
||||||
|
{ // Mostly empty.
|
||||||
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "ns"},
|
||||||
|
},
|
||||||
|
{ // Basic fields.
|
||||||
|
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: "ns"},
|
||||||
|
Spec: api.PodSpec{
|
||||||
|
Volumes: []api.Volume{{Name: "vol"}},
|
||||||
|
Containers: []api.Container{{Name: "ctr", Image: "image"}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Spec: api.PodSpec{},
|
{ // Just about everything.
|
||||||
})
|
ObjectMeta: api.ObjectMeta{Name: "abc.123.do-re-mi", Namespace: "ns"},
|
||||||
if len(errs) != 1 {
|
Spec: api.PodSpec{
|
||||||
t.Errorf("Unexpected error list: %#v", errs)
|
Volumes: []api.Volume{
|
||||||
|
{Name: "vol"},
|
||||||
|
},
|
||||||
|
Containers: []api.Container{{Name: "ctr", Image: "image"}},
|
||||||
|
RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{}},
|
||||||
|
DNSPolicy: api.DNSClusterFirst,
|
||||||
|
NodeSelector: map[string]string{
|
||||||
|
"key": "value",
|
||||||
|
},
|
||||||
|
Host: "foobar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, pod := range successCases {
|
||||||
|
if errs := ValidatePod(&pod); len(errs) != 0 {
|
||||||
|
t.Errorf("expected success: %v", errs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
errorCases := map[string]api.Pod{
|
||||||
|
"bad name": {ObjectMeta: api.ObjectMeta{Name: "", Namespace: "ns"}},
|
||||||
|
"bad namespace": {ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: ""}},
|
||||||
|
"bad spec": {
|
||||||
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "ns"},
|
||||||
|
Spec: api.PodSpec{
|
||||||
|
Containers: []api.Container{{}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for k, v := range errorCases {
|
||||||
|
if errs := ValidatePod(&v); len(errs) == 0 {
|
||||||
|
t.Errorf("expected failure for %s", k)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -623,6 +688,57 @@ func TestValidatePodUpdate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidateBoundPods(t *testing.T) {
|
||||||
|
successCases := []api.BoundPod{
|
||||||
|
{ // Mostly empty.
|
||||||
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "ns"},
|
||||||
|
},
|
||||||
|
{ // Basic fields.
|
||||||
|
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: "ns"},
|
||||||
|
Spec: api.PodSpec{
|
||||||
|
Volumes: []api.Volume{{Name: "vol"}},
|
||||||
|
Containers: []api.Container{{Name: "ctr", Image: "image"}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ // Just about everything.
|
||||||
|
ObjectMeta: api.ObjectMeta{Name: "abc.123.do-re-mi", Namespace: "ns"},
|
||||||
|
Spec: api.PodSpec{
|
||||||
|
Volumes: []api.Volume{
|
||||||
|
{Name: "vol"},
|
||||||
|
},
|
||||||
|
Containers: []api.Container{{Name: "ctr", Image: "image"}},
|
||||||
|
RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{}},
|
||||||
|
DNSPolicy: api.DNSClusterFirst,
|
||||||
|
NodeSelector: map[string]string{
|
||||||
|
"key": "value",
|
||||||
|
},
|
||||||
|
Host: "foobar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, pod := range successCases {
|
||||||
|
if errs := ValidateBoundPod(&pod); len(errs) != 0 {
|
||||||
|
t.Errorf("expected success: %v", errs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
errorCases := map[string]api.Pod{
|
||||||
|
"bad name": {ObjectMeta: api.ObjectMeta{Name: "", Namespace: "ns"}},
|
||||||
|
"bad namespace": {ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: ""}},
|
||||||
|
"bad spec": {
|
||||||
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "ns"},
|
||||||
|
Spec: api.PodSpec{
|
||||||
|
Containers: []api.Container{{}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for k, v := range errorCases {
|
||||||
|
if errs := ValidatePod(&v); len(errs) == 0 {
|
||||||
|
t.Errorf("expected failure for %s", k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestValidateService(t *testing.T) {
|
func TestValidateService(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
|
Loading…
Reference in New Issue
Block a user