2023-05-09 14:25:55 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-05-09 14:25:55 +00:00
|
|
|
|
|
|
|
package controller_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-12-13 15:06:39 +00:00
|
|
|
"fmt"
|
2024-01-12 16:54:07 +00:00
|
|
|
"sync/atomic"
|
2023-05-09 14:25:55 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
svctest "github.com/hashicorp/consul/agent/grpc-external/services/resource/testing"
|
2023-12-13 15:06:39 +00:00
|
|
|
controller "github.com/hashicorp/consul/internal/controller"
|
|
|
|
"github.com/hashicorp/consul/internal/controller/cache"
|
|
|
|
"github.com/hashicorp/consul/internal/controller/cache/index"
|
|
|
|
"github.com/hashicorp/consul/internal/controller/cache/indexers"
|
|
|
|
"github.com/hashicorp/consul/internal/controller/dependency"
|
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
2023-05-09 14:25:55 +00:00
|
|
|
"github.com/hashicorp/consul/internal/resource/demo"
|
2023-12-13 15:06:39 +00:00
|
|
|
"github.com/hashicorp/consul/internal/resource/resourcetest"
|
2023-05-09 14:25:55 +00:00
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
2023-12-13 15:06:39 +00:00
|
|
|
pbdemov1 "github.com/hashicorp/consul/proto/private/pbdemo/v1"
|
|
|
|
pbdemov2 "github.com/hashicorp/consul/proto/private/pbdemo/v2"
|
2023-05-09 14:25:55 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/prototest"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2024-01-12 16:54:07 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2023-05-09 14:25:55 +00:00
|
|
|
)
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
var injectedError = errors.New("injected error")
|
|
|
|
|
|
|
|
func errQuery(_ cache.ReadOnlyCache, _ ...any) (cache.ResourceIterator, error) {
|
|
|
|
return nil, injectedError
|
|
|
|
}
|
|
|
|
|
2023-05-09 14:25:55 +00:00
|
|
|
func TestController_API(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
idx := indexers.DecodedSingleIndexer("genre", index.SingleValueFromArgs(func(value string) ([]byte, error) {
|
|
|
|
var b index.Builder
|
|
|
|
b.String(value)
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}), func(res *resource.DecodedResource[*pbdemov2.Artist]) (bool, []byte, error) {
|
|
|
|
var b index.Builder
|
|
|
|
b.String(res.Data.Genre.String())
|
|
|
|
return true, b.Bytes(), nil
|
|
|
|
})
|
|
|
|
|
2023-05-09 14:25:55 +00:00
|
|
|
rec := newTestReconciler()
|
2024-01-19 22:59:44 +00:00
|
|
|
init := newTestInitializer(1)
|
2023-12-05 19:00:06 +00:00
|
|
|
client := svctest.NewResourceServiceBuilder().
|
|
|
|
WithRegisterFns(demo.RegisterTypes).
|
|
|
|
Run(t)
|
2023-05-09 14:25:55 +00:00
|
|
|
|
2023-08-17 20:34:18 +00:00
|
|
|
concertsChan := make(chan controller.Event)
|
|
|
|
defer close(concertsChan)
|
|
|
|
concertSource := &controller.Source{Source: concertsChan}
|
|
|
|
concertMapper := func(ctx context.Context, rt controller.Runtime, event controller.Event) ([]controller.Request, error) {
|
|
|
|
artistID := event.Obj.(*Concert).artistID
|
|
|
|
var requests []controller.Request
|
|
|
|
requests = append(requests, controller.Request{ID: artistID})
|
|
|
|
return requests, nil
|
|
|
|
}
|
|
|
|
|
2023-05-09 14:25:55 +00:00
|
|
|
ctrl := controller.
|
2023-12-13 15:06:39 +00:00
|
|
|
NewController("artist", pbdemov2.ArtistType, idx).
|
|
|
|
WithWatch(pbdemov2.AlbumType, dependency.MapOwner, indexers.OwnerIndex("owner")).
|
|
|
|
WithQuery("some-query", errQuery).
|
2023-08-17 20:34:18 +00:00
|
|
|
WithCustomWatch(concertSource, concertMapper).
|
2023-05-09 14:25:55 +00:00
|
|
|
WithBackoff(10*time.Millisecond, 100*time.Millisecond).
|
2024-01-19 17:47:48 +00:00
|
|
|
WithReconciler(rec).
|
|
|
|
WithInitializer(init)
|
2023-05-09 14:25:55 +00:00
|
|
|
|
|
|
|
mgr := controller.NewManager(client, testutil.Logger(t))
|
|
|
|
mgr.Register(ctrl)
|
|
|
|
mgr.SetRaftLeader(true)
|
|
|
|
go mgr.Run(testContext(t))
|
|
|
|
|
2024-01-19 22:59:44 +00:00
|
|
|
// Wait for initialization to complete
|
|
|
|
init.wait(t)
|
2024-01-19 17:47:48 +00:00
|
|
|
|
2023-05-09 14:25:55 +00:00
|
|
|
t.Run("managed resource type", func(t *testing.T) {
|
|
|
|
res, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rsp, err := client.Write(testContext(t), &pbresource.WriteRequest{Resource: res})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
rt, req := rec.wait(t)
|
2023-05-09 14:25:55 +00:00
|
|
|
prototest.AssertDeepEqual(t, rsp.Resource.Id, req.ID)
|
2023-12-13 15:06:39 +00:00
|
|
|
|
|
|
|
// ensure that the cache index is being properly managed
|
|
|
|
dec := resourcetest.MustDecode[*pbdemov2.Artist](t, res)
|
|
|
|
resources, err := rt.Cache.List(pbdemov2.ArtistType, "genre", dec.Data.Genre.String())
|
|
|
|
require.NoError(t, err)
|
|
|
|
prototest.AssertElementsMatch(t, []*pbresource.Resource{rsp.Resource}, resources)
|
|
|
|
|
|
|
|
// ensure that the query was successfully registered - as we should not do equality
|
|
|
|
// checks on functions we are using a constant error return query to ensure it was
|
|
|
|
// registered properly.
|
|
|
|
iter, err := rt.Cache.Query("some-query", "irrelevant")
|
|
|
|
require.ErrorIs(t, err, injectedError)
|
|
|
|
require.Nil(t, iter)
|
2023-05-09 14:25:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("watched resource type", func(t *testing.T) {
|
|
|
|
res, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rsp, err := client.Write(testContext(t), &pbresource.WriteRequest{Resource: res})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
_, req := rec.wait(t)
|
2023-05-09 14:25:55 +00:00
|
|
|
prototest.AssertDeepEqual(t, rsp.Resource.Id, req.ID)
|
|
|
|
|
|
|
|
rec.expectNoRequest(t, 500*time.Millisecond)
|
|
|
|
|
|
|
|
album, err := demo.GenerateV2Album(rsp.Resource.Id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
albumRsp1, err := client.Write(testContext(t), &pbresource.WriteRequest{Resource: album})
|
2023-05-09 14:25:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
_, req = rec.wait(t)
|
2023-05-09 14:25:55 +00:00
|
|
|
prototest.AssertDeepEqual(t, rsp.Resource.Id, req.ID)
|
2023-12-13 15:06:39 +00:00
|
|
|
|
|
|
|
album2, err := demo.GenerateV2Album(rsp.Resource.Id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
albumRsp2, err := client.Write(testContext(t), &pbresource.WriteRequest{Resource: album2})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rt, req := rec.wait(t)
|
|
|
|
prototest.AssertDeepEqual(t, rsp.Resource.Id, req.ID)
|
|
|
|
|
|
|
|
// ensure that the watched type cache is being updated
|
|
|
|
resources, err := rt.Cache.List(pbdemov2.AlbumType, "owner", rsp.Resource.Id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
prototest.AssertElementsMatch(t, []*pbresource.Resource{albumRsp1.Resource, albumRsp2.Resource}, resources)
|
2023-05-09 14:25:55 +00:00
|
|
|
})
|
|
|
|
|
2023-08-17 20:34:18 +00:00
|
|
|
t.Run("custom watched resource type", func(t *testing.T) {
|
|
|
|
res, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rsp, err := client.Write(testContext(t), &pbresource.WriteRequest{Resource: res})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
_, req := rec.wait(t)
|
2023-08-17 20:34:18 +00:00
|
|
|
prototest.AssertDeepEqual(t, rsp.Resource.Id, req.ID)
|
|
|
|
|
|
|
|
rec.expectNoRequest(t, 500*time.Millisecond)
|
|
|
|
|
|
|
|
concertsChan <- controller.Event{Obj: &Concert{name: "test-concert", artistID: rsp.Resource.Id}}
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
_, watchedReq := rec.wait(t)
|
2023-08-17 20:34:18 +00:00
|
|
|
prototest.AssertDeepEqual(t, req.ID, watchedReq.ID)
|
|
|
|
|
|
|
|
otherArtist, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
concertsChan <- controller.Event{Obj: &Concert{name: "test-concert", artistID: otherArtist.Id}}
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
_, watchedReq = rec.wait(t)
|
2023-08-17 20:34:18 +00:00
|
|
|
prototest.AssertDeepEqual(t, otherArtist.Id, watchedReq.ID)
|
|
|
|
})
|
|
|
|
|
2023-05-09 14:25:55 +00:00
|
|
|
t.Run("error retries", func(t *testing.T) {
|
|
|
|
rec.failNext(errors.New("KABOOM"))
|
|
|
|
|
|
|
|
res, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rsp, err := client.Write(testContext(t), &pbresource.WriteRequest{Resource: res})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
_, req := rec.wait(t)
|
2023-05-09 14:25:55 +00:00
|
|
|
prototest.AssertDeepEqual(t, rsp.Resource.Id, req.ID)
|
|
|
|
|
|
|
|
// Reconciler should be called with the same request again.
|
2023-12-13 15:06:39 +00:00
|
|
|
_, req = rec.wait(t)
|
2023-05-09 14:25:55 +00:00
|
|
|
prototest.AssertDeepEqual(t, rsp.Resource.Id, req.ID)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("panic retries", func(t *testing.T) {
|
|
|
|
rec.panicNext("KABOOM")
|
|
|
|
|
|
|
|
res, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rsp, err := client.Write(testContext(t), &pbresource.WriteRequest{Resource: res})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
_, req := rec.wait(t)
|
2023-05-09 14:25:55 +00:00
|
|
|
prototest.AssertDeepEqual(t, rsp.Resource.Id, req.ID)
|
|
|
|
|
|
|
|
// Reconciler should be called with the same request again.
|
2023-12-13 15:06:39 +00:00
|
|
|
_, req = rec.wait(t)
|
2023-05-09 14:25:55 +00:00
|
|
|
prototest.AssertDeepEqual(t, rsp.Resource.Id, req.ID)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("defer", func(t *testing.T) {
|
|
|
|
rec.failNext(controller.RequeueAfter(1 * time.Second))
|
|
|
|
|
|
|
|
res, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rsp, err := client.Write(testContext(t), &pbresource.WriteRequest{Resource: res})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
_, req := rec.wait(t)
|
2023-05-09 14:25:55 +00:00
|
|
|
prototest.AssertDeepEqual(t, rsp.Resource.Id, req.ID)
|
|
|
|
|
|
|
|
rec.expectNoRequest(t, 750*time.Millisecond)
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
_, req = rec.wait(t)
|
2023-05-09 14:25:55 +00:00
|
|
|
prototest.AssertDeepEqual(t, rsp.Resource.Id, req.ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-19 22:59:44 +00:00
|
|
|
func TestController_API_InitializeRetry(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Configure initializer to error initially in order to test retries
|
|
|
|
expectedInitAttempts := 2
|
|
|
|
init := newTestInitializer(expectedInitAttempts)
|
|
|
|
|
|
|
|
client := svctest.NewResourceServiceBuilder().
|
|
|
|
WithRegisterFns(demo.RegisterTypes).
|
|
|
|
Run(t)
|
|
|
|
rec := newTestReconciler()
|
|
|
|
|
|
|
|
ctrl := controller.
|
|
|
|
NewController("artist", pbdemov2.ArtistType).
|
|
|
|
WithBackoff(10*time.Millisecond, 100*time.Millisecond).
|
|
|
|
WithReconciler(rec).
|
|
|
|
WithInitializer(init)
|
|
|
|
|
|
|
|
mgr := controller.NewManager(client, testutil.Logger(t))
|
|
|
|
mgr.Register(ctrl)
|
|
|
|
mgr.SetRaftLeader(true)
|
|
|
|
go mgr.Run(testContext(t))
|
|
|
|
|
|
|
|
// Wait for initialization attempts to complete
|
|
|
|
for i := 0; i < expectedInitAttempts; i++ {
|
|
|
|
init.wait(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a resource and expect it to reconcile now that initialization is complete
|
|
|
|
res, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = client.Write(testContext(t), &pbresource.WriteRequest{Resource: res})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rec.wait(t)
|
|
|
|
}
|
|
|
|
|
2024-01-12 16:54:07 +00:00
|
|
|
func waitForAtomicBoolValue(t testutil.TestingTB, actual *atomic.Bool, expected bool) {
|
|
|
|
t.Helper()
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
require.Equal(r, expected, actual.Load())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-09 14:25:55 +00:00
|
|
|
func TestController_Placement(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("singleton", func(t *testing.T) {
|
2024-01-12 16:54:07 +00:00
|
|
|
var running atomic.Bool
|
|
|
|
running.Store(false)
|
|
|
|
|
2023-05-09 14:25:55 +00:00
|
|
|
rec := newTestReconciler()
|
2023-12-05 19:00:06 +00:00
|
|
|
client := svctest.NewResourceServiceBuilder().
|
|
|
|
WithRegisterFns(demo.RegisterTypes).
|
2024-01-12 16:54:07 +00:00
|
|
|
WithCloningDisabled().
|
2023-12-05 19:00:06 +00:00
|
|
|
Run(t)
|
2023-05-09 14:25:55 +00:00
|
|
|
|
|
|
|
ctrl := controller.
|
2023-12-13 15:06:39 +00:00
|
|
|
NewController("artist", pbdemov2.ArtistType).
|
|
|
|
WithWatch(pbdemov2.AlbumType, dependency.MapOwner).
|
2023-05-09 14:25:55 +00:00
|
|
|
WithPlacement(controller.PlacementSingleton).
|
2024-01-12 16:54:07 +00:00
|
|
|
WithNotifyStart(func(context.Context, controller.Runtime) {
|
|
|
|
running.Store(true)
|
|
|
|
}).
|
|
|
|
WithNotifyStop(func(context.Context, controller.Runtime) {
|
|
|
|
running.Store(false)
|
|
|
|
}).
|
2023-05-09 14:25:55 +00:00
|
|
|
WithReconciler(rec)
|
|
|
|
|
|
|
|
mgr := controller.NewManager(client, testutil.Logger(t))
|
|
|
|
mgr.Register(ctrl)
|
|
|
|
go mgr.Run(testContext(t))
|
|
|
|
|
|
|
|
res, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Reconciler should not be called until we're the Raft leader.
|
|
|
|
_, err = client.Write(testContext(t), &pbresource.WriteRequest{Resource: res})
|
|
|
|
require.NoError(t, err)
|
|
|
|
rec.expectNoRequest(t, 500*time.Millisecond)
|
|
|
|
|
|
|
|
// Become the leader and check the reconciler is called.
|
|
|
|
mgr.SetRaftLeader(true)
|
2024-01-12 16:54:07 +00:00
|
|
|
waitForAtomicBoolValue(t, &running, true)
|
2023-12-13 15:06:39 +00:00
|
|
|
_, _ = rec.wait(t)
|
2023-05-09 14:25:55 +00:00
|
|
|
|
|
|
|
// Should not be called after losing leadership.
|
|
|
|
mgr.SetRaftLeader(false)
|
2024-01-12 16:54:07 +00:00
|
|
|
waitForAtomicBoolValue(t, &running, false)
|
2023-05-09 14:25:55 +00:00
|
|
|
_, err = client.Write(testContext(t), &pbresource.WriteRequest{Resource: res})
|
|
|
|
require.NoError(t, err)
|
|
|
|
rec.expectNoRequest(t, 500*time.Millisecond)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("each server", func(t *testing.T) {
|
2024-01-12 16:54:07 +00:00
|
|
|
var running atomic.Bool
|
|
|
|
running.Store(false)
|
|
|
|
|
2023-05-09 14:25:55 +00:00
|
|
|
rec := newTestReconciler()
|
2023-12-05 19:00:06 +00:00
|
|
|
client := svctest.NewResourceServiceBuilder().
|
|
|
|
WithRegisterFns(demo.RegisterTypes).
|
|
|
|
Run(t)
|
2023-05-09 14:25:55 +00:00
|
|
|
|
|
|
|
ctrl := controller.
|
2023-12-13 15:06:39 +00:00
|
|
|
NewController("artist", pbdemov2.ArtistType).
|
|
|
|
WithWatch(pbdemov2.AlbumType, dependency.MapOwner).
|
2023-05-09 14:25:55 +00:00
|
|
|
WithPlacement(controller.PlacementEachServer).
|
2024-01-12 16:54:07 +00:00
|
|
|
WithNotifyStart(func(context.Context, controller.Runtime) {
|
|
|
|
running.Store(true)
|
|
|
|
}).
|
2023-05-09 14:25:55 +00:00
|
|
|
WithReconciler(rec)
|
|
|
|
|
|
|
|
mgr := controller.NewManager(client, testutil.Logger(t))
|
|
|
|
mgr.Register(ctrl)
|
|
|
|
go mgr.Run(testContext(t))
|
2024-01-12 16:54:07 +00:00
|
|
|
waitForAtomicBoolValue(t, &running, true)
|
2023-05-09 14:25:55 +00:00
|
|
|
|
|
|
|
res, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Reconciler should be called even though we're not the Raft leader.
|
|
|
|
_, err = client.Write(testContext(t), &pbresource.WriteRequest{Resource: res})
|
|
|
|
require.NoError(t, err)
|
2023-12-13 15:06:39 +00:00
|
|
|
_, _ = rec.wait(t)
|
2023-05-09 14:25:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestController_String(t *testing.T) {
|
|
|
|
ctrl := controller.
|
2023-12-13 15:06:39 +00:00
|
|
|
NewController("artist", pbdemov2.ArtistType).
|
|
|
|
WithWatch(pbdemov2.AlbumType, dependency.MapOwner).
|
2023-05-09 14:25:55 +00:00
|
|
|
WithBackoff(5*time.Second, 1*time.Hour).
|
|
|
|
WithPlacement(controller.PlacementEachServer)
|
|
|
|
|
|
|
|
require.Equal(t,
|
2023-12-13 15:06:39 +00:00
|
|
|
`<Controller managed_type=demo.v2.Artist, watched_types=[demo.v2.Album], backoff=<base=5s, max=1h0m0s>, placement=each-server>`,
|
2023-05-09 14:25:55 +00:00
|
|
|
ctrl.String(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestController_NoReconciler(t *testing.T) {
|
2023-12-05 19:00:06 +00:00
|
|
|
client := svctest.NewResourceServiceBuilder().
|
|
|
|
WithRegisterFns(demo.RegisterTypes).
|
|
|
|
Run(t)
|
|
|
|
|
2023-05-09 14:25:55 +00:00
|
|
|
mgr := controller.NewManager(client, testutil.Logger(t))
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
ctrl := controller.NewController("artist", pbdemov2.ArtistType)
|
2023-05-09 14:25:55 +00:00
|
|
|
require.PanicsWithValue(t,
|
2023-12-13 15:06:39 +00:00
|
|
|
fmt.Sprintf("cannot register controller without a reconciler %s", ctrl.String()),
|
2023-05-09 14:25:55 +00:00
|
|
|
func() { mgr.Register(ctrl) })
|
|
|
|
}
|
|
|
|
|
2023-11-03 20:03:07 +00:00
|
|
|
func TestController_Watch(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("partitioned scoped resources", func(t *testing.T) {
|
|
|
|
rec := newTestReconciler()
|
|
|
|
|
2023-12-05 19:00:06 +00:00
|
|
|
client := svctest.NewResourceServiceBuilder().
|
|
|
|
WithRegisterFns(demo.RegisterTypes).
|
|
|
|
Run(t)
|
2023-11-03 20:03:07 +00:00
|
|
|
|
|
|
|
ctrl := controller.
|
2023-12-13 15:06:39 +00:00
|
|
|
NewController("labels", pbdemov1.RecordLabelType).
|
2023-11-03 20:03:07 +00:00
|
|
|
WithReconciler(rec)
|
|
|
|
|
|
|
|
mgr := controller.NewManager(client, testutil.Logger(t))
|
|
|
|
mgr.SetRaftLeader(true)
|
|
|
|
mgr.Register(ctrl)
|
|
|
|
|
|
|
|
ctx := testContext(t)
|
|
|
|
go mgr.Run(ctx)
|
|
|
|
|
|
|
|
res, err := demo.GenerateV1RecordLabel("test")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rsp, err := client.Write(testContext(t), &pbresource.WriteRequest{Resource: res})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
_, req := rec.wait(t)
|
2023-11-03 20:03:07 +00:00
|
|
|
prototest.AssertDeepEqual(t, rsp.Resource.Id, req.ID)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("cluster scoped resources", func(t *testing.T) {
|
|
|
|
rec := newTestReconciler()
|
|
|
|
|
2023-12-05 19:00:06 +00:00
|
|
|
client := svctest.NewResourceServiceBuilder().
|
|
|
|
WithRegisterFns(demo.RegisterTypes).
|
|
|
|
Run(t)
|
2023-11-03 20:03:07 +00:00
|
|
|
|
|
|
|
ctrl := controller.
|
2023-12-13 15:06:39 +00:00
|
|
|
NewController("executives", pbdemov1.ExecutiveType).
|
2023-11-03 20:03:07 +00:00
|
|
|
WithReconciler(rec)
|
|
|
|
|
|
|
|
mgr := controller.NewManager(client, testutil.Logger(t))
|
|
|
|
mgr.SetRaftLeader(true)
|
|
|
|
mgr.Register(ctrl)
|
|
|
|
|
|
|
|
go mgr.Run(testContext(t))
|
|
|
|
|
|
|
|
exec, err := demo.GenerateV1Executive("test", "CEO")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rsp, err := client.Write(testContext(t), &pbresource.WriteRequest{Resource: exec})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
_, req := rec.wait(t)
|
2023-11-03 20:03:07 +00:00
|
|
|
prototest.AssertDeepEqual(t, rsp.Resource.Id, req.ID)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("namespace scoped resources", func(t *testing.T) {
|
|
|
|
rec := newTestReconciler()
|
|
|
|
|
2023-12-05 19:00:06 +00:00
|
|
|
client := svctest.NewResourceServiceBuilder().
|
|
|
|
WithRegisterFns(demo.RegisterTypes).
|
|
|
|
Run(t)
|
2023-11-03 20:03:07 +00:00
|
|
|
|
|
|
|
ctrl := controller.
|
2023-12-13 15:06:39 +00:00
|
|
|
NewController("artists", pbdemov2.ArtistType).
|
2023-11-03 20:03:07 +00:00
|
|
|
WithReconciler(rec)
|
|
|
|
|
|
|
|
mgr := controller.NewManager(client, testutil.Logger(t))
|
|
|
|
mgr.SetRaftLeader(true)
|
|
|
|
mgr.Register(ctrl)
|
|
|
|
|
|
|
|
go mgr.Run(testContext(t))
|
|
|
|
|
|
|
|
artist, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rsp, err := client.Write(testContext(t), &pbresource.WriteRequest{Resource: artist})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
_, req := rec.wait(t)
|
2023-11-03 20:03:07 +00:00
|
|
|
prototest.AssertDeepEqual(t, rsp.Resource.Id, req.ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-09 14:25:55 +00:00
|
|
|
func newTestReconciler() *testReconciler {
|
|
|
|
return &testReconciler{
|
2023-12-13 15:06:39 +00:00
|
|
|
calls: make(chan requestArgs),
|
2023-05-09 14:25:55 +00:00
|
|
|
errors: make(chan error, 1),
|
|
|
|
panics: make(chan any, 1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
type requestArgs struct {
|
|
|
|
req controller.Request
|
|
|
|
rt controller.Runtime
|
|
|
|
}
|
|
|
|
|
2023-05-09 14:25:55 +00:00
|
|
|
type testReconciler struct {
|
2023-12-13 15:06:39 +00:00
|
|
|
calls chan requestArgs
|
2023-05-09 14:25:55 +00:00
|
|
|
errors chan error
|
|
|
|
panics chan any
|
|
|
|
}
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
func (r *testReconciler) Reconcile(_ context.Context, rt controller.Runtime, req controller.Request) error {
|
|
|
|
r.calls <- requestArgs{req: req, rt: rt}
|
2023-05-09 14:25:55 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-r.errors:
|
|
|
|
return err
|
|
|
|
case p := <-r.panics:
|
|
|
|
panic(p)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *testReconciler) failNext(err error) { r.errors <- err }
|
|
|
|
func (r *testReconciler) panicNext(p any) { r.panics <- p }
|
|
|
|
|
|
|
|
func (r *testReconciler) expectNoRequest(t *testing.T, duration time.Duration) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
started := time.Now()
|
|
|
|
select {
|
2023-12-13 15:06:39 +00:00
|
|
|
case args := <-r.calls:
|
|
|
|
t.Fatalf("expected no request for %s, but got: %s after %s", duration, args.req.ID, time.Since(started))
|
2023-05-09 14:25:55 +00:00
|
|
|
case <-time.After(duration):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
func (r *testReconciler) wait(t *testing.T) (controller.Runtime, controller.Request) {
|
2023-05-09 14:25:55 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
var args requestArgs
|
2023-05-09 14:25:55 +00:00
|
|
|
select {
|
2023-12-13 15:06:39 +00:00
|
|
|
case args = <-r.calls:
|
2023-05-09 14:25:55 +00:00
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
t.Fatal("Reconcile was not called after 500ms")
|
|
|
|
}
|
2023-12-13 15:06:39 +00:00
|
|
|
return args.rt, args.req
|
2023-05-09 14:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testContext(t *testing.T) context.Context {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|
2023-08-17 20:34:18 +00:00
|
|
|
|
|
|
|
type Concert struct {
|
|
|
|
name string
|
|
|
|
artistID *pbresource.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Concert) Key() string {
|
|
|
|
return c.name
|
|
|
|
}
|
2024-01-19 17:47:48 +00:00
|
|
|
|
|
|
|
func newTestInitializer(errorCount int) *testInitializer {
|
|
|
|
return &testInitializer{
|
|
|
|
calls: make(chan error, 1),
|
|
|
|
expectedAttempts: errorCount,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testInitializer struct {
|
|
|
|
expectedAttempts int // number of times the initializer should run to test retries
|
|
|
|
attempts int // running count of times initialize is called
|
|
|
|
calls chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *testInitializer) Initialize(_ context.Context, _ controller.Runtime) error {
|
|
|
|
i.attempts++
|
|
|
|
if i.attempts < i.expectedAttempts {
|
|
|
|
// Return an error to cause a retry
|
|
|
|
err := errors.New("initialization error")
|
|
|
|
i.calls <- err
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
i.calls <- nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *testInitializer) wait(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
select {
|
|
|
|
case err := <-i.calls:
|
|
|
|
if err == nil {
|
|
|
|
// Initialize did not error, no more calls should be expected
|
|
|
|
close(i.calls)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
case <-time.After(1000 * time.Millisecond):
|
|
|
|
t.Fatal("Initialize was not called after 1000ms")
|
|
|
|
}
|
|
|
|
}
|