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.
if len(errs) != 1 { numErrs: 1,
t.Errorf("Unexpected error list: %#v", errs)
}
errs = ValidateService(&api.Service{
Port: 6502,
JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{
"foo": "bar",
}, },
}) {
if len(errs) != 0 { name: "invalid id",
t.Errorf("Unexpected non-zero 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,
Selector: map[string]string{
"foo": "bar",
}, },
}) // Should fail because the ID is invalid.
if len(errs) != 1 { numErrs: 1,
t.Errorf("Unexpected error list: %#v", errs) },
} {
name: "missing port",
errs = ValidateService(&api.Service{ svc: api.Service{
Port: 6502, JSONBase: api.JSONBase{ID: "abc123"},
Selector: map[string]string{"foo": "bar"},
},
// Should fail because the port number is missing/invalid.
numErrs: 1,
},
{
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"}, JSONBase: api.JSONBase{ID: "foo"},
}) Port: 8675,
if len(errs) != 1 { },
t.Errorf("Unexpected error list: %#v", errs) // 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,
},
} }
errs = ValidateService(&api.Service{}) for _, tc := range testCases {
if len(errs) != 3 { errs := ValidateService(&tc.svc)
t.Errorf("Unexpected error list: %#v", errs) if len(errs) != tc.numErrs {
t.Errorf("Unexpected error list for case %q: %+v", tc.name, errs)
}
} }
} }