Skip to content

Commit

Permalink
Merge pull request #16 from six-group/increase-api-test-coverage
Browse files Browse the repository at this point in the history
Add more tests for the API/model mapping
  • Loading branch information
snorwin committed Jan 2, 2024
2 parents dc26c88 + a1c814e commit ed3b047
Show file tree
Hide file tree
Showing 17 changed files with 423 additions and 155 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
cache: false
- uses: actions/checkout@v4
- run: go run github.com/onsi/ginkgo/v2/ginkgo@v2.1.4 -r --randomize-all --randomize-suites --race --trace --fail-on-pending --keep-going --vet off --cover
- run: cat coverprofile.out | grep -v "zz_generated." > coverprofile.out.filtered
- uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: coverprofile.out
path-to-profile: coverprofile.out.filtered
42 changes: 21 additions & 21 deletions apis/config/v1alpha1/backend_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
parser "github.com/haproxytech/config-parser/v4"
"github.com/six-group/haproxy-operator/pkg/hash"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
)

// BackendSpec defines the desired state of Backend
Expand Down Expand Up @@ -75,13 +75,13 @@ func (b *Backend) Model() (models.Backend, error) {
}

if b.Spec.CheckTimeout != nil {
model.CheckTimeout = pointer.Int64(b.Spec.CheckTimeout.Milliseconds())
model.CheckTimeout = ptr.To(b.Spec.CheckTimeout.Milliseconds())
}

