[NET-6444] Add tenancy to Reaper Tests (#19550)

This commit is contained in:
Kumar Kavish 2023-11-10 01:14:33 +05:30 committed by GitHub
parent f09dbb99e9
commit 3df8b58479
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 174 additions and 149 deletions

View File

@ -4,6 +4,7 @@
package reaper
import (
"github.com/hashicorp/consul/internal/resource/resourcetest"
"testing"
"time"
@ -20,10 +21,15 @@ import (
)
func TestReconcile_ResourceWithNoChildren(t *testing.T) {
client := svctest.RunResourceService(t, demo.RegisterTypes)
client := svctest.RunResourceServiceWithTenancies(t, demo.RegisterTypes)
runReaperTestCaseWithTenancies(func(tenancy *pbresource.Tenancy) {
// Seed the database with an artist.
res, err := demo.GenerateV2Artist()
// set resource tenancy from default to test tenancy
res.Id.Tenancy = tenancy
require.NoError(t, err)
ctx := testutil.TestContext(t)
writeRsp, err := client.Write(ctx, &pbresource.WriteRequest{Resource: res})
@ -36,7 +42,7 @@ func TestReconcile_ResourceWithNoChildren(t *testing.T) {
// Retrieve tombstone
listRsp, err := client.List(ctx, &pbresource.ListRequest{
Type: resource.TypeV1Tombstone,
Tenancy: writeRsp.Resource.Id.Tenancy,
Tenancy: tenancy,
})
require.NoError(t, err)
require.Len(t, listRsp.Resources, 1)
@ -69,13 +75,19 @@ func TestReconcile_ResourceWithNoChildren(t *testing.T) {
// Reconcile again to verify no-op on an already deleted tombstone
require.NoError(t, rec.Reconcile(ctx, runtime, req))
})
}
func TestReconcile_ResourceWithChildren(t *testing.T) {
client := svctest.RunResourceService(t, demo.RegisterTypes)
client := svctest.RunResourceServiceWithTenancies(t, demo.RegisterTypes)
runReaperTestCaseWithTenancies(func(tenancy *pbresource.Tenancy) {
// Seed the database with an artist
res, err := demo.GenerateV2Artist()
// set resource tenancy from default to test tenancy
res.Id.Tenancy = tenancy
require.NoError(t, err)
ctx := testutil.TestContext(t)
writeRsp, err := client.Write(ctx, &pbresource.WriteRequest{Resource: res})
@ -146,13 +158,19 @@ func TestReconcile_ResourceWithChildren(t *testing.T) {
})
require.NoError(t, err)
require.Len(t, listRsp.Resources, numAlbums)
})
}
func TestReconcile_RequeueWithDelayWhenSecondPassDelayNotElapsed(t *testing.T) {
client := svctest.RunResourceService(t, demo.RegisterTypes)
client := svctest.RunResourceServiceWithTenancies(t, demo.RegisterTypes)
runReaperTestCaseWithTenancies(func(tenancy *pbresource.Tenancy) {
// Seed the database with an artist.
res, err := demo.GenerateV2Artist()
// set resource tenancy from default to test tenancy
res.Id.Tenancy = tenancy
require.NoError(t, err)
ctx := testutil.TestContext(t)
writeRsp, err := client.Write(ctx, &pbresource.WriteRequest{Resource: res})
@ -190,4 +208,11 @@ func TestReconcile_RequeueWithDelayWhenSecondPassDelayNotElapsed(t *testing.T) {
// Verify requeued for second pass since secondPassDelay time has not elapsed
require.ErrorIs(t, controller.RequeueAfterError(secondPassDelay), rec.Reconcile(ctx, runtime, req))
})
}
func runReaperTestCaseWithTenancies(testCase func(tenancy *pbresource.Tenancy)) {
for _, tenancy := range resourcetest.TestTenancies() {
testCase(tenancy)
}
}