layer1: add router namespace subscriber step 22
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/fission/fission/pkg/utils"
|
||||
"github.com/fission/fission/pkg/utils/manager"
|
||||
)
|
||||
|
||||
type routerNamespaceAdder interface {
|
||||
AddNamespace(ctx context.Context, ns string, mgr manager.Interface) error
|
||||
}
|
||||
|
||||
func NewNamespaceSubscriber(ts routerNamespaceAdder, mgr manager.Interface) utils.NamespaceSubscriber {
|
||||
return utils.NamespaceSubscriberFuncs{
|
||||
SubscriberName: "router",
|
||||
AddFunc: func(ctx context.Context, record utils.NamespaceRecord) error {
|
||||
return registerRouterNamespace(ctx, record.Name, ts, mgr)
|
||||
},
|
||||
ResyncFunc: func(ctx context.Context, record utils.NamespaceRecord) error {
|
||||
return registerRouterNamespace(ctx, record.Name, ts, mgr)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func registerRouterNamespace(ctx context.Context, namespace string, ts routerNamespaceAdder, mgr manager.Interface) error {
|
||||
if namespace == "" || ts == nil {
|
||||
return nil
|
||||
}
|
||||
return ts.AddNamespace(ctx, namespace, mgr)
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/fission/fission/pkg/utils"
|
||||
"github.com/fission/fission/pkg/utils/manager"
|
||||
)
|
||||
|
||||
type fakeRouterNamespaceAdder struct {
|
||||
lastNamespace string
|
||||
calls int
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeRouterNamespaceAdder) AddNamespace(ctx context.Context, ns string, mgr manager.Interface) error {
|
||||
f.lastNamespace = ns
|
||||
f.calls++
|
||||
return f.err
|
||||
}
|
||||
|
||||
func TestNewNamespaceSubscriberAddAndResync(t *testing.T) {
|
||||
adder := &fakeRouterNamespaceAdder{}
|
||||
subscriber := NewNamespaceSubscriber(adder, nil)
|
||||
record := utils.NamespaceRecord{Name: "tenant-router-a"}
|
||||
|
||||
if subscriber.Name() != "router" {
|
||||
t.Fatalf("expected router subscriber name")
|
||||
}
|
||||
if err := subscriber.OnNamespaceAdd(context.Background(), record); err != nil {
|
||||
t.Fatalf("expected add to succeed: %v", err)
|
||||
}
|
||||
if err := subscriber.OnNamespaceResync(context.Background(), record); err != nil {
|
||||
t.Fatalf("expected resync to succeed: %v", err)
|
||||
}
|
||||
if adder.calls != 2 {
|
||||
t.Fatalf("expected adder to be called for add and resync")
|
||||
}
|
||||
if adder.lastNamespace != "tenant-router-a" {
|
||||
t.Fatalf("expected namespace to be forwarded")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterRouterNamespacePropagatesError(t *testing.T) {
|
||||
adder := &fakeRouterNamespaceAdder{err: errors.New("add failed")}
|
||||
err := registerRouterNamespace(context.Background(), "tenant-router-a", adder, nil)
|
||||
if err == nil || err.Error() != "add failed" {
|
||||
t.Fatalf("expected router add error to be propagated, got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user