diff --git a/pkg/apiserver/rest/dualwriter.go b/pkg/apiserver/rest/dualwriter.go index 932637b5b6b..94b3ae1f595 100644 --- a/pkg/apiserver/rest/dualwriter.go +++ b/pkg/apiserver/rest/dualwriter.go @@ -241,7 +241,7 @@ func removeMeta(obj runtime.Object) []byte { // we don't want to compare meta fields delete(unstObj, "metadata") - jsonObj, err := json.Marshal(cpy) + jsonObj, err := json.Marshal(unstObj) if err != nil { return nil } diff --git a/pkg/apiserver/rest/dualwriter_test.go b/pkg/apiserver/rest/dualwriter_test.go index e72349ec4b8..6b95fdeb09f 100644 --- a/pkg/apiserver/rest/dualwriter_test.go +++ b/pkg/apiserver/rest/dualwriter_test.go @@ -8,7 +8,11 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + + // nolint:depguard + playlist "github.com/grafana/grafana/pkg/apis/playlist/v0alpha1" ) func TestSetDualWritingMode(t *testing.T) { @@ -59,24 +63,44 @@ func TestSetDualWritingMode(t *testing.T) { } func TestCompare(t *testing.T) { + var examplePlaylistGen1 = &playlist.Playlist{ObjectMeta: metav1.ObjectMeta{Generation: 1}, Spec: playlist.Spec{Title: "Example Playlist"}} + var examplePlaylistGen2 = &playlist.Playlist{ObjectMeta: metav1.ObjectMeta{Generation: 2}, Spec: playlist.Spec{Title: "Example Playlist"}} + var anotherPlaylist = &playlist.Playlist{ObjectMeta: metav1.ObjectMeta{Generation: 2}, Spec: playlist.Spec{Title: "Another Playlist"}} + testCase := []struct { name string - input runtime.Object + input1 runtime.Object + input2 runtime.Object expected bool }{ { name: "should return true when both objects are the same", - input: exampleObj, + input1: exampleObj, + input2: exampleObj, expected: true, }, { - name: "should return false when objects are different", - input: anotherObj, + name: "should return false when objects are different", + input1: exampleObj, + input2: anotherObj, + expected: false, + }, + { + name: "should return true when Playlists are the same, but different metadata (generation)", + input1: examplePlaylistGen1, + input2: examplePlaylistGen2, + expected: true, + }, + { + name: "should return false when Playlists different", + input1: examplePlaylistGen1, + input2: anotherPlaylist, + expected: false, }, } for _, tt := range testCase { t.Run(tt.name, func(t *testing.T) { - assert.Equal(t, tt.expected, Compare(tt.input, exampleObj)) + assert.Equal(t, tt.expected, Compare(tt.input1, tt.input2)) }) } }