Unified Storage: Fix Compare method (#90455)

* Unified Storage: Fix Compare method

Signed-off-by: Maicon Costa <maiconscosta@gmail.com>

---------

Signed-off-by: Maicon Costa <maiconscosta@gmail.com>
This commit is contained in:
maicon 2024-07-16 11:36:21 -03:00 committed by GitHub
parent 1c031277af
commit 93221f12da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 6 deletions

View File

@ -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
}

View File

@ -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))
})
}
}