Do not raise an error proposing to use '--overwrite' when labeling with the same value (#105936)

* Do not propose to use '--overwrite' when labeling with the same value

* Check expected error value in label_test.TestLabelFunc
This commit is contained in:
Alexandre Garnier 2022-05-04 02:17:49 +02:00 committed by GitHub
parent 0401cc2762
commit 10eb7092f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 9 deletions

View File

@ -408,9 +408,9 @@ func updateDataChangeMsg(oldObj []byte, newObj []byte, overwrite bool) string {
func validateNoOverwrites(accessor metav1.Object, labels map[string]string) error {
allErrs := []error{}
for key := range labels {
if value, found := accessor.GetLabels()[key]; found {
allErrs = append(allErrs, fmt.Errorf("'%s' already has a value (%s), and --overwrite is false", key, value))
for key, value := range labels {
if currValue, found := accessor.GetLabels()[key]; found && currValue != value {
allErrs = append(allErrs, fmt.Errorf("'%s' already has a value (%s), and --overwrite is false", key, currValue))
}
}
return utilerrors.NewAggregate(allErrs)

View File

@ -166,7 +166,7 @@ func TestLabelFunc(t *testing.T) {
labels map[string]string
remove []string
expected runtime.Object
expectErr bool
expectErr string
}{
{
obj: &v1.Pod{
@ -174,8 +174,21 @@ func TestLabelFunc(t *testing.T) {
Labels: map[string]string{"a": "b"},
},
},
labels: map[string]string{"a": "b"},
expectErr: true,
labels: map[string]string{"a": "b"},
expected: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a": "b"},
},
},
},
{
obj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a": "b"},
},
},
labels: map[string]string{"a": "c"},
expectErr: "'a' already has a value (b), and --overwrite is false",
},
{
obj: &v1.Pod{
@ -264,13 +277,16 @@ func TestLabelFunc(t *testing.T) {
}
for _, test := range tests {
err := labelFunc(test.obj, test.overwrite, test.version, test.labels, test.remove)
if test.expectErr {
if test.expectErr != "" {
if err == nil {
t.Errorf("unexpected non-error: %v", test)
}
if err.Error() != test.expectErr {
t.Errorf("error expected: %v, got: %v", test.expectErr, err.Error())
}
continue
}
if !test.expectErr && err != nil {
if test.expectErr == "" && err != nil {
t.Errorf("unexpected error: %v %v", err, test)
}
if !reflect.DeepEqual(test.obj, test.expected) {
@ -586,7 +602,6 @@ func TestLabelMsg(t *testing.T) {
},
labels: map[string]string{"a": "b"},
expectMsg: MsgNotLabeled,
expectErr: true,
},
{
obj: &v1.Pod{