Skip to content

Commit f0374d0

Browse files
committed
refactor crud tests to be self-contained
1 parent 3088e74 commit f0374d0

File tree

13 files changed

+85
-108
lines changed

13 files changed

+85
-108
lines changed

test-integration/utils/databasemutationhelpers/resource_crud_test_util.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,37 @@ import (
2424
"strings"
2525
"testing"
2626

27+
"github.com/stretchr/testify/require"
28+
2729
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
2830

2931
"github.com/Azure/ARO-HCP/internal/api"
32+
"github.com/Azure/ARO-HCP/test-integration/utils/integrationutils"
3033
)
3134

3235
type ResourceMutationTest struct {
33-
testDir fs.FS
34-
cosmosContainer *azcosmos.ContainerClient
36+
testDir fs.FS
3537

3638
steps []IntegrationTestStep
3739
}
3840

3941
type IntegrationTestStep interface {
4042
StepID() StepID
41-
RunTest(ctx context.Context, t *testing.T)
43+
RunTest(ctx context.Context, t *testing.T, cosmosContainer *azcosmos.ContainerClient)
4244
}
4345

44-
func NewResourceMutationTest[InternalAPIType any](ctx context.Context, specializer ResourceCRUDTestSpecializer[InternalAPIType], cosmosContainer *azcosmos.ContainerClient, testName string, testDir fs.FS) (*ResourceMutationTest, error) {
45-
steps, err := readSteps(ctx, testDir, specializer, cosmosContainer)
46+
func NewResourceMutationTest[InternalAPIType any](ctx context.Context, specializer ResourceCRUDTestSpecializer[InternalAPIType], testName string, testDir fs.FS) (*ResourceMutationTest, error) {
47+
steps, err := readSteps(ctx, testDir, specializer)
4648
if err != nil {
4749
return nil, fmt.Errorf("failed to read steps for test %q: %w", testName, err)
4850
}
4951
return &ResourceMutationTest{
50-
testDir: testDir,
51-
cosmosContainer: cosmosContainer,
52-
steps: steps,
52+
testDir: testDir,
53+
steps: steps,
5354
}, nil
5455
}
5556

56-
func readSteps[InternalAPIType any](ctx context.Context, testDir fs.FS, specializer ResourceCRUDTestSpecializer[InternalAPIType], cosmosContainer *azcosmos.ContainerClient) ([]IntegrationTestStep, error) {
57+
func readSteps[InternalAPIType any](ctx context.Context, testDir fs.FS, specializer ResourceCRUDTestSpecializer[InternalAPIType]) ([]IntegrationTestStep, error) {
5758
steps := []IntegrationTestStep{}
5859

5960
testContent := api.Must(fs.ReadDir(testDir, "."))
@@ -71,7 +72,7 @@ func readSteps[InternalAPIType any](ctx context.Context, testDir fs.FS, speciali
7172
stepType := filenameParts[1]
7273
stepName, _ := strings.CutSuffix(filenameParts[2], ".json")
7374

74-
testStep, err := newStep(index, stepType, stepName, testDir, dirEntry.Name(), specializer, cosmosContainer)
75+
testStep, err := newStep(index, stepType, stepName, testDir, dirEntry.Name(), specializer)
7576
if err != nil {
7677
return nil, fmt.Errorf("failed to create new step %q: %w", dirEntry.Name(), err)
7778
}
@@ -83,17 +84,17 @@ func readSteps[InternalAPIType any](ctx context.Context, testDir fs.FS, speciali
8384
}
8485

8586
func (tt *ResourceMutationTest) RunTest(t *testing.T) {
86-
_, testInfo, err := NewFrontendFromTestingEnv(ctx, t)
87+
testInfo, err := integrationutils.NewCosmosFromTestingEnv(t.Context())
8788
require.NoError(t, err)
8889
defer testInfo.Cleanup(context.Background())
8990

9091
for _, step := range tt.steps {
9192
t.Logf("Running step %s", step.StepID())
92-
step.RunTest(t.Context(), t)
93+
step.RunTest(t.Context(), t, testInfo.CosmosResourcesContainer())
9394
}
9495
}
9596

96-
func newStep[InternalAPIType any](indexString, stepType, stepName string, testDir fs.FS, path string, specializer ResourceCRUDTestSpecializer[InternalAPIType], cosmosContainer *azcosmos.ContainerClient) (IntegrationTestStep, error) {
97+
func newStep[InternalAPIType any](indexString, stepType, stepName string, testDir fs.FS, path string, specializer ResourceCRUDTestSpecializer[InternalAPIType]) (IntegrationTestStep, error) {
9798
itoInt, err := strconv.Atoi(indexString)
9899
if err != nil {
99100
return nil, fmt.Errorf("failed to convert %s to int: %w", indexString, err)
@@ -106,40 +107,40 @@ func newStep[InternalAPIType any](indexString, stepType, stepName string, testDi
106107

107108
switch stepType {
108109
case "load":
109-
return NewLoadStep(stepID, cosmosContainer, stepDir)
110+
return NewLoadStep(stepID, stepDir)
110111

111112
case "create":
112-
return newCreateStep(stepID, specializer, cosmosContainer, stepDir)
113+
return newCreateStep(stepID, specializer, stepDir)
113114

114115
case "replace":
115-
return newReplaceStep(stepID, specializer, cosmosContainer, stepDir)
116+
return newReplaceStep(stepID, specializer, stepDir)
116117

117118
case "get":
118-
return newGetStep(stepID, specializer, cosmosContainer, stepDir)
119+
return newGetStep(stepID, specializer, stepDir)
119120

120121
case "getByID":
121-
return newGetByIDStep(stepID, specializer, cosmosContainer, stepDir)
122+
return newGetByIDStep(stepID, specializer, stepDir)
122123

123124
case "untypedGet":
124-
return newUntypedGetStep(stepID, cosmosContainer, stepDir)
125+
return newUntypedGetStep(stepID, stepDir)
125126

126127
case "list":
127-
return newListStep(stepID, specializer, cosmosContainer, stepDir)
128+
return newListStep(stepID, specializer, stepDir)
128129

129130
case "listActiveOperations":
130-
return newListActiveOperationsStep(stepID, cosmosContainer, stepDir)
131+
return newListActiveOperationsStep(stepID, stepDir)
131132

132133
case "untypedListRecursive":
133-
return newUntypedListRecursiveStep(stepID, cosmosContainer, stepDir)
134+
return newUntypedListRecursiveStep(stepID, stepDir)
134135

135136
case "untypedList":
136-
return newUntypedListStep(stepID, cosmosContainer, stepDir)
137+
return newUntypedListStep(stepID, stepDir)
137138

138139
case "delete":
139-
return newDeleteStep(stepID, specializer, cosmosContainer, stepDir)
140+
return newDeleteStep(stepID, specializer, stepDir)
140141

141142
case "untypedDelete":
142-
return newUntypedDeleteStep(stepID, cosmosContainer, stepDir)
143+
return newUntypedDeleteStep(stepID, stepDir)
143144

144145
default:
145146
return nil, fmt.Errorf("unknown step type: %s", stepType)

test-integration/utils/databasemutationhelpers/step_create.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ type createStep[InternalAPIType any] struct {
3131
key CosmosCRUDKey
3232
specializer ResourceCRUDTestSpecializer[InternalAPIType]
3333

34-
cosmosContainer *azcosmos.ContainerClient
35-
resources []*InternalAPIType
34+
resources []*InternalAPIType
3635
}
3736

38-
func newCreateStep[InternalAPIType any](stepID StepID, specializer ResourceCRUDTestSpecializer[InternalAPIType], cosmosContainer *azcosmos.ContainerClient, stepDir fs.FS) (*createStep[InternalAPIType], error) {
37+
func newCreateStep[InternalAPIType any](stepID StepID, specializer ResourceCRUDTestSpecializer[InternalAPIType], stepDir fs.FS) (*createStep[InternalAPIType], error) {
3938
keyBytes, err := fs.ReadFile(stepDir, "00-key.json")
4039
if err != nil {
4140
return nil, fmt.Errorf("failed to read key.json: %w", err)
@@ -51,11 +50,10 @@ func newCreateStep[InternalAPIType any](stepID StepID, specializer ResourceCRUDT
5150
}
5251

5352
return &createStep[InternalAPIType]{
54-
stepID: stepID,
55-
key: key,
56-
specializer: specializer,
57-
cosmosContainer: cosmosContainer,
58-
resources: resources,
53+
stepID: stepID,
54+
key: key,
55+
specializer: specializer,
56+
resources: resources,
5957
}, nil
6058
}
6159

@@ -65,8 +63,8 @@ func (l *createStep[InternalAPIType]) StepID() StepID {
6563
return l.stepID
6664
}
6765

68-
func (l *createStep[InternalAPIType]) RunTest(ctx context.Context, t *testing.T) {
69-
controllerCRUDClient := l.specializer.ResourceCRUDFromKey(t, l.cosmosContainer, l.key)
66+
func (l *createStep[InternalAPIType]) RunTest(ctx context.Context, t *testing.T, cosmosContainer *azcosmos.ContainerClient) {
67+
controllerCRUDClient := l.specializer.ResourceCRUDFromKey(t, cosmosContainer, l.key)
7068

7169
for _, resource := range l.resources {
7270
_, err := controllerCRUDClient.Create(ctx, resource, nil)

test-integration/utils/databasemutationhelpers/step_delete.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ type deleteStep[InternalAPIType any] struct {
3939
key CosmosDeleteKey
4040
specializer ResourceCRUDTestSpecializer[InternalAPIType]
4141

42-
cosmosContainer *azcosmos.ContainerClient
43-
expectedError string
42+
expectedError string
4443
}
4544

46-
func newDeleteStep[InternalAPIType any](stepID StepID, specializer ResourceCRUDTestSpecializer[InternalAPIType], cosmosContainer *azcosmos.ContainerClient, stepDir fs.FS) (*deleteStep[InternalAPIType], error) {
45+
func newDeleteStep[InternalAPIType any](stepID StepID, specializer ResourceCRUDTestSpecializer[InternalAPIType], stepDir fs.FS) (*deleteStep[InternalAPIType], error) {
4746
keyBytes, err := fs.ReadFile(stepDir, "00-key.json")
4847
if err != nil {
4948
return nil, fmt.Errorf("failed to read key.json: %w", err)
@@ -60,11 +59,10 @@ func newDeleteStep[InternalAPIType any](stepID StepID, specializer ResourceCRUDT
6059
expectedError := strings.TrimSpace(string(expectedErrorBytes))
6160

6261
return &deleteStep[InternalAPIType]{
63-
stepID: stepID,
64-
key: key,
65-
specializer: specializer,
66-
cosmosContainer: cosmosContainer,
67-
expectedError: expectedError,
62+
stepID: stepID,
63+
key: key,
64+
specializer: specializer,
65+
expectedError: expectedError,
6866
}, nil
6967
}
7068

@@ -74,8 +72,8 @@ func (l *deleteStep[InternalAPIType]) StepID() StepID {
7472
return l.stepID
7573
}
7674

77-
func (l *deleteStep[InternalAPIType]) RunTest(ctx context.Context, t *testing.T) {
78-
controllerCRUDClient := l.specializer.ResourceCRUDFromKey(t, l.cosmosContainer, l.key.CosmosCRUDKey)
75+
func (l *deleteStep[InternalAPIType]) RunTest(ctx context.Context, t *testing.T, cosmosContainer *azcosmos.ContainerClient) {
76+
controllerCRUDClient := l.specializer.ResourceCRUDFromKey(t, cosmosContainer, l.key.CosmosCRUDKey)
7977
err := controllerCRUDClient.Delete(ctx, l.key.DeleteResourceName)
8078
switch {
8179
case len(l.expectedError) > 0:

test-integration/utils/databasemutationhelpers/step_get.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ type getStep[InternalAPIType any] struct {
3333
key CosmosCRUDKey
3434
specializer ResourceCRUDTestSpecializer[InternalAPIType]
3535

36-
cosmosContainer *azcosmos.ContainerClient
3736
expectedResource *InternalAPIType
3837
expectedError string
3938
}
4039

41-
func newGetStep[InternalAPIType any](stepID StepID, specializer ResourceCRUDTestSpecializer[InternalAPIType], cosmosContainer *azcosmos.ContainerClient, stepDir fs.FS) (*getStep[InternalAPIType], error) {
40+
func newGetStep[InternalAPIType any](stepID StepID, specializer ResourceCRUDTestSpecializer[InternalAPIType], stepDir fs.FS) (*getStep[InternalAPIType], error) {
4241
keyBytes, err := fs.ReadFile(stepDir, "00-key.json")
4342
if err != nil {
4443
return nil, fmt.Errorf("failed to read key.json: %w", err)
@@ -75,7 +74,6 @@ func newGetStep[InternalAPIType any](stepID StepID, specializer ResourceCRUDTest
7574
stepID: stepID,
7675
key: key,
7776
specializer: specializer,
78-
cosmosContainer: cosmosContainer,
7977
expectedResource: expectedResource,
8078
expectedError: expectedError,
8179
}, nil
@@ -87,8 +85,8 @@ func (l *getStep[InternalAPIType]) StepID() StepID {
8785
return l.stepID
8886
}
8987

90-
func (l *getStep[InternalAPIType]) RunTest(ctx context.Context, t *testing.T) {
91-
controllerCRUDClient := l.specializer.ResourceCRUDFromKey(t, l.cosmosContainer, l.key)
88+
func (l *getStep[InternalAPIType]) RunTest(ctx context.Context, t *testing.T, cosmosContainer *azcosmos.ContainerClient) {
89+
controllerCRUDClient := l.specializer.ResourceCRUDFromKey(t, cosmosContainer, l.key)
9290
resourceName := l.specializer.NameFromInstance(l.expectedResource)
9391
actualController, err := controllerCRUDClient.Get(ctx, resourceName)
9492
switch {

test-integration/utils/databasemutationhelpers/step_getbyid.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ type getByIDStep[InternalAPIType any] struct {
3939
key GetByIDCRUDKey
4040
specializer ResourceCRUDTestSpecializer[InternalAPIType]
4141

42-
cosmosContainer *azcosmos.ContainerClient
4342
expectedResource *InternalAPIType
4443
expectedError string
4544
}
4645

47-
func newGetByIDStep[InternalAPIType any](stepID StepID, specializer ResourceCRUDTestSpecializer[InternalAPIType], cosmosContainer *azcosmos.ContainerClient, stepDir fs.FS) (*getByIDStep[InternalAPIType], error) {
46+
func newGetByIDStep[InternalAPIType any](stepID StepID, specializer ResourceCRUDTestSpecializer[InternalAPIType], stepDir fs.FS) (*getByIDStep[InternalAPIType], error) {
4847
keyBytes, err := fs.ReadFile(stepDir, "00-key.json")
4948
if err != nil {
5049
return nil, fmt.Errorf("failed to read key.json: %w", err)
@@ -81,7 +80,6 @@ func newGetByIDStep[InternalAPIType any](stepID StepID, specializer ResourceCRUD
8180
stepID: stepID,
8281
key: key,
8382
specializer: specializer,
84-
cosmosContainer: cosmosContainer,
8583
expectedResource: expectedResource,
8684
expectedError: expectedError,
8785
}, nil
@@ -93,8 +91,8 @@ func (l *getByIDStep[InternalAPIType]) StepID() StepID {
9391
return l.stepID
9492
}
9593

96-
func (l *getByIDStep[InternalAPIType]) RunTest(ctx context.Context, t *testing.T) {
97-
controllerCRUDClient := l.specializer.ResourceCRUDFromKey(t, l.cosmosContainer, l.key.CosmosCRUDKey)
94+
func (l *getByIDStep[InternalAPIType]) RunTest(ctx context.Context, t *testing.T, cosmosContainer *azcosmos.ContainerClient) {
95+
controllerCRUDClient := l.specializer.ResourceCRUDFromKey(t, cosmosContainer, l.key.CosmosCRUDKey)
9896
actualController, err := controllerCRUDClient.GetByID(ctx, l.key.CosmosID)
9997
switch {
10098
case len(l.expectedError) > 0:

test-integration/utils/databasemutationhelpers/step_list.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ type listStep[InternalAPIType any] struct {
3131
key CosmosCRUDKey
3232
specializer ResourceCRUDTestSpecializer[InternalAPIType]
3333

34-
cosmosContainer *azcosmos.ContainerClient
3534
expectedResources []*InternalAPIType
3635
}
3736

38-
func newListStep[InternalAPIType any](stepID StepID, specializer ResourceCRUDTestSpecializer[InternalAPIType], cosmosContainer *azcosmos.ContainerClient, stepDir fs.FS) (*listStep[InternalAPIType], error) {
37+
func newListStep[InternalAPIType any](stepID StepID, specializer ResourceCRUDTestSpecializer[InternalAPIType], stepDir fs.FS) (*listStep[InternalAPIType], error) {
3938
keyBytes, err := fs.ReadFile(stepDir, "00-key.json")
4039
if err != nil {
4140
return nil, fmt.Errorf("failed to read key.json: %w", err)
@@ -54,7 +53,6 @@ func newListStep[InternalAPIType any](stepID StepID, specializer ResourceCRUDTes
5453
stepID: stepID,
5554
key: key,
5655
specializer: specializer,
57-
cosmosContainer: cosmosContainer,
5856
expectedResources: expectedResources,
5957
}, nil
6058
}
@@ -65,8 +63,8 @@ func (l *listStep[InternalAPIType]) StepID() StepID {
6563
return l.stepID
6664
}
6765

68-
func (l *listStep[InternalAPIType]) RunTest(ctx context.Context, t *testing.T) {
69-
controllerCRUDClient := l.specializer.ResourceCRUDFromKey(t, l.cosmosContainer, l.key)
66+
func (l *listStep[InternalAPIType]) RunTest(ctx context.Context, t *testing.T, cosmosContainer *azcosmos.ContainerClient) {
67+
controllerCRUDClient := l.specializer.ResourceCRUDFromKey(t, cosmosContainer, l.key)
7068
actualControllersIterator, err := controllerCRUDClient.List(ctx, nil)
7169
require.NoError(t, err)
7270

test-integration/utils/databasemutationhelpers/step_list_active_operations.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ type listActiveOperationsStep struct {
3434
stepID StepID
3535
key CosmosCRUDKey
3636

37-
cosmosContainer *azcosmos.ContainerClient
3837
expectedOperations []*api.Operation
3938
}
4039

41-
func newListActiveOperationsStep(stepID StepID, cosmosContainer *azcosmos.ContainerClient, stepDir fs.FS) (*listActiveOperationsStep, error) {
40+
func newListActiveOperationsStep(stepID StepID, stepDir fs.FS) (*listActiveOperationsStep, error) {
4241
keyBytes, err := fs.ReadFile(stepDir, "00-key.json")
4342
if err != nil {
4443
return nil, fmt.Errorf("failed to read key.json: %w", err)
@@ -56,7 +55,6 @@ func newListActiveOperationsStep(stepID StepID, cosmosContainer *azcosmos.Contai
5655
return &listActiveOperationsStep{
5756
stepID: stepID,
5857
key: key,
59-
cosmosContainer: cosmosContainer,
6058
expectedOperations: expectedResources,
6159
}, nil
6260
}
@@ -67,11 +65,11 @@ func (l *listActiveOperationsStep) StepID() StepID {
6765
return l.stepID
6866
}
6967

70-
func (l *listActiveOperationsStep) RunTest(ctx context.Context, t *testing.T) {
68+
func (l *listActiveOperationsStep) RunTest(ctx context.Context, t *testing.T, cosmosContainer *azcosmos.ContainerClient) {
7169
parentResourceID, err := azcorearm.ParseResourceID(l.key.ParentResourceID)
7270
require.NoError(t, err)
7371

74-
operationsCRUD := database.NewOperationCRUD(l.cosmosContainer, parentResourceID.SubscriptionID)
72+
operationsCRUD := database.NewOperationCRUD(cosmosContainer, parentResourceID.SubscriptionID)
7573
actualControllersIterator := operationsCRUD.ListActiveOperations(nil)
7674
require.NoError(t, err)
7775

test-integration/utils/databasemutationhelpers/step_load.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ import (
3131
type loadStep struct {
3232
stepID StepID
3333

34-
cosmosContainer *azcosmos.ContainerClient
35-
contents [][]byte
34+
contents [][]byte
3635
}
3736

38-
func NewLoadStep(stepID StepID, cosmosContainer *azcosmos.ContainerClient, stepDir fs.FS) (*loadStep, error) {
37+
func NewLoadStep(stepID StepID, stepDir fs.FS) (*loadStep, error) {
3938

4039
contents := [][]byte{}
4140
testContent := api.Must(fs.ReadDir(stepDir, "."))
@@ -55,9 +54,8 @@ func NewLoadStep(stepID StepID, cosmosContainer *azcosmos.ContainerClient, stepD
5554
}
5655

5756
return &loadStep{
58-
stepID: stepID,
59-
cosmosContainer: cosmosContainer,
60-
contents: contents,
57+
stepID: stepID,
58+
contents: contents,
6159
}, nil
6260
}
6361

@@ -67,9 +65,9 @@ func (l *loadStep) StepID() StepID {
6765
return l.stepID
6866
}
6967

70-
func (l *loadStep) RunTest(ctx context.Context, t *testing.T) {
68+
func (l *loadStep) RunTest(ctx context.Context, t *testing.T, cosmosContainer *azcosmos.ContainerClient) {
7169
for _, content := range l.contents {
72-
err := integrationutils.LoadCosmosContent(ctx, l.cosmosContainer, content)
70+
err := integrationutils.LoadCosmosContent(ctx, cosmosContainer, content)
7371
require.NoError(t, err, "failed to load cosmos content: %v", string(content))
7472
}
7573
}

0 commit comments

Comments
 (0)