Skip to content
Merged
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
46 changes: 46 additions & 0 deletions exec_service/cmd/exec_server/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2026 Google LLC
#
# 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.

load("@rules_go//go:def.bzl", "go_binary", "go_library", "go_test")

go_library(
name = "exec_server_lib",
srcs = ["main.go"],
importpath = "github.com/google/agent-shell-tools/exec_service/cmd/exec_server",
deps = [
"//exec_service:exec_service_go_proto",
"//exec_service/server",
"@org_golang_google_grpc//:grpc",
],
)

go_binary(
name = "exec_server",
embed = [":exec_server_lib"],
visibility = ["//visibility:public"],
)

go_test(
name = "exec_server_test",
srcs = ["exec_server_test.go"],
data = [":exec_server"],
env = {"EXEC_SERVER_BIN": "$(rlocationpath :exec_server)"},
tags = ["local"],
deps = [
"//exec_service:exec_service_go_proto",
"@org_golang_google_grpc//:grpc",
"@org_golang_google_grpc//credentials/insecure",
"@rules_go//go/runfiles",
],
)
188 changes: 188 additions & 0 deletions exec_service/cmd/exec_server/exec_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// Copyright 2026 Google LLC
//
// 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 main_test

import (
"context"
"io"
"net"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"testing"
"time"

"github.com/bazelbuild/rules_go/go/runfiles"
pb "github.com/google/agent-shell-tools/exec_service/execservicepb"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func binPath(t *testing.T) string {
t.Helper()
rloc := os.Getenv("EXEC_SERVER_BIN")
if rloc == "" {
t.Fatal("EXEC_SERVER_BIN not set")
}
r, err := runfiles.New()
if err != nil {
t.Fatalf("runfiles: %v", err)
}
p, err := r.Rlocation(rloc)
if err != nil {
t.Fatalf("rlocation(%q): %v", rloc, err)
}
return p
}

// startServer launches the exec_server binary on a temporary Unix socket and
// returns a connected gRPC client. The server process is killed on cleanup.
func startServer(t *testing.T) pb.ExecServiceClient {
t.Helper()
bin := binPath(t)
sock := filepath.Join(t.TempDir(), "exec.sock")

cmd := exec.Command(bin, "-addr", sock)
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
t.Fatalf("start server: %v", err)
}
t.Cleanup(func() {
cmd.Process.Signal(syscall.SIGTERM)
cmd.Wait()
})

// Wait for the socket to appear.
deadline := time.Now().Add(5 * time.Second)
for time.Now().Before(deadline) {
if _, err := os.Stat(sock); err == nil {
break
}
time.Sleep(20 * time.Millisecond)
}

conn, err := grpc.NewClient("unix://"+sock,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
t.Fatalf("dial: %v", err)
}
t.Cleanup(func() { conn.Close() })

// Verify connectivity before returning.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn.Connect()
for conn.GetState() != 2 /* READY */ {
if !conn.WaitForStateChange(ctx, conn.GetState()) {
t.Fatal("server did not become ready")
}
}

return pb.NewExecServiceClient(conn)
}

func collect(t *testing.T, stream pb.ExecService_RunCommandClient) (output string, exitCode int32, errMsg string) {
t.Helper()
for {
ev, err := stream.Recv()
if err == io.EOF {
return
}
if err != nil {
t.Fatalf("Recv: %v", err)
}
switch e := ev.Event.(type) {
case *pb.ServerEvent_Output:
output += string(e.Output)
case *pb.ServerEvent_Exited:
exitCode = e.Exited.GetExitCode()
errMsg = e.Exited.GetErrorMessage()
}
}
}

func TestRunCommand(t *testing.T) {
client := startServer(t)
stream, err := client.RunCommand(context.Background(), &pb.StartCommandRequest{
CommandLine: "echo hello",
})
if err != nil {
t.Fatal(err)
}
output, exitCode, _ := collect(t, stream)
if exitCode != 0 {
t.Errorf("exit code = %d, want 0", exitCode)
}
if got := strings.TrimSpace(output); got != "hello" {
t.Errorf("output = %q, want %q", got, "hello")
}
}

func TestMissingAddrFlag(t *testing.T) {
bin := binPath(t)
cmd := exec.Command(bin)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatal("expected non-zero exit when -addr is omitted")
}
if !strings.Contains(string(out), "-addr is required") {
t.Errorf("output = %q, want it to contain %q", out, "-addr is required")
}
}

func TestSIGTERMShutdown(t *testing.T) {
bin := binPath(t)
sock := filepath.Join(t.TempDir(), "exec.sock")

cmd := exec.Command(bin, "-addr", sock)
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
t.Fatalf("start server: %v", err)
}

// Wait for the socket to appear.
deadline := time.Now().Add(5 * time.Second)
for time.Now().Before(deadline) {
if _, err := os.Stat(sock); err == nil {
break
}
time.Sleep(20 * time.Millisecond)
}

cmd.Process.Signal(syscall.SIGTERM)

done := make(chan error, 1)
go func() { done <- cmd.Wait() }()

select {
case err := <-done:
if err != nil {
t.Logf("server exited with: %v (expected for signal exit)", err)
}
case <-time.After(5 * time.Second):
cmd.Process.Kill()
t.Fatal("server did not exit within 5s after SIGTERM")
}

// Socket should no longer accept connections.
if _, err := net.Dial("unix", sock); err == nil {
t.Error("socket still accepting connections after shutdown")
}
}
62 changes: 62 additions & 0 deletions exec_service/cmd/exec_server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2026 Google LLC
//
// 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.

// Binary exec_server runs the ExecService gRPC server.
package main

import (
"flag"
"log"
"net"
"os"
"os/signal"
"syscall"

pb "github.com/google/agent-shell-tools/exec_service/execservicepb"
"github.com/google/agent-shell-tools/exec_service/server"
"google.golang.org/grpc"
)

func main() {
addr := flag.String("addr", "", "Unix socket path to listen on (required)")
flag.Parse()

if *addr == "" {
log.Fatal("-addr is required")
}

lis, err := net.Listen("unix", *addr)
if err != nil {
log.Fatalf("Failed to listen on %s: %v", *addr, err)
}

srv := grpc.NewServer()
pb.RegisterExecServiceServer(srv, &server.ExecServer{})

// GracefulStop waits for active RPCs to finish. If a RunCommand stream
// is executing a long-lived child process, the server will block until
// that command exits (or an external SIGKILL arrives).
go func() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
log.Println("Shutting down...")
srv.GracefulStop()
}()

log.Printf("ExecService listening on unix %s", *addr)
if err := srv.Serve(lis); err != nil {
log.Fatalf("Server exited with error: %v", err)
}
}
Loading