Add more validation for updating node.

This commit is contained in:
Deyuan Deng
2014-11-17 13:22:27 -05:00
parent cc310e0e71
commit c20ceea170
5 changed files with 107 additions and 37 deletions

View File

@@ -535,3 +535,15 @@ func ValidateMinion(minion *api.Minion) errs.ValidationErrorList {
allErrs = append(allErrs, validateLabels(minion.Labels)...)
return allErrs
}
// ValidateMinionUpdate tests to make sure a minion update can be applied. Modifies oldMinion.
func ValidateMinionUpdate(oldMinion *api.Minion, minion *api.Minion) errs.ValidationErrorList {
allErrs := errs.ValidationErrorList{}
// TODO: why we need two labels for minion.
oldMinion.Labels = minion.Labels
oldMinion.ObjectMeta.Labels = minion.ObjectMeta.Labels
if !reflect.DeepEqual(oldMinion, minion) {
allErrs = append(allErrs, fmt.Errorf("Update contains more than labels changes"))
}
return allErrs
}

View File

@@ -1065,3 +1065,62 @@ func TestValidateMinion(t *testing.T) {
}
}
}
func TestValidateMinionUpdate(t *testing.T) {
tests := []struct {
oldMinion api.Minion
minion api.Minion
valid bool
}{
{api.Minion{}, api.Minion{}, true},
{api.Minion{
ObjectMeta: api.ObjectMeta{
Name: "foo"}},
api.Minion{
ObjectMeta: api.ObjectMeta{
Name: "bar"},
}, false},
{api.Minion{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{"foo": "bar"},
},
}, api.Minion{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{"foo": "baz"},
},
}, true},
{api.Minion{
ObjectMeta: api.ObjectMeta{
Name: "foo",
},
}, api.Minion{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{"foo": "baz"},
},
}, true},
{api.Minion{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{"bar": "foo"},
},
}, api.Minion{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{"foo": "baz"},
},
}, true},
}
for _, test := range tests {
errs := ValidateMinionUpdate(&test.oldMinion, &test.minion)
if test.valid && len(errs) > 0 {
t.Errorf("Unexpected error: %v", errs)
t.Logf("%#v vs %#v", test.oldMinion.ObjectMeta, test.minion.ObjectMeta)
}
if !test.valid && len(errs) == 0 {
t.Errorf("Unexpected non-error")
}
}
}