Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit test updates #79

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.0

require (
github.com/google/go-cmp v0.6.0
github.com/lithammer/dedent v1.1.0
github.com/pkg/errors v0.9.1
k8s.io/api v0.30.0
k8s.io/apimachinery v0.30.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lithammer/dedent v1.1.0 h1:VNzHMVCBNG1j0fh3OrsFRkVUwStdDArbgBWoPAffktY=
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
Expand Down
258 changes: 252 additions & 6 deletions pkg/loadbalancer/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/lithammer/dedent"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"
)

func makeNode(name string, ip string) *v1.Node {
Expand Down Expand Up @@ -218,6 +220,47 @@ func Test_generateConfig(t *testing.T) {
},
},
},
{
name: "session affinity",
service: &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Spec: v1.ServiceSpec{
Type: v1.ServiceTypeLoadBalancer,
ExternalTrafficPolicy: v1.ServiceExternalTrafficPolicyCluster,
IPFamilies: []v1.IPFamily{v1.IPv4Protocol},
Ports: []v1.ServicePort{
{
Port: 80,
TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 8080},
NodePort: 30000,
Protocol: v1.ProtocolTCP,
},
},
SessionAffinity: v1.ServiceAffinityClientIP,
SessionAffinityConfig: &v1.SessionAffinityConfig{
ClientIP: &v1.ClientIPConfig{
// FIXME: This is currently ignored
TimeoutSeconds: ptr.To[int32](60),
},
},
},
},
nodes: []*v1.Node{
makeNode("a", "10.0.0.1"),
},
want: &proxyConfigData{
HealthCheckPort: 10256,
ServicePorts: map[string]servicePort{
"IPv4_80_TCP": servicePort{
Listener: endpoint{Address: "0.0.0.0", Port: 80, Protocol: string(v1.ProtocolTCP)},
Cluster: []endpoint{{"10.0.0.1", 30000, string(v1.ProtocolTCP)}},
},
},
SessionAffinity: "ClientIP",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -230,7 +273,6 @@ func Test_generateConfig(t *testing.T) {
}

func Test_proxyConfig(t *testing.T) {
t.Skip("TODO left for someone with more patience to deal with the indentation issues")
tests := []struct {
name string
template string
Expand All @@ -254,11 +296,86 @@ func Test_proxyConfig(t *testing.T) {
},
},
wantConfig: `
`,
resources:
- "@type": type.googleapis.com/envoy.config.cluster.v3.Cluster
name: cluster_IPv4_443
connect_timeout: 5s
type: STATIC
lb_policy: RANDOM
health_checks:
- timeout: 5s
interval: 3s
unhealthy_threshold: 2
healthy_threshold: 1
no_traffic_interval: 5s
always_log_health_check_failures: true
always_log_health_check_success: true
event_log_path: /dev/stdout
http_health_check:
path: /healthz
load_assignment:
cluster_name: cluster_IPv4_443
endpoints:
- lb_endpoints:
- endpoint:
health_check_config:
port_value: 32764
address:
socket_address:
address: 192.168.8.2
port_value: 31497
protocol: TCP
- lb_endpoints:
- endpoint:
health_check_config:
port_value: 32764
address:
socket_address:
address: 192.168.8.3
port_value: 31497
protocol: TCP
- "@type": type.googleapis.com/envoy.config.cluster.v3.Cluster
name: cluster_IPv4_80
connect_timeout: 5s
type: STATIC
lb_policy: RANDOM
health_checks:
- timeout: 5s
interval: 3s
unhealthy_threshold: 2
healthy_threshold: 1
no_traffic_interval: 5s
always_log_health_check_failures: true
always_log_health_check_success: true
event_log_path: /dev/stdout
http_health_check:
path: /healthz
load_assignment:
cluster_name: cluster_IPv4_80
endpoints:
- lb_endpoints:
- endpoint:
health_check_config:
port_value: 32764
address:
socket_address:
address: 192.168.8.2
port_value: 30497
protocol: TCP
- lb_endpoints:
- endpoint:
health_check_config:
port_value: 32764
address:
socket_address:
address: 192.168.8.3
port_value: 30497
protocol: TCP
`,
},
{
name: "ipv4 LDS",
template: proxyCDSConfigTemplate,
template: proxyLDSConfigTemplate,
data: &proxyConfigData{
HealthCheckPort: 32764,
ServicePorts: map[string]servicePort{
Expand All @@ -273,7 +390,135 @@ func Test_proxyConfig(t *testing.T) {
},
},
wantConfig: `
`,
resources:
- "@type": type.googleapis.com/envoy.config.listener.v3.Listener
name: listener_IPv4_443
address:
socket_address:
address: 0.0.0.0
port_value: 443
protocol: TCP
filter_chains:
- filters:
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
access_log:
- name: envoy.file_access_log
typed_config:
"@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
stat_prefix: tcp_proxy
cluster: cluster_IPv4_443
- "@type": type.googleapis.com/envoy.config.listener.v3.Listener
name: listener_IPv4_80
address:
socket_address:
address: 0.0.0.0
port_value: 80
protocol: TCP
filter_chains:
- filters:
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
access_log:
- name: envoy.file_access_log
typed_config:
"@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
stat_prefix: tcp_proxy
cluster: cluster_IPv4_80
`,
},
{
name: "ipv4 CDS with affinity",
template: proxyCDSConfigTemplate,
data: &proxyConfigData{
HealthCheckPort: 32764,
ServicePorts: map[string]servicePort{
"IPv4_80": servicePort{
Listener: endpoint{Address: "0.0.0.0", Port: 80, Protocol: string(v1.ProtocolTCP)},
Cluster: []endpoint{{"192.168.8.2", 30497, string(v1.ProtocolTCP)}, {"192.168.8.3", 30497, string(v1.ProtocolTCP)}},
},
},
SessionAffinity: "ClientIP",
},
wantConfig: `
resources:
- "@type": type.googleapis.com/envoy.config.cluster.v3.Cluster
name: cluster_IPv4_80
connect_timeout: 5s
type: STATIC
lb_policy: RING_HASH
health_checks:
- timeout: 5s
interval: 3s
unhealthy_threshold: 2
healthy_threshold: 1
no_traffic_interval: 5s
always_log_health_check_failures: true
always_log_health_check_success: true
event_log_path: /dev/stdout
http_health_check:
path: /healthz
load_assignment:
cluster_name: cluster_IPv4_80
endpoints:
- lb_endpoints:
- endpoint:
health_check_config:
port_value: 32764
address:
socket_address:
address: 192.168.8.2
port_value: 30497
protocol: TCP
- lb_endpoints:
- endpoint:
health_check_config:
port_value: 32764
address:
socket_address:
address: 192.168.8.3
port_value: 30497
protocol: TCP
`,
},
{
name: "ipv4 LDS with affinity",
template: proxyLDSConfigTemplate,
data: &proxyConfigData{
HealthCheckPort: 32764,
ServicePorts: map[string]servicePort{
"IPv4_80": servicePort{
Listener: endpoint{Address: "0.0.0.0", Port: 80, Protocol: string(v1.ProtocolTCP)},
Cluster: []endpoint{{"192.168.8.2", 30497, string(v1.ProtocolTCP)}, {"192.168.8.3", 30497, string(v1.ProtocolTCP)}},
},
},
SessionAffinity: "ClientIP",
},
wantConfig: `
resources:
- "@type": type.googleapis.com/envoy.config.listener.v3.Listener
name: listener_IPv4_80
address:
socket_address:
address: 0.0.0.0
port_value: 80
protocol: TCP
filter_chains:
- filters:
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
access_log:
- name: envoy.file_access_log
typed_config:
"@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
stat_prefix: tcp_proxy
cluster: cluster_IPv4_80
hash_policy:
source_ip: {}
`,
},
}
for _, tt := range tests {
Expand All @@ -283,9 +528,10 @@ func Test_proxyConfig(t *testing.T) {
t.Errorf("proxyConfig() error = %v", err)
return
}
if gotConfig != tt.wantConfig {
wantConfig := dedent.Dedent(tt.wantConfig)
if gotConfig != wantConfig {
t.Logf("%s", gotConfig)
t.Errorf("proxyConfig() not expected\n%v", cmp.Diff(gotConfig, tt.wantConfig))
t.Errorf("proxyConfig() not expected\n%v", cmp.Diff(gotConfig, wantConfig))
}
})
}
Expand Down
11 changes: 11 additions & 0 deletions vendor/github.com/lithammer/dedent/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/lithammer/dedent/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading