From 5cdbb62751bc71f253f3bc753fbebeb9ded0239f Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 31 May 2026 08:32:59 +0200 Subject: [PATCH] Increase test coverage (#6677) Co-authored-by: Claude --- agent/rpc/auth_interceptor_test.go | 54 +++++++++++++++++++++++++++++ agent/rpc/client_grpc_test.go | 54 +++++++++++++++++++++++++++++ agent/rpc/dial_test.go | 55 ++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 agent/rpc/auth_interceptor_test.go create mode 100644 agent/rpc/client_grpc_test.go create mode 100644 agent/rpc/dial_test.go diff --git a/agent/rpc/auth_interceptor_test.go b/agent/rpc/auth_interceptor_test.go new file mode 100644 index 000000000..232b9016c --- /dev/null +++ b/agent/rpc/auth_interceptor_test.go @@ -0,0 +1,54 @@ +// Copyright 2026 Woodpecker 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 rpc + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "google.golang.org/grpc/metadata" +) + +func TestAuthInterceptorAttachToken(t *testing.T) { + tc := []struct { + name string + token string + }{ + {"populated token", "secret-token"}, + {"empty token", ""}, + } + + for _, c := range tc { + t.Run(c.name, func(t *testing.T) { + interceptor := &AuthInterceptor{accessToken: c.token} + ctx := interceptor.attachToken(context.Background()) + + md, ok := metadata.FromOutgoingContext(ctx) + assert.True(t, ok) + assert.Equal(t, []string{c.token}, md.Get("token")) + }) + } + + t.Run("preserves existing metadata", func(t *testing.T) { + base := metadata.AppendToOutgoingContext(context.Background(), "extra", "v") + interceptor := &AuthInterceptor{accessToken: "tok"} + ctx := interceptor.attachToken(base) + + md, _ := metadata.FromOutgoingContext(ctx) + assert.Equal(t, []string{"tok"}, md.Get("token")) + assert.Equal(t, []string{"v"}, md.Get("extra")) + }) +} diff --git a/agent/rpc/client_grpc_test.go b/agent/rpc/client_grpc_test.go new file mode 100644 index 000000000..1136cefdd --- /dev/null +++ b/agent/rpc/client_grpc_test.go @@ -0,0 +1,54 @@ +// Copyright 2026 Woodpecker 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 rpc + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestSetConnectionRetryTimeout(t *testing.T) { + tc := []struct { + name string + timeout time.Duration + }{ + {"finite", 5 * time.Minute}, + {"zero means infinite", 0}, + } + + for _, c := range tc { + t.Run(c.name, func(t *testing.T) { + cl := &client{} + SetConnectionRetryTimeout(c.timeout)(cl) + assert.Equal(t, c.timeout, cl.connectionRetryTimeout) + }) + } +} + +func TestIsConnected(t *testing.T) { + cl := &client{conn: newTestConn(t)} + defer cl.conn.Close() + + t.Run("idle connection reports connected", func(t *testing.T) { + assert.True(t, cl.IsConnected()) + }) + + t.Run("closed connection reports not connected", func(t *testing.T) { + assert.NoError(t, cl.conn.Close()) + assert.False(t, cl.IsConnected()) + }) +} diff --git a/agent/rpc/dial_test.go b/agent/rpc/dial_test.go new file mode 100644 index 000000000..12d6646ce --- /dev/null +++ b/agent/rpc/dial_test.go @@ -0,0 +1,55 @@ +// Copyright 2026 Woodpecker 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 rpc + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" +) + +func newTestConn(t *testing.T) *grpc.ClientConn { + t.Helper() + conn, err := grpc.NewClient("localhost:0", grpc.WithTransportCredentials(insecure.NewCredentials())) + assert.NoError(t, err) + return conn +} + +func TestAgentConnClose(t *testing.T) { + t.Run("both nil is safe", func(t *testing.T) { + c := &AgentConn{} + assert.NotPanics(t, c.Close) + }) + + t.Run("only auth set", func(t *testing.T) { + c := &AgentConn{AuthConn: newTestConn(t)} + assert.NotPanics(t, c.Close) + }) + + t.Run("only main set", func(t *testing.T) { + c := &AgentConn{MainConn: newTestConn(t)} + assert.NotPanics(t, c.Close) + }) + + t.Run("both set closes both", func(t *testing.T) { + c := &AgentConn{ + AuthConn: newTestConn(t), + MainConn: newTestConn(t), + } + assert.NotPanics(t, c.Close) + }) +}