Make null and empty as equivalent in ObjectMetaEquivalent

This commit is contained in:
Marcin Wielgus 2016-11-10 01:52:25 +01:00
parent abd653bd97
commit 92c3e20e93
3 changed files with 20 additions and 6 deletions

View File

@ -65,10 +65,10 @@ func ObjectMetaEquivalent(a, b api_v1.ObjectMeta) bool {
if a.Namespace != b.Namespace {
return false
}
if !reflect.DeepEqual(a.Labels, b.Labels) {
if !reflect.DeepEqual(a.Labels, b.Labels) && (len(a.Labels) != 0 || len(b.Labels) != 0) {
return false
}
if !reflect.DeepEqual(a.Annotations, b.Annotations) {
if !reflect.DeepEqual(a.Annotations, b.Annotations) && (len(a.Annotations) != 0 || len(b.Annotations) != 0) {
return false
}
return true

View File

@ -56,6 +56,18 @@ func TestObjectMeta(t *testing.T) {
ResourceVersion: "1231255531412",
Annotations: map[string]string{"A": "B"},
}
o7 := api_v1.ObjectMeta{
Namespace: "ns1",
Name: "s1",
ResourceVersion: "1231255531412",
Annotations: map[string]string{},
Labels: map[string]string{},
}
o8 := api_v1.ObjectMeta{
Namespace: "ns1",
Name: "s1",
ResourceVersion: "1231255531412",
}
assert.Equal(t, 0, len(o2.UID))
assert.Equal(t, 0, len(o2.ResourceVersion))
assert.Equal(t, o1.Name, o2.Name)
@ -64,6 +76,8 @@ func TestObjectMeta(t *testing.T) {
assert.True(t, ObjectMetaEquivalent(o3, o4))
assert.True(t, ObjectMetaEquivalent(o5, o6))
assert.True(t, ObjectMetaEquivalent(o3, o5))
assert.True(t, ObjectMetaEquivalent(o7, o8))
assert.True(t, ObjectMetaEquivalent(o8, o7))
}
func TestObjectMetaAndSpec(t *testing.T) {

View File

@ -242,11 +242,11 @@ func CompareObjectMeta(a, b api_v1.ObjectMeta) error {
if a.Name != b.Name {
return fmt.Errorf("Different name expected:%s observed:%s", a.Namespace, b.Namespace)
}
if !reflect.DeepEqual(a.Annotations, b.Annotations) {
return fmt.Errorf("Annotations are different expected:%v observerd:%v", a.Annotations, b.Annotations)
if !reflect.DeepEqual(a.Labels, b.Labels) && (len(a.Labels) != 0 || len(b.Labels) != 0) {
return fmt.Errorf("Labels are different expected:%v observerd:%v", a.Labels, b.Labels)
}
if !reflect.DeepEqual(a.Labels, b.Labels) {
return fmt.Errorf("Annotations are different expected:%v observerd:%v", a.Labels, b.Labels)
if !reflect.DeepEqual(a.Annotations, b.Annotations) && (len(a.Annotations) != 0 || len(b.Annotations) != 0) {
return fmt.Errorf("Annotations are different expected:%v observerd:%v", a.Annotations, b.Annotations)
}
return nil
}