Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions test/integration/bbr/harness_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2025 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bbr

import (
"context"
"fmt"
"strings"
"testing"

extProcPb "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

runserver "sigs.k8s.io/gateway-api-inference-extension/pkg/bbr/server"
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
"sigs.k8s.io/gateway-api-inference-extension/test/integration"
)

var logger = logutil.NewTestLogger().V(logutil.VERBOSE)

// BBRHarness encapsulates the environment for a single isolated BBR test run.
type BBRHarness struct {
t *testing.T
ctx context.Context
Client extProcPb.ExternalProcessor_ProcessClient

// Internal handles for cleanup
server *runserver.ExtProcServerRunner
grpcConn *grpc.ClientConn
}

// NewBBRHarness boots up an isolated BBR server on a random port.
// streaming: determines if the BBR server runs in streaming mode or unary/buffered mode.
func NewBBRHarness(t *testing.T, ctx context.Context, streaming bool) *BBRHarness {
t.Helper()

// 1. Allocate Free Port
tcpAddr, err := integration.GetFreePort()
require.NoError(t, err, "failed to acquire free port for BBR server")
port := tcpAddr.Port

// 2. Configure BBR Server
// BBR is simpler than EPP; it doesn't need a K8s Manager.
runner := runserver.NewDefaultExtProcServerRunner(port, false)
runner.SecureServing = false
runner.Streaming = streaming

// 3. Start Server in Background
serverCtx, serverCancel := context.WithCancel(ctx)
go func() {
logger.Info("Starting BBR server", "port", port, "streaming", streaming)
if err := runner.AsRunnable(logger.WithName("bbr-server")).Start(serverCtx); err != nil {
// Context cancellation is expected during teardown.
if !strings.Contains(err.Error(), "context canceled") {
logger.Error(err, "BBR server stopped unexpectedly")
}
}
}()

// 4. Connect Client
// Blocking dial ensures the server is reachable before the test logic begins.
addr := fmt.Sprintf("127.0.0.1:%d", port)
conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err, "failed to create grpc connection to BBR server")

extProcClient, err := extProcPb.NewExternalProcessorClient(conn).Process(ctx)
require.NoError(t, err, "failed to initialize ext_proc stream client")

h := &BBRHarness{
t: t,
ctx: ctx,
Client: extProcClient,
server: runner,
grpcConn: conn,
}

// 5. Register Cleanup
t.Cleanup(func() {
logger.Info("Tearing down BBR server", "port", port)
serverCancel()
if err := h.grpcConn.Close(); err != nil {
t.Logf("Warning: failed to close grpc connection: %v", err)
}
})

return h
}
Loading