|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/haproxytech/client-native/v6/models" |
| 7 | + cn_runtime "github.com/haproxytech/client-native/v6/runtime" |
| 8 | + "github.com/haproxytech/dataplaneapi/misc" |
| 9 | +) |
| 10 | + |
| 11 | +func TestSerializeRuntimeAddServer(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + srv *models.RuntimeAddServer |
| 15 | + version *cn_runtime.HAProxyVersion |
| 16 | + want string |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "basic server", |
| 20 | + srv: &models.RuntimeAddServer{ |
| 21 | + Address: "127.0.0.1", |
| 22 | + Port: misc.Int64P(8080), |
| 23 | + }, |
| 24 | + version: &cn_runtime.HAProxyVersion{}, |
| 25 | + want: " 127.0.0.1:8080", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "server with weight", |
| 29 | + srv: &models.RuntimeAddServer{ |
| 30 | + Address: "192.168.1.100", |
| 31 | + Port: misc.Int64P(9000), |
| 32 | + Weight: misc.Int64P(50), |
| 33 | + }, |
| 34 | + version: &cn_runtime.HAProxyVersion{}, |
| 35 | + want: " 192.168.1.100:9000 weight 50", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "server with maintenance", |
| 39 | + srv: &models.RuntimeAddServer{ |
| 40 | + Address: "10.0.0.10", |
| 41 | + Maintenance: "enabled", |
| 42 | + }, |
| 43 | + version: &cn_runtime.HAProxyVersion{}, |
| 44 | + want: " 10.0.0.10 disabled", |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "server with maintenance disabled", |
| 48 | + srv: &models.RuntimeAddServer{ |
| 49 | + Address: "10.0.0.10", |
| 50 | + Maintenance: "disabled", |
| 51 | + }, |
| 52 | + version: &cn_runtime.HAProxyVersion{}, |
| 53 | + want: " 10.0.0.10 enabled", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "server with maintenance disabled, HAProxy 3.0", |
| 57 | + srv: &models.RuntimeAddServer{ |
| 58 | + Address: "10.0.0.10", |
| 59 | + Maintenance: "disabled", |
| 60 | + }, |
| 61 | + version: func() *cn_runtime.HAProxyVersion { |
| 62 | + v := new(cn_runtime.HAProxyVersion) |
| 63 | + v.ParseHAProxyVersion("3.0.0") |
| 64 | + return v |
| 65 | + }(), |
| 66 | + want: " 10.0.0.10", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "server with agent check", |
| 70 | + srv: &models.RuntimeAddServer{ |
| 71 | + Address: "172.16.0.5", |
| 72 | + AgentCheck: "enabled", |
| 73 | + }, |
| 74 | + version: &cn_runtime.HAProxyVersion{}, |
| 75 | + want: " 172.16.0.5 agent-check", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "server with agent addr", |
| 79 | + srv: &models.RuntimeAddServer{ |
| 80 | + Address: "172.16.0.6", |
| 81 | + AgentAddr: "127.0.0.1", |
| 82 | + }, |
| 83 | + version: &cn_runtime.HAProxyVersion{}, |
| 84 | + want: ` 172.16.0.6 agent-addr 127.0.0.1`, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "server with multiple options", |
| 88 | + srv: &models.RuntimeAddServer{ |
| 89 | + Address: "10.1.1.10", |
| 90 | + Port: misc.Int64P(80), |
| 91 | + Weight: misc.Int64P(10), |
| 92 | + Check: "enabled", |
| 93 | + Backup: "enabled", |
| 94 | + Maintenance: "enabled", |
| 95 | + AgentCheck: "enabled", |
| 96 | + AgentAddr: "127.0.0.1", |
| 97 | + AgentPort: misc.Int64P(5000), |
| 98 | + HealthCheckAddress: "127.0.0.2", |
| 99 | + }, |
| 100 | + version: &cn_runtime.HAProxyVersion{}, |
| 101 | + want: ` 10.1.1.10:80 agent-check agent-addr 127.0.0.1 agent-port 5000 backup check addr 127.0.0.2 disabled weight 10`, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "server with all fields", |
| 105 | + srv: &models.RuntimeAddServer{ |
| 106 | + Address: "10.1.1.10", |
| 107 | + Port: misc.Int64P(80), |
| 108 | + Weight: misc.Int64P(10), |
| 109 | + Check: "enabled", |
| 110 | + Backup: "enabled", |
| 111 | + Maintenance: "enabled", |
| 112 | + AgentCheck: "enabled", |
| 113 | + AgentAddr: "127.0.0.1", |
| 114 | + AgentPort: misc.Int64P(5000), |
| 115 | + AgentInter: misc.Int64P(1000), |
| 116 | + AgentSend: "foobar", |
| 117 | + Allow0rtt: true, |
| 118 | + Alpn: "h2,http/1.1", |
| 119 | + CheckAlpn: "h2", |
| 120 | + CheckProto: "HTTP", |
| 121 | + CheckSendProxy: "enabled", |
| 122 | + CheckSni: "example.com", |
| 123 | + CheckSsl: "enabled", |
| 124 | + CheckViaSocks4: "enabled", |
| 125 | + Ciphers: "HIGH:!aNULL:!MD5", |
| 126 | + Ciphersuites: "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256", |
| 127 | + CrlFile: "/path/to/crl.pem", |
| 128 | + SslCertificate: "/path/to/cert.pem", |
| 129 | + Downinter: misc.Int64P(2000), |
| 130 | + ErrorLimit: misc.Int64P(10), |
| 131 | + Fall: misc.Int64P(2), |
| 132 | + Fastinter: misc.Int64P(500), |
| 133 | + ForceSslv3: "enabled", |
| 134 | + ForceTlsv10: "enabled", |
| 135 | + ForceTlsv11: "enabled", |
| 136 | + ForceTlsv12: "enabled", |
| 137 | + ForceTlsv13: "enabled", |
| 138 | + HealthCheckAddress: "127.0.0.2", |
| 139 | + HealthCheckPort: misc.Int64P(8080), |
| 140 | + Inter: misc.Int64P(3000), |
| 141 | + Maxconn: misc.Int64P(100), |
| 142 | + Maxqueue: misc.Int64P(200), |
| 143 | + Minconn: misc.Int64P(50), |
| 144 | + Rise: misc.Int64P(1), |
| 145 | + }, |
| 146 | + version: &cn_runtime.HAProxyVersion{}, |
| 147 | + want: ` 10.1.1.10:80 agent-check agent-addr 127.0.0.1 agent-port 5000 agent-inter 1000 agent-send foobar allow-0rtt alpn h2,http/1.1 backup check check-alpn h2 addr 127.0.0.2 port 8080 check-proto HTTP check-send-proxy check-sni example.com check-ssl check-via-socks4 ciphers HIGH:!aNULL:!MD5 ciphersuites TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256 crl-file /path/to/crl.pem crt /path/to/cert.pem disabled downinter 2000 error-limit 10 fall 2 fastinter 500 force-sslv3 force-tlsv10 force-tlsv11 force-tlsv12 force-tlsv13 inter 3000 maxconn 100 maxqueue 200 minconn 50 rise 1 weight 10`, |
| 148 | + }, |
| 149 | + } |
| 150 | + for _, tt := range tests { |
| 151 | + t.Run(tt.name, func(t *testing.T) { |
| 152 | + if tt.version == nil { |
| 153 | + tt.version = &cn_runtime.HAProxyVersion{} |
| 154 | + } |
| 155 | + if got := SerializeRuntimeAddServer(tt.srv, tt.version); got != tt.want { |
| 156 | + t.Errorf("SerializeRuntimeAddServer() = %v, want %v", got, tt.want) |
| 157 | + } |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
0 commit comments