You've already forked woodpecker
mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2026-06-03 16:35:37 +02:00
Increase test coverage (#6677)
Co-authored-by: Claude <claude@anthropic.com>
This commit is contained in:
@@ -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"))
|
||||
})
|
||||
}
|
||||
@@ -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())
|
||||
})
|
||||
}
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user