Skip to content

Commit

Permalink
add tests and fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
romain-pfund authored and squakez committed Jun 18, 2024
1 parent b0bf3ec commit 39bdbd1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
9 changes: 4 additions & 5 deletions pkg/trait/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ package trait
import (
"errors"
"fmt"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
"strings"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
)

const (
Expand Down Expand Up @@ -128,7 +127,7 @@ func (t *ingressTrait) Apply(e *Environment) error {
},
}

if len(t.TLSHosts) > 0 || len(strings.TrimSpace(t.TLSSecretName)) > 0 {
if len(t.TLSHosts) > 0 && t.TLSSecretName != "" {
ingress.Spec.TLS = []networkingv1.IngressTLS{
{
Hosts: t.TLSHosts,
Expand Down
59 changes: 59 additions & 0 deletions pkg/trait/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,66 @@ func TestConfigureTLSIngressTraitWDoesSucceed(t *testing.T) {
conditions := environment.Integration.Status.Conditions
assert.Len(t, conditions, 1)
assert.Equal(t, "service-name(hostname) -> service-name(http)", conditions[0].Message)
}

func TestConfigureTLSWithoutHostsIngressTraitWDoesSucceed(t *testing.T) {
ingressTrait, environment := createNominalIngressTest()
ingressTrait.TLSSecretName = "nginxWildcard"

err := ingressTrait.Apply(environment)

require.NoError(t, err)
assert.Len(t, environment.Integration.Status.Conditions, 1)

assert.Len(t, environment.Resources.Items(), 2)
environment.Resources.Visit(func(resource runtime.Object) {
if ingress, ok := resource.(*networkingv1.Ingress); ok {
assert.Equal(t, "service-name", ingress.Name)
assert.Equal(t, "namespace", ingress.Namespace)
assert.Len(t, ingress.Spec.Rules, 1)
assert.Equal(t, "hostname", ingress.Spec.Rules[0].Host)
assert.Len(t, ingress.Spec.Rules[0].HTTP.Paths, 1)
assert.Equal(t, "service-name", ingress.Spec.Rules[0].HTTP.Paths[0].Backend.Service.Name)
assert.Equal(t, "/", ingress.Spec.Rules[0].HTTP.Paths[0].Path)
assert.NotNil(t, *ingress.Spec.Rules[0].HTTP.Paths[0].PathType)
assert.Equal(t, networkingv1.PathTypePrefix, *ingress.Spec.Rules[0].HTTP.Paths[0].PathType)
assert.Nil(t, ingress.Spec.TLS)
}
})

conditions := environment.Integration.Status.Conditions
assert.Len(t, conditions, 1)
assert.Equal(t, "service-name(hostname) -> service-name(http)", conditions[0].Message)
}

func TestConfigureTLSWithoutSecretNameIngressTraitWDoesSucceed(t *testing.T) {
ingressTrait, environment := createNominalIngressTest()
ingressTrait.TLSHosts = []string{"host1.com", "host2.com"}

err := ingressTrait.Apply(environment)

require.NoError(t, err)
assert.Len(t, environment.Integration.Status.Conditions, 1)

assert.Len(t, environment.Resources.Items(), 2)
environment.Resources.Visit(func(resource runtime.Object) {
if ingress, ok := resource.(*networkingv1.Ingress); ok {
assert.Equal(t, "service-name", ingress.Name)
assert.Equal(t, "namespace", ingress.Namespace)
assert.Len(t, ingress.Spec.Rules, 1)
assert.Equal(t, "hostname", ingress.Spec.Rules[0].Host)
assert.Len(t, ingress.Spec.Rules[0].HTTP.Paths, 1)
assert.Equal(t, "service-name", ingress.Spec.Rules[0].HTTP.Paths[0].Backend.Service.Name)
assert.Equal(t, "/", ingress.Spec.Rules[0].HTTP.Paths[0].Path)
assert.NotNil(t, *ingress.Spec.Rules[0].HTTP.Paths[0].PathType)
assert.Equal(t, networkingv1.PathTypePrefix, *ingress.Spec.Rules[0].HTTP.Paths[0].PathType)
assert.Nil(t, ingress.Spec.TLS)
}
})

conditions := environment.Integration.Status.Conditions
assert.Len(t, conditions, 1)
assert.Equal(t, "service-name(hostname) -> service-name(http)", conditions[0].Message)
}

func createNominalIngressTest() (*ingressTrait, *Environment) {
Expand Down

0 comments on commit 39bdbd1

Please sign in to comment.