A failed state-label change was dropped at three points: the forge.Forge interface returned nothing, forgeext logged the extension's gRPC error instead of returning it, and the forgerpc client and server discarded the response error. SetStateLabel now returns an error on the interface and on NoopForge, forgeext propagates the extension's error, and the forgerpc server carries it in the response envelope while the client returns it, matching the other forgerpc handlers. The proto is unchanged: a gRPC error rides the status and SetStateLabelResponse is already an empty message. TestSetStateLabelFailureReachesTheCaller pins the full chain: an extension whose SetStateLabel RPC fails, through forgeext and the forgerpc client/server, to the caller receiving the error. Refs #280
130 lines
4.1 KiB
Go
130 lines
4.1 KiB
Go
package forgeext
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"net"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
goplugin "github.com/hashicorp/go-plugin"
|
|
natsserver "github.com/nats-io/nats-server/v2/server"
|
|
"github.com/nats-io/nats.go"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
"google.golang.org/grpc/status"
|
|
|
|
forgev1 "github.com/samcharles93/archie-core/internal/contracts/forge/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
"github.com/samcharles93/archie-core/internal/forge"
|
|
"github.com/samcharles93/archie-core/internal/forgerpc"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/extension"
|
|
)
|
|
|
|
// failingLabelExtension is a forge extension whose state-label change always
|
|
// fails, so the test can watch the failure travel to the host.
|
|
type failingLabelExtension struct {
|
|
forgev1.UnimplementedForgeServiceServer
|
|
}
|
|
|
|
func (failingLabelExtension) Configure(context.Context, *forgev1.ConfigureRequest) (*forgev1.ConfigureResponse, error) {
|
|
return &forgev1.ConfigureResponse{}, nil
|
|
}
|
|
|
|
func (failingLabelExtension) SetStateLabel(context.Context, *forgev1.SetStateLabelRequest) (*forgev1.SetStateLabelResponse, error) {
|
|
return nil, status.Error(codes.Internal, "label write failed")
|
|
}
|
|
|
|
// stubHost stands in for the process host: Start hands back an
|
|
// already-connected extension client instead of launching one.
|
|
type stubHost struct {
|
|
client forgev1.ForgeServiceClient
|
|
}
|
|
|
|
func (h stubHost) Start(context.Context, extension.Spec, string, goplugin.Plugin) (any, error) {
|
|
return h.client, nil
|
|
}
|
|
|
|
func (stubHost) Stop(string) {}
|
|
func (stubHost) Alive(string) bool { return true }
|
|
|
|
type runsByToken map[string]*workflow.Task
|
|
|
|
func (r runsByToken) TaskForCredential(_ context.Context, token string) (*workflow.Task, error) {
|
|
if task, ok := r[token]; ok {
|
|
return task, nil
|
|
}
|
|
return nil, errors.New("unknown")
|
|
}
|
|
|
|
// TestSetStateLabelFailureReachesTheCaller pins the whole chain: an
|
|
// extension whose SetStateLabel RPC fails, through the forgeext adapter and
|
|
// the forgerpc server, to the forgerpc client's caller receiving the error.
|
|
func TestSetStateLabelFailureReachesTheCaller(t *testing.T) {
|
|
ctx := t.Context()
|
|
|
|
// The extension: a gRPC server whose state-label change fails.
|
|
lc := net.ListenConfig{}
|
|
ln, err := lc.Listen(ctx, "tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gs := grpc.NewServer()
|
|
forgev1.RegisterForgeServiceServer(gs, failingLabelExtension{})
|
|
go func() { _ = gs.Serve(ln) }()
|
|
t.Cleanup(gs.Stop)
|
|
conn, err := grpc.NewClient(ln.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = conn.Close() })
|
|
|
|
f := &Forge{
|
|
host: stubHost{client: forgev1.NewForgeServiceClient(conn)},
|
|
inst: Instance{Spec: extension.Spec{Name: "stub"}},
|
|
log: slog.New(slog.DiscardHandler),
|
|
}
|
|
|
|
// The daemon: forgerpc served over an embedded broker, resolving runs to
|
|
// the extension above.
|
|
ns, err := natsserver.NewServer(&natsserver.Options{Host: "127.0.0.1", Port: -1, NoLog: true, NoSigs: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ns.Start()
|
|
if !ns.ReadyForConnections(10 * time.Second) {
|
|
t.Fatal("embedded nats server did not become ready")
|
|
}
|
|
t.Cleanup(ns.Shutdown)
|
|
nc, err := nats.Connect(ns.ClientURL())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(nc.Close)
|
|
|
|
fs := &forgerpc.Server{
|
|
Runs: runsByToken{"live": {Identity: "bot", Owner: "acme", Repo: "app"}},
|
|
Resolve: func(context.Context, *workflow.Task) (forge.Forge, error) {
|
|
return f, nil
|
|
},
|
|
Log: slog.New(slog.DiscardHandler),
|
|
}
|
|
unsubscribe, err := fs.RegisterFor(nc, "bot")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(unsubscribe)
|
|
|
|
client := &forgerpc.Client{Conn: nc, Identity: "bot", Credential: "live", Timeout: 5 * time.Second}
|
|
err = client.SetStateLabel(ctx, "acme", "app", 7, "in-progress", []string{"in-progress", "blocked"})
|
|
if err == nil {
|
|
t.Fatal("SetStateLabel succeeded; the extension's failure must reach the caller")
|
|
}
|
|
if !strings.Contains(err.Error(), "label write failed") {
|
|
t.Fatalf("SetStateLabel error = %q, want the extension's failure", err)
|
|
}
|
|
}
|