grafana/pkg/apiserver/rest/dualwriter_mode2_test.go
Arati R 79eab65f94
Storage: Add mode-specific DeleteCollection implementations (#86065)
* Add DeleteCollection methods to modes 1 and 4
* Add DeleteCollection implementations for modes 2 and 3
2024-04-16 17:05:31 +02:00

83 lines
2.8 KiB
Go

package rest
import (
"context"
"testing"
"github.com/zeebo/assert"
"k8s.io/apimachinery/pkg/api/meta"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
func TestMode2(t *testing.T) {
var ls = (LegacyStorage)(nil)
var s = (Storage)(nil)
lsSpy := NewLegacyStorageSpyClient(ls)
sSpy := NewStorageSpyClient(s)
dw := NewDualWriterMode2(lsSpy, sSpy)
// Create: it should use the Legacy Create implementation
_, err := dw.Create(context.Background(), &dummyObject{}, func(context.Context, runtime.Object) error { return nil }, &metav1.CreateOptions{})
assert.NoError(t, err)
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.Create"))
assert.Equal(t, 1, sSpy.Counts("Storage.Create"))
// Get: it should read from Storage with LegacyStorage as a fallback
// #TODO: Currently only testing the happy path. Refactor testing to more easily test other cases.
_, err = dw.Get(context.Background(), kind, &metav1.GetOptions{})
assert.NoError(t, err)
assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.Get"))
assert.Equal(t, 1, sSpy.Counts("Storage.Get"))
// List: it should use call both Legacy and Storage List methods
l, err := dw.List(context.Background(), &metainternalversion.ListOptions{})
assert.NoError(t, err)
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.List"))
assert.Equal(t, 1, sSpy.Counts("Storage.List"))
resList, err := meta.ExtractList(l)
assert.NoError(t, err)
expectedItems := map[string]string{
// Item 1: Storage should override Legacy
"Item 1": "Storage field 1",
// Item 2 shouldn't be included because it's not in Storage
// Item 3 should because it's in Legacy
"Item 3": "Legacy field 3",
}
assert.Equal(t, len(expectedItems), len(resList))
for _, obj := range resList {
v, ok := obj.(*dummyObject)
assert.True(t, ok)
accessor, err := meta.Accessor(v)
assert.NoError(t, err)
k, ok := expectedItems[accessor.GetName()]
assert.True(t, ok)
assert.Equal(t, k, v.Foo)
}
// Delete: it should use call both Legacy and Storage Delete methods
var deleteValidation = func(ctx context.Context, obj runtime.Object) error { return nil }
_, _, err = dw.Delete(context.Background(), kind, deleteValidation, &metav1.DeleteOptions{})
assert.NoError(t, err)
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.Delete"))
assert.Equal(t, 1, sSpy.Counts("Storage.Delete"))
// DeleteCollection: it should delete from both LegacyStorage and Storage
_, err = dw.DeleteCollection(
context.Background(),
func(context.Context, runtime.Object) error { return nil },
&metav1.DeleteOptions{},
&metainternalversion.ListOptions{},
)
assert.NoError(t, err)
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.DeleteCollection"))
assert.Equal(t, 1, sSpy.Counts("Storage.DeleteCollection"))
}