Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
norbjd committed Jul 14, 2023
1 parent 8d93436 commit d809ab2
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 8 deletions.
8 changes: 4 additions & 4 deletions pkg/config/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ const (
enableCryptoMB = "enable-cryptomb"

tracingPrefix = "tracing"
tracingEnabled = tracingPrefix + "-enabled"
tracingCollectorHost = tracingPrefix + "-collector-host"
tracingCollectorPort = tracingPrefix + "-collector-port"
tracingCollectorEndpoint = tracingPrefix + "-collector-endpoint"
TracingEnabled = tracingPrefix + "-enabled"
TracingCollectorHost = tracingPrefix + "-collector-host"
TracingCollectorPort = tracingPrefix + "-collector-port"
TracingCollectorEndpoint = tracingPrefix + "-collector-endpoint"
)

func DefaultConfig() *Kourier {
Expand Down
8 changes: 4 additions & 4 deletions pkg/config/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ func TestKourierConfig(t *testing.T) {
},
},
data: map[string]string{
tracingEnabled: "true",
tracingCollectorHost: "jaeger.default.svc.cluster.local",
tracingCollectorPort: "9411",
tracingCollectorEndpoint: "/api/v2/spans",
TracingEnabled: "true",
TracingCollectorHost: "jaeger.default.svc.cluster.local",
TracingCollectorPort: "9411",
TracingCollectorEndpoint: "/api/v2/spans",
},
}}

Expand Down
109 changes: 109 additions & 0 deletions pkg/generator/caches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@ package generator
import (
"context"
"sort"
"strconv"
"testing"

v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
listener "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
tracev3 "github.com/envoyproxy/go-control-plane/envoy/config/trace/v3"
http_connection_managerv3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
"github.com/envoyproxy/go-control-plane/pkg/resource/v3"
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/wrapperspb"
"gotest.tools/v3/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -298,6 +307,106 @@ func TestTLSListenerWithInternalCertSecret(t *testing.T) {
})
}

// TestListenersAndClustersWithTracing verifies that when we enable tracing
// a cluster is added for the tracing backend, and tracing configuration is added to all listeners.
func TestListenersAndClustersWithTracing(t *testing.T) {
tracingCollectorHost := "jaeger.default.svc.cluster.local"
tracingCollectorPort := 9411
tracingCollectorEndpoint := "/api/v2/spans"

kourierCfg, err := config.NewConfigFromMap(map[string]string{
config.TracingEnabled: "true",
config.TracingCollectorHost: tracingCollectorHost,
config.TracingCollectorPort: strconv.Itoa(tracingCollectorPort),
config.TracingCollectorEndpoint: tracingCollectorEndpoint,
})
assert.NilError(t, err)

testConfig := &rconfig.Config{
Kourier: kourierCfg,
}

kubeClient := fake.Clientset{}
cfg := testConfig.DeepCopy()
ctx := (&testConfigStore{config: cfg}).ToContext(context.Background())

caches, err := NewCaches(ctx, &kubeClient, false)
assert.NilError(t, err)

t.Run("check a tracing cluster exist, and tracing is configured on listeners", func(t *testing.T) {
translatedIngress := &translatedIngress{}
err := caches.addTranslatedIngress(translatedIngress)
assert.NilError(t, err)

snapshot, err := caches.ToEnvoySnapshot(ctx)
assert.NilError(t, err)

tracingCollectorCluster := snapshot.GetResources(resource.ClusterType)["tracing-collector"].(*v3.Cluster)

expectedTracingCollectorCluster := &v3.Cluster{
Name: "tracing-collector",
ClusterDiscoveryType: &v3.Cluster_Type{Type: v3.Cluster_STRICT_DNS},
LoadAssignment: &endpoint.ClusterLoadAssignment{
ClusterName: "tracing-collector",
Endpoints: []*endpoint.LocalityLbEndpoints{{
LbEndpoints: []*endpoint.LbEndpoint{{
HostIdentifier: &endpoint.LbEndpoint_Endpoint{
Endpoint: &endpoint.Endpoint{
Address: &core.Address{
Address: &core.Address_SocketAddress{
SocketAddress: &core.SocketAddress{
Protocol: core.SocketAddress_TCP,
Address: tracingCollectorHost,
PortSpecifier: &core.SocketAddress_PortValue{
PortValue: uint32(tracingCollectorPort),
},
Ipv4Compat: true,
},
},
},
},
},
}},
}},
},
}

assert.DeepEqual(t, expectedTracingCollectorCluster, tracingCollectorCluster,
cmpopts.IgnoreUnexported(
v3.Cluster{}, endpoint.ClusterLoadAssignment{}, endpoint.LocalityLbEndpoints{}, endpoint.LbEndpoint{},
endpoint.Endpoint{}, core.Address{}, core.SocketAddress{},
),
)

listenersPorts := []uint32{
config.HTTPPortExternal, config.HTTPPortInternal, config.HTTPPortProb,
}

for _, listenerPort := range listenersPorts {
currentListener := snapshot.GetResources(resource.ListenerType)[envoy.CreateListenerName(listenerPort)].(*listener.Listener)

expectedTracingConfig := &tracev3.ZipkinConfig{
CollectorCluster: "tracing-collector",
CollectorEndpoint: tracingCollectorEndpoint,
SharedSpanContext: wrapperspb.Bool(false),
CollectorEndpointVersion: tracev3.ZipkinConfig_HTTP_JSON,
}

httpConnectionManagerFilter := currentListener.FilterChains[0].Filters[0]
assert.Equal(t, wellknown.HTTPConnectionManager, httpConnectionManagerFilter.Name)

httpConnectionManagerConfig := &http_connection_managerv3.HttpConnectionManager{}
err = anypb.UnmarshalTo(httpConnectionManagerFilter.GetTypedConfig(), httpConnectionManagerConfig, proto.UnmarshalOptions{})
assert.NilError(t, err)

tracingConfig := &tracev3.ZipkinConfig{}
err = anypb.UnmarshalTo(httpConnectionManagerConfig.Tracing.Provider.GetTypedConfig(), tracingConfig, proto.UnmarshalOptions{})
assert.NilError(t, err)
assert.DeepEqual(t, expectedTracingConfig, tracingConfig, cmpopts.IgnoreUnexported(tracev3.ZipkinConfig{}, wrapperspb.BoolValue{}))
}
})
}

// Creates an ingress translation and listeners from the given names an
// associates them with the ingress name/namespace received.
func createTestDataForIngress(
Expand Down

0 comments on commit d809ab2

Please sign in to comment.