mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
UniStore: Fix DualWriter compare method (#94705)
* UniStore: Fix DualWriter compare method Signed-off-by: Maicon Costa <maiconscosta@gmail.com> --------- Signed-off-by: Maicon Costa <maiconscosta@gmail.com>
This commit is contained in:
parent
4a60f29709
commit
c4f906f7fa
@ -274,20 +274,18 @@ func Compare(storageObj, legacyObj runtime.Object) bool {
|
|||||||
if storageObj == nil || legacyObj == nil {
|
if storageObj == nil || legacyObj == nil {
|
||||||
return storageObj == nil && legacyObj == nil
|
return storageObj == nil && legacyObj == nil
|
||||||
}
|
}
|
||||||
return bytes.Equal(removeMeta(storageObj), removeMeta(legacyObj))
|
return bytes.Equal(extractSpec(storageObj), extractSpec(legacyObj))
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeMeta(obj runtime.Object) []byte {
|
func extractSpec(obj runtime.Object) []byte {
|
||||||
cpy := obj.DeepCopyObject()
|
cpy := obj.DeepCopyObject()
|
||||||
unstObj, err := defaultConverter.ToUnstructured(cpy)
|
unstObj, err := defaultConverter.ToUnstructured(cpy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// we don't want to compare meta fields
|
|
||||||
delete(unstObj, "metadata")
|
|
||||||
delete(unstObj, "objectMeta")
|
|
||||||
|
|
||||||
jsonObj, err := json.Marshal(unstObj)
|
// we just want to compare the spec field
|
||||||
|
jsonObj, err := json.Marshal(unstObj["spec"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -66,6 +66,7 @@ func TestSetDualWritingMode(t *testing.T) {
|
|||||||
func TestCompare(t *testing.T) {
|
func TestCompare(t *testing.T) {
|
||||||
var exampleObjGen1 = &example.Pod{ObjectMeta: metav1.ObjectMeta{Generation: 1}, Spec: example.PodSpec{Hostname: "one"}, Status: example.PodStatus{StartTime: &metav1.Time{Time: time.Unix(0, 0)}}}
|
var exampleObjGen1 = &example.Pod{ObjectMeta: metav1.ObjectMeta{Generation: 1}, Spec: example.PodSpec{Hostname: "one"}, Status: example.PodStatus{StartTime: &metav1.Time{Time: time.Unix(0, 0)}}}
|
||||||
var exampleObjGen2 = &example.Pod{ObjectMeta: metav1.ObjectMeta{Generation: 2}, Spec: example.PodSpec{Hostname: "one"}, Status: example.PodStatus{StartTime: &metav1.Time{Time: time.Unix(0, 0)}}}
|
var exampleObjGen2 = &example.Pod{ObjectMeta: metav1.ObjectMeta{Generation: 2}, Spec: example.PodSpec{Hostname: "one"}, Status: example.PodStatus{StartTime: &metav1.Time{Time: time.Unix(0, 0)}}}
|
||||||
|
var exampleObjGen3 = &example.Pod{TypeMeta: metav1.TypeMeta{Kind: "pod", APIVersion: "pods/v0"}, ObjectMeta: metav1.ObjectMeta{Generation: 2}, Spec: example.PodSpec{Hostname: "one"}, Status: example.PodStatus{StartTime: &metav1.Time{Time: time.Unix(0, 0)}}}
|
||||||
var exampleObjDifferentTitle = &example.Pod{ObjectMeta: metav1.ObjectMeta{Generation: 2}, Spec: example.PodSpec{Hostname: "two"}, Status: example.PodStatus{StartTime: &metav1.Time{Time: time.Unix(0, 0)}}}
|
var exampleObjDifferentTitle = &example.Pod{ObjectMeta: metav1.ObjectMeta{Generation: 2}, Spec: example.PodSpec{Hostname: "two"}, Status: example.PodStatus{StartTime: &metav1.Time{Time: time.Unix(0, 0)}}}
|
||||||
|
|
||||||
testCase := []struct {
|
testCase := []struct {
|
||||||
@ -86,6 +87,12 @@ func TestCompare(t *testing.T) {
|
|||||||
input2: exampleObjGen2,
|
input2: exampleObjGen2,
|
||||||
expected: true,
|
expected: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "should return true when objects are the same, but different TypeMeta (kind and apiversion)",
|
||||||
|
input1: exampleObjGen1,
|
||||||
|
input2: exampleObjGen3,
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "should return false when objects are different",
|
name: "should return false when objects are different",
|
||||||
input1: exampleObjGen1,
|
input1: exampleObjGen1,
|
||||||
|
Loading…
Reference in New Issue
Block a user