Skip to content

Commit 2c6e0a2

Browse files
ivanmatmatiMo3m3n
authored andcommitted
MINOR: add customer resource manager and afferent test
1 parent e5cd2b6 commit 2c6e0a2

File tree

4 files changed

+190
-2
lines changed

4 files changed

+190
-2
lines changed

.gitlab-ci.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,11 @@ lint-commit-msg:
6262
unit-tests:
6363
stage: unit-tests
6464
image:
65-
name: $CI_REGISTRY_GO/golang:1.17
65+
name: $CI_REGISTRY_GO/haproxy-alpine:2.5-go1.17
6666
entrypoint: [""]
6767
tags:
6868
- go
6969
script:
70-
- apt update
7170
- go build -v .
7271
- go test -v ./...
7372
only:

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ linters:
3030
- wrapcheck
3131
- wsl
3232
- nakedret
33+
- paralleltest
34+
- testpackage
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 customresources
16+
17+
import (
18+
"github.com/stretchr/testify/assert"
19+
20+
"github.com/haproxytech/kubernetes-ingress/deploy/tests/e2e"
21+
)
22+
23+
func (suite *CustomResourceSuite) TestGlobalCR() {
24+
eventChan, s, globalCREvt := suite.GlobalCRFixture()
25+
26+
globals := s.GetNamespace(globalCREvt.Namespace).CRs.Global
27+
logtrargets := s.GetNamespace(globalCREvt.Namespace).CRs.LogTargets
28+
t := suite.T()
29+
30+
suite.Run("Adding a global CR creates global and logTargets objects", func() {
31+
eventChan <- globalCREvt
32+
suite.Require().Eventually(func() bool {
33+
return assert.Len(t, globals, 1, "Should find one and only one Global CR") ||
34+
assert.Len(t, logtrargets, 1, "Should find one and only one LogTargets CR") ||
35+
assert.Containsf(t, globals, globalCREvt.Name, "Global CR of name '%s' not found", globalCREvt.Name)
36+
}, e2e.WaitDuration, e2e.TickDuration)
37+
})
38+
39+
suite.Run("Deleting a global CR removes global and logTargets objects", func() {
40+
globalCREvt.Data = nil
41+
eventChan <- globalCREvt
42+
suite.Require().Eventually(func() bool {
43+
return assert.Len(t, globals, 0, "No Global CR should be present") ||
44+
assert.Len(t, logtrargets, 0, "No LogTargets should be present")
45+
}, e2e.WaitDuration, e2e.TickDuration)
46+
})
47+
48+
close(eventChan)
49+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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, 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 customresources
16+
17+
import (
18+
"io/ioutil"
19+
"path/filepath"
20+
"testing"
21+
22+
"github.com/haproxytech/client-native/v2/models"
23+
corev1alpha1 "github.com/haproxytech/kubernetes-ingress/crs/api/core/v1alpha1"
24+
c "github.com/haproxytech/kubernetes-ingress/pkg/controller"
25+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/config"
26+
"github.com/haproxytech/kubernetes-ingress/pkg/k8s"
27+
"github.com/haproxytech/kubernetes-ingress/pkg/store"
28+
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
29+
"github.com/jessevdk/go-flags"
30+
"github.com/stretchr/testify/suite"
31+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32+
"k8s.io/apimachinery/pkg/watch"
33+
)
34+
35+
type CustomResourceSuite struct {
36+
suite.Suite
37+
test Test
38+
}
39+
40+
func TestCustomResource(t *testing.T) {
41+
suite.Run(t, new(CustomResourceSuite))
42+
}
43+
44+
type Test struct {
45+
TempDir string
46+
Controller *c.HAProxyController
47+
}
48+
49+
func (suite *CustomResourceSuite) BeforeTest(suiteName, testName string) {
50+
tempDir, err := ioutil.TempDir("", "ut-"+testName+"-*")
51+
if err != nil {
52+
suite.T().Fatalf("Suite '%s': Test '%s' : error : %s", suiteName, testName, err)
53+
}
54+
suite.test.TempDir = tempDir
55+
}
56+
57+
var haproxyConfig = `global
58+
daemon
59+
master-worker
60+
pidfile /var/run/haproxy.pid
61+
stats socket /var/run/haproxy-runtime-api.sock level admin expose-fd listeners
62+
default-path config
63+
64+
peers localinstance
65+
peer local 127.0.0.1:10000
66+
67+
frontend https
68+
mode http
69+
bind 127.0.0.1:8080 name v4
70+
http-request set-var(txn.base) base
71+
use_backend %[var(txn.path_match),field(1,.)]
72+
73+
frontend http
74+
mode http
75+
bind 127.0.0.1:4443 name v4
76+
http-request set-var(txn.base) base
77+
use_backend %[var(txn.path_match),field(1,.)]
78+
79+
frontend healthz
80+
bind 127.0.0.1:1042 name v4
81+
mode http
82+
monitor-uri /healthz
83+
option dontlog-normal
84+
85+
frontend stats
86+
mode http
87+
bind *:1024
88+
http-request set-var(txn.base) base
89+
http-request use-service prometheus-exporter if { path /metrics }
90+
stats enable
91+
stats uri /
92+
stats refresh 10s`
93+
94+
func (suite *CustomResourceSuite) GlobalCRFixture() (eventChan chan k8s.SyncDataEvent, s store.K8s, globalCREvt k8s.SyncDataEvent) {
95+
var osArgs utils.OSArgs
96+
parser := flags.NewParser(&osArgs, flags.IgnoreUnknown)
97+
_, _ = parser.Parse()
98+
osArgs.Test = true
99+
eventChan = make(chan k8s.SyncDataEvent, watch.DefaultChanSize*6)
100+
s = store.NewK8sStore(osArgs)
101+
globalCREvt = k8s.SyncDataEvent{
102+
SyncType: k8s.CR_GLOBAL,
103+
Data: &corev1alpha1.Global{
104+
ObjectMeta: metav1.ObjectMeta{
105+
Name: "fake",
106+
},
107+
Spec: corev1alpha1.GlobalSpec{
108+
Config: &models.Global{},
109+
LogTargets: models.LogTargets{},
110+
},
111+
},
112+
Name: "globalcrjob",
113+
}
114+
115+
haproxyEnv := config.Env{
116+
Binary: "/usr/local/sbin/haproxy",
117+
MainCFGFile: filepath.Join(suite.test.TempDir, "haproxy.cfg"),
118+
CfgDir: suite.test.TempDir,
119+
RuntimeDir: filepath.Join(suite.test.TempDir, "run"),
120+
StateDir: filepath.Join(suite.test.TempDir, "state/haproxy/"),
121+
Proxies: config.Proxies{
122+
FrontHTTP: "http",
123+
FrontHTTPS: "https",
124+
FrontSSL: "ssl",
125+
BackSSL: "ssl",
126+
},
127+
}
128+
129+
controller := c.NewBuilder().
130+
WithHaproxyCfgFile([]byte(haproxyConfig)).
131+
WithEventChan(eventChan).
132+
WithStore(s).
133+
WithHaproxyEnv(haproxyEnv).
134+
WithArgs(osArgs).Build()
135+
136+
go controller.Start()
137+
return
138+
}

0 commit comments

Comments
 (0)