|
| 1 | +package nginx |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/nginxinc/nginx-plus-go-client/client" |
| 7 | +) |
| 8 | + |
| 9 | +// Helper functions to create pointers |
| 10 | +func ptrInt(i int) *int { return &i } |
| 11 | +func ptrBool(b bool) *bool { return &b } |
| 12 | + |
| 13 | +func TestFormatUpdateServersInPlusLog(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + input []client.UpstreamServer |
| 17 | + expected string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "Empty input", |
| 21 | + input: []client.UpstreamServer{}, |
| 22 | + expected: "[]", |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "Single server with all fields set", |
| 26 | + input: []client.UpstreamServer{ |
| 27 | + { |
| 28 | + MaxConns: ptrInt(100), |
| 29 | + MaxFails: ptrInt(3), |
| 30 | + Backup: ptrBool(true), |
| 31 | + Down: ptrBool(false), |
| 32 | + Weight: ptrInt(10), |
| 33 | + Server: "192.168.1.1:8080", |
| 34 | + FailTimeout: "30s", |
| 35 | + SlowStart: "10s", |
| 36 | + Route: "route1", |
| 37 | + Service: "serviceA", |
| 38 | + ID: 0, |
| 39 | + Drain: true, |
| 40 | + }, |
| 41 | + }, |
| 42 | + expected: "[{MaxConns:100 MaxFails:3 Backup:true Down:false Weight:10 Server:192.168.1.1:8080 FailTimeout:30s SlowStart:10s Route:route1 Service:serviceA ID:0 Drain:true}]", |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "Multiple servers", |
| 46 | + input: []client.UpstreamServer{ |
| 47 | + { |
| 48 | + MaxConns: ptrInt(50), |
| 49 | + MaxFails: ptrInt(2), |
| 50 | + Backup: ptrBool(false), |
| 51 | + Down: ptrBool(true), |
| 52 | + Weight: ptrInt(5), |
| 53 | + Server: "192.168.1.2:8080", |
| 54 | + FailTimeout: "20s", |
| 55 | + SlowStart: "5s", |
| 56 | + Route: "route2", |
| 57 | + Service: "serviceB", |
| 58 | + ID: 1, |
| 59 | + Drain: false, |
| 60 | + }, |
| 61 | + { |
| 62 | + MaxConns: ptrInt(150), |
| 63 | + MaxFails: ptrInt(5), |
| 64 | + Backup: ptrBool(true), |
| 65 | + Down: ptrBool(false), |
| 66 | + Weight: ptrInt(15), |
| 67 | + Server: "192.168.1.3:8080", |
| 68 | + FailTimeout: "40s", |
| 69 | + SlowStart: "20s", |
| 70 | + Route: "route3", |
| 71 | + Service: "serviceC", |
| 72 | + ID: 2, |
| 73 | + Drain: true, |
| 74 | + }, |
| 75 | + }, |
| 76 | + expected: "[{MaxConns:50 MaxFails:2 Backup:false Down:true Weight:5 Server:192.168.1.2:8080 FailTimeout:20s SlowStart:5s Route:route2 Service:serviceB ID:1 Drain:false} {MaxConns:150 MaxFails:5 Backup:true Down:false Weight:15 Server:192.168.1.3:8080 FailTimeout:40s SlowStart:20s Route:route3 Service:serviceC ID:2 Drain:true}]", |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "Servers with nil pointer fields", |
| 80 | + input: []client.UpstreamServer{ |
| 81 | + { |
| 82 | + MaxConns: nil, // Should default to 0 |
| 83 | + MaxFails: ptrInt(4), |
| 84 | + Backup: nil, // Should default to false |
| 85 | + Down: ptrBool(true), |
| 86 | + Weight: nil, // Should default to 0 |
| 87 | + Server: "192.168.1.4:8080", |
| 88 | + FailTimeout: "", |
| 89 | + SlowStart: "", |
| 90 | + Route: "", |
| 91 | + Service: "", |
| 92 | + ID: 0, |
| 93 | + Drain: false, |
| 94 | + }, |
| 95 | + }, |
| 96 | + expected: "[{MaxConns:0 MaxFails:4 Backup:false Down:true Weight:0 Server:192.168.1.4:8080 FailTimeout: SlowStart: Route: Service: ID:0 Drain:false}]", |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + for _, tc := range tests { |
| 101 | + t.Run(tc.name, func(t *testing.T) { |
| 102 | + actual := formatUpdateServersInPlusLog(tc.input) |
| 103 | + if actual != tc.expected { |
| 104 | + t.Errorf("FormatUpdateServersInPlusLog() = %v, want %v", actual, tc.expected) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
0 commit comments