1
0
Fork 0
mirror of https://github.com/chrislusf/seaweedfs synced 2025-07-24 20:42:47 +02:00
seaweedfs/weed/worker/client_test.go
Chris Lu aa66852304
Admin UI add maintenance menu (#6944)
* add ui for maintenance

* valid config loading. fix workers page.

* refactor

* grpc between admin and workers

* add a long-running bidirectional grpc call between admin and worker
* use the grpc call to heartbeat
* use the grpc call to communicate
* worker can remove the http client
* admin uses http port + 10000 as its default grpc port

* one task one package

* handles connection failures gracefully with exponential backoff

* grpc with insecure tls

* grpc with optional tls

* fix detecting tls

* change time config from nano seconds to seconds

* add tasks with 3 interfaces

* compiles reducing hard coded

* remove a couple of tasks

* remove hard coded references

* reduce hard coded values

* remove hard coded values

* remove hard coded from templ

* refactor maintenance package

* fix import cycle

* simplify

* simplify

* auto register

* auto register factory

* auto register task types

* self register types

* refactor

* simplify

* remove one task

* register ui

* lazy init executor factories

* use registered task types

* DefaultWorkerConfig remove hard coded task types

* remove more hard coded

* implement get maintenance task

* dynamic task configuration

* "System Settings" should only have system level settings

* adjust menu for tasks

* ensure menu not collapsed

* render job configuration well

* use templ for ui of task configuration

* fix ordering

* fix bugs

* saving duration in seconds

* use value and unit for duration

* Delete WORKER_REFACTORING_PLAN.md

* Delete maintenance.json

* Delete custom_worker_example.go

* remove address from workers

* remove old code from ec task

* remove creating collection button

* reconnect with exponential backoff

* worker use security.toml

* start admin server with tls info from security.toml

* fix "weed admin" cli description
2025-07-06 13:57:02 -07:00

111 lines
3.4 KiB
Go

package worker
import (
"context"
"testing"
"github.com/seaweedfs/seaweedfs/weed/pb"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func TestGrpcConnection(t *testing.T) {
// Test that we can create a gRPC connection with insecure credentials
// This tests the connection setup without requiring a running server
adminAddress := "localhost:33646" // gRPC port for admin server on port 23646
// This should not fail with transport security errors
conn, err := pb.GrpcDial(context.Background(), adminAddress, false, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
// Connection failure is expected when no server is running
// But it should NOT be a transport security error
if err.Error() == "grpc: no transport security set" {
t.Fatalf("Transport security error should not occur with insecure credentials: %v", err)
}
t.Logf("Connection failed as expected (no server running): %v", err)
} else {
// If connection succeeds, clean up
conn.Close()
t.Log("Connection succeeded")
}
}
func TestGrpcAdminClient_Connect(t *testing.T) {
// Test that the GrpcAdminClient can be created and attempt connection
dialOption := grpc.WithTransportCredentials(insecure.NewCredentials())
client := NewGrpcAdminClient("localhost:23646", "test-worker", dialOption)
// This should not fail with transport security errors
err := client.Connect()
if err != nil {
// Connection failure is expected when no server is running
// But it should NOT be a transport security error
if err.Error() == "grpc: no transport security set" {
t.Fatalf("Transport security error should not occur with insecure credentials: %v", err)
}
t.Logf("Connection failed as expected (no server running): %v", err)
} else {
// If connection succeeds, clean up
client.Disconnect()
t.Log("Connection succeeded")
}
}
func TestAdminAddressToGrpcAddress(t *testing.T) {
tests := []struct {
adminAddress string
expected string
}{
{"localhost:9333", "localhost:19333"},
{"localhost:23646", "localhost:33646"},
{"admin.example.com:9333", "admin.example.com:19333"},
{"127.0.0.1:8080", "127.0.0.1:18080"},
}
for _, test := range tests {
dialOption := grpc.WithTransportCredentials(insecure.NewCredentials())
client := NewGrpcAdminClient(test.adminAddress, "test-worker", dialOption)
result := client.adminAddress
if result != test.expected {
t.Errorf("For admin address %s, expected gRPC address %s, got %s",
test.adminAddress, test.expected, result)
}
}
}
func TestMockAdminClient(t *testing.T) {
// Test that the mock client works correctly
client := NewMockAdminClient()
// Should be able to connect/disconnect without errors
err := client.Connect()
if err != nil {
t.Fatalf("Mock client connect failed: %v", err)
}
if !client.IsConnected() {
t.Error("Mock client should be connected")
}
err = client.Disconnect()
if err != nil {
t.Fatalf("Mock client disconnect failed: %v", err)
}
if client.IsConnected() {
t.Error("Mock client should be disconnected")
}
}
func TestCreateAdminClient(t *testing.T) {
// Test client creation
dialOption := grpc.WithTransportCredentials(insecure.NewCredentials())
client, err := CreateAdminClient("localhost:9333", "test-worker", dialOption)
if err != nil {
t.Fatalf("Failed to create admin client: %v", err)
}
if client == nil {
t.Fatal("Client should not be nil")
}
}