1
0
mirror of https://github.com/rancher/steve.git synced 2025-09-09 11:19:12 +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:
Silvio Moioli
2025-02-12 09:46:10 +01:00
committed by GitHub
parent e71f8c455d
commit 3350323f91
16 changed files with 160 additions and 121 deletions

View File

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