if b.Spec.Forwardfor != nil {
var enabled *string
if b.Spec.Forwardfor.Enabled {
enabled = pointer.String(models.ForwardforEnabledEnabled)
enabled = ptr.To(models.ForwardforEnabledEnabled)
}
model.Forwardfor = &models.Forwardfor{
Enabled: enabled,
Expand All @@ -97,7 +97,7 @@ func (b *Backend) Model() (models.Backend, error) {

if b.Spec.Redispatch != nil && *b.Spec.Redispatch {
model.Redispatch = &models.Redispatch{
Enabled: pointer.String(models.RedispatchEnabledEnabled),
Enabled: ptr.To(models.RedispatchEnabledEnabled),
Interval: 3,
}
}
Expand All @@ -111,24 +111,24 @@ func (b *Backend) Model() (models.Backend, error) {

if b.Spec.Balance != nil {
model.Balance = &models.Balance{
Algorithm: pointer.String(strings.ToLower(b.Spec.Balance.Algorithm)),
Algorithm: ptr.To(strings.ToLower(b.Spec.Balance.Algorithm)),
}
}

if b.Spec.Cookie != nil {
name := hash.GetMD5Hash(b.Spec.Cookie.Name)

model.Cookie = &models.Cookie{
Httponly: pointer.BoolDeref(b.Spec.Cookie.HTTPOnly, false),
Indirect: pointer.BoolDeref(b.Spec.Cookie.Indirect, false),
Httponly: ptr.Deref(b.Spec.Cookie.HTTPOnly, false),
Indirect: ptr.Deref(b.Spec.Cookie.Indirect, false),
Maxidle: b.Spec.Cookie.MaxIdle,
Maxlife: b.Spec.Cookie.MaxLife,
Name: pointer.String(name),
Nocache: pointer.BoolDeref(b.Spec.Cookie.NoCache, false),
Postonly: pointer.BoolDeref(b.Spec.Cookie.PostOnly, false),
Preserve: pointer.BoolDeref(b.Spec.Cookie.Preserve, false),
Secure: pointer.BoolDeref(b.Spec.Cookie.Secure, false),
Dynamic: pointer.BoolDeref(b.Spec.Cookie.Dynamic, false),
Name: ptr.To(name),
Nocache: ptr.Deref(b.Spec.Cookie.NoCache, false),
Postonly: ptr.Deref(b.Spec.Cookie.PostOnly, false),
Preserve: ptr.Deref(b.Spec.Cookie.Preserve, false),
Secure: ptr.Deref(b.Spec.Cookie.Secure, false),
Dynamic: ptr.Deref(b.Spec.Cookie.Dynamic, false),
}

for _, attr := range b.Spec.Cookie.Attribute {
Expand All @@ -154,27 +154,27 @@ func (b *Backend) Model() (models.Backend, error) {
return models.Backend{}, fmt.Errorf("you can only select one cookie mode")
}

if pointer.BoolDeref(b.Spec.Cookie.Dynamic, false) {
if ptr.Deref(b.Spec.Cookie.Dynamic, false) {
model.DynamicCookieKey = name
}
}

for name, timeout := range b.Spec.Timeouts {
switch name {
case "check":
model.CheckTimeout = pointer.Int64(timeout.Milliseconds())
model.CheckTimeout = ptr.To(timeout.Milliseconds())
case "connect":
model.ConnectTimeout = pointer.Int64(timeout.Milliseconds())
model.ConnectTimeout = ptr.To(timeout.Milliseconds())
case "http-keep-alive":
model.HTTPKeepAliveTimeout = pointer.Int64(timeout.Milliseconds())
model.HTTPKeepAliveTimeout = ptr.To(timeout.Milliseconds())
case "http-request":
model.HTTPRequestTimeout = pointer.Int64(timeout.Milliseconds())
model.HTTPRequestTimeout = ptr.To(timeout.Milliseconds())
case "queue":
model.QueueTimeout = pointer.Int64(timeout.Milliseconds())
model.QueueTimeout = ptr.To(timeout.Milliseconds())
case "server":
model.ServerTimeout = pointer.Int64(timeout.Milliseconds())
model.ServerTimeout = ptr.To(timeout.Milliseconds())
case "tunnel":
model.TunnelTimeout = pointer.Int64(timeout.Milliseconds())
model.TunnelTimeout = ptr.To(timeout.Milliseconds())
default:
return model, fmt.Errorf("timeout %s unknown", name)
}
Expand Down
161 changes: 145 additions & 16 deletions apis/config/v1alpha1/backend_types_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package v1alpha1_test

import (
"fmt"
"time"

"k8s.io/utils/ptr"

parser "github.com/haproxytech/config-parser/v4"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
configv1alpha1 "github.com/six-group/haproxy-operator/apis/config/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
)

var simpleBackend = `
Expand Down Expand Up @@ -61,7 +63,7 @@ var _ = Describe("Backend", Label("type"), func() {
backend := &configv1alpha1.Backend{
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Spec: configv1alpha1.BackendSpec{
Redispatch: pointer.Bool(true),
Redispatch: ptr.To(true),
},
}
Ω(backend.AddToParser(p)).ShouldNot(HaveOccurred())
Expand Down Expand Up @@ -100,7 +102,7 @@ var _ = Describe("Backend", Label("type"), func() {
},
Alpn: []string{"h2", "http/1.0"},
},
Weight: pointer.Int64(256),
Weight: ptr.To(int64(256)),
Check: &configv1alpha1.Check{
Enabled: true,
Inter: &metav1.Duration{Duration: 5 * time.Second},
Expand Down Expand Up @@ -144,7 +146,7 @@ var _ = Describe("Backend", Label("type"), func() {
ObjectMeta: metav1.ObjectMeta{Name: "openshift_default"},
Spec: configv1alpha1.BackendSpec{
BaseSpec: configv1alpha1.BaseSpec{
HTTPPretendKeepalive: pointer.Bool(true),
HTTPPretendKeepalive: ptr.To(true),
},
},
}
Expand Down Expand Up @@ -175,12 +177,12 @@ var _ = Describe("Backend", Label("type"), func() {
Mode: configv1alpha1.CookieMode{
Rewrite: true,
},
Indirect: pointer.Bool(true),
NoCache: pointer.Bool(true),
PostOnly: pointer.Bool(true),
Preserve: pointer.Bool(true),
HTTPOnly: pointer.Bool(true),
Secure: pointer.Bool(true),
Indirect: ptr.To(true),
NoCache: ptr.To(true),
PostOnly: ptr.To(true),
Preserve: ptr.To(true),
HTTPOnly: ptr.To(true),
Secure: ptr.To(true),
Domain: []string{
"domain1", ".openshift",
},
Expand All @@ -194,7 +196,7 @@ var _ = Describe("Backend", Label("type"), func() {
Ω(backend.AddToParser(p)).ShouldNot(HaveOccurred())
Ω(p.String()).Should(ContainSubstring("cookie 098f6bcd4621d373cade4e832627b4f6 domain domain1 domain .openshift attr SameSite=None httponly indirect maxidle 120 maxlife 45 nocache postonly preserve rewrite secure\n"))
})
It("an error should appear for selecting more than one cookie mode", func() {
It("should return an error for selecting more than one cookie mode", func() {
backend := &configv1alpha1.Backend{
ObjectMeta: metav1.ObjectMeta{Name: "set_cookie"},
Spec: configv1alpha1.BackendSpec{
Expand Down Expand Up @@ -227,7 +229,7 @@ var _ = Describe("Backend", Label("type"), func() {
},
Alpn: []string{"h2", "http/1.0"},
},
Weight: pointer.Int64(256),
Weight: ptr.To(int64(256)),
Check: &configv1alpha1.Check{
Enabled: true,
Inter: &metav1.Duration{Duration: 5 * time.Second},
Expand All @@ -254,7 +256,7 @@ var _ = Describe("Backend", Label("type"), func() {
ConditionType: "unless",
Condition: "has_www",
},
Code: pointer.Int64(301),
Code: ptr.To(int64(301)),
Type: configv1alpha1.RedirectType{
Location: true,
},
Expand All @@ -280,7 +282,7 @@ var _ = Describe("Backend", Label("type"), func() {
ConditionType: "unless",
Condition: "begins_with_api",
},
Code: pointer.Int64(301),
Code: ptr.To(int64(301)),
Type: configv1alpha1.RedirectType{
Prefix: true,
},
Expand Down Expand Up @@ -319,7 +321,7 @@ var _ = Describe("Backend", Label("type"), func() {
Ω(backend.AddToParser(p)).ShouldNot(HaveOccurred())
Ω(p.String()).Should(ContainSubstring("http-request redirect scheme https unless is_https\n"))
})
It("an error should appear for selecting more than one redirect type", func() {
It("should return an error for selecting more than one redirect type", func() {
backend := &configv1alpha1.Backend{
ObjectMeta: metav1.ObjectMeta{Name: "openshift_default"},
Spec: configv1alpha1.BackendSpec{
Expand Down Expand Up @@ -414,7 +416,7 @@ var _ = Describe("Backend", Label("type"), func() {
Ω(backend.AddToParser(p)).ShouldNot(HaveOccurred())
Ω(p.String()).Should(ContainSubstring("server server1 localhost:80 send-proxy-v2-ssl\n"))
})
It("an error should appear for selecting more than one proxy protocol", func() {
It("should return an error for selecting more than one proxy protocol", func() {
backend := &configv1alpha1.Backend{
ObjectMeta: metav1.ObjectMeta{Name: "openshift_default"},
Spec: configv1alpha1.BackendSpec{
Expand Down Expand Up @@ -533,5 +535,132 @@ var _ = Describe("Backend", Label("type"), func() {
}
Ω(backend.AddToParser(p)).Should(HaveOccurred())
})
It("should set timeouts", func() {
timeouts := map[string]metav1.Duration{
"check": {Duration: 5 * time.Second},
"connect": {Duration: 10 * time.Second},
"http-keep-alive": {Duration: 15 * time.Second},
"http-request": {Duration: 20 * time.Second},
"queue": {Duration: 25 * time.Second},
"server": {Duration: 30 * time.Second},
"tunnel": {Duration: 35 * time.Second},
}

backend := &configv1alpha1.Backend{
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Spec: configv1alpha1.BackendSpec{
BaseSpec: configv1alpha1.BaseSpec{
Timeouts: timeouts,
},
},
}
Ω(backend.AddToParser(p)).ShouldNot(HaveOccurred())

for name, duration := range timeouts {
Ω(p.String()).Should(ContainSubstring(fmt.Sprintf("timeout %s %d\n", name, duration.Duration.Milliseconds())))
}
})
It("should not set invalid timeouts", func() {
timeouts := map[string]metav1.Duration{
"client": {Duration: 5 * time.Second},
"tunnel-idle": {Duration: 10 * time.Second},
}

backend := &configv1alpha1.Backend{
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Spec: configv1alpha1.BackendSpec{
BaseSpec: configv1alpha1.BaseSpec{
Timeouts: timeouts,
},
},
}
Ω(backend.AddToParser(p)).Should(HaveOccurred())
})
It("should set server SSL config for server template", func() {
backend := &configv1alpha1.Backend{
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Spec: configv1alpha1.BackendSpec{
ServerTemplates: []configv1alpha1.ServerTemplate{
{
ServerParams: configv1alpha1.ServerParams{
SSL: &configv1alpha1.SSL{
Enabled: true,
MinVersion: "TLSv1.3",
Verify: "required",
CACertificate: &configv1alpha1.SSLCertificate{
Name: "my-ca",
},
Certificate: &configv1alpha1.SSLCertificate{
Name: "my-cert",
},
SNI: "test.svc.cluster.local",
Alpn: []string{"h2", "http/1.1"},
},
},
FQDN: "test.com",
Port: 9443,
Prefix: "test_",
},
},
},
}
Ω(backend.AddToParser(p)).ShouldNot(HaveOccurred())
Ω(p.String()).Should(ContainSubstring("server-template test_ 0 test.com:9443 ssl ca-file /usr/local/etc/haproxy/my-ca.crt crt /usr/local/etc/haproxy/my-cert.crt sni test.svc.cluster.local ssl-min-ver TLSv1.3 verify required\n"))
})
It("should set sendProxy with proxy protocol v2 and options for server templates", func() {
backend := &configv1alpha1.Backend{
ObjectMeta: metav1.ObjectMeta{Name: "openshift_default"},
Spec: configv1alpha1.BackendSpec{
ServerTemplates: []configv1alpha1.ServerTemplate{
{
ServerParams: configv1alpha1.ServerParams{
SendProxyV2: &configv1alpha1.ProxyProtocol{
V2: &configv1alpha1.ProxyProtocolV2{
Enabled: true,
Options: &configv1alpha1.ProxyProtocolV2Options{
CertCn: true,
CertSig: true,
UniqueID: true,
Ssl: true,
},
},
},
},
FQDN: "test.com",
Port: 9443,
Prefix: "test_",
},
},
},
}
Ω(backend.AddToParser(p)).ShouldNot(HaveOccurred())
Ω(p.String()).Should(ContainSubstring("server-template test_ 0 test.com:9443 send-proxy-v2 proxy-v2-options ssl,cert-cn,cert-sig,unique-id\n"))
})
It("should set tcp request rule", func() {
backend := &configv1alpha1.Backend{
ObjectMeta: metav1.ObjectMeta{Name: "openshift_default"},
Spec: configv1alpha1.BackendSpec{
BaseSpec: configv1alpha1.BaseSpec{
TCPRequest: []configv1alpha1.TCPRequestRule{
{
Type: "inspect-delay",
Timeout: ptr.To(metav1.Duration{Duration: 5 * time.Second}),
},
{
Type: "content",
Action: ptr.To("accept"),
Rule: configv1alpha1.Rule{
ConditionType: "if",
Condition: "{ req_ssl_hello_type 1 }",
},
},
},
},
},
}
Ω(backend.AddToParser(p)).ShouldNot(HaveOccurred())
Ω(p.String()).Should(ContainSubstring("tcp-request inspect-delay 5000\n"))
Ω(p.String()).Should(ContainSubstring("tcp-request content accept if { req_ssl_hello_type 1 }\n"))
})
})
})
Loading

0 comments on commit ed3b047

Please sign in to comment.