use gogen to add unit tests for getPreviousKey (#4)
* use gogen to add unit tests for getPreviousKey together with its supporting function * address comments * address comments * Address comments * Add more comments * Add more unit test cases * change the text of the unit tests
This commit is contained in:
Родитель
fd4a7a7714
Коммит
e12e194b97
|
@ -29,6 +29,10 @@ func NewEventCountKey(timestamp time.Time, kind string, namespace string, name s
|
|||
return &EventCountKey{PartitionId: partitionId, Kind: kind, Namespace: namespace, Name: name, Uid: uid}
|
||||
}
|
||||
|
||||
func NewEventCountKeyComparator(kind string, namespace string, name string, uid string) *EventCountKey {
|
||||
return &EventCountKey{Kind: kind, Namespace: namespace, Name: name, Uid: uid}
|
||||
}
|
||||
|
||||
func (_ *EventCountKey) TableName() string {
|
||||
return "eventcount"
|
||||
}
|
||||
|
@ -54,7 +58,11 @@ func (k *EventCountKey) Parse(key string) error {
|
|||
|
||||
//todo: need to make sure it can work as keyPrefix when some fields are empty
|
||||
func (k *EventCountKey) String() string {
|
||||
return fmt.Sprintf("/%v/%v/%v/%v/%v/%v", k.TableName(), k.PartitionId, k.Kind, k.Namespace, k.Name, k.Uid)
|
||||
if k.Uid == "" {
|
||||
return fmt.Sprintf("/%v/%v/%v/%v/%v", k.TableName(), k.PartitionId, k.Kind, k.Namespace, k.Name)
|
||||
} else {
|
||||
return fmt.Sprintf("/%v/%v/%v/%v/%v/%v", k.TableName(), k.PartitionId, k.Kind, k.Namespace, k.Name, k.Uid)
|
||||
}
|
||||
}
|
||||
|
||||
func (_ *EventCountKey) ValidateKey(key string) error {
|
||||
|
|
|
@ -38,34 +38,6 @@ func Test_EventCountTableKey_ParseCorrect(t *testing.T) {
|
|||
assert.Equal(t, someUid, k.Uid)
|
||||
}
|
||||
|
||||
func helper_update_eventcount_table(t *testing.T) (badgerwrap.DB, *ResourceEventCountsTable) {
|
||||
untyped.TestHookSetPartitionDuration(time.Hour)
|
||||
var keys []string
|
||||
for i := 'a'; i < 'd'; i++ {
|
||||
// add keys in ascending order
|
||||
keys = append(keys, NewEventCountKey(someTs, someKind, someNamespace, someName+string(i), someUid).String())
|
||||
}
|
||||
expectedResult := &ResourceEventCounts{MapMinToEvents: make(map[int64]*EventCounts)}
|
||||
expectedResult.MapMinToEvents[someMinute] = &EventCounts{MapReasonToCount: make(map[string]int32)}
|
||||
expectedResult.MapMinToEvents[someMinute].MapReasonToCount[someReason] = someCount
|
||||
|
||||
b, err := (&badgerwrap.MockFactory{}).Open(badger.DefaultOptions(""))
|
||||
assert.Nil(t, err)
|
||||
et := OpenResourceEventCountsTable()
|
||||
err = b.Update(func(txn badgerwrap.Txn) error {
|
||||
var txerr error
|
||||
for _, key := range keys {
|
||||
txerr = et.Set(txn, key, expectedResult)
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
return b, et
|
||||
}
|
||||
|
||||
func Test_EventCount_PutThenGet_SameData(t *testing.T) {
|
||||
untyped.TestHookSetPartitionDuration(time.Hour)
|
||||
key := NewEventCountKey(someTs, someKind, someNamespace, someName, someUid).String()
|
||||
|
@ -101,7 +73,7 @@ func Test_EventCount_PutThenGet_SameData(t *testing.T) {
|
|||
}
|
||||
|
||||
func Test_EventCount_TestMinAndMaxKeys(t *testing.T) {
|
||||
db, rt := helper_update_eventcount_table(t)
|
||||
db, rt := helper_update_ResourceEventCountsTable(t, (&EventCountKey{}).SetTestKeys(), &ResourceEventCounts{})
|
||||
var minKey string
|
||||
var maxKey string
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
|
@ -110,12 +82,12 @@ func Test_EventCount_TestMinAndMaxKeys(t *testing.T) {
|
|||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "/eventcount/001546398000/somekind/somenamespace/somenamea/68510937-4ffc-11e9-8e26-1418775557c8", minKey)
|
||||
assert.Equal(t, "/eventcount/001546398000/somekind/somenamespace/somenamec/68510937-4ffc-11e9-8e26-1418775557c8", maxKey)
|
||||
assert.Equal(t, "/eventcount/001546398000/somekind/somenamespace/somename/68510937-4ffc-11e9-8e26-1418775557c8", minKey)
|
||||
assert.Equal(t, "/eventcount/001546405200/somekind/somenamespace/somename/68510937-4ffc-11e9-8e26-1418775557c8c", maxKey)
|
||||
}
|
||||
|
||||
func Test_EventCount_TestGetMinMaxPartitions(t *testing.T) {
|
||||
db, rt := helper_update_eventcount_table(t)
|
||||
db, rt := helper_update_ResourceEventCountsTable(t, (&EventCountKey{}).SetTestKeys(), &ResourceEventCounts{})
|
||||
var minPartition string
|
||||
var maxPartition string
|
||||
var found bool
|
||||
|
@ -126,8 +98,77 @@ func Test_EventCount_TestGetMinMaxPartitions(t *testing.T) {
|
|||
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, untyped.GetPartitionId(someTs), minPartition)
|
||||
assert.Equal(t, untyped.GetPartitionId(someTs), maxPartition)
|
||||
assert.Equal(t, someMinPartition, minPartition)
|
||||
assert.Equal(t, someMaxPartition, maxPartition)
|
||||
}
|
||||
|
||||
func Test_EventCount_getLastMatchingKeyInPartition_FoundInPreviousPartition(t *testing.T) {
|
||||
db, wt := helper_update_ResourceEventCountsTable(t, (&EventCountKey{}).SetTestKeys(), (&EventCountKey{}).SetTestValue())
|
||||
var keyRes *EventCountKey
|
||||
var err1 error
|
||||
var found bool
|
||||
curKey := NewEventCountKey(someMaxTs, someKind, someNamespace, someName, someUid)
|
||||
keyComparator := NewEventCountKeyComparator(someKind, someNamespace, someName, someUid)
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMiddlePartition, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
assert.True(t, found)
|
||||
expectedKey := NewEventCountKey(someMiddleTs, someKind, someNamespace, someName, someUid+"b")
|
||||
assert.Equal(t, expectedKey, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func Test_EventCount_getLastMatchingKeyInPartition_FoundInSamePartition(t *testing.T) {
|
||||
db, wt := helper_update_ResourceEventCountsTable(t, (&EventCountKey{}).SetTestKeys(), (&EventCountKey{}).SetTestValue())
|
||||
var keyRes *EventCountKey
|
||||
var err1 error
|
||||
var found bool
|
||||
curKey := NewEventCountKey(someTs, someKind, someNamespace, someName, someUid)
|
||||
keyComparator := NewEventCountKeyComparator(someKind, someNamespace, someName, someUid+"a")
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMinPartition, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
|
||||
assert.True(t, found)
|
||||
expectedKey := NewEventCountKey(someTs, someKind, someNamespace, someName, someUid+"a")
|
||||
assert.Equal(t, expectedKey, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func Test_EventCount_getLastMatchingKeyInPartition_SameKeySearch(t *testing.T) {
|
||||
db, wt := helper_update_ResourceEventCountsTable(t, (&EventCountKey{}).SetTestKeys(), (&EventCountKey{}).SetTestValue())
|
||||
var keyRes *EventCountKey
|
||||
var err1 error
|
||||
var found bool
|
||||
curKey := NewEventCountKey(someTs, someKind, someNamespace, someName, someUid+"a")
|
||||
keyComparator := NewEventCountKeyComparator(someKind, someNamespace, someName, someUid+"a")
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMinPartition, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
|
||||
assert.False(t, found)
|
||||
assert.Equal(t, &EventCountKey{}, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func Test_EventCount_getLastMatchingKeyInPartition_NotFound(t *testing.T) {
|
||||
db, wt := helper_update_ResourceEventCountsTable(t, (&EventCountKey{}).SetTestKeys(), (&EventCountKey{}).SetTestValue())
|
||||
var keyRes *EventCountKey
|
||||
var err1 error
|
||||
var found bool
|
||||
curKey := NewEventCountKey(someMaxTs, someKind, someNamespace, someName, someUid)
|
||||
keyComparator := NewEventCountKeyComparator(someKind, someNamespace, someName, someUid+"dfd")
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMinPartition, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
|
||||
assert.False(t, found)
|
||||
assert.Equal(t, &EventCountKey{}, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func (_ *EventCountKey) GetTestKey() string {
|
||||
|
@ -138,3 +179,20 @@ func (_ *EventCountKey) GetTestKey() string {
|
|||
func (_ *EventCountKey) GetTestValue() *ResourceEventCounts {
|
||||
return &ResourceEventCounts{}
|
||||
}
|
||||
|
||||
func (_ *EventCountKey) SetTestKeys() []string {
|
||||
untyped.TestHookSetPartitionDuration(time.Hour)
|
||||
var keys []string
|
||||
i := 'a'
|
||||
for curTime := someTs; !curTime.After(someMaxTs); curTime = curTime.Add(untyped.GetPartitionDuration()) {
|
||||
// add keys in ascending order
|
||||
keys = append(keys, NewEventCountKey(curTime, someKind, someNamespace, someName, someUid).String())
|
||||
keys = append(keys, NewEventCountKey(curTime, someKind, someNamespace, someName, someUid+string(i)).String())
|
||||
i++
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
func (_ *EventCountKey) SetTestValue() *ResourceEventCounts {
|
||||
return &ResourceEventCounts{}
|
||||
}
|
||||
|
|
|
@ -132,7 +132,6 @@ func (t *ResourceEventCountsTable) GetMinMaxPartitions(txn badgerwrap.Txn) (bool
|
|||
return true, minKey.PartitionId, maxKey.PartitionId
|
||||
}
|
||||
|
||||
//todo: need to add unit test
|
||||
func (t *ResourceEventCountsTable) GetUniquePartitionList(txn badgerwrap.Txn) ([]string, error) {
|
||||
resources := []string{}
|
||||
ok, minPar, maxPar := t.GetMinMaxPartitions(txn)
|
||||
|
@ -152,7 +151,6 @@ func (t *ResourceEventCountsTable) GetUniquePartitionList(txn badgerwrap.Txn) ([
|
|||
return resources, nil
|
||||
}
|
||||
|
||||
//todo: need to add unit test
|
||||
func (t *ResourceEventCountsTable) GetPreviousKey(txn badgerwrap.Txn, key *EventCountKey, keyComparator *EventCountKey) (*EventCountKey, error) {
|
||||
partitionList, err := t.GetUniquePartitionList(txn)
|
||||
if err != nil {
|
||||
|
@ -176,7 +174,6 @@ func (t *ResourceEventCountsTable) GetPreviousKey(txn badgerwrap.Txn, key *Event
|
|||
return &EventCountKey{}, fmt.Errorf("failed to get any previous key in table:%v, for key:%v, keyComparator:%v", t.tableName, key.String(), keyComparator)
|
||||
}
|
||||
|
||||
//todo: need to add unit test
|
||||
func (t *ResourceEventCountsTable) getLastMatchingKeyInPartition(txn badgerwrap.Txn, curPartition string, curKey *EventCountKey, keyComparator *EventCountKey) (bool, *EventCountKey, error) {
|
||||
iterOpt := badger.DefaultIteratorOptions
|
||||
iterOpt.Reverse = true
|
||||
|
|
|
@ -13,6 +13,7 @@ package typed
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/salesforce/sloop/pkg/sloop/kubeextractor"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -48,5 +49,145 @@ func Test_ResourceEventCountsTable_SetWorks(t *testing.T) {
|
|||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
}
|
||||
|
||||
func helper_update_ResourceEventCountsTable(t *testing.T, keys []string, val *ResourceEventCounts) (badgerwrap.DB, *ResourceEventCountsTable) {
|
||||
b, err := (&badgerwrap.MockFactory{}).Open(badger.DefaultOptions(""))
|
||||
assert.Nil(t, err)
|
||||
wt := OpenResourceEventCountsTable()
|
||||
err = b.Update(func(txn badgerwrap.Txn) error {
|
||||
var txerr error
|
||||
for _, key := range keys {
|
||||
txerr = wt.Set(txn, key, val)
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
}
|
||||
// Add some keys outside the range
|
||||
txerr = txn.Set([]byte("/a/123/"), []byte{})
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
txerr = txn.Set([]byte("/zzz/123/"), []byte{})
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
return b, wt
|
||||
}
|
||||
|
||||
func Test_ResourceEventCountsTable_GetUniquePartitionList_Success(t *testing.T) {
|
||||
if helper_ResourceEventCounts_ShouldSkip() {
|
||||
return
|
||||
}
|
||||
|
||||
db, wt := helper_update_ResourceEventCountsTable(t, (&EventCountKey{}).SetTestKeys(), (&EventCountKey{}).SetTestValue())
|
||||
var partList []string
|
||||
var err1 error
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partList, err1 = wt.GetUniquePartitionList(txn)
|
||||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, err1)
|
||||
assert.Len(t, partList, 3)
|
||||
assert.Contains(t, partList, someMinPartition)
|
||||
assert.Contains(t, partList, someMiddlePartition)
|
||||
assert.Contains(t, partList, someMaxPartition)
|
||||
}
|
||||
|
||||
func Test_ResourceEventCountsTable_GetUniquePartitionList_EmptyPartition(t *testing.T) {
|
||||
if helper_ResourceEventCounts_ShouldSkip() {
|
||||
return
|
||||
}
|
||||
|
||||
db, wt := helper_update_ResourceEventCountsTable(t, []string{}, (&EventCountKey{}).GetTestValue())
|
||||
var partList []string
|
||||
var err1 error
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partList, err1 = wt.GetUniquePartitionList(txn)
|
||||
return err1
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, partList, 0)
|
||||
}
|
||||
|
||||
func helper_testKeys() []string {
|
||||
return []string{
|
||||
// someMaxTs partition
|
||||
"/eventcount/001546405200/Pod/user-j/sync-123/sam-partition-testdata",
|
||||
"/eventcount/001546405200/Pod/user-j/sync-123/sam-partition-test",
|
||||
|
||||
"/eventcount/001546405200/somekind/somenamespace/somename/previous-partition-test",
|
||||
"/eventcount/001546405200/user-a/somenamespace/somename/478-aaa",
|
||||
"/eventcount/001546405200/user-b/somenamespace/somename/477-bbb",
|
||||
"/eventcount/001546405200/somekind/somenamespacetest/somename/skipped-partition",
|
||||
|
||||
// someMiddleTs partition
|
||||
"/eventcount/001546401600/somekind/somenamespace/somename/previous-partition-test",
|
||||
"/eventcount/001546401600/somekind/somenamespace/somename/cde-4ffc-11e9-8e26-123",
|
||||
|
||||
// someTs partition
|
||||
"/eventcount/001546398000/somekind/somenamespace/somename/previous-partition-test",
|
||||
"/eventcount/001546398000/somekind123/somenamespace/somename/68510937-4ffc-11e9-8e26-123",
|
||||
"/eventcount/001546398000/somekind/somenamespacetest/somename/skipped-partition",
|
||||
|
||||
"/eventcount/001546398000/somekindaaa/somenamespaceaaa/somenameaaa/aaaa",
|
||||
}
|
||||
}
|
||||
|
||||
func Test_EventCount_GetPreviousKey_Success(t *testing.T) {
|
||||
untyped.TestHookSetPartitionDuration(time.Hour)
|
||||
db, wt := helper_update_ResourceEventCountsTable(t, helper_testKeys(), (&EventCountKey{}).SetTestValue())
|
||||
var partRes *EventCountKey
|
||||
var err1 error
|
||||
|
||||
// testing in the same partition
|
||||
curKey := NewEventCountKey(someMaxTs, kubeextractor.PodKind, "user-j", "sync-123", "sam-partition-testdata")
|
||||
keyComparator := NewEventCountKeyComparator(kubeextractor.PodKind, "user-j", "sync-123", "")
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partRes, err1 = wt.GetPreviousKey(txn, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
expectedKey := NewEventCountKey(someMaxTs, kubeextractor.PodKind, "user-j", "sync-123", "sam-partition-test")
|
||||
assert.Equal(t, expectedKey, partRes)
|
||||
|
||||
// testing in the previous partition
|
||||
curKey = NewEventCountKey(someMaxTs, someKind, someNamespace, someName, "previous-partition-test")
|
||||
keyComparator = NewEventCountKeyComparator(someKind, someNamespace, someName, "previous-partition-test")
|
||||
err = db.View(func(txn badgerwrap.Txn) error {
|
||||
partRes, err1 = wt.GetPreviousKey(txn, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
expectedKey = NewEventCountKey(someMiddleTs, someKind, someNamespace, someName, "previous-partition-test")
|
||||
assert.Equal(t, expectedKey, partRes)
|
||||
|
||||
// testing in skipped partition
|
||||
curKey = NewEventCountKey(someMaxTs, someKind, "somenamespacetest", someName, "skipped-partition")
|
||||
keyComparator = NewEventCountKeyComparator(someKind, "somenamespacetest", someName, "skipped-partition")
|
||||
err = db.View(func(txn badgerwrap.Txn) error {
|
||||
partRes, err1 = wt.GetPreviousKey(txn, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
expectedKey = NewEventCountKey(someTs, someKind, "somenamespacetest", someName, "skipped-partition")
|
||||
assert.Equal(t, expectedKey, partRes)
|
||||
}
|
||||
|
||||
func Test_EventCount_GetPreviousKey_Fail(t *testing.T) {
|
||||
db, wt := helper_update_ResourceEventCountsTable(t, (&EventCountKey{}).SetTestKeys(), (&EventCountKey{}).SetTestValue())
|
||||
var partRes *EventCountKey
|
||||
var err1 error
|
||||
curKey := NewEventCountKey(someMaxTs, someKind, someNamespace, someName, someUid)
|
||||
keyComparator := NewEventCountKeyComparator(someKind+"b", someNamespace, someName, someUid)
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partRes, err1 = wt.GetPreviousKey(txn, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, &EventCountKey{}, partRes)
|
||||
}
|
||||
|
|
|
@ -35,6 +35,10 @@ func NewResourceSummaryKey(timestamp time.Time, kind string, namespace string, n
|
|||
return &ResourceSummaryKey{PartitionId: partitionId, Kind: kind, Namespace: namespace, Name: name, Uid: uid}
|
||||
}
|
||||
|
||||
func NewResourceSummaryKeyComparator(kind string, namespace string, name string, uid string) *ResourceSummaryKey {
|
||||
return &ResourceSummaryKey{Kind: kind, Namespace: namespace, Name: name, Uid: uid}
|
||||
}
|
||||
|
||||
func (_ *ResourceSummaryKey) TableName() string {
|
||||
return "ressum"
|
||||
}
|
||||
|
@ -60,7 +64,11 @@ func (k *ResourceSummaryKey) Parse(key string) error {
|
|||
|
||||
//todo: need to make sure it can work as keyPrefix when some fields are empty
|
||||
func (k *ResourceSummaryKey) String() string {
|
||||
return fmt.Sprintf("/%v/%v/%v/%v/%v/%v", k.TableName(), k.PartitionId, k.Kind, k.Namespace, k.Name, k.Uid)
|
||||
if k.Uid == "" {
|
||||
return fmt.Sprintf("/%v/%v/%v/%v/%v", k.TableName(), k.PartitionId, k.Kind, k.Namespace, k.Name)
|
||||
} else {
|
||||
return fmt.Sprintf("/%v/%v/%v/%v/%v/%v", k.TableName(), k.PartitionId, k.Kind, k.Namespace, k.Name, k.Uid)
|
||||
}
|
||||
}
|
||||
|
||||
func (k *ResourceSummaryKey) SetPartitionId(newPartitionId string) {
|
||||
|
|
|
@ -79,8 +79,8 @@ func Test_ResourceSummary_RangeRead(t *testing.T) {
|
|||
createTimeProto, err := ptypes.TimestampProto(someFirstSeenTime)
|
||||
assert.Nil(t, err)
|
||||
|
||||
key1 := NewResourceSummaryKey(someTs, someKind, someNamespace, someName+"a", someUid)
|
||||
key2 := NewResourceSummaryKey(someTs, someKind, someNamespace, someName+"b", someUid)
|
||||
key1 := NewResourceSummaryKey(someTs, someKind, someNamespace, someName, someUid+"a")
|
||||
key2 := NewResourceSummaryKey(someTs, someKind, someNamespace, someName, someUid+"b")
|
||||
val := &ResourceSummary{FirstSeen: createTimeProto}
|
||||
|
||||
b, err := (&badgerwrap.MockFactory{}).Open(badger.DefaultOptions(""))
|
||||
|
@ -192,7 +192,7 @@ func Test_ResourceSummary_TestMinAndMaxKeys(t *testing.T) {
|
|||
var keys []string
|
||||
for i := 'a'; i < 'd'; i++ {
|
||||
// add keys in ascending order
|
||||
keys = append(keys, NewResourceSummaryKey(someTs, someKind, someNamespace, someName+string(i), someUid).String())
|
||||
keys = append(keys, NewResourceSummaryKey(someTs, someKind, someNamespace, someName, someUid+string(i)).String())
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
@ -205,8 +205,8 @@ func Test_ResourceSummary_TestMinAndMaxKeys(t *testing.T) {
|
|||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "/ressum/001546398000/somekind/somenamespace/somenamea/68510937-4ffc-11e9-8e26-1418775557c8", minKey)
|
||||
assert.Equal(t, "/ressum/001546398000/somekind/somenamespace/somenamec/68510937-4ffc-11e9-8e26-1418775557c8", maxKey)
|
||||
assert.Equal(t, "/ressum/001546398000/somekind/somenamespace/somename/68510937-4ffc-11e9-8e26-1418775557c8a", minKey)
|
||||
assert.Equal(t, "/ressum/001546398000/somekind/somenamespace/somename/68510937-4ffc-11e9-8e26-1418775557c8c", maxKey)
|
||||
}
|
||||
|
||||
func Test_ResourceSummary_TestGetMinMaxParititons(t *testing.T) {
|
||||
|
@ -214,7 +214,7 @@ func Test_ResourceSummary_TestGetMinMaxParititons(t *testing.T) {
|
|||
var keys []string
|
||||
for i := 'a'; i < 'd'; i++ {
|
||||
// add keys in ascending order
|
||||
keys = append(keys, NewResourceSummaryKey(someTs, someKind, someNamespace, someName+string(i), someUid).String())
|
||||
keys = append(keys, NewResourceSummaryKey(someTs, someKind, someNamespace, someName, someUid+string(i)).String())
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
@ -234,20 +234,19 @@ func Test_ResourceSummary_TestGetMinMaxParititons(t *testing.T) {
|
|||
}
|
||||
|
||||
func Test_ResourceSummary_RangeReadWithTimeRange(t *testing.T) {
|
||||
var someTs = time.Date(2019, 1, 2, 3, 4, 5, 6, time.UTC)
|
||||
keysFn := func() []string {
|
||||
var keys []string
|
||||
for i := 'a'; i < 'c'; i++ {
|
||||
// add keys in ascending order
|
||||
keys = append(keys, NewResourceSummaryKey(someTs, someKind, someNamespace, someName+string(i), someUid).String())
|
||||
keys = append(keys, NewResourceSummaryKey(someTs, someKind, someNamespace, someName, someUid+string(i)).String())
|
||||
}
|
||||
for i := 'c'; i < 'e'; i++ {
|
||||
// add keys in ascending order
|
||||
keys = append(keys, NewResourceSummaryKey(someTs.Add(1*time.Hour), someKind, someNamespace, someName+string(i), someUid).String())
|
||||
keys = append(keys, NewResourceSummaryKey(someMiddleTs, someKind, someNamespace, someName, someUid+string(i)).String())
|
||||
}
|
||||
for i := 'e'; i < 'g'; i++ {
|
||||
// add keys in ascending order
|
||||
keys = append(keys, NewResourceSummaryKey(someTs.Add(2*time.Hour), someKind, someNamespace, someName+string(i), someUid).String())
|
||||
keys = append(keys, NewResourceSummaryKey(someMaxTs, someKind, someNamespace, someName, someUid+string(i)).String())
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
@ -265,14 +264,83 @@ func Test_ResourceSummary_RangeReadWithTimeRange(t *testing.T) {
|
|||
assert.Nil(t, err)
|
||||
assert.Len(t, retval, 2)
|
||||
expectedKey := &ResourceSummaryKey{}
|
||||
err = expectedKey.Parse("/ressum/001546401600/somekind/somenamespace/somenamec/68510937-4ffc-11e9-8e26-1418775557c8")
|
||||
err = expectedKey.Parse("/ressum/001546401600/somekind/somenamespace/somename/68510937-4ffc-11e9-8e26-1418775557c8c")
|
||||
assert.Nil(t, err)
|
||||
assert.Contains(t, retval, *expectedKey)
|
||||
err = expectedKey.Parse("/ressum/001546401600/somekind/somenamespace/somenamed/68510937-4ffc-11e9-8e26-1418775557c8")
|
||||
err = expectedKey.Parse("/ressum/001546401600/somekind/somenamespace/somename/68510937-4ffc-11e9-8e26-1418775557c8d")
|
||||
assert.Nil(t, err)
|
||||
assert.Contains(t, retval, *expectedKey)
|
||||
}
|
||||
|
||||
func Test_ResourceSummary_getLastMatchingKeyInPartition_FoundInPreviousPartition(t *testing.T) {
|
||||
db, wt := helper_update_ResourceSummaryTable(t, (&ResourceSummaryKey{}).SetTestKeys(), (&ResourceSummaryKey{}).SetTestValue())
|
||||
var keyRes *ResourceSummaryKey
|
||||
var err1 error
|
||||
var found bool
|
||||
curKey := NewResourceSummaryKey(someMaxTs, someKind, someNamespace, someName, someUid+"c")
|
||||
keyComparator := NewResourceSummaryKeyComparator(someKind, someNamespace, someName, someUid+"b")
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMiddlePartition, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
assert.True(t, found)
|
||||
expectedKey := NewResourceSummaryKey(someMiddleTs, someKind, someNamespace, someName, someUid+"b")
|
||||
assert.Equal(t, expectedKey, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func Test_ResourceSummary_getLastMatchingKeyInPartition_FoundInSamePartition(t *testing.T) {
|
||||
db, wt := helper_update_ResourceSummaryTable(t, (&ResourceSummaryKey{}).SetTestKeys(), (&ResourceSummaryKey{}).SetTestValue())
|
||||
var keyRes *ResourceSummaryKey
|
||||
var err1 error
|
||||
var found bool
|
||||
curKey := NewResourceSummaryKey(someTs, someKind, someNamespace, someName, someUid+"a")
|
||||
keyComparator := NewResourceSummaryKeyComparator(someKind, someNamespace, someName, someUid)
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMinPartition, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
|
||||
assert.True(t, found)
|
||||
expectedKey := NewResourceSummaryKey(someTs, someKind, someNamespace, someName, someUid)
|
||||
assert.Equal(t, expectedKey, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func Test_ResourceSummary_getLastMatchingKeyInPartition_SameKeySearch(t *testing.T) {
|
||||
db, wt := helper_update_ResourceSummaryTable(t, (&ResourceSummaryKey{}).SetTestKeys(), (&ResourceSummaryKey{}).SetTestValue())
|
||||
var keyRes *ResourceSummaryKey
|
||||
var err1 error
|
||||
var found bool
|
||||
curKey := NewResourceSummaryKey(someTs, someKind, someNamespace, someName, someUid+"a")
|
||||
keyComparator := NewResourceSummaryKeyComparator(someKind, someNamespace, someName, someUid+"a")
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMinPartition, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
|
||||
assert.False(t, found)
|
||||
assert.Equal(t, &ResourceSummaryKey{}, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func Test_ResourceSummary_getLastMatchingKeyInPartition_NotFound(t *testing.T) {
|
||||
db, wt := helper_update_ResourceSummaryTable(t, (&ResourceSummaryKey{}).SetTestKeys(), (&ResourceSummaryKey{}).SetTestValue())
|
||||
var keyRes *ResourceSummaryKey
|
||||
var err1 error
|
||||
var found bool
|
||||
curKey := NewResourceSummaryKey(someMaxTs, someKind, someNamespace, someName, someUid)
|
||||
keyComparator := NewResourceSummaryKeyComparator(someKind+"c", someNamespace, someName, someUid)
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMinPartition, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
|
||||
assert.False(t, found)
|
||||
assert.Equal(t, &ResourceSummaryKey{}, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func (_ *ResourceSummaryKey) GetTestKey() string {
|
||||
k := NewResourceSummaryKey(someTs, "someKind", "someNamespace", "someName", "someUuid")
|
||||
return k.String()
|
||||
|
@ -281,3 +349,22 @@ func (_ *ResourceSummaryKey) GetTestKey() string {
|
|||
func (_ *ResourceSummaryKey) GetTestValue() *ResourceSummary {
|
||||
return &ResourceSummary{}
|
||||
}
|
||||
|
||||
func (_ *ResourceSummaryKey) SetTestKeys() []string {
|
||||
untyped.TestHookSetPartitionDuration(time.Hour)
|
||||
var keys []string
|
||||
i := 'a'
|
||||
for curTime := someTs; !curTime.After(someMaxTs); curTime = curTime.Add(untyped.GetPartitionDuration()) {
|
||||
// add keys in ascending order
|
||||
keys = append(keys, NewResourceSummaryKey(curTime, someKind, someNamespace, someName, someUid).String())
|
||||
keys = append(keys, NewResourceSummaryKey(curTime, someKind, someNamespace, someName, someUid+string(i)).String())
|
||||
i++
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
func (_ *ResourceSummaryKey) SetTestValue() *ResourceSummary {
|
||||
createTimeProto, _ := ptypes.TimestampProto(someFirstSeenTime)
|
||||
return &ResourceSummary{FirstSeen: createTimeProto}
|
||||
}
|
||||
|
|
|
@ -132,7 +132,6 @@ func (t *ResourceSummaryTable) GetMinMaxPartitions(txn badgerwrap.Txn) (bool, st
|
|||
return true, minKey.PartitionId, maxKey.PartitionId
|
||||
}
|
||||
|
||||
//todo: need to add unit test
|
||||
func (t *ResourceSummaryTable) GetUniquePartitionList(txn badgerwrap.Txn) ([]string, error) {
|
||||
resources := []string{}
|
||||
ok, minPar, maxPar := t.GetMinMaxPartitions(txn)
|
||||
|
@ -152,7 +151,6 @@ func (t *ResourceSummaryTable) GetUniquePartitionList(txn badgerwrap.Txn) ([]str
|
|||
return resources, nil
|
||||
}
|
||||
|
||||
//todo: need to add unit test
|
||||
func (t *ResourceSummaryTable) GetPreviousKey(txn badgerwrap.Txn, key *ResourceSummaryKey, keyComparator *ResourceSummaryKey) (*ResourceSummaryKey, error) {
|
||||
partitionList, err := t.GetUniquePartitionList(txn)
|
||||
if err != nil {
|
||||
|
@ -176,7 +174,6 @@ func (t *ResourceSummaryTable) GetPreviousKey(txn badgerwrap.Txn, key *ResourceS
|
|||
return &ResourceSummaryKey{}, fmt.Errorf("failed to get any previous key in table:%v, for key:%v, keyComparator:%v", t.tableName, key.String(), keyComparator)
|
||||
}
|
||||
|
||||
//todo: need to add unit test
|
||||
func (t *ResourceSummaryTable) getLastMatchingKeyInPartition(txn badgerwrap.Txn, curPartition string, curKey *ResourceSummaryKey, keyComparator *ResourceSummaryKey) (bool, *ResourceSummaryKey, error) {
|
||||
iterOpt := badger.DefaultIteratorOptions
|
||||
iterOpt.Reverse = true
|
||||
|
|
|
@ -48,5 +48,96 @@ func Test_ResourceSummaryTable_SetWorks(t *testing.T) {
|
|||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
}
|
||||
|
||||
func helper_update_ResourceSummaryTable(t *testing.T, keys []string, val *ResourceSummary) (badgerwrap.DB, *ResourceSummaryTable) {
|
||||
b, err := (&badgerwrap.MockFactory{}).Open(badger.DefaultOptions(""))
|
||||
assert.Nil(t, err)
|
||||
wt := OpenResourceSummaryTable()
|
||||
err = b.Update(func(txn badgerwrap.Txn) error {
|
||||
var txerr error
|
||||
for _, key := range keys {
|
||||
txerr = wt.Set(txn, key, val)
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
}
|
||||
// Add some keys outside the range
|
||||
txerr = txn.Set([]byte("/a/123/"), []byte{})
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
txerr = txn.Set([]byte("/zzz/123/"), []byte{})
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
return b, wt
|
||||
}
|
||||
|
||||
func Test_ResourceSummaryTable_GetUniquePartitionList_Success(t *testing.T) {
|
||||
if helper_ResourceSummary_ShouldSkip() {
|
||||
return
|
||||
}
|
||||
|
||||
db, wt := helper_update_ResourceSummaryTable(t, (&ResourceSummaryKey{}).SetTestKeys(), (&ResourceSummaryKey{}).SetTestValue())
|
||||
var partList []string
|
||||
var err1 error
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partList, err1 = wt.GetUniquePartitionList(txn)
|
||||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, err1)
|
||||
assert.Len(t, partList, 3)
|
||||
assert.Contains(t, partList, someMinPartition)
|
||||
assert.Contains(t, partList, someMiddlePartition)
|
||||
assert.Contains(t, partList, someMaxPartition)
|
||||
}
|
||||
|
||||
func Test_ResourceSummaryTable_GetUniquePartitionList_EmptyPartition(t *testing.T) {
|
||||
if helper_ResourceSummary_ShouldSkip() {
|
||||
return
|
||||
}
|
||||
|
||||
db, wt := helper_update_ResourceSummaryTable(t, []string{}, &ResourceSummary{})
|
||||
var partList []string
|
||||
var err1 error
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partList, err1 = wt.GetUniquePartitionList(txn)
|
||||
return err1
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, partList, 0)
|
||||
}
|
||||
|
||||
func Test_ResourceSummary_GetPreviousKey_Success(t *testing.T) {
|
||||
db, wt := helper_update_ResourceSummaryTable(t, (&ResourceSummaryKey{}).SetTestKeys(), (&ResourceSummaryKey{}).SetTestValue())
|
||||
var partRes *ResourceSummaryKey
|
||||
var err1 error
|
||||
curKey := NewResourceSummaryKey(someMaxTs, someKind, someNamespace, someName, someUid+"c")
|
||||
keyComparator := NewResourceSummaryKeyComparator(someKind, someNamespace, someName, someUid+"b")
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partRes, err1 = wt.GetPreviousKey(txn, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
expectedKey := NewResourceSummaryKey(someTs.Add(1*time.Hour), someKind, someNamespace, someName, someUid+"b")
|
||||
assert.Equal(t, expectedKey, partRes)
|
||||
}
|
||||
|
||||
func Test_ResourceSummary_GetPreviousKey_Fail(t *testing.T) {
|
||||
db, wt := helper_update_ResourceSummaryTable(t, (&ResourceSummaryKey{}).SetTestKeys(), (&ResourceSummaryKey{}).SetTestValue())
|
||||
var partRes *ResourceSummaryKey
|
||||
var err1 error
|
||||
curKey := NewResourceSummaryKey(someTs.Add(2*time.Hour), someKind, someNamespace, someName, someUid)
|
||||
keyComparator := NewResourceSummaryKeyComparator(someKind+"b", someNamespace, someName, someUid)
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partRes, err1 = wt.GetPreviousKey(txn, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, &ResourceSummaryKey{}, partRes)
|
||||
}
|
||||
|
|
|
@ -132,7 +132,6 @@ func (t *ValueTypeTable) GetMinMaxPartitions(txn badgerwrap.Txn) (bool, string,
|
|||
return true, minKey.PartitionId, maxKey.PartitionId
|
||||
}
|
||||
|
||||
//todo: need to add unit test
|
||||
func (t *ValueTypeTable) GetUniquePartitionList(txn badgerwrap.Txn) ([]string, error) {
|
||||
resources := []string{}
|
||||
ok, minPar, maxPar := t.GetMinMaxPartitions(txn)
|
||||
|
@ -152,7 +151,6 @@ func (t *ValueTypeTable) GetUniquePartitionList(txn badgerwrap.Txn) ([]string, e
|
|||
return resources, nil
|
||||
}
|
||||
|
||||
//todo: need to add unit test
|
||||
func (t *ValueTypeTable) GetPreviousKey(txn badgerwrap.Txn, key *KeyType, keyComparator *KeyType) (*KeyType, error) {
|
||||
partitionList, err := t.GetUniquePartitionList(txn)
|
||||
if err != nil {
|
||||
|
@ -176,7 +174,6 @@ func (t *ValueTypeTable) GetPreviousKey(txn badgerwrap.Txn, key *KeyType, keyCom
|
|||
return &KeyType{}, fmt.Errorf("failed to get any previous key in table:%v, for key:%v, keyComparator:%v", t.tableName, key.String(), keyComparator)
|
||||
}
|
||||
|
||||
//todo: need to add unit test
|
||||
func (t *ValueTypeTable) getLastMatchingKeyInPartition(txn badgerwrap.Txn, curPartition string, curKey *KeyType, keyComparator *KeyType) (bool, *KeyType, error) {
|
||||
iterOpt := badger.DefaultIteratorOptions
|
||||
iterOpt.Reverse = true
|
||||
|
|
|
@ -48,5 +48,67 @@ func Test_ValueTypeTable_SetWorks(t *testing.T) {
|
|||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
}
|
||||
|
||||
func helper_update_ValueTypeTable(t *testing.T, keys []string, val *ValueType) (badgerwrap.DB, *ValueTypeTable) {
|
||||
b, err := (&badgerwrap.MockFactory{}).Open(badger.DefaultOptions(""))
|
||||
assert.Nil(t, err)
|
||||
wt := OpenValueTypeTable()
|
||||
err = b.Update(func(txn badgerwrap.Txn) error {
|
||||
var txerr error
|
||||
for _, key := range keys {
|
||||
txerr = wt.Set(txn, key, val)
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
}
|
||||
// Add some keys outside the range
|
||||
txerr = txn.Set([]byte("/a/123/"), []byte{})
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
txerr = txn.Set([]byte("/zzz/123/"), []byte{})
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
return b, wt
|
||||
}
|
||||
|
||||
func Test_ValueTypeTable_GetUniquePartitionList_Success(t *testing.T) {
|
||||
if helper_ValueType_ShouldSkip() {
|
||||
return
|
||||
}
|
||||
|
||||
db, wt := helper_update_ValueTypeTable(t, (&KeyType{}).SetTestKeys(), (&KeyType{}).SetTestValue())
|
||||
var partList []string
|
||||
var err1 error
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partList, err1 = wt.GetUniquePartitionList(txn)
|
||||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, err1)
|
||||
assert.Len(t, partList, 3)
|
||||
assert.Contains(t, partList, someMinPartition)
|
||||
assert.Contains(t, partList, someMiddlePartition)
|
||||
assert.Contains(t, partList, someMaxPartition)
|
||||
}
|
||||
|
||||
func Test_ValueTypeTable_GetUniquePartitionList_EmptyPartition(t *testing.T) {
|
||||
if helper_ValueType_ShouldSkip() {
|
||||
return
|
||||
}
|
||||
|
||||
db, wt := helper_update_ValueTypeTable(t, []string{}, &ValueType{})
|
||||
var partList []string
|
||||
var err1 error
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partList, err1 = wt.GetUniquePartitionList(txn)
|
||||
return err1
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, partList, 0)
|
||||
}
|
||||
|
|
|
@ -48,6 +48,10 @@ func (_ *KeyType) GetTestKey() string {
|
|||
panic("Placeholder key should not be used")
|
||||
}
|
||||
|
||||
func (_ *KeyType) SetTestKeys() []string {
|
||||
panic("Placeholder key should not be used")
|
||||
}
|
||||
|
||||
func (_ *KeyType) String() string {
|
||||
panic("Placeholder key should not be used")
|
||||
}
|
||||
|
@ -56,6 +60,10 @@ func (_ *KeyType) GetTestValue() *ValueType {
|
|||
panic("Placeholder key should not be used")
|
||||
}
|
||||
|
||||
func (p *KeyType) SetTestValue() *ValueType {
|
||||
return &ValueType{}
|
||||
}
|
||||
|
||||
func (_ *KeyType) SetPartitionId(newPartitionId string) {
|
||||
panic("Placeholder key should not be used")
|
||||
}
|
||||
|
|
|
@ -33,6 +33,10 @@ func NewWatchActivityKey(partitionId string, kind string, namespace string, name
|
|||
return &WatchActivityKey{PartitionId: partitionId, Kind: kind, Namespace: namespace, Name: name, Uid: uid}
|
||||
}
|
||||
|
||||
func NewWatchActivityKeyComparator(kind string, namespace string, name string, uid string) *WatchActivityKey {
|
||||
return &WatchActivityKey{Kind: kind, Namespace: namespace, Name: name, Uid: uid}
|
||||
}
|
||||
|
||||
func (_ *WatchActivityKey) TableName() string {
|
||||
return "watchactivity"
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
package typed
|
||||
|
||||
import (
|
||||
"github.com/dgraph-io/badger"
|
||||
"github.com/salesforce/sloop/pkg/sloop/store/untyped"
|
||||
"github.com/salesforce/sloop/pkg/sloop/store/untyped/badgerwrap"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -32,7 +31,7 @@ func Test_WatchActivityKey_ParseCorrect(t *testing.T) {
|
|||
k := &WatchActivityKey{}
|
||||
err := k.Parse(someWatchActivityKey)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, somePartition, k.PartitionId)
|
||||
assert.Equal(t, someMinPartition, k.PartitionId)
|
||||
assert.Equal(t, someNamespace, k.Namespace)
|
||||
assert.Equal(t, someName, k.Name)
|
||||
}
|
||||
|
@ -41,48 +40,12 @@ func Test_WatchActivityKey_ValidateWorks(t *testing.T) {
|
|||
assert.Nil(t, (&WatchActivityKey{}).ValidateKey(someWatchActivityKey))
|
||||
}
|
||||
|
||||
func helper_update_watchactivity_table(t *testing.T) (badgerwrap.DB, *WatchActivityTable) {
|
||||
untyped.TestHookSetPartitionDuration(time.Hour)
|
||||
partitionId := untyped.GetPartitionId(someTs)
|
||||
var keys []string
|
||||
for i := 'a'; i < 'd'; i++ {
|
||||
// add keys in ascending order
|
||||
keys = append(keys, NewWatchActivityKey(partitionId, someKind+string(i), someNamespace, someName, someUid).String())
|
||||
}
|
||||
val := &WatchActivity{}
|
||||
b, err := (&badgerwrap.MockFactory{}).Open(badger.DefaultOptions(""))
|
||||
assert.Nil(t, err)
|
||||
wat := OpenWatchActivityTable()
|
||||
err = b.Update(func(txn badgerwrap.Txn) error {
|
||||
var txerr error
|
||||
for _, key := range keys {
|
||||
txerr = wat.Set(txn, key, val)
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
}
|
||||
// Add some keys outside the range
|
||||
txerr = txn.Set([]byte("/a/123/"), []byte{})
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
txerr = txn.Set([]byte("/zzz/123/"), []byte{})
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
return b, wat
|
||||
}
|
||||
|
||||
func Test_WatchActivity_PutThenGet_SameData(t *testing.T) {
|
||||
db, wat := helper_update_watchactivity_table(t)
|
||||
|
||||
db, wat := helper_update_WatchActivityTable(t, (&WatchActivityKey{}).SetTestKeys(), (&WatchActivityKey{}).SetTestValue())
|
||||
var retval *WatchActivity
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
var txerr error
|
||||
retval, txerr = wat.Get(txn, "/watchactivity/001546398000/somekinda/somenamespace/somename/68510937-4ffc-11e9-8e26-1418775557c8")
|
||||
retval, txerr = wat.Get(txn, "/watchactivity/001546398000/somekind/somenamespace/somename/68510937-4ffc-11e9-8e26-1418775557c8")
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
|
@ -93,7 +56,7 @@ func Test_WatchActivity_PutThenGet_SameData(t *testing.T) {
|
|||
}
|
||||
|
||||
func Test_WatchActivity_TestMinAndMaxKeys(t *testing.T) {
|
||||
db, wt := helper_update_watchactivity_table(t)
|
||||
db, wt := helper_update_WatchActivityTable(t, (&WatchActivityKey{}).SetTestKeys(), (&WatchActivityKey{}).SetTestValue())
|
||||
var minKey string
|
||||
var maxKey string
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
|
@ -102,12 +65,12 @@ func Test_WatchActivity_TestMinAndMaxKeys(t *testing.T) {
|
|||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "/watchactivity/001546398000/somekinda/somenamespace/somename/68510937-4ffc-11e9-8e26-1418775557c8", minKey)
|
||||
assert.Equal(t, "/watchactivity/001546398000/somekindc/somenamespace/somename/68510937-4ffc-11e9-8e26-1418775557c8", maxKey)
|
||||
assert.Equal(t, "/watchactivity/001546398000/somekind/somenamespace/somename/68510937-4ffc-11e9-8e26-1418775557c8", minKey)
|
||||
assert.Equal(t, "/watchactivity/001546405200/somekind/somenamespace/somename/68510937-4ffc-11e9-8e26-1418775557c8c", maxKey)
|
||||
}
|
||||
|
||||
func Test_WatchActivity_TestGetMinMaxPartitions(t *testing.T) {
|
||||
db, wt := helper_update_watchactivity_table(t)
|
||||
db, wt := helper_update_WatchActivityTable(t, (&WatchActivityKey{}).SetTestKeys(), (&WatchActivityKey{}).SetTestValue())
|
||||
var minPartition string
|
||||
var maxPartition string
|
||||
var found bool
|
||||
|
@ -118,15 +81,104 @@ func Test_WatchActivity_TestGetMinMaxPartitions(t *testing.T) {
|
|||
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, somePartition, minPartition)
|
||||
assert.Equal(t, somePartition, maxPartition)
|
||||
assert.Equal(t, someMinPartition, minPartition)
|
||||
assert.Equal(t, someMaxPartition, maxPartition)
|
||||
}
|
||||
|
||||
func Test_WatchActivity_getLastMatchingKeyInPartition_FoundInPreviousPartition(t *testing.T) {
|
||||
db, wt := helper_update_WatchActivityTable(t, (&WatchActivityKey{}).SetTestKeys(), (&WatchActivityKey{}).GetTestValue())
|
||||
var keyRes *WatchActivityKey
|
||||
var err1 error
|
||||
var found bool
|
||||
|
||||
curKey := NewWatchActivityKey(someMaxPartition, someKind, someNamespace, someName, someUid)
|
||||
keyPrefix := NewWatchActivityKey(someMiddlePartition, someKind, someNamespace, someName, someUid)
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMiddlePartition, curKey, keyPrefix)
|
||||
return err1
|
||||
})
|
||||
assert.True(t, found)
|
||||
expectedKey := NewWatchActivityKey(someMiddlePartition, someKind, someNamespace, someName, someUid+"b")
|
||||
assert.Equal(t, expectedKey, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func Test_WatchActivity_getLastMatchingKeyInPartition_FoundInSamePartition(t *testing.T) {
|
||||
db, wt := helper_update_WatchActivityTable(t, (&WatchActivityKey{}).SetTestKeys(), (&WatchActivityKey{}).GetTestValue())
|
||||
var keyRes *WatchActivityKey
|
||||
var err1 error
|
||||
var found bool
|
||||
curKey := NewWatchActivityKey(someMaxPartition, someKind, someNamespace, someName, someUid+"a")
|
||||
keyPrefix := NewWatchActivityKey(someMaxPartition, someKind, someNamespace, someName, someUid)
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMaxPartition, curKey, keyPrefix)
|
||||
return err1
|
||||
})
|
||||
|
||||
assert.True(t, found)
|
||||
expectedKey := NewWatchActivityKey(someMaxPartition, someKind, someNamespace, someName, someUid)
|
||||
assert.Equal(t, expectedKey, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func Test_WatchActivity_getLastMatchingKeyInPartition_SameKeySearch_NotFound(t *testing.T) {
|
||||
db, wt := helper_update_WatchActivityTable(t, (&WatchActivityKey{}).SetTestKeys(), (&WatchActivityKey{}).GetTestValue())
|
||||
var keyRes *WatchActivityKey
|
||||
var err1 error
|
||||
var found bool
|
||||
curKey := NewWatchActivityKey(someMaxPartition, someKind, someNamespace, someName, someUid+"c")
|
||||
keyPrefix := NewWatchActivityKey(someMaxPartition, someKind, someNamespace, someName, someUid+"c")
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMaxPartition, curKey, keyPrefix)
|
||||
return err1
|
||||
})
|
||||
|
||||
assert.False(t, found)
|
||||
assert.Equal(t, &WatchActivityKey{}, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func Test_WatchActivity_getLastMatchingKeyInPartition_NotFound(t *testing.T) {
|
||||
db, wt := helper_update_WatchActivityTable(t, (&WatchActivityKey{}).SetTestKeys(), (&WatchActivityKey{}).GetTestValue())
|
||||
var keyRes *WatchActivityKey
|
||||
var err1 error
|
||||
var found bool
|
||||
curKey := NewWatchActivityKey(someMaxPartition, someKind, someNamespace, someName, someUid)
|
||||
keyPrefix := NewWatchActivityKey(someMinPartition, someKind+"c", someNamespace, someName, someUid)
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMinPartition, curKey, keyPrefix)
|
||||
return err1
|
||||
})
|
||||
|
||||
assert.False(t, found)
|
||||
assert.Equal(t, &WatchActivityKey{}, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func (_ *WatchActivityKey) GetTestKey() string {
|
||||
k := NewWatchActivityKey("001546398000", someKind, someNamespace, someName, someUid)
|
||||
k := NewWatchActivityKey(someMinPartition, someKind, someNamespace, someName, someUid)
|
||||
return k.String()
|
||||
}
|
||||
|
||||
func (_ *WatchActivityKey) GetTestValue() *WatchActivity {
|
||||
return &WatchActivity{}
|
||||
}
|
||||
|
||||
func (_ *WatchActivityKey) SetTestKeys() []string {
|
||||
untyped.TestHookSetPartitionDuration(time.Hour)
|
||||
var keys []string
|
||||
var partitionId string
|
||||
gap := 0
|
||||
for i := 'a'; i < 'd'; i++ {
|
||||
// add keys in ascending order
|
||||
partitionId = untyped.GetPartitionId(someTs.Add(time.Hour * time.Duration(gap)))
|
||||
keys = append(keys, NewWatchActivityKey(partitionId, someKind, someNamespace, someName, someUid).String())
|
||||
keys = append(keys, NewWatchActivityKey(partitionId, someKind, someNamespace, someName, someUid+string(i)).String())
|
||||
gap++
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
func (_ *WatchActivityKey) SetTestValue() *WatchActivity {
|
||||
return &WatchActivity{}
|
||||
}
|
||||
|
|
|
@ -132,7 +132,6 @@ func (t *WatchActivityTable) GetMinMaxPartitions(txn badgerwrap.Txn) (bool, stri
|
|||
return true, minKey.PartitionId, maxKey.PartitionId
|
||||
}
|
||||
|
||||
//todo: need to add unit test
|
||||
func (t *WatchActivityTable) GetUniquePartitionList(txn badgerwrap.Txn) ([]string, error) {
|
||||
resources := []string{}
|
||||
ok, minPar, maxPar := t.GetMinMaxPartitions(txn)
|
||||
|
@ -152,7 +151,6 @@ func (t *WatchActivityTable) GetUniquePartitionList(txn badgerwrap.Txn) ([]strin
|
|||
return resources, nil
|
||||
}
|
||||
|
||||
//todo: need to add unit test
|
||||
func (t *WatchActivityTable) GetPreviousKey(txn badgerwrap.Txn, key *WatchActivityKey, keyComparator *WatchActivityKey) (*WatchActivityKey, error) {
|
||||
partitionList, err := t.GetUniquePartitionList(txn)
|
||||
if err != nil {
|
||||
|
@ -176,7 +174,6 @@ func (t *WatchActivityTable) GetPreviousKey(txn badgerwrap.Txn, key *WatchActivi
|
|||
return &WatchActivityKey{}, fmt.Errorf("failed to get any previous key in table:%v, for key:%v, keyComparator:%v", t.tableName, key.String(), keyComparator)
|
||||
}
|
||||
|
||||
//todo: need to add unit test
|
||||
func (t *WatchActivityTable) getLastMatchingKeyInPartition(txn badgerwrap.Txn, curPartition string, curKey *WatchActivityKey, keyComparator *WatchActivityKey) (bool, *WatchActivityKey, error) {
|
||||
iterOpt := badger.DefaultIteratorOptions
|
||||
iterOpt.Reverse = true
|
||||
|
|
|
@ -48,5 +48,97 @@ func Test_WatchActivityTable_SetWorks(t *testing.T) {
|
|||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
}
|
||||
|
||||
func helper_update_WatchActivityTable(t *testing.T, keys []string, val *WatchActivity) (badgerwrap.DB, *WatchActivityTable) {
|
||||
untyped.TestHookSetPartitionDuration(time.Hour)
|
||||
b, err := (&badgerwrap.MockFactory{}).Open(badger.DefaultOptions(""))
|
||||
assert.Nil(t, err)
|
||||
wt := OpenWatchActivityTable()
|
||||
err = b.Update(func(txn badgerwrap.Txn) error {
|
||||
var txerr error
|
||||
for _, key := range keys {
|
||||
txerr = wt.Set(txn, key, val)
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
}
|
||||
// Add some keys outside the range
|
||||
txerr = txn.Set([]byte("/a/123/"), []byte{})
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
txerr = txn.Set([]byte("/zzz/123/"), []byte{})
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
return b, wt
|
||||
}
|
||||
|
||||
func Test_WatchActivityTable_GetUniquePartitionList_Success(t *testing.T) {
|
||||
if helper_WatchActivity_ShouldSkip() {
|
||||
return
|
||||
}
|
||||
|
||||
db, wt := helper_update_WatchActivityTable(t, (&WatchActivityKey{}).SetTestKeys(), (&WatchActivityKey{}).SetTestValue())
|
||||
var partList []string
|
||||
var err1 error
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partList, err1 = wt.GetUniquePartitionList(txn)
|
||||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, err1)
|
||||
assert.Len(t, partList, 3)
|
||||
assert.Contains(t, partList, someMinPartition)
|
||||
assert.Contains(t, partList, someMiddlePartition)
|
||||
assert.Contains(t, partList, someMaxPartition)
|
||||
}
|
||||
|
||||
func Test_WatchActivityTable_GetUniquePartitionList_EmptyPartition(t *testing.T) {
|
||||
if helper_WatchActivity_ShouldSkip() {
|
||||
return
|
||||
}
|
||||
|
||||
db, wt := helper_update_WatchActivityTable(t, []string{}, &WatchActivity{})
|
||||
var partList []string
|
||||
var err1 error
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partList, err1 = wt.GetUniquePartitionList(txn)
|
||||
return err1
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, partList, 0)
|
||||
}
|
||||
|
||||
func Test_WatchActivity_GetPreviousKey_Success(t *testing.T) {
|
||||
db, wt := helper_update_WatchActivityTable(t, (&WatchActivityKey{}).SetTestKeys(), (&WatchActivityKey{}).GetTestValue())
|
||||
var partRes *WatchActivityKey
|
||||
var err1 error
|
||||
curKey := NewWatchActivityKey(someMaxPartition, someKind, someNamespace, someName, someUid+"c")
|
||||
keyComarator := NewWatchActivityKeyComparator(someKind, someNamespace, someName, someUid)
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partRes, err1 = wt.GetPreviousKey(txn, curKey, keyComarator)
|
||||
return err1
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
expectedKey := NewWatchActivityKey(someMaxPartition, someKind, someNamespace, someName, someUid)
|
||||
assert.Equal(t, expectedKey, partRes)
|
||||
}
|
||||
|
||||
func Test_WatchActivity_GetPreviousKey_Fail(t *testing.T) {
|
||||
db, wt := helper_update_WatchActivityTable(t, (&WatchActivityKey{}).SetTestKeys(), (&WatchActivityKey{}).GetTestValue())
|
||||
var partRes *WatchActivityKey
|
||||
var err1 error
|
||||
curKey := NewWatchActivityKey(someMaxPartition, someKind, someNamespace, someName, someUid)
|
||||
keyComarator := NewWatchActivityKeyComparator(someKind+"a", someNamespace, someName, someUid)
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partRes, err1 = wt.GetPreviousKey(txn, curKey, keyComarator)
|
||||
return err1
|
||||
})
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, &WatchActivityKey{}, partRes)
|
||||
}
|
||||
|
|
|
@ -35,6 +35,10 @@ func NewWatchTableKey(partitionId string, kind string, namespace string, name st
|
|||
return &WatchTableKey{PartitionId: partitionId, Kind: kind, Namespace: namespace, Name: name, Timestamp: timestamp}
|
||||
}
|
||||
|
||||
func NewWatchTableKeyComparator(kind string, namespace string, name string, timestamp time.Time) *WatchTableKey {
|
||||
return &WatchTableKey{Kind: kind, Namespace: namespace, Name: name, Timestamp: timestamp}
|
||||
}
|
||||
|
||||
func (_ *WatchTableKey) TableName() string {
|
||||
return "watch"
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
package typed
|
||||
|
||||
import (
|
||||
"github.com/dgraph-io/badger"
|
||||
"github.com/salesforce/sloop/pkg/sloop/store/untyped"
|
||||
"github.com/salesforce/sloop/pkg/sloop/store/untyped/badgerwrap"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -17,11 +16,16 @@ import (
|
|||
)
|
||||
|
||||
var someTs = time.Date(2019, 1, 2, 3, 4, 5, 6, time.UTC)
|
||||
var someMiddleTs = time.Date(2019, 1, 2, 4, 4, 5, 6, time.UTC)
|
||||
var someMaxTs = time.Date(2019, 1, 2, 5, 4, 5, 6, time.UTC)
|
||||
var zeroData time.Time
|
||||
|
||||
const someKind = "somekind"
|
||||
const someNamespace = "somenamespace"
|
||||
const someName = "somename"
|
||||
const somePartition = "001546398000"
|
||||
const someMinPartition = "001546398000"
|
||||
const someMiddlePartition = "001546401600"
|
||||
const someMaxPartition = "001546405200"
|
||||
|
||||
func Test_WatchTableKey_OutputCorrect(t *testing.T) {
|
||||
untyped.TestHookSetPartitionDuration(time.Hour)
|
||||
|
@ -35,7 +39,7 @@ func Test_WatchTableKey_ParseCorrect(t *testing.T) {
|
|||
k := &WatchTableKey{}
|
||||
err := k.Parse("/watch/001546398000/somekind/somenamespace/somename/1546398245000000006")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, somePartition, k.PartitionId)
|
||||
assert.Equal(t, someMinPartition, k.PartitionId)
|
||||
assert.Equal(t, someNamespace, k.Namespace)
|
||||
assert.Equal(t, someName, k.Name)
|
||||
assert.Equal(t, someTs, k.Timestamp)
|
||||
|
@ -46,48 +50,13 @@ func Test_WatchTableKey_ValidateWorks(t *testing.T) {
|
|||
assert.Nil(t, (&WatchTableKey{}).ValidateKey(testKey))
|
||||
}
|
||||
|
||||
func helper_update_watch_table(t *testing.T) (badgerwrap.DB, *KubeWatchResultTable) {
|
||||
untyped.TestHookSetPartitionDuration(time.Hour)
|
||||
partitionId := untyped.GetPartitionId(someTs)
|
||||
var keys []string
|
||||
for i := 'a'; i < 'd'; i++ {
|
||||
// add keys in ascending order
|
||||
keys = append(keys, NewWatchTableKey(partitionId, someKind+string(i), someNamespace, someName, someTs).String())
|
||||
}
|
||||
val := &KubeWatchResult{Kind: someKind}
|
||||
b, err := (&badgerwrap.MockFactory{}).Open(badger.DefaultOptions(""))
|
||||
assert.Nil(t, err)
|
||||
wt := OpenKubeWatchResultTable()
|
||||
err = b.Update(func(txn badgerwrap.Txn) error {
|
||||
var txerr error
|
||||
for _, key := range keys {
|
||||
txerr = wt.Set(txn, key, val)
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
}
|
||||
// Add some keys outside the range
|
||||
txerr = txn.Set([]byte("/a/123/"), []byte{})
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
txerr = txn.Set([]byte("/zzz/123/"), []byte{})
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
return b, wt
|
||||
}
|
||||
|
||||
func Test_WatchTable_PutThenGet_SameData(t *testing.T) {
|
||||
db, wt := helper_update_watch_table(t)
|
||||
db, wt := helper_update_KubeWatchResultTable(t, (&WatchTableKey{}).SetTestKeys(), (&WatchTableKey{}).SetTestValue())
|
||||
|
||||
var retval *KubeWatchResult
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
var txerr error
|
||||
retval, txerr = wt.Get(txn, "/watch/001546398000/somekinda/somenamespace/somename/1546398245000000006")
|
||||
retval, txerr = wt.Get(txn, "/watch/001546398000/somekind/somenamespace/somename/1546398245000000006")
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
|
@ -98,7 +67,7 @@ func Test_WatchTable_PutThenGet_SameData(t *testing.T) {
|
|||
}
|
||||
|
||||
func Test_WatchTable_TestMinAndMaxKeys(t *testing.T) {
|
||||
db, wt := helper_update_watch_table(t)
|
||||
db, wt := helper_update_KubeWatchResultTable(t, (&WatchTableKey{}).SetTestKeys(), (&WatchTableKey{}).SetTestValue())
|
||||
var minKey string
|
||||
var maxKey string
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
|
@ -107,12 +76,12 @@ func Test_WatchTable_TestMinAndMaxKeys(t *testing.T) {
|
|||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "/watch/001546398000/somekinda/somenamespace/somename/1546398245000000006", minKey)
|
||||
assert.Equal(t, "/watch/001546398000/somekindc/somenamespace/somename/1546398245000000006", maxKey)
|
||||
assert.Equal(t, "/watch/001546398000/somekind/somenamespace/somename/1546380245000000006", minKey)
|
||||
assert.Equal(t, "/watch/001546405200/somekind/somenamespace/somename/1546398245000000006", maxKey)
|
||||
}
|
||||
|
||||
func Test_WatchTable_TestGetMinMaxPartitions(t *testing.T) {
|
||||
db, wt := helper_update_watch_table(t)
|
||||
db, wt := helper_update_KubeWatchResultTable(t, (&WatchTableKey{}).SetTestKeys(), (&WatchTableKey{}).SetTestValue())
|
||||
var minPartition string
|
||||
var maxPartition string
|
||||
var found bool
|
||||
|
@ -123,16 +92,103 @@ func Test_WatchTable_TestGetMinMaxPartitions(t *testing.T) {
|
|||
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, somePartition, minPartition)
|
||||
assert.Equal(t, somePartition, maxPartition)
|
||||
assert.Equal(t, someMinPartition, minPartition)
|
||||
assert.Equal(t, someMaxPartition, maxPartition)
|
||||
}
|
||||
|
||||
func Test_getLastMatchingKeyInPartition_FoundInPreviousPartition(t *testing.T) {
|
||||
db, wt := helper_update_KubeWatchResultTable(t, (&WatchTableKey{}).SetTestKeys(), (&WatchTableKey{}).SetTestValue())
|
||||
var keyRes *WatchTableKey
|
||||
var err1 error
|
||||
var found bool
|
||||
curKey := NewWatchTableKey(someMaxPartition, someKind, someNamespace, someName, someTs)
|
||||
keyComparator := NewWatchTableKeyComparator(someKind, someNamespace, someName, zeroData)
|
||||
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMiddlePartition, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
assert.True(t, found)
|
||||
expectedKey := NewWatchTableKey(someMiddlePartition, someKind, someNamespace, someName, someTs)
|
||||
assert.Equal(t, expectedKey, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func Test_getLastMatchingKeyInPartition_FoundInSamePartition(t *testing.T) {
|
||||
db, wt := helper_update_KubeWatchResultTable(t, (&WatchTableKey{}).SetTestKeys(), (&WatchTableKey{}).SetTestValue())
|
||||
var keyRes *WatchTableKey
|
||||
var err1 error
|
||||
var found bool
|
||||
curKey := NewWatchTableKey(someMaxPartition, someKind, someNamespace, someName, someTs)
|
||||
keyComparator := NewWatchTableKeyComparator(someKind, someNamespace, someName, zeroData)
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMaxPartition, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
|
||||
assert.True(t, found)
|
||||
expectedKey := NewWatchTableKey(someMaxPartition, someKind, someNamespace, someName, someTs.Add(time.Hour*-5))
|
||||
assert.Equal(t, expectedKey, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func Test_getLastMatchingKeyInPartition_SameKeySearch(t *testing.T) {
|
||||
db, wt := helper_update_KubeWatchResultTable(t, (&WatchTableKey{}).SetTestKeys(), (&WatchTableKey{}).SetTestValue())
|
||||
var keyRes *WatchTableKey
|
||||
var err1 error
|
||||
var found bool
|
||||
curKey := NewWatchTableKey(someMaxPartition, someKind, someNamespace, someName, someTs)
|
||||
keyComparator := NewWatchTableKeyComparator(someKind, someNamespace, someName, someTs)
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMaxPartition, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
|
||||
assert.False(t, found)
|
||||
assert.Equal(t, &WatchTableKey{}, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func Test_getLastMatchingKeyInPartition_NotFound(t *testing.T) {
|
||||
db, wt := helper_update_KubeWatchResultTable(t, (&WatchTableKey{}).SetTestKeys(), (&WatchTableKey{}).SetTestValue())
|
||||
var keyRes *WatchTableKey
|
||||
var err1 error
|
||||
var found bool
|
||||
curKey := NewWatchTableKey(someMaxPartition, someKind, someNamespace, someName, someTs)
|
||||
keyComparator := NewWatchTableKeyComparator(someKind+"c", someNamespace, someName, someTs)
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
found, keyRes, err1 = wt.getLastMatchingKeyInPartition(txn, someMinPartition, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
|
||||
assert.False(t, found)
|
||||
assert.Equal(t, &WatchTableKey{}, keyRes)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func (_ *WatchTableKey) GetTestKey() string {
|
||||
k := NewWatchTableKey("001546398000", "someKind", "someNamespace", "someName", someTs)
|
||||
k := NewWatchTableKey(someMinPartition, someKind, someNamespace, someName, someTs)
|
||||
return k.String()
|
||||
}
|
||||
|
||||
func (_ *WatchTableKey) GetTestValue() *KubeWatchResult {
|
||||
return &KubeWatchResult{}
|
||||
}
|
||||
|
||||
func (_ *WatchTableKey) SetTestKeys() []string {
|
||||
untyped.TestHookSetPartitionDuration(time.Hour)
|
||||
var keys []string
|
||||
var partitionId string
|
||||
for curTime := someTs; !curTime.After(someMaxTs); curTime = curTime.Add(untyped.GetPartitionDuration()) {
|
||||
// add keys in ascending order
|
||||
partitionId = untyped.GetPartitionId(curTime)
|
||||
keys = append(keys, NewWatchTableKey(partitionId, someKind, someNamespace, someName, someTs.Add(time.Hour*-5)).String())
|
||||
keys = append(keys, NewWatchTableKey(partitionId, someKind, someNamespace, someName, someTs).String())
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
func (_ *WatchTableKey) SetTestValue() *KubeWatchResult {
|
||||
return &KubeWatchResult{Kind: someKind}
|
||||
}
|
||||
|
|
|
@ -132,7 +132,6 @@ func (t *KubeWatchResultTable) GetMinMaxPartitions(txn badgerwrap.Txn) (bool, st
|
|||
return true, minKey.PartitionId, maxKey.PartitionId
|
||||
}
|
||||
|
||||
//todo: need to add unit test
|
||||
func (t *KubeWatchResultTable) GetUniquePartitionList(txn badgerwrap.Txn) ([]string, error) {
|
||||
resources := []string{}
|
||||
ok, minPar, maxPar := t.GetMinMaxPartitions(txn)
|
||||
|
@ -152,7 +151,6 @@ func (t *KubeWatchResultTable) GetUniquePartitionList(txn badgerwrap.Txn) ([]str
|
|||
return resources, nil
|
||||
}
|
||||
|
||||
//todo: need to add unit test
|
||||
func (t *KubeWatchResultTable) GetPreviousKey(txn badgerwrap.Txn, key *WatchTableKey, keyComparator *WatchTableKey) (*WatchTableKey, error) {
|
||||
partitionList, err := t.GetUniquePartitionList(txn)
|
||||
if err != nil {
|
||||
|
@ -176,7 +174,6 @@ func (t *KubeWatchResultTable) GetPreviousKey(txn badgerwrap.Txn, key *WatchTabl
|
|||
return &WatchTableKey{}, fmt.Errorf("failed to get any previous key in table:%v, for key:%v, keyComparator:%v", t.tableName, key.String(), keyComparator)
|
||||
}
|
||||
|
||||
//todo: need to add unit test
|
||||
func (t *KubeWatchResultTable) getLastMatchingKeyInPartition(txn badgerwrap.Txn, curPartition string, curKey *WatchTableKey, keyComparator *WatchTableKey) (bool, *WatchTableKey, error) {
|
||||
iterOpt := badger.DefaultIteratorOptions
|
||||
iterOpt.Reverse = true
|
||||
|
|
|
@ -48,5 +48,96 @@ func Test_KubeWatchResultTable_SetWorks(t *testing.T) {
|
|||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
}
|
||||
|
||||
func helper_update_KubeWatchResultTable(t *testing.T, keys []string, val *KubeWatchResult) (badgerwrap.DB, *KubeWatchResultTable) {
|
||||
b, err := (&badgerwrap.MockFactory{}).Open(badger.DefaultOptions(""))
|
||||
assert.Nil(t, err)
|
||||
wt := OpenKubeWatchResultTable()
|
||||
err = b.Update(func(txn badgerwrap.Txn) error {
|
||||
var txerr error
|
||||
for _, key := range keys {
|
||||
txerr = wt.Set(txn, key, val)
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
}
|
||||
// Add some keys outside the range
|
||||
txerr = txn.Set([]byte("/a/123/"), []byte{})
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
txerr = txn.Set([]byte("/zzz/123/"), []byte{})
|
||||
if txerr != nil {
|
||||
return txerr
|
||||
}
|
||||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
return b, wt
|
||||
}
|
||||
|
||||
func Test_KubeWatchResultTable_GetUniquePartitionList_Success(t *testing.T) {
|
||||
if helper_KubeWatchResult_ShouldSkip() {
|
||||
return
|
||||
}
|
||||
|
||||
db, wt := helper_update_KubeWatchResultTable(t, (&WatchTableKey{}).SetTestKeys(), (&WatchTableKey{}).SetTestValue())
|
||||
var partList []string
|
||||
var err1 error
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partList, err1 = wt.GetUniquePartitionList(txn)
|
||||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, err1)
|
||||
assert.Len(t, partList, 3)
|
||||
assert.Contains(t, partList, someMinPartition)
|
||||
assert.Contains(t, partList, someMiddlePartition)
|
||||
assert.Contains(t, partList, someMaxPartition)
|
||||
}
|
||||
|
||||
func Test_KubeWatchResultTable_GetUniquePartitionList_EmptyPartition(t *testing.T) {
|
||||
if helper_KubeWatchResult_ShouldSkip() {
|
||||
return
|
||||
}
|
||||
|
||||
db, wt := helper_update_KubeWatchResultTable(t, []string{}, &KubeWatchResult{})
|
||||
var partList []string
|
||||
var err1 error
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partList, err1 = wt.GetUniquePartitionList(txn)
|
||||
return err1
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, partList, 0)
|
||||
}
|
||||
|
||||
func Test_GetPreviousKey_Success(t *testing.T) {
|
||||
db, wt := helper_update_KubeWatchResultTable(t, (&WatchTableKey{}).SetTestKeys(), (&WatchTableKey{}).SetTestValue())
|
||||
var partRes *WatchTableKey
|
||||
var err1 error
|
||||
curKey := NewWatchTableKey(someMaxPartition, someKind, someNamespace, someName, someTs)
|
||||
keyComparator := NewWatchTableKeyComparator(someKind, someNamespace, someName, zeroData)
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partRes, err1 = wt.GetPreviousKey(txn, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
expectedKey := NewWatchTableKey(someMaxPartition, someKind, someNamespace, someName, someTs.Add(time.Hour*-5))
|
||||
assert.Equal(t, expectedKey, partRes)
|
||||
}
|
||||
|
||||
func Test_GetPreviousKey_Fail(t *testing.T) {
|
||||
db, wt := helper_update_KubeWatchResultTable(t, (&WatchTableKey{}).SetTestKeys(), (&WatchTableKey{}).SetTestValue())
|
||||
var partRes *WatchTableKey
|
||||
var err1 error
|
||||
curKey := NewWatchTableKey(someMaxPartition, someKind, someNamespace, someName, someTs)
|
||||
keyComparator := NewWatchTableKeyComparator(someKind+"c", someNamespace, someName, zeroData)
|
||||
err := db.View(func(txn badgerwrap.Txn) error {
|
||||
partRes, err1 = wt.GetPreviousKey(txn, curKey, keyComparator)
|
||||
return err1
|
||||
})
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, &WatchTableKey{}, partRes)
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче