make service validation test be table-driven

This commit is contained in:
Tim Hockin 2014-09-10 09:41:31 -07:00
parent 6eeb967d3d
commit b566339f0c

View File

@ -365,50 +365,92 @@ func TestValidatePod(t *testing.T) {
} }
func TestValidateService(t *testing.T) { func TestValidateService(t *testing.T) {
// This test should fail because the port number is invalid i.e. testCases := []struct {
// the Port field has a default value of 0. name string
errs := ValidateService(&api.Service{ svc api.Service
JSONBase: api.JSONBase{ID: "foo"}, numErrs int
Selector: map[string]string{ }{
"foo": "bar", {
name: "missing id",
svc: api.Service{
Port: 8675,
Selector: map[string]string{"foo": "bar"},
},
// Should fail because the ID is missing.
numErrs: 1,
}, },
}) {
if len(errs) != 1 { name: "invalid id",
t.Errorf("Unexpected error list: %#v", errs) svc: api.Service{
} JSONBase: api.JSONBase{ID: "123abc"},
Port: 8675,
errs = ValidateService(&api.Service{ Selector: map[string]string{"foo": "bar"},
Port: 6502, },
JSONBase: api.JSONBase{ID: "foo"}, // Should fail because the ID is invalid.
Selector: map[string]string{ numErrs: 1,
"foo": "bar",
}, },
}) {
if len(errs) != 0 { name: "missing port",
t.Errorf("Unexpected non-zero error list: %#v", errs) svc: api.Service{
} JSONBase: api.JSONBase{ID: "abc123"},
Selector: map[string]string{"foo": "bar"},
errs = ValidateService(&api.Service{ },
Port: 6502, // Should fail because the port number is missing/invalid.
Selector: map[string]string{ numErrs: 1,
"foo": "bar", },
{
name: "invalid port",
svc: api.Service{
JSONBase: api.JSONBase{ID: "abc123"},
Port: 65536,
Selector: map[string]string{"foo": "bar"},
},
// Should fail because the port number is invalid.
numErrs: 1,
},
{
name: "missing selector",
svc: api.Service{
JSONBase: api.JSONBase{ID: "foo"},
Port: 8675,
},
// Should fail because the selector is missing.
numErrs: 1,
},
{
name: "valid 1",
svc: api.Service{
JSONBase: api.JSONBase{ID: "abc123"},
Port: 1,
Selector: map[string]string{"foo": "bar"},
},
numErrs: 0,
},
{
name: "valid 2",
svc: api.Service{
JSONBase: api.JSONBase{ID: "abc123"},
Port: 65535,
Selector: map[string]string{"foo": "bar"},
},
numErrs: 0,
},
{
name: "valid 3",
svc: api.Service{
JSONBase: api.JSONBase{ID: "abc123"},
Port: 80,
Selector: map[string]string{"foo": "bar"},
},
numErrs: 0,
}, },
})
if len(errs) != 1 {
t.Errorf("Unexpected error list: %#v", errs)
} }
errs = ValidateService(&api.Service{ for _, tc := range testCases {
Port: 6502, errs := ValidateService(&tc.svc)
JSONBase: api.JSONBase{ID: "foo"}, if len(errs) != tc.numErrs {
}) t.Errorf("Unexpected error list for case %q: %+v", tc.name, errs)
if len(errs) != 1 { }
t.Errorf("Unexpected error list: %#v", errs)
}
errs = ValidateService(&api.Service{})
if len(errs) != 3 {
t.Errorf("Unexpected error list: %#v", errs)
} }
} }