aboutsummaryrefslogtreecommitdiff
path: root/weed/worker/client_test.go
blob: c57ea0240f53719f609a90c31897beea0e007b63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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")
	}
}