Enhance testing and configuration for Tai service

- Add TAI_TEST_HOST_IP environment variable to CI workflows for unit tests, allowing better connectivity to the gRPC server from Docker containers.
- Update the `run.go` file to parse command-line arguments correctly.
- Modify the test utility to return the gRPC address reachable from Docker, improving integration test reliability.
- Refactor integration tests to utilize the new relay address function, ensuring proper communication with the Yao gRPC server.

These changes improve the testing framework and enhance the configuration for better service interaction during CI runs.
This commit is contained in:
Max 2026-03-04 17:13:06 +08:00
parent c6e1c449e1
commit df63584a6f
5 changed files with 31 additions and 7 deletions

View file

@ -1757,6 +1757,7 @@ jobs:
TAI_TEST_K8S_HOST: "127.0.0.1" TAI_TEST_K8S_HOST: "127.0.0.1"
TAI_TEST_K8S_PORT: "6443" TAI_TEST_K8S_PORT: "6443"
TAI_TEST_KUBECONFIG: "${{ runner.temp }}/kubeconfig-tai.yml" TAI_TEST_KUBECONFIG: "${{ runner.temp }}/kubeconfig-tai.yml"
TAI_TEST_HOST_IP: "172.17.0.1"
run: make unit-test-tai run: make unit-test-tai
- name: Codecov Report - name: Codecov Report

View file

@ -1311,6 +1311,7 @@ jobs:
TAI_TEST_K8S_HOST: "127.0.0.1" TAI_TEST_K8S_HOST: "127.0.0.1"
TAI_TEST_K8S_PORT: "6443" TAI_TEST_K8S_PORT: "6443"
TAI_TEST_KUBECONFIG: "${{ runner.temp }}/kubeconfig-tai.yml" TAI_TEST_KUBECONFIG: "${{ runner.temp }}/kubeconfig-tai.yml"
TAI_TEST_HOST_IP: "172.17.0.1"
run: make unit-test-tai run: make unit-test-tai
- name: Codecov Report - name: Codecov Report

View file

@ -85,7 +85,7 @@ func runGRPC(cred *Credential, args []string) {
color.Green(L("Run: %s gRPC: %s\n"), name, cred.GRPCAddr) color.Green(L("Run: %s gRPC: %s\n"), name, cred.GRPCAddr)
} }
pargs := parseRunArgs(args[1:]) pargs := parseRunArgs(args)
argsJSON, err := jsoniter.Marshal(pargs) argsJSON, err := jsoniter.Marshal(pargs)
if err != nil { if err != nil {

View file

@ -2,6 +2,8 @@ package testutils
import ( import (
"context" "context"
"net"
"os"
"strings" "strings"
"testing" "testing"
@ -43,7 +45,7 @@ func Prepare(t *testing.T) *grpc.ClientConn {
cfg := config.Conf cfg := config.Conf
cfg.GRPC.Port = 0 cfg.GRPC.Port = 0
cfg.GRPC.Host = "127.0.0.1" cfg.GRPC.Host = "0.0.0.0"
cfg.GRPC.Enabled = "" cfg.GRPC.Enabled = ""
test.Prepare(t, config.Conf) test.Prepare(t, config.Conf)
@ -129,6 +131,26 @@ func Addr() string {
return addrs[0] return addrs[0]
} }
// RelayAddr returns the gRPC address reachable from a Docker container.
// When TAI_TEST_HOST_IP is set (e.g. to the docker bridge gateway),
// it replaces the host portion so that the Tai container can reach the
// Yao gRPC server running on the CI host.
func RelayAddr() string {
addr := Addr()
if addr == "" {
return ""
}
hostIP := os.Getenv("TAI_TEST_HOST_IP")
if hostIP == "" {
return addr
}
_, port, err := net.SplitHostPort(addr)
if err != nil {
return addr
}
return hostIP + ":" + port
}
// ObtainAccessToken mints a token with the given scopes via oauth.MakeAccessToken. // ObtainAccessToken mints a token with the given scopes via oauth.MakeAccessToken.
func ObtainAccessToken(t *testing.T, scopes ...string) string { func ObtainAccessToken(t *testing.T, scopes ...string) string {
t.Helper() t.Helper()

View file

@ -280,11 +280,11 @@ func setupRelayClient(t *testing.T, scopes ...string) *yaogrpc.Client {
testutils.Clean() testutils.Clean()
}) })
yaoAddr := testutils.Addr() yaoAddr := testutils.RelayAddr()
token := testutils.ObtainAccessToken(t, scopes...) token := testutils.ObtainAccessToken(t, scopes...)
refreshToken := testutils.ObtainRefreshToken(t, scopes...) refreshToken := testutils.ObtainRefreshToken(t, scopes...)
// upstream = Yao gRPC address; taiMode = true // upstream = Yao gRPC address reachable from the Tai container
tm := yaogrpc.NewTokenManager(token, refreshToken, "relay-sandbox", yaoAddr) tm := yaogrpc.NewTokenManager(token, refreshToken, "relay-sandbox", yaoAddr)
client, err := yaogrpc.Dial(taiAddr, tm) client, err := yaogrpc.Dial(taiAddr, tm)
require.NoError(t, err) require.NoError(t, err)
@ -305,7 +305,7 @@ func TestRelay_Healthz(t *testing.T) {
testutils.Clean() testutils.Clean()
}() }()
yaoAddr := testutils.Addr() yaoAddr := testutils.RelayAddr()
tm := yaogrpc.NewTokenManager("", "", "", yaoAddr) tm := yaogrpc.NewTokenManager("", "", "", yaoAddr)
client, err := yaogrpc.Dial(taiAddr, tm) client, err := yaogrpc.Dial(taiAddr, tm)
require.NoError(t, err) require.NoError(t, err)
@ -375,7 +375,7 @@ func TestRelay_Run_NoToken(t *testing.T) {
testutils.Clean() testutils.Clean()
}() }()
yaoAddr := testutils.Addr() yaoAddr := testutils.RelayAddr()
tm := yaogrpc.NewTokenManager("", "", "", yaoAddr) tm := yaogrpc.NewTokenManager("", "", "", yaoAddr)
client, err := yaogrpc.Dial(taiAddr, tm) client, err := yaogrpc.Dial(taiAddr, tm)
require.NoError(t, err) require.NoError(t, err)
@ -398,7 +398,7 @@ func TestRelay_TokenRefresh(t *testing.T) {
testutils.Clean() testutils.Clean()
}() }()
yaoAddr := testutils.Addr() yaoAddr := testutils.RelayAddr()
scopes := []string{"grpc:run"} scopes := []string{"grpc:run"}
expiredToken := testutils.ObtainExpiredAccessToken(t, scopes...) expiredToken := testutils.ObtainExpiredAccessToken(t, scopes...)