Files
open-cluster-management/pkg/server/grpc/clients_test.go
Jian Qiu 8cb4f13e74
Some checks failed
Scorecard supply-chain security / Scorecard analysis (push) Failing after 42s
Post / coverage (push) Failing after 37s
Post / images (amd64, addon-manager) (push) Failing after 37s
Post / images (amd64, placement) (push) Failing after 35s
Post / images (amd64, registration) (push) Failing after 33s
Post / images (amd64, registration-operator) (push) Failing after 36s
Post / images (amd64, work) (push) Failing after 33s
Post / images (arm64, addon-manager) (push) Failing after 33s
Post / images (arm64, placement) (push) Failing after 34s
Post / images (arm64, registration) (push) Failing after 36s
Post / images (arm64, registration-operator) (push) Failing after 38s
Post / images (arm64, work) (push) Failing after 39s
Post / image manifest (addon-manager) (push) Has been skipped
Post / image manifest (placement) (push) Has been skipped
Post / image manifest (registration) (push) Has been skipped
Post / image manifest (registration-operator) (push) Has been skipped
Post / image manifest (work) (push) Has been skipped
Post / trigger clusteradm e2e (push) Has been skipped
Close stale issues and PRs / stale (push) Failing after 40s
Improve unit test coverage for low-coverage packages (#1188)
This commit enhances unit test coverage for packages with the lowest
test coverage, focusing on previously untested methods and edge cases.

Changes:
- pkg/server/grpc: Increased coverage from 31.6% to 81.6%
  - Added comprehensive tests for Clients.Run() method
  - Added tests for GRPCServerOptions.Run() method
  - Covered error handling, configuration validation, and context cancellation

- pkg/singleton/spoke: Enhanced test suite with additional edge cases
  - Added method signature validation tests
  - Added configuration setup and struct initialization tests
  - Fixed race condition issues in existing tests

- pkg/server/grpc coverage improvements:
  - Clients.Run(): 0% → 100% coverage
  - GRPCServerOptions.Run(): 0% → 88.2% coverage

The new tests cover normal operation, error conditions, edge cases,
and defensive programming scenarios, significantly improving overall
code quality and test reliability.

🤖 Generated with [Claude Code](https://claude.ai/code)

Signed-off-by: Jian Qiu <jqiu@redhat.com>
Co-authored-by: Claude <noreply@anthropic.com>
2025-09-19 06:22:38 +00:00

141 lines
3.5 KiB
Go

package grpc
import (
"context"
"testing"
"time"
"github.com/openshift/library-go/pkg/controller/controllercmd"
"k8s.io/client-go/rest"
)
func TestNewClientsInitializesFields(t *testing.T) {
t.Parallel()
controllerContext := &controllercmd.ControllerContext{
KubeConfig: &rest.Config{Host: "https://example.com"},
}
clients, err := NewClients(controllerContext)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if clients.KubeClient == nil || clients.ClusterClient == nil || clients.WorkClient == nil || clients.AddOnClient == nil {
t.Fatal("expected all clientsets to be initialized")
}
if clients.KubeInformers == nil || clients.ClusterInformers == nil || clients.WorkInformers == nil || clients.AddOnInformers == nil {
t.Fatal("expected all informer factories to be initialized")
}
}
func TestNewClients_ConfigValidation(t *testing.T) {
tests := []struct {
name string
host string
expectError bool
expectClients bool
}{
{
name: "valid host",
host: "https://example.com",
expectError: false,
expectClients: true,
},
{
name: "malformed host",
host: "://",
expectError: true,
expectClients: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
controllerContext := &controllercmd.ControllerContext{
KubeConfig: &rest.Config{
Host: tt.host,
},
}
clients, err := NewClients(controllerContext)
if tt.expectError {
if err == nil {
t.Error("Expected error but got none")
}
if clients != nil {
t.Error("Expected clients to be nil when error occurs")
}
} else {
if err != nil {
t.Errorf("Expected no error but got: %v", err)
}
if clients == nil {
t.Error("Expected clients to be non-nil when no error occurs")
}
}
})
}
}
func TestClientsRunMethodSignature(t *testing.T) {
t.Parallel()
// Compile-time assertion: (*Clients).Run must be func(*Clients, context.Context)
var _ func(*Clients, context.Context) = (*Clients).Run
}
func TestClientsRun(t *testing.T) {
t.Parallel()
controllerContext := &controllercmd.ControllerContext{
KubeConfig: &rest.Config{Host: "https://example.com"},
}
clients, err := NewClients(controllerContext)
if err != nil {
t.Fatalf("expected no error creating clients, got %v", err)
}
// Create a context that we can cancel to test that Run respects context cancellation
ctx, cancel := context.WithCancel(context.Background())
// Start the clients in a goroutine
done := make(chan struct{})
go func() {
defer close(done)
clients.Run(ctx)
}()
// Cancel the context to stop the informers
cancel()
// Verify that Run exits when context is canceled
timeoutCtx, timeoutCancel := context.WithTimeout(context.Background(), 1*time.Second)
defer timeoutCancel()
select {
case <-done:
// Run method completed as expected
case <-timeoutCtx.Done():
t.Error("Run method did not exit within timeout after context cancellation")
}
}
func TestClientsRunWithNilClients(t *testing.T) {
// Test that Run panics when called on clients struct with nil informers
// This demonstrates the expected behavior when informers are not properly initialized
clients := &Clients{}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// We expect this to panic due to nil informers
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic when informers are nil, but got none")
}
}()
clients.Run(ctx)
}