Skip to content

Commit a8af0e4

Browse files
committed
TEST: test ACLs Backend CRD
1 parent 87733bf commit a8af0e4

File tree

3 files changed

+242
-0
lines changed

3 files changed

+242
-0
lines changed

deploy/tests/tnr/routeacl/suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func (suite *UseBackendSuite) UseBackendFixture() (eventChan chan k8ssync.SyncDa
108108
s := store.NewK8sStore(osArgs)
109109

110110
haproxyEnv := env.Env{
111+
CfgDir: suite.test.TempDir,
111112
Proxies: env.Proxies{
112113
FrontHTTP: "http",
113114
FrontHTTPS: "https",

deploy/tests/ut/acls/acls.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
package acls
16+
17+
import (
18+
"os"
19+
"path/filepath"
20+
"strings"
21+
)
22+
23+
func (suite *ACLSuite) TestACL() {
24+
suite.UseACLFixture()
25+
contents, err := os.ReadFile(filepath.Join(suite.test.TempDir, "haproxy.cfg"))
26+
if err != nil {
27+
suite.T().Error(err.Error())
28+
}
29+
30+
suite.Run("acl cookie_found", func() {
31+
c := strings.Count(string(contents), "acl cookie_found cook(JSESSIONID) -m found")
32+
suite.Exactly(c, 1, "acl cookie_found is repeated %d times but expected 1", c)
33+
c = strings.Count(string(contents), "acl is_ticket path_beg -i /ticket")
34+
suite.Exactly(c, 1, "acl is_ticket is repeated %d times but expected 1", c)
35+
})
36+
37+
suite.Run("acl is_ticket", func() {
38+
c := strings.Count(string(contents), "acl is_ticket path_beg -i /ticket")
39+
suite.Exactly(c, 1, "acl is_ticket is repeated %d times but expected 1", c)
40+
})
41+
}

deploy/tests/ut/acls/suite_test.go

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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+
package acls
16+
17+
import (
18+
_ "embed"
19+
"os"
20+
"testing"
21+
22+
"github.com/haproxytech/client-native/v5/models"
23+
v1 "github.com/haproxytech/kubernetes-ingress/crs/api/ingress/v1"
24+
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
25+
c "github.com/haproxytech/kubernetes-ingress/pkg/controller"
26+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
27+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/env"
28+
"github.com/haproxytech/kubernetes-ingress/pkg/ingress"
29+
k8ssync "github.com/haproxytech/kubernetes-ingress/pkg/k8s/sync"
30+
"github.com/haproxytech/kubernetes-ingress/pkg/store"
31+
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
32+
"github.com/jessevdk/go-flags"
33+
"github.com/stretchr/testify/assert"
34+
"github.com/stretchr/testify/suite"
35+
networkingv1 "k8s.io/api/networking/v1"
36+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37+
"k8s.io/apimachinery/pkg/watch"
38+
)
39+
40+
type FakeUpdateSatusManager struct{}
41+
42+
func (m *FakeUpdateSatusManager) AddIngress(ingress *ingress.Ingress) {}
43+
func (m *FakeUpdateSatusManager) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (err error) {
44+
return
45+
}
46+
47+
type ACLSuite struct {
48+
suite.Suite
49+
test Test
50+
}
51+
52+
func TestACL(t *testing.T) {
53+
suite.Run(t, new(ACLSuite))
54+
}
55+
56+
type Test struct {
57+
Controller *c.HAProxyController
58+
TempDir string
59+
}
60+
61+
func (suite *ACLSuite) BeforeTest(suiteName, testName string) {
62+
tempDir, err := os.MkdirTemp("", "tnr-"+testName+"-*")
63+
if err != nil {
64+
suite.T().Fatalf("Suite '%s': Test '%s' : error : %s", suiteName, testName, err)
65+
}
66+
suite.test.TempDir = tempDir
67+
suite.T().Logf("temporary configuration dir %s", suite.test.TempDir)
68+
}
69+
70+
func (suite *ACLSuite) UseACLFixture() (eventChan chan k8ssync.SyncDataEvent) {
71+
var osArgs utils.OSArgs
72+
os.Args = []string{os.Args[0], "-e", "-t", "--config-dir=" + suite.test.TempDir}
73+
parser := flags.NewParser(&osArgs, flags.IgnoreUnknown)
74+
_, errParsing := parser.Parse() //nolint:ifshort
75+
if errParsing != nil {
76+
suite.T().Fatal(errParsing)
77+
}
78+
79+
s := store.NewK8sStore(osArgs)
80+
os.Setenv("POD_NAME", "haproxy-kubernetes-ingress-68c9fc6d86-zn9qz")
81+
82+
haproxyEnv := env.Env{
83+
CfgDir: suite.test.TempDir,
84+
Proxies: env.Proxies{
85+
FrontHTTP: "http",
86+
FrontHTTPS: "https",
87+
FrontSSL: "ssl",
88+
BackSSL: "ssl",
89+
},
90+
}
91+
haproxyConfig, err := os.ReadFile("../../../../fs/usr/local/etc/haproxy/haproxy.cfg")
92+
if err != nil {
93+
//nolint:testifylint
94+
assert.Failf(suite.T(), "error in opening init haproxy configuration file", err.Error())
95+
}
96+
97+
eventChan = make(chan k8ssync.SyncDataEvent, watch.DefaultChanSize*6)
98+
controller := c.NewBuilder().
99+
WithHaproxyCfgFile(haproxyConfig).
100+
WithEventChan(eventChan).
101+
WithStore(s).
102+
WithHaproxyEnv(haproxyEnv).
103+
WithUpdateStatusManager(&FakeUpdateSatusManager{}).
104+
WithArgs(osArgs).Build()
105+
106+
go controller.Start()
107+
108+
backend := v1.Backend{
109+
ObjectMeta: metav1.ObjectMeta{
110+
Name: "backend1cr",
111+
Namespace: "ns1",
112+
},
113+
Spec: v1.BackendSpec{
114+
Config: &models.Backend{
115+
Name: "backend1",
116+
},
117+
Acls: models.Acls{
118+
{
119+
ACLName: "cookie_found",
120+
Criterion: "cook(JSESSIONID)",
121+
Index: utils.Ptr[int64](0),
122+
Value: "-m found",
123+
},
124+
{
125+
ACLName: "is_ticket",
126+
Criterion: "path_beg",
127+
Index: utils.Ptr[int64](1),
128+
Value: "-i /ticket",
129+
},
130+
},
131+
},
132+
}
133+
134+
eventChan <- k8ssync.SyncDataEvent{SyncType: k8ssync.CR_BACKEND, Namespace: backend.Namespace, Name: backend.Name, Data: &backend}
135+
136+
// Now sending store events for test setup
137+
ns := store.Namespace{Name: "ns", Status: store.ADDED}
138+
eventChan <- k8ssync.SyncDataEvent{SyncType: k8ssync.NAMESPACE, Namespace: ns.Name, Data: &ns}
139+
140+
endpoints := &store.Endpoints{
141+
SliceName: "myappservice",
142+
Service: "myappservice",
143+
Namespace: ns.Name,
144+
Ports: map[string]*store.PortEndpoints{
145+
"https": {
146+
Port: int64(3001),
147+
Addresses: map[string]struct{}{"10.244.0.9": {}},
148+
},
149+
},
150+
Status: store.ADDED,
151+
}
152+
153+
eventChan <- k8ssync.SyncDataEvent{SyncType: k8ssync.ENDPOINTS, Namespace: endpoints.Namespace, Data: endpoints}
154+
155+
service := &store.Service{
156+
Name: "myappservice",
157+
Namespace: ns.Name,
158+
Annotations: map[string]string{"cr-backend": backend.Namespace + "/" + backend.Name},
159+
Ports: []store.ServicePort{
160+
{
161+
Name: "https",
162+
Protocol: "TCP",
163+
Port: 8443,
164+
Status: store.ADDED,
165+
},
166+
},
167+
Status: store.ADDED,
168+
}
169+
eventChan <- k8ssync.SyncDataEvent{SyncType: k8ssync.SERVICE, Namespace: service.Namespace, Data: service}
170+
171+
ingress := &store.Ingress{
172+
IngressCore: store.IngressCore{
173+
APIVersion: store.NETWORKINGV1,
174+
Name: "myapping",
175+
Namespace: ns.Name,
176+
Annotations: map[string]string{"haproxy.org/ingress.class": "haproxy"},
177+
Rules: map[string]*store.IngressRule{
178+
"": {
179+
Paths: map[string]*store.IngressPath{
180+
string(networkingv1.PathTypePrefix) + "-/": {
181+
Path: "/",
182+
PathTypeMatch: string(networkingv1.PathTypePrefix),
183+
SvcNamespace: service.Namespace,
184+
SvcPortString: "https",
185+
SvcName: service.Name,
186+
},
187+
},
188+
},
189+
},
190+
},
191+
Status: store.ADDED,
192+
}
193+
194+
eventChan <- k8ssync.SyncDataEvent{SyncType: k8ssync.INGRESS, Namespace: ingress.Namespace, Data: ingress}
195+
controllerHasWorked := make(chan struct{})
196+
eventChan <- k8ssync.SyncDataEvent{SyncType: k8ssync.COMMAND}
197+
eventChan <- k8ssync.SyncDataEvent{EventProcessed: controllerHasWorked}
198+
<-controllerHasWorked
199+
return
200+
}

0 commit comments

Comments
 (0)