|
| 1 | +package manager |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "strconv" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 12 | + "k8s.io/client-go/tools/record" |
| 13 | + "k8s.io/component-base/config" |
| 14 | + "k8s.io/klog/v2/klogr" |
| 15 | + |
| 16 | + "github.com/authzed/controller-idioms/queue" |
| 17 | + "github.com/authzed/controller-idioms/typed" |
| 18 | +) |
| 19 | + |
| 20 | +func TestManager(t *testing.T) { |
| 21 | + t.Parallel() |
| 22 | + ctx, cancel := context.WithCancel(context.Background()) |
| 23 | + |
| 24 | + m := NewManager(&config.DebuggingConfiguration{ |
| 25 | + EnableProfiling: false, |
| 26 | + EnableContentionProfiling: false, |
| 27 | + }, ":"+getFreePort(t), nil, nil) |
| 28 | + |
| 29 | + ready := make(chan struct{}) |
| 30 | + go func() { |
| 31 | + require.NoError(t, m.Start(ctx, ready, testController(t, "a"))) |
| 32 | + }() |
| 33 | + <-ready |
| 34 | + |
| 35 | + requireCancelFnCount(t, m, 1) |
| 36 | + |
| 37 | + // Ensure that the manager can't be started twice. |
| 38 | + require.Error(t, m.Start(ctx, ready), "manager already started") |
| 39 | + |
| 40 | + // Add some controllers after start |
| 41 | + require.NoError(t, m.Go(testController(t, "b"), testController(t, "c"))) |
| 42 | + requireCancelFnCount(t, m, 3) |
| 43 | + |
| 44 | + specificCtrl := testController(t, "d") |
| 45 | + require.NoError(t, m.Go(specificCtrl)) |
| 46 | + requireCancelFnCount(t, m, 4) |
| 47 | + |
| 48 | + // stop a specific controller |
| 49 | + m.Cancel(specificCtrl) |
| 50 | + requireCancelFnCount(t, m, 3) |
| 51 | + |
| 52 | + // cancel the manager's context, which will clean up all controllers |
| 53 | + cancel() |
| 54 | + |
| 55 | + requireCancelFnCount(t, m, 0) |
| 56 | +} |
| 57 | + |
| 58 | +func getFreePort(t testing.TB) string { |
| 59 | + t.Helper() |
| 60 | + |
| 61 | + a, err := net.ResolveTCPAddr("tcp", "localhost:0") |
| 62 | + require.NoError(t, err) |
| 63 | + l, err := net.ListenTCP("tcp", a) |
| 64 | + require.NoError(t, err) |
| 65 | + defer func() { |
| 66 | + require.NoError(t, l.Close()) |
| 67 | + }() |
| 68 | + return strconv.Itoa(l.Addr().(*net.TCPAddr).Port) |
| 69 | +} |
| 70 | + |
| 71 | +func testController(t *testing.T, name string) Controller { |
| 72 | + gvr := schema.GroupVersionResource{ |
| 73 | + Group: "example.com", |
| 74 | + Version: "v1", |
| 75 | + Resource: "mytypes", |
| 76 | + } |
| 77 | + CtxQueue := queue.NewQueueOperationsCtx() |
| 78 | + registry := typed.NewRegistry() |
| 79 | + broadcaster := record.NewBroadcaster() |
| 80 | + |
| 81 | + return NewOwnedResourceController(klogr.New(), name, gvr, CtxQueue, registry, broadcaster, func(_ context.Context, gvr schema.GroupVersionResource, namespace, name string) { |
| 82 | + t.Log("processing", gvr, namespace, name) |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +func requireCancelFnCount(t *testing.T, m *Manager, count int) { |
| 87 | + t.Helper() |
| 88 | + require.Eventually(t, func() bool { |
| 89 | + m.RLock() |
| 90 | + defer m.RUnlock() |
| 91 | + return len(m.cancelFuncs) == count |
| 92 | + }, 100*time.Second, 10*time.Millisecond) |
| 93 | +} |
0 commit comments