mirror of
https://github.com/rancher/steve.git
synced 2025-09-16 07:18:28 +00:00
sql: propagate and use contexts (#465)
Previous SQLite-related code used context.Background() and context.TODO() because it was not developed with context awareness. This commit propagates the main Steve context so that it can be used when interacting with SQL context-aware functions. This PR removes all production-code use of context.Background() and context.TODO() and replaces test-code use of TODO with Background. Contributes to rancher/rancher#47825
This commit is contained in:
@@ -4,6 +4,7 @@ Package factory provides a cache factory for the sql-based cache.
|
||||
package factory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
@@ -43,7 +44,7 @@ type guardedInformer struct {
|
||||
mutex *sync.Mutex
|
||||
}
|
||||
|
||||
type newInformer func(client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt bool, namespace bool) (*informer.Informer, error)
|
||||
type newInformer func(ctx context.Context, client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt bool, namespace bool) (*informer.Informer, error)
|
||||
|
||||
type Cache struct {
|
||||
informer.ByOptionsLister
|
||||
@@ -84,7 +85,7 @@ func NewCacheFactory() (*CacheFactory, error) {
|
||||
|
||||
// CacheFor returns an informer for given GVK, using sql store indexed with fields, using the specified client. For virtual fields, they must be added by the transform function
|
||||
// and specified by fields to be used for later fields.
|
||||
func (f *CacheFactory) CacheFor(fields [][]string, transform cache.TransformFunc, client dynamic.ResourceInterface, gvk schema.GroupVersionKind, namespaced bool, watchable bool) (Cache, error) {
|
||||
func (f *CacheFactory) CacheFor(ctx context.Context, fields [][]string, transform cache.TransformFunc, client dynamic.ResourceInterface, gvk schema.GroupVersionKind, namespaced bool, watchable bool) (Cache, error) {
|
||||
// First of all block Reset() until we are done
|
||||
f.mutex.RLock()
|
||||
defer f.mutex.RUnlock()
|
||||
@@ -120,7 +121,7 @@ func (f *CacheFactory) CacheFor(fields [][]string, transform cache.TransformFunc
|
||||
|
||||
_, encryptResourceAlways := defaultEncryptedResourceTypes[gvk]
|
||||
shouldEncrypt := f.encryptAll || encryptResourceAlways
|
||||
i, err := f.newInformer(client, fields, transform, gvk, f.dbClient, shouldEncrypt, namespaced)
|
||||
i, err := f.newInformer(ctx, client, fields, transform, gvk, f.dbClient, shouldEncrypt, namespaced)
|
||||
if err != nil {
|
||||
return Cache{}, err
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package factory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -73,7 +74,7 @@ func TestCacheFor(t *testing.T) {
|
||||
expectedC := Cache{
|
||||
ByOptionsLister: i,
|
||||
}
|
||||
testNewInformer := func(client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt bool, namespaced bool) (*informer.Informer, error) {
|
||||
testNewInformer := func(ctx context.Context, client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt bool, namespaced bool) (*informer.Informer, error) {
|
||||
assert.Equal(t, client, dynamicClient)
|
||||
assert.Equal(t, fields, fields)
|
||||
assert.Equal(t, expectedGVK, gvk)
|
||||
@@ -95,12 +96,12 @@ func TestCacheFor(t *testing.T) {
|
||||
}()
|
||||
var c Cache
|
||||
var err error
|
||||
c, err = f.CacheFor(fields, nil, dynamicClient, expectedGVK, false, true)
|
||||
c, err = f.CacheFor(context.Background(), fields, nil, dynamicClient, expectedGVK, false, true)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, expectedC, c)
|
||||
// this sleep is critical to the test. It ensure there has been enough time for expected function like Run to be invoked in their go routines.
|
||||
time.Sleep(1 * time.Second)
|
||||
c2, err := f.CacheFor(fields, nil, dynamicClient, expectedGVK, false, true)
|
||||
c2, err := f.CacheFor(context.Background(), fields, nil, dynamicClient, expectedGVK, false, true)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, c, c2)
|
||||
}})
|
||||
@@ -118,7 +119,7 @@ func TestCacheFor(t *testing.T) {
|
||||
// need to set this so Run function is not nil
|
||||
SharedIndexInformer: sii,
|
||||
}
|
||||
testNewInformer := func(client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt, namespaced bool) (*informer.Informer, error) {
|
||||
testNewInformer := func(ctx context.Context, client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt, namespaced bool) (*informer.Informer, error) {
|
||||
assert.Equal(t, client, dynamicClient)
|
||||
assert.Equal(t, fields, fields)
|
||||
assert.Equal(t, expectedGVK, gvk)
|
||||
@@ -138,7 +139,7 @@ func TestCacheFor(t *testing.T) {
|
||||
close(f.stopCh)
|
||||
}()
|
||||
var err error
|
||||
_, err = f.CacheFor(fields, nil, dynamicClient, expectedGVK, false, true)
|
||||
_, err = f.CacheFor(context.Background(), fields, nil, dynamicClient, expectedGVK, false, true)
|
||||
assert.NotNil(t, err)
|
||||
time.Sleep(2 * time.Second)
|
||||
}})
|
||||
@@ -160,7 +161,7 @@ func TestCacheFor(t *testing.T) {
|
||||
expectedC := Cache{
|
||||
ByOptionsLister: i,
|
||||
}
|
||||
testNewInformer := func(client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt, namespaced bool) (*informer.Informer, error) {
|
||||
testNewInformer := func(ctx context.Context, client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt, namespaced bool) (*informer.Informer, error) {
|
||||
assert.Equal(t, client, dynamicClient)
|
||||
assert.Equal(t, fields, fields)
|
||||
assert.Equal(t, expectedGVK, gvk)
|
||||
@@ -178,7 +179,7 @@ func TestCacheFor(t *testing.T) {
|
||||
close(f.stopCh)
|
||||
var c Cache
|
||||
var err error
|
||||
c, err = f.CacheFor(fields, nil, dynamicClient, expectedGVK, false, true)
|
||||
c, err = f.CacheFor(context.Background(), fields, nil, dynamicClient, expectedGVK, false, true)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, expectedC, c)
|
||||
time.Sleep(1 * time.Second)
|
||||
@@ -199,7 +200,7 @@ func TestCacheFor(t *testing.T) {
|
||||
expectedC := Cache{
|
||||
ByOptionsLister: i,
|
||||
}
|
||||
testNewInformer := func(client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt, namespaced bool) (*informer.Informer, error) {
|
||||
testNewInformer := func(ctx context.Context, client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt, namespaced bool) (*informer.Informer, error) {
|
||||
assert.Equal(t, client, dynamicClient)
|
||||
assert.Equal(t, fields, fields)
|
||||
assert.Equal(t, expectedGVK, gvk)
|
||||
@@ -221,7 +222,7 @@ func TestCacheFor(t *testing.T) {
|
||||
}()
|
||||
var c Cache
|
||||
var err error
|
||||
c, err = f.CacheFor(fields, nil, dynamicClient, expectedGVK, false, true)
|
||||
c, err = f.CacheFor(context.Background(), fields, nil, dynamicClient, expectedGVK, false, true)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, expectedC, c)
|
||||
time.Sleep(1 * time.Second)
|
||||
@@ -247,7 +248,7 @@ func TestCacheFor(t *testing.T) {
|
||||
expectedC := Cache{
|
||||
ByOptionsLister: i,
|
||||
}
|
||||
testNewInformer := func(client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt, namespaced bool) (*informer.Informer, error) {
|
||||
testNewInformer := func(ctx context.Context, client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt, namespaced bool) (*informer.Informer, error) {
|
||||
assert.Equal(t, client, dynamicClient)
|
||||
assert.Equal(t, fields, fields)
|
||||
assert.Equal(t, expectedGVK, gvk)
|
||||
@@ -269,7 +270,7 @@ func TestCacheFor(t *testing.T) {
|
||||
}()
|
||||
var c Cache
|
||||
var err error
|
||||
c, err = f.CacheFor(fields, nil, dynamicClient, expectedGVK, false, true)
|
||||
c, err = f.CacheFor(context.Background(), fields, nil, dynamicClient, expectedGVK, false, true)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, expectedC, c)
|
||||
time.Sleep(1 * time.Second)
|
||||
@@ -294,7 +295,7 @@ func TestCacheFor(t *testing.T) {
|
||||
expectedC := Cache{
|
||||
ByOptionsLister: i,
|
||||
}
|
||||
testNewInformer := func(client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt, namespaced bool) (*informer.Informer, error) {
|
||||
testNewInformer := func(ctx context.Context, client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt, namespaced bool) (*informer.Informer, error) {
|
||||
assert.Equal(t, client, dynamicClient)
|
||||
assert.Equal(t, fields, fields)
|
||||
assert.Equal(t, expectedGVK, gvk)
|
||||
@@ -316,7 +317,7 @@ func TestCacheFor(t *testing.T) {
|
||||
}()
|
||||
var c Cache
|
||||
var err error
|
||||
c, err = f.CacheFor(fields, nil, dynamicClient, expectedGVK, false, true)
|
||||
c, err = f.CacheFor(context.Background(), fields, nil, dynamicClient, expectedGVK, false, true)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, expectedC, c)
|
||||
time.Sleep(1 * time.Second)
|
||||
@@ -341,7 +342,7 @@ func TestCacheFor(t *testing.T) {
|
||||
expectedC := Cache{
|
||||
ByOptionsLister: i,
|
||||
}
|
||||
testNewInformer := func(client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt bool, namespaced bool) (*informer.Informer, error) {
|
||||
testNewInformer := func(ctx context.Context, client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt bool, namespaced bool) (*informer.Informer, error) {
|
||||
// we can't test func == func, so instead we check if the output was as expected
|
||||
input := "someinput"
|
||||
ouput, err := transform(input)
|
||||
@@ -371,7 +372,7 @@ func TestCacheFor(t *testing.T) {
|
||||
}()
|
||||
var c Cache
|
||||
var err error
|
||||
c, err = f.CacheFor(fields, transformFunc, dynamicClient, expectedGVK, false, true)
|
||||
c, err = f.CacheFor(context.Background(), fields, transformFunc, dynamicClient, expectedGVK, false, true)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, expectedC, c)
|
||||
time.Sleep(1 * time.Second)
|
||||
|
@@ -43,6 +43,8 @@ const (
|
||||
|
||||
// Indexer is a SQLite-backed cache.Indexer which builds upon Store adding an index table
|
||||
type Indexer struct {
|
||||
ctx context.Context
|
||||
|
||||
Store
|
||||
indexers cache.Indexers
|
||||
indexersLock sync.RWMutex
|
||||
@@ -75,10 +77,10 @@ type Store interface {
|
||||
}
|
||||
|
||||
// NewIndexer returns a cache.Indexer backed by SQLite for objects of the given example type
|
||||
func NewIndexer(indexers cache.Indexers, s Store) (*Indexer, error) {
|
||||
func NewIndexer(ctx context.Context, indexers cache.Indexers, s Store) (*Indexer, error) {
|
||||
dbName := db.Sanitize(s.GetName())
|
||||
|
||||
err := s.WithTransaction(context.Background(), true, func(tx transaction.Client) error {
|
||||
err := s.WithTransaction(ctx, true, func(tx transaction.Client) error {
|
||||
createTableQuery := fmt.Sprintf(createTableFmt, dbName)
|
||||
_, err := tx.Exec(createTableQuery)
|
||||
if err != nil {
|
||||
@@ -96,6 +98,7 @@ func NewIndexer(indexers cache.Indexers, s Store) (*Indexer, error) {
|
||||
}
|
||||
|
||||
i := &Indexer{
|
||||
ctx: ctx,
|
||||
Store: s,
|
||||
indexers: indexers,
|
||||
}
|
||||
@@ -187,7 +190,7 @@ func (i *Indexer) Index(indexName string, obj any) (result []any, err error) {
|
||||
params = append(params, value)
|
||||
}
|
||||
|
||||
rows, err := i.QueryForRows(context.TODO(), stmt, params...)
|
||||
rows, err := i.QueryForRows(i.ctx, stmt, params...)
|
||||
if err != nil {
|
||||
return nil, &db.QueryError{QueryString: query, Err: err}
|
||||
}
|
||||
@@ -197,7 +200,7 @@ func (i *Indexer) Index(indexName string, obj any) (result []any, err error) {
|
||||
// ByIndex returns the stored objects whose set of indexed values
|
||||
// for the named index includes the given indexed value
|
||||
func (i *Indexer) ByIndex(indexName, indexedValue string) ([]any, error) {
|
||||
rows, err := i.QueryForRows(context.TODO(), i.listByIndexStmt, indexName, indexedValue)
|
||||
rows, err := i.QueryForRows(i.ctx, i.listByIndexStmt, indexName, indexedValue)
|
||||
if err != nil {
|
||||
return nil, &db.QueryError{QueryString: i.listByIndexQuery, Err: err}
|
||||
}
|
||||
@@ -213,7 +216,7 @@ func (i *Indexer) IndexKeys(indexName, indexedValue string) ([]string, error) {
|
||||
return nil, fmt.Errorf("Index with name %s does not exist", indexName)
|
||||
}
|
||||
|
||||
rows, err := i.QueryForRows(context.TODO(), i.listKeysByIndexStmt, indexName, indexedValue)
|
||||
rows, err := i.QueryForRows(i.ctx, i.listKeysByIndexStmt, indexName, indexedValue)
|
||||
if err != nil {
|
||||
return nil, &db.QueryError{QueryString: i.listKeysByIndexQuery, Err: err}
|
||||
}
|
||||
@@ -231,7 +234,7 @@ func (i *Indexer) ListIndexFuncValues(name string) []string {
|
||||
|
||||
// safeListIndexFuncValues returns all the indexed values of the given index
|
||||
func (i *Indexer) safeListIndexFuncValues(indexName string) ([]string, error) {
|
||||
rows, err := i.QueryForRows(context.TODO(), i.listIndexValuesStmt, indexName)
|
||||
rows, err := i.QueryForRows(i.ctx, i.listIndexValuesStmt, indexName)
|
||||
if err != nil {
|
||||
return nil, &db.QueryError{QueryString: i.listIndexValuesQuery, Err: err}
|
||||
}
|
||||
|
@@ -64,7 +64,7 @@ func TestNewIndexer(t *testing.T) {
|
||||
store.EXPECT().Prepare(fmt.Sprintf(listByIndexFmt, storeName, storeName))
|
||||
store.EXPECT().Prepare(fmt.Sprintf(listKeyByIndexFmt, storeName))
|
||||
store.EXPECT().Prepare(fmt.Sprintf(listIndexValuesFmt, storeName))
|
||||
indexer, err := NewIndexer(indexers, store)
|
||||
indexer, err := NewIndexer(context.Background(), indexers, store)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, cache.Indexers(indexers), indexer.indexers)
|
||||
}})
|
||||
@@ -79,7 +79,7 @@ func TestNewIndexer(t *testing.T) {
|
||||
}
|
||||
store.EXPECT().GetName().AnyTimes().Return("someStoreName")
|
||||
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(fmt.Errorf("error"))
|
||||
_, err := NewIndexer(indexers, store)
|
||||
_, err := NewIndexer(context.Background(), indexers, store)
|
||||
assert.NotNil(t, err)
|
||||
}})
|
||||
tests = append(tests, testCase{description: "NewIndexer() with Client Exec() error on first call to Exec(), should return error", test: func(t *testing.T) {
|
||||
@@ -103,7 +103,7 @@ func TestNewIndexer(t *testing.T) {
|
||||
t.Fail()
|
||||
}
|
||||
})
|
||||
_, err := NewIndexer(indexers, store)
|
||||
_, err := NewIndexer(context.Background(), indexers, store)
|
||||
assert.NotNil(t, err)
|
||||
}})
|
||||
tests = append(tests, testCase{description: "NewIndexer() with Client Exec() error on second call to Exec(), should return error", test: func(t *testing.T) {
|
||||
@@ -129,7 +129,7 @@ func TestNewIndexer(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
_, err := NewIndexer(indexers, store)
|
||||
_, err := NewIndexer(context.Background(), indexers, store)
|
||||
assert.NotNil(t, err)
|
||||
}})
|
||||
tests = append(tests, testCase{description: "NewIndexer() with Client Commit() error, should return error", test: func(t *testing.T) {
|
||||
@@ -153,7 +153,7 @@ func TestNewIndexer(t *testing.T) {
|
||||
t.Fail()
|
||||
}
|
||||
})
|
||||
_, err := NewIndexer(indexers, store)
|
||||
_, err := NewIndexer(context.Background(), indexers, store)
|
||||
assert.NotNil(t, err)
|
||||
}})
|
||||
t.Parallel()
|
||||
@@ -177,6 +177,7 @@ func TestAfterUpsert(t *testing.T) {
|
||||
deleteIndicesStmt := NewMockStmt(gomock.NewController(t))
|
||||
addIndexStmt := NewMockStmt(gomock.NewController(t))
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
indexers: map[string]cache.IndexFunc{
|
||||
"a": func(obj interface{}) ([]string, error) {
|
||||
@@ -199,6 +200,7 @@ func TestAfterUpsert(t *testing.T) {
|
||||
objKey := "key"
|
||||
deleteIndicesStmt := NewMockStmt(gomock.NewController(t))
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
|
||||
indexers: map[string]cache.IndexFunc{
|
||||
@@ -221,6 +223,7 @@ func TestAfterUpsert(t *testing.T) {
|
||||
addIndexStmt := NewMockStmt(gomock.NewController(t))
|
||||
objKey := "key"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
indexers: map[string]cache.IndexFunc{
|
||||
"a": func(obj interface{}) ([]string, error) {
|
||||
@@ -258,6 +261,7 @@ func TestIndex(t *testing.T) {
|
||||
objKey := "key"
|
||||
indexName := "someindexname"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
listByIndexStmt: listStmt,
|
||||
indexers: map[string]cache.IndexFunc{
|
||||
@@ -268,7 +272,7 @@ func TestIndex(t *testing.T) {
|
||||
}
|
||||
testObject := testStoreObject{Id: "something", Val: "a"}
|
||||
|
||||
store.EXPECT().QueryForRows(context.TODO(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().QueryForRows(context.Background(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().GetType().Return(reflect.TypeOf(testObject))
|
||||
store.EXPECT().GetShouldEncrypt().Return(false)
|
||||
store.EXPECT().ReadObjects(rows, reflect.TypeOf(testObject), false).Return([]any{testObject}, nil)
|
||||
@@ -283,6 +287,7 @@ func TestIndex(t *testing.T) {
|
||||
objKey := "key"
|
||||
indexName := "someindexname"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
listByIndexStmt: listStmt,
|
||||
indexers: map[string]cache.IndexFunc{
|
||||
@@ -293,7 +298,7 @@ func TestIndex(t *testing.T) {
|
||||
}
|
||||
testObject := testStoreObject{Id: "something", Val: "a"}
|
||||
|
||||
store.EXPECT().QueryForRows(context.TODO(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().QueryForRows(context.Background(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().GetType().Return(reflect.TypeOf(testObject))
|
||||
store.EXPECT().GetShouldEncrypt().Return(false)
|
||||
store.EXPECT().ReadObjects(rows, reflect.TypeOf(testObject), false).Return([]any{testObject, testObject}, nil)
|
||||
@@ -308,6 +313,7 @@ func TestIndex(t *testing.T) {
|
||||
objKey := "key"
|
||||
indexName := "someindexname"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
listByIndexStmt: listStmt,
|
||||
indexers: map[string]cache.IndexFunc{
|
||||
@@ -318,7 +324,7 @@ func TestIndex(t *testing.T) {
|
||||
}
|
||||
testObject := testStoreObject{Id: "something", Val: "a"}
|
||||
|
||||
store.EXPECT().QueryForRows(context.TODO(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().QueryForRows(context.Background(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().GetType().Return(reflect.TypeOf(testObject))
|
||||
store.EXPECT().GetShouldEncrypt().Return(false)
|
||||
store.EXPECT().ReadObjects(rows, reflect.TypeOf(testObject), false).Return([]any{}, nil)
|
||||
@@ -332,6 +338,7 @@ func TestIndex(t *testing.T) {
|
||||
objKey := "key"
|
||||
indexName := "someindexname"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
listByIndexStmt: listStmt,
|
||||
indexers: map[string]cache.IndexFunc{
|
||||
@@ -351,6 +358,7 @@ func TestIndex(t *testing.T) {
|
||||
objKey := "key"
|
||||
indexName := "someindexname"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
listByIndexStmt: listStmt,
|
||||
indexers: map[string]cache.IndexFunc{
|
||||
@@ -361,7 +369,7 @@ func TestIndex(t *testing.T) {
|
||||
}
|
||||
testObject := testStoreObject{Id: "something", Val: "a"}
|
||||
|
||||
store.EXPECT().QueryForRows(context.TODO(), indexer.listByIndexStmt, indexName, objKey).Return(nil, fmt.Errorf("error"))
|
||||
store.EXPECT().QueryForRows(context.Background(), indexer.listByIndexStmt, indexName, objKey).Return(nil, fmt.Errorf("error"))
|
||||
_, err := indexer.Index(indexName, testObject)
|
||||
assert.NotNil(t, err)
|
||||
}})
|
||||
@@ -372,6 +380,7 @@ func TestIndex(t *testing.T) {
|
||||
objKey := "key"
|
||||
indexName := "someindexname"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
listByIndexStmt: listStmt,
|
||||
indexers: map[string]cache.IndexFunc{
|
||||
@@ -382,7 +391,7 @@ func TestIndex(t *testing.T) {
|
||||
}
|
||||
testObject := testStoreObject{Id: "something", Val: "a"}
|
||||
|
||||
store.EXPECT().QueryForRows(context.TODO(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().QueryForRows(context.Background(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().GetType().Return(reflect.TypeOf(testObject))
|
||||
store.EXPECT().GetShouldEncrypt().Return(false)
|
||||
store.EXPECT().ReadObjects(rows, reflect.TypeOf(testObject), false).Return([]any{testObject}, fmt.Errorf("error"))
|
||||
@@ -396,6 +405,7 @@ func TestIndex(t *testing.T) {
|
||||
objKey := "key"
|
||||
indexName := "someindexname"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
listByIndexStmt: listStmt,
|
||||
indexers: map[string]cache.IndexFunc{
|
||||
@@ -409,7 +419,7 @@ func TestIndex(t *testing.T) {
|
||||
store.EXPECT().GetName().Return("name")
|
||||
stmt := &sql.Stmt{}
|
||||
store.EXPECT().Prepare(fmt.Sprintf(selectQueryFmt, "name", ", ?")).Return(stmt)
|
||||
store.EXPECT().QueryForRows(context.TODO(), indexer.listByIndexStmt, indexName, objKey, objKey+"2").Return(rows, nil)
|
||||
store.EXPECT().QueryForRows(context.Background(), indexer.listByIndexStmt, indexName, objKey, objKey+"2").Return(rows, nil)
|
||||
store.EXPECT().GetType().Return(reflect.TypeOf(testObject))
|
||||
store.EXPECT().GetShouldEncrypt().Return(false)
|
||||
store.EXPECT().ReadObjects(rows, reflect.TypeOf(testObject), false).Return([]any{testObject}, nil)
|
||||
@@ -439,12 +449,13 @@ func TestByIndex(t *testing.T) {
|
||||
objKey := "key"
|
||||
indexName := "someindexname"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
listByIndexStmt: listStmt,
|
||||
}
|
||||
testObject := testStoreObject{Id: "something", Val: "a"}
|
||||
|
||||
store.EXPECT().QueryForRows(context.TODO(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().QueryForRows(context.Background(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().GetType().Return(reflect.TypeOf(testObject))
|
||||
store.EXPECT().GetShouldEncrypt().Return(false)
|
||||
store.EXPECT().ReadObjects(rows, reflect.TypeOf(testObject), false).Return([]any{testObject}, nil)
|
||||
@@ -459,12 +470,13 @@ func TestByIndex(t *testing.T) {
|
||||
objKey := "key"
|
||||
indexName := "someindexname"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
listByIndexStmt: listStmt,
|
||||
}
|
||||
testObject := testStoreObject{Id: "something", Val: "a"}
|
||||
|
||||
store.EXPECT().QueryForRows(context.TODO(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().QueryForRows(context.Background(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().GetType().Return(reflect.TypeOf(testObject))
|
||||
store.EXPECT().GetShouldEncrypt().Return(false)
|
||||
store.EXPECT().ReadObjects(rows, reflect.TypeOf(testObject), false).Return([]any{testObject, testObject}, nil)
|
||||
@@ -479,12 +491,13 @@ func TestByIndex(t *testing.T) {
|
||||
objKey := "key"
|
||||
indexName := "someindexname"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
listByIndexStmt: listStmt,
|
||||
}
|
||||
testObject := testStoreObject{Id: "something", Val: "a"}
|
||||
|
||||
store.EXPECT().QueryForRows(context.TODO(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().QueryForRows(context.Background(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().GetType().Return(reflect.TypeOf(testObject))
|
||||
store.EXPECT().GetShouldEncrypt().Return(false)
|
||||
store.EXPECT().ReadObjects(rows, reflect.TypeOf(testObject), false).Return([]any{}, nil)
|
||||
@@ -498,11 +511,12 @@ func TestByIndex(t *testing.T) {
|
||||
objKey := "key"
|
||||
indexName := "someindexname"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
listByIndexStmt: listStmt,
|
||||
}
|
||||
|
||||
store.EXPECT().QueryForRows(context.TODO(), indexer.listByIndexStmt, indexName, objKey).Return(nil, fmt.Errorf("error"))
|
||||
store.EXPECT().QueryForRows(context.Background(), indexer.listByIndexStmt, indexName, objKey).Return(nil, fmt.Errorf("error"))
|
||||
_, err := indexer.ByIndex(indexName, objKey)
|
||||
assert.NotNil(t, err)
|
||||
}})
|
||||
@@ -513,12 +527,13 @@ func TestByIndex(t *testing.T) {
|
||||
objKey := "key"
|
||||
indexName := "someindexname"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
listByIndexStmt: listStmt,
|
||||
}
|
||||
testObject := testStoreObject{Id: "something", Val: "a"}
|
||||
|
||||
store.EXPECT().QueryForRows(context.TODO(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().QueryForRows(context.Background(), indexer.listByIndexStmt, indexName, objKey).Return(rows, nil)
|
||||
store.EXPECT().GetType().Return(reflect.TypeOf(testObject))
|
||||
store.EXPECT().GetShouldEncrypt().Return(false)
|
||||
store.EXPECT().ReadObjects(rows, reflect.TypeOf(testObject), false).Return([]any{testObject}, fmt.Errorf("error"))
|
||||
@@ -545,10 +560,11 @@ func TestListIndexFuncValues(t *testing.T) {
|
||||
listStmt := &sql.Stmt{}
|
||||
indexName := "someindexname"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
listByIndexStmt: listStmt,
|
||||
}
|
||||
store.EXPECT().QueryForRows(context.TODO(), indexer.listIndexValuesStmt, indexName).Return(rows, nil)
|
||||
store.EXPECT().QueryForRows(context.Background(), indexer.listIndexValuesStmt, indexName).Return(rows, nil)
|
||||
store.EXPECT().ReadStrings(rows).Return([]string{"somestrings"}, nil)
|
||||
vals := indexer.ListIndexFuncValues(indexName)
|
||||
assert.Equal(t, []string{"somestrings"}, vals)
|
||||
@@ -558,10 +574,11 @@ func TestListIndexFuncValues(t *testing.T) {
|
||||
listStmt := &sql.Stmt{}
|
||||
indexName := "someindexname"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
listByIndexStmt: listStmt,
|
||||
}
|
||||
store.EXPECT().QueryForRows(context.TODO(), indexer.listIndexValuesStmt, indexName).Return(nil, fmt.Errorf("error"))
|
||||
store.EXPECT().QueryForRows(context.Background(), indexer.listIndexValuesStmt, indexName).Return(nil, fmt.Errorf("error"))
|
||||
assert.Panics(t, func() { indexer.ListIndexFuncValues(indexName) })
|
||||
}})
|
||||
tests = append(tests, testCase{description: "ListIndexFuncvalues() with ReadStrings() error returned from store, should panic", test: func(t *testing.T) {
|
||||
@@ -570,10 +587,11 @@ func TestListIndexFuncValues(t *testing.T) {
|
||||
listStmt := &sql.Stmt{}
|
||||
indexName := "someindexname"
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
Store: store,
|
||||
listByIndexStmt: listStmt,
|
||||
}
|
||||
store.EXPECT().QueryForRows(context.TODO(), indexer.listIndexValuesStmt, indexName).Return(rows, nil)
|
||||
store.EXPECT().QueryForRows(context.Background(), indexer.listIndexValuesStmt, indexName).Return(rows, nil)
|
||||
store.EXPECT().ReadStrings(rows).Return([]string{"somestrings"}, fmt.Errorf("error"))
|
||||
assert.Panics(t, func() { indexer.ListIndexFuncValues(indexName) })
|
||||
}})
|
||||
@@ -599,6 +617,7 @@ func TestGetIndexers(t *testing.T) {
|
||||
},
|
||||
}
|
||||
indexer := &Indexer{
|
||||
ctx: context.Background(),
|
||||
indexers: expectedIndexers,
|
||||
}
|
||||
indexers := indexer.GetIndexers()
|
||||
|
@@ -35,14 +35,14 @@ var newInformer = cache.NewSharedIndexInformer
|
||||
|
||||
// NewInformer returns a new SQLite-backed Informer for the type specified by schema in unstructured.Unstructured form
|
||||
// using the specified client
|
||||
func NewInformer(client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt bool, namespaced bool) (*Informer, error) {
|
||||
func NewInformer(ctx context.Context, client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt bool, namespaced bool) (*Informer, error) {
|
||||
listWatcher := &cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
a, err := client.List(context.Background(), options)
|
||||
a, err := client.List(ctx, options)
|
||||
return a, err
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return client.Watch(context.Background(), options)
|
||||
return client.Watch(ctx, options)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -69,11 +69,11 @@ func NewInformer(client dynamic.ResourceInterface, fields [][]string, transform
|
||||
|
||||
name := informerNameFromGVK(gvk)
|
||||
|
||||
s, err := sqlStore.NewStore(example, cache.DeletionHandlingMetaNamespaceKeyFunc, db, shouldEncrypt, name)
|
||||
s, err := sqlStore.NewStore(ctx, example, cache.DeletionHandlingMetaNamespaceKeyFunc, db, shouldEncrypt, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
loi, err := NewListOptionIndexer(fields, s, namespaced)
|
||||
loi, err := NewListOptionIndexer(ctx, fields, s, namespaced)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@@ -79,7 +79,7 @@ func TestNewInformer(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
informer, err := NewInformer(dynamicClient, fields, nil, gvk, dbClient, false, true)
|
||||
informer, err := NewInformer(context.Background(), dynamicClient, fields, nil, gvk, dbClient, false, true)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, informer.ByOptionsLister)
|
||||
assert.NotNil(t, informer.SharedIndexInformer)
|
||||
@@ -103,7 +103,7 @@ func TestNewInformer(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
_, err := NewInformer(dynamicClient, fields, nil, gvk, dbClient, false, true)
|
||||
_, err := NewInformer(context.Background(), dynamicClient, fields, nil, gvk, dbClient, false, true)
|
||||
assert.NotNil(t, err)
|
||||
}})
|
||||
tests = append(tests, testCase{description: "NewInformer() with errors returned from NewIndexer(), should return an error", test: func(t *testing.T) {
|
||||
@@ -138,7 +138,7 @@ func TestNewInformer(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
_, err := NewInformer(dynamicClient, fields, nil, gvk, dbClient, false, true)
|
||||
_, err := NewInformer(context.Background(), dynamicClient, fields, nil, gvk, dbClient, false, true)
|
||||
assert.NotNil(t, err)
|
||||
}})
|
||||
tests = append(tests, testCase{description: "NewInformer() with errors returned from NewListOptionIndexer(), should return an error", test: func(t *testing.T) {
|
||||
@@ -190,7 +190,7 @@ func TestNewInformer(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
_, err := NewInformer(dynamicClient, fields, nil, gvk, dbClient, false, true)
|
||||
_, err := NewInformer(context.Background(), dynamicClient, fields, nil, gvk, dbClient, false, true)
|
||||
assert.NotNil(t, err)
|
||||
}})
|
||||
tests = append(tests, testCase{description: "NewInformer() with transform func", test: func(t *testing.T) {
|
||||
@@ -253,7 +253,7 @@ func TestNewInformer(t *testing.T) {
|
||||
transformFunc := func(input interface{}) (interface{}, error) {
|
||||
return "someoutput", nil
|
||||
}
|
||||
informer, err := NewInformer(dynamicClient, fields, transformFunc, gvk, dbClient, false, true)
|
||||
informer, err := NewInformer(context.Background(), dynamicClient, fields, transformFunc, gvk, dbClient, false, true)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, informer.ByOptionsLister)
|
||||
assert.NotNil(t, informer.SharedIndexInformer)
|
||||
@@ -289,7 +289,7 @@ func TestNewInformer(t *testing.T) {
|
||||
transformFunc := func(input interface{}) (interface{}, error) {
|
||||
return "someoutput", nil
|
||||
}
|
||||
_, err := NewInformer(dynamicClient, fields, transformFunc, gvk, dbClient, false, true)
|
||||
_, err := NewInformer(context.Background(), dynamicClient, fields, transformFunc, gvk, dbClient, false, true)
|
||||
assert.Error(t, err)
|
||||
newInformer = cache.NewSharedIndexInformer
|
||||
}})
|
||||
@@ -324,8 +324,8 @@ func TestInformerListByOptions(t *testing.T) {
|
||||
}
|
||||
expectedTotal := len(expectedList.Items)
|
||||
expectedContinueToken := "123"
|
||||
indexer.EXPECT().ListByOptions(context.TODO(), lo, partitions, ns).Return(expectedList, expectedTotal, expectedContinueToken, nil)
|
||||
list, total, continueToken, err := informer.ListByOptions(context.TODO(), lo, partitions, ns)
|
||||
indexer.EXPECT().ListByOptions(context.Background(), lo, partitions, ns).Return(expectedList, expectedTotal, expectedContinueToken, nil)
|
||||
list, total, continueToken, err := informer.ListByOptions(context.Background(), lo, partitions, ns)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, expectedList, list)
|
||||
assert.Equal(t, len(expectedList.Items), total)
|
||||
@@ -339,8 +339,8 @@ func TestInformerListByOptions(t *testing.T) {
|
||||
lo := ListOptions{}
|
||||
var partitions []partition.Partition
|
||||
ns := "somens"
|
||||
indexer.EXPECT().ListByOptions(context.TODO(), lo, partitions, ns).Return(nil, 0, "", fmt.Errorf("error"))
|
||||
_, _, _, err := informer.ListByOptions(context.TODO(), lo, partitions, ns)
|
||||
indexer.EXPECT().ListByOptions(context.Background(), lo, partitions, ns).Return(nil, 0, "", fmt.Errorf("error"))
|
||||
_, _, _, err := informer.ListByOptions(context.Background(), lo, partitions, ns)
|
||||
assert.NotNil(t, err)
|
||||
}})
|
||||
t.Parallel()
|
||||
|
@@ -73,12 +73,12 @@ const (
|
||||
// NewListOptionIndexer returns a SQLite-backed cache.Indexer of unstructured.Unstructured Kubernetes resources of a certain GVK
|
||||
// ListOptionIndexer is also able to satisfy ListOption queries on indexed (sub)fields.
|
||||
// Fields are specified as slices (e.g. "metadata.resourceVersion" is ["metadata", "resourceVersion"])
|
||||
func NewListOptionIndexer(fields [][]string, s Store, namespaced bool) (*ListOptionIndexer, error) {
|
||||
func NewListOptionIndexer(ctx context.Context, fields [][]string, s Store, namespaced bool) (*ListOptionIndexer, error) {
|
||||
// necessary in order to gob/ungob unstructured.Unstructured objects
|
||||
gob.Register(map[string]interface{}{})
|
||||
gob.Register([]interface{}{})
|
||||
|
||||
i, err := NewIndexer(cache.Indexers{}, s)
|
||||
i, err := NewIndexer(ctx, cache.Indexers{}, s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -114,7 +114,7 @@ func NewListOptionIndexer(fields [][]string, s Store, namespaced bool) (*ListOpt
|
||||
qmarks := make([]string, len(indexedFields))
|
||||
setStatements := make([]string, len(indexedFields))
|
||||
|
||||
err = l.WithTransaction(context.Background(), true, func(tx transaction.Client) error {
|
||||
err = l.WithTransaction(ctx, true, func(tx transaction.Client) error {
|
||||
_, err = tx.Exec(fmt.Sprintf(createFieldsTableFmt, dbName, strings.Join(columnDefs, ", ")))
|
||||
if err != nil {
|
||||
return err
|
||||
|
@@ -72,7 +72,7 @@ func TestNewListOptionIndexer(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
loi, err := NewListOptionIndexer(fields, store, true)
|
||||
loi, err := NewListOptionIndexer(context.Background(), fields, store, true)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, loi)
|
||||
}})
|
||||
@@ -93,7 +93,7 @@ func TestNewListOptionIndexer(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
_, err := NewListOptionIndexer(fields, store, false)
|
||||
_, err := NewListOptionIndexer(context.Background(), fields, store, false)
|
||||
assert.NotNil(t, err)
|
||||
}})
|
||||
tests = append(tests, testCase{description: "NewListOptionIndexer() with error returned from Begin(), should return an error", test: func(t *testing.T) {
|
||||
@@ -122,7 +122,7 @@ func TestNewListOptionIndexer(t *testing.T) {
|
||||
|
||||
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(fmt.Errorf("error"))
|
||||
|
||||
_, err := NewListOptionIndexer(fields, store, false)
|
||||
_, err := NewListOptionIndexer(context.Background(), fields, store, false)
|
||||
assert.NotNil(t, err)
|
||||
}})
|
||||
tests = append(tests, testCase{description: "NewListOptionIndexer() with error from Exec() when creating fields table, should return an error", test: func(t *testing.T) {
|
||||
@@ -159,7 +159,7 @@ func TestNewListOptionIndexer(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
_, err := NewListOptionIndexer(fields, store, true)
|
||||
_, err := NewListOptionIndexer(context.Background(), fields, store, true)
|
||||
assert.NotNil(t, err)
|
||||
}})
|
||||
tests = append(tests, testCase{description: "NewListOptionIndexer() with error from create-labels, should return an error", test: func(t *testing.T) {
|
||||
@@ -200,7 +200,7 @@ func TestNewListOptionIndexer(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
_, err := NewListOptionIndexer(fields, store, true)
|
||||
_, err := NewListOptionIndexer(context.Background(), fields, store, true)
|
||||
assert.NotNil(t, err)
|
||||
}})
|
||||
tests = append(tests, testCase{description: "NewListOptionIndexer() with error from Commit(), should return an error", test: func(t *testing.T) {
|
||||
@@ -242,7 +242,7 @@ func TestNewListOptionIndexer(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
_, err := NewListOptionIndexer(fields, store, true)
|
||||
_, err := NewListOptionIndexer(context.Background(), fields, store, true)
|
||||
assert.NotNil(t, err)
|
||||
}})
|
||||
|
||||
@@ -962,7 +962,7 @@ func TestListByOptions(t *testing.T) {
|
||||
store.EXPECT().ReadInt(rows).Return(len(test.expectedList.Items), nil)
|
||||
store.EXPECT().CloseStmt(stmt).Return(nil)
|
||||
}
|
||||
list, total, contToken, err := lii.executeQuery(context.TODO(), queryInfo)
|
||||
list, total, contToken, err := lii.executeQuery(context.Background(), queryInfo)
|
||||
if test.expectedErr == nil {
|
||||
assert.Nil(t, err)
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user