Skip to content

Commit 1bf6e37

Browse files
committed
TEST: add e2e test for proxy-proctol on haproxy side
1 parent 232aa0d commit 1bf6e37

File tree

11 files changed

+167
-39
lines changed

11 files changed

+167
-39
lines changed

deploy/tests/e2e/client.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ import (
2020
"context"
2121
"crypto/tls"
2222
"fmt"
23+
"io/ioutil"
2324
"net"
2425
"net/http"
2526
"os"
2627
"strings"
28+
29+
proxyproto "github.com/pires/go-proxyproto"
2730
)
2831

2932
type Client struct {
@@ -128,6 +131,52 @@ func (c *Client) Do() (res *http.Response, close func() error, err error) {
128131
return
129132
}
130133

134+
func ProxyProtoConn() (result []byte, err error) {
135+
kindURL := os.Getenv("KIND_URL")
136+
if kindURL == "" {
137+
kindURL = "127.0.0.1"
138+
}
139+
dstPort := HTTP_PORT
140+
141+
target, errAddr := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", kindURL, dstPort))
142+
if errAddr != nil {
143+
return nil, errAddr
144+
}
145+
146+
conn, errConn := net.DialTCP("tcp", nil, target)
147+
if err != nil {
148+
return nil, errConn
149+
}
150+
defer conn.Close()
151+
152+
// Create a proxyprotocol header
153+
header := &proxyproto.Header{
154+
Version: 1,
155+
Command: proxyproto.PROXY,
156+
TransportProtocol: proxyproto.TCPv4,
157+
SourceAddr: &net.TCPAddr{
158+
IP: net.ParseIP("10.1.1.1"),
159+
Port: 1000,
160+
},
161+
DestinationAddr: &net.TCPAddr{
162+
IP: net.ParseIP("20.2.2.2"),
163+
Port: 2000,
164+
},
165+
}
166+
167+
_, err = header.WriteTo(conn)
168+
if err != nil {
169+
return
170+
}
171+
172+
_, err = conn.Write([]byte("HEAD / HTTP/1.0\r\n\r\n"))
173+
if err != nil {
174+
return
175+
}
176+
177+
return ioutil.ReadAll(conn)
178+
}
179+
131180
func GetGlobalHAProxyInfo() (info GlobalHAProxyInfo, err error) {
132181
kindURL := os.Getenv("KIND_URL")
133182
if kindURL == "" {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: haproxy-configmap
5+
namespace: haproxy-controller
6+
data:
7+
# Mandatory config
8+
global-config-snippet: |
9+
stats socket 0.0.0.0:31024
10+
syslog-server: |
11+
address: stdout, format: raw, facility:daemon
12+
# Optional config
13+
proxy-protocol: 0.0.0.0/0
14+
maxconn: "1000"
15+
server-slots: "4"
16+
timeout-client: 50s
17+
timeout-connect: 5s
18+
timeout-http-keep-alive: 1m
19+
timeout-http-request: 5s
20+
timeout-queue: 5s
21+
timeout-server: 50s
22+
timeout-tunnel: 1h
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: haproxy-configmap
5+
namespace: haproxy-controller
6+
data:
7+
# Mandatory config
8+
global-config-snippet: |
9+
stats socket 0.0.0.0:31024
10+
syslog-server: |
11+
address: stdout, format: raw, facility:daemon
12+
# Optional config
13+
proxy-protocol: 192.168.1.1
14+
maxconn: "1000"
15+
server-slots: "4"
16+
timeout-client: 50s
17+
timeout-connect: 5s
18+
timeout-http-keep-alive: 1m
19+
timeout-http-request: 5s
20+
timeout-queue: 5s
21+
timeout-server: 50s
22+
timeout-tunnel: 1h

deploy/tests/e2e/global-config/maxconn_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
func (suite *GlobalConfigSuite) TestMaxconn() {
24-
suite.NoError(suite.test.Apply("config/configmap.yaml", "", nil))
24+
suite.NoError(suite.test.Apply("config/configmap-maxconn.yaml", "", nil))
2525
suite.maxconn = "1111"
2626
suite.Eventually(suite.checkMaxconn, e2e.WaitDuration, e2e.TickDuration)
2727

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2019 HAProxy Technologies LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build e2e_sequential
16+
17+
package globalconfig
18+
19+
import (
20+
"strings"
21+
22+
"github.com/haproxytech/kubernetes-ingress/deploy/tests/e2e"
23+
)
24+
25+
func (suite *GlobalConfigSuite) Test_Proxy_Protocol() {
26+
suite.Run("Source_IP_OK", func() {
27+
suite.NoError(suite.test.Apply("config/configmap-pp-1.yaml", "", nil))
28+
suite.Eventually(func() bool {
29+
res, err := e2e.ProxyProtoConn()
30+
if err != nil {
31+
suite.T().Logf("Connection ERROR: %s", err.Error())
32+
return false
33+
}
34+
return strings.Contains(string(res), "404 Not Found")
35+
}, e2e.WaitDuration, e2e.TickDuration)
36+
})
37+
38+
suite.Run("Source_IP_KO", func() {
39+
suite.NoError(suite.test.Apply("config/configmap-pp-2.yaml", "", nil))
40+
suite.Eventually(func() bool {
41+
res, err := e2e.ProxyProtoConn()
42+
if err != nil {
43+
suite.T().Logf("Connection ERROR: %s", err.Error())
44+
return false
45+
}
46+
suite.T().Logf("Result: %s", string(res))
47+
return strings.Contains(string(res), "400 Bad request")
48+
}, e2e.WaitDuration, e2e.TickDuration)
49+
})
50+
51+
// revert to initial configmap
52+
suite.NoError(suite.test.Apply("../../config/3.configmap.yaml", "", nil))
53+
}

deploy/tests/e2e/proxy-protocol/send_proxy_test.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

deploy/tests/e2e/proxy-protocol/suite_test.go renamed to deploy/tests/e2e/send-proxy-protocol/suite_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package proxyprotocol
1818

1919
import (
20+
"io/ioutil"
2021
"testing"
2122

2223
"github.com/stretchr/testify/suite"
@@ -45,6 +46,22 @@ func (suite *ProxyProtocolSuite) SetupSuite() {
4546
suite.Require().NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData))
4647
}
4748

49+
func (suite *ProxyProtocolSuite) Test_Send_Proxy() {
50+
suite.Eventually(func() bool {
51+
res, cls, err := suite.client.Do()
52+
if err != nil {
53+
suite.T().Logf("Connection ERROR: %s", err.Error())
54+
return false
55+
}
56+
defer cls()
57+
body, err := ioutil.ReadAll(res.Body)
58+
if err != nil {
59+
return false
60+
}
61+
return string(body) == "hello!"
62+
}, e2e.WaitDuration, e2e.TickDuration)
63+
}
64+
4865
func (suite *ProxyProtocolSuite) TearDownSuite() {
4966
suite.test.TearDown()
5067
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/haproxytech/client-native/v2 v2.5.2-0.20211021104411-038f5cf8ec14
99
github.com/haproxytech/config-parser/v4 v4.0.0-rc2.0.20211021093817-f9021b6ca61c
1010
github.com/jessevdk/go-flags v1.4.0
11+
github.com/pires/go-proxyproto v0.6.1
1112
github.com/stretchr/testify v1.7.0
1213
k8s.io/api v0.22.2
1314
k8s.io/apimachinery v0.22.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtP
305305
github.com/pelletier/go-toml v1.4.0/go.mod h1:PN7xzY2wHTK0K9p34ErDQMlFxa51Fk0OUruD3k1mMwo=
306306
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
307307
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
308+
github.com/pires/go-proxyproto v0.6.1 h1:EBupykFmo22SDjv4fQVQd2J9NOoLPmyZA/15ldOGkPw=
309+
github.com/pires/go-proxyproto v0.6.1/go.mod h1:Odh9VFOZJCf9G8cLW5o435Xf1J95Jw9Gw5rnCjcwzAY=
308310
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
309311
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
310312
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

0 commit comments

Comments
 (0)