Skip to content

Commit 247199b

Browse files
committed
TEST/MINOR: add integration tests for disabling snippet configs.
1 parent 6d18884 commit 247199b

File tree

6 files changed

+575
-122
lines changed

6 files changed

+575
-122
lines changed
+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Copyright 2023 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, softwarehaproxyConfig
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+
package integration
16+
17+
import (
18+
"os"
19+
20+
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
21+
c "github.com/haproxytech/kubernetes-ingress/pkg/controller"
22+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
23+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/env"
24+
"github.com/haproxytech/kubernetes-ingress/pkg/ingress"
25+
"github.com/haproxytech/kubernetes-ingress/pkg/k8s"
26+
"github.com/haproxytech/kubernetes-ingress/pkg/store"
27+
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
28+
"github.com/jessevdk/go-flags"
29+
"github.com/stretchr/testify/suite"
30+
"k8s.io/apimachinery/pkg/watch"
31+
)
32+
33+
//nolint:dupword
34+
var haproxyConfig = `global
35+
daemon
36+
master-worker
37+
pidfile /var/run/haproxy.pid
38+
stats socket /var/run/haproxy-runtime-api.sock level admin expose-fd listeners
39+
default-path config
40+
41+
peers localinstance
42+
peer local 127.0.0.1:10000
43+
44+
frontend https
45+
mode http
46+
bind 127.0.0.1:8080 name v4
47+
http-request set-var(txn.base) base
48+
use_backend %[var(txn.path_match),field(1,.)]
49+
50+
frontend http
51+
mode http
52+
bind 127.0.0.1:4443 name v4
53+
http-request set-var(txn.base) base
54+
use_backend %[var(txn.path_match),field(1,.)]
55+
56+
frontend healthz
57+
bind 127.0.0.1:1042 name v4
58+
mode http
59+
monitor-uri /healthz
60+
option dontlog-normal
61+
62+
frontend stats
63+
mode http
64+
bind *:1024 name stats
65+
stats enable
66+
stats uri /
67+
stats refresh 10s
68+
http-request set-var(txn.base) base
69+
http-request use-service prometheus-exporter if { path /metrics }
70+
`
71+
72+
type updateStatusManager struct{}
73+
74+
func (m *updateStatusManager) AddIngress(ingress *ingress.Ingress) {}
75+
func (m *updateStatusManager) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (reload bool, err error) {
76+
return
77+
}
78+
79+
type TestController struct {
80+
TempDir string
81+
Controller *c.HAProxyController
82+
Store store.K8s
83+
EventChan chan k8s.SyncDataEvent
84+
OSArgs utils.OSArgs
85+
}
86+
87+
type BaseSuite struct {
88+
suite.Suite
89+
TestControllers map[string]*TestController
90+
}
91+
92+
// BeforeTest:
93+
// - create a tempDir for haproxy config + maps + ....
94+
// - prepares and sets some common default start parameters for haproxy controller :
95+
// - "-e", "-t" and "--config-dir"
96+
//
97+
// To customize the controller start parameters, refer (as example) to
98+
//
99+
// func (suite *DisableConfigSnippetSuite) BeforeTest(suiteName, testName string) {
100+
func (suite *BaseSuite) BeforeTest(suiteName, testName string) {
101+
tempDir, err := os.MkdirTemp("", "tnr-"+testName+"-*")
102+
if err != nil {
103+
suite.T().Fatalf("Suite '%s': Test '%s' : error : %s", suiteName, testName, err)
104+
}
105+
if suite.TestControllers == nil {
106+
suite.TestControllers = make(map[string]*TestController)
107+
}
108+
109+
// Some common default arguments
110+
var osArgs utils.OSArgs
111+
os.Args = []string{os.Args[0], "-e", "-t", "--config-dir=" + tempDir}
112+
parser := flags.NewParser(&osArgs, flags.IgnoreUnknown)
113+
_, errParsing := parser.Parse() //nolint:ifshort
114+
if errParsing != nil {
115+
suite.T().Fatal(errParsing)
116+
}
117+
118+
suite.TestControllers[suite.T().Name()] = &TestController{
119+
TempDir: tempDir,
120+
OSArgs: osArgs,
121+
}
122+
suite.T().Logf("temporary configuration dir %s", tempDir)
123+
}
124+
125+
// AfterTest stops the controllers and delete the entry from the test map
126+
func (suite *BaseSuite) AfterTest(suiteName, testName string) {
127+
delete(suite.TestControllers, suite.T().Name())
128+
}
129+
130+
// StartController starts a controller
131+
// It is not started in BeforeTest() or SetupSubTest() but must be called when desired in the test/subtest.
132+
func (suite *BaseSuite) StartController() {
133+
testController := suite.TestControllers[suite.T().Name()]
134+
135+
testController.Store = store.NewK8sStore(testController.OSArgs)
136+
137+
haproxyEnv := env.Env{
138+
Proxies: env.Proxies{
139+
FrontHTTP: "http",
140+
FrontHTTPS: "https",
141+
FrontSSL: "ssl",
142+
BackSSL: "ssl",
143+
},
144+
}
145+
146+
testController.EventChan = make(chan k8s.SyncDataEvent, watch.DefaultChanSize*6)
147+
testController.Controller = c.NewBuilder().
148+
WithHaproxyCfgFile([]byte(haproxyConfig)).
149+
WithEventChan(testController.EventChan).
150+
WithStore(testController.Store).
151+
WithHaproxyEnv(haproxyEnv).
152+
WithUpdateStatusManager(&updateStatusManager{}).
153+
WithArgs(testController.OSArgs).Build()
154+
155+
annotations.InitCfgSnippet()
156+
annotations.DisableConfigSnippets(testController.OSArgs.DisableConfigSnippets)
157+
158+
go testController.Controller.Start()
159+
}
160+
161+
func (suite *BaseSuite) StopController() {
162+
testController := suite.TestControllers[suite.T().Name()]
163+
testController.Controller.Stop()
164+
testController.Store.Clean()
165+
close(testController.EventChan)
166+
}

0 commit comments

Comments
 (0)