mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 12:43:23 +00:00
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:
parent
0401cc2762
commit
10eb7092f8
@ -408,9 +408,9 @@ func updateDataChangeMsg(oldObj []byte, newObj []byte, overwrite bool) string {
|
|||||||
|
|
||||||
func validateNoOverwrites(accessor metav1.Object, labels map[string]string) error {
|
func validateNoOverwrites(accessor metav1.Object, labels map[string]string) error {
|
||||||
allErrs := []error{}
|
allErrs := []error{}
|
||||||
for key := range labels {
|
for key, value := range labels {
|
||||||
if value, found := accessor.GetLabels()[key]; found {
|
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, value))
|
allErrs = append(allErrs, fmt.Errorf("'%s' already has a value (%s), and --overwrite is false", key, currValue))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return utilerrors.NewAggregate(allErrs)
|
return utilerrors.NewAggregate(allErrs)
|
||||||
|
@ -166,7 +166,7 @@ func TestLabelFunc(t *testing.T) {
|
|||||||
labels map[string]string
|
labels map[string]string
|
||||||
remove []string
|
remove []string
|
||||||
expected runtime.Object
|
expected runtime.Object
|
||||||
expectErr bool
|
expectErr string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
obj: &v1.Pod{
|
obj: &v1.Pod{
|
||||||
@ -174,8 +174,21 @@ func TestLabelFunc(t *testing.T) {
|
|||||||
Labels: map[string]string{"a": "b"},
|
Labels: map[string]string{"a": "b"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
labels: map[string]string{"a": "b"},
|
labels: map[string]string{"a": "b"},
|
||||||
expectErr: true,
|
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{
|
obj: &v1.Pod{
|
||||||
@ -264,13 +277,16 @@ func TestLabelFunc(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
err := labelFunc(test.obj, test.overwrite, test.version, test.labels, test.remove)
|
err := labelFunc(test.obj, test.overwrite, test.version, test.labels, test.remove)
|
||||||
if test.expectErr {
|
if test.expectErr != "" {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("unexpected non-error: %v", test)
|
t.Errorf("unexpected non-error: %v", test)
|
||||||
}
|
}
|
||||||
|
if err.Error() != test.expectErr {
|
||||||
|
t.Errorf("error expected: %v, got: %v", test.expectErr, err.Error())
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !test.expectErr && err != nil {
|
if test.expectErr == "" && err != nil {
|
||||||
t.Errorf("unexpected error: %v %v", err, test)
|
t.Errorf("unexpected error: %v %v", err, test)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(test.obj, test.expected) {
|
if !reflect.DeepEqual(test.obj, test.expected) {
|
||||||
@ -586,7 +602,6 @@ func TestLabelMsg(t *testing.T) {
|
|||||||
},
|
},
|
||||||
labels: map[string]string{"a": "b"},
|
labels: map[string]string{"a": "b"},
|
||||||
expectMsg: MsgNotLabeled,
|
expectMsg: MsgNotLabeled,
|
||||||
expectErr: true,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
obj: &v1.Pod{
|
obj: &v1.Pod{
|
||||||
|
Loading…
Reference in New Issue
Block a user