Skip to content

Commit

Permalink
Add unit tests to verify update of Infra CR with Ingress LB IPs
Browse files Browse the repository at this point in the history
Add tests for the AWS Platform where the Ingress LB's Hostname is
available but the Infra CR needs to be updated with its IP.
  • Loading branch information
sadasu committed Nov 22, 2024
1 parent 4b1cc71 commit a8096c0
Showing 1 changed file with 86 additions and 13 deletions.
99 changes: 86 additions & 13 deletions pkg/operator/controller/ingress/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"golang.org/x/exp/slices"

configv1 "github.com/openshift/api/config/v1"
operatorv1 "github.com/openshift/api/operator/v1"
Expand Down Expand Up @@ -1617,9 +1616,34 @@ func Test_IsProxyProtocolNeeded(t *testing.T) {

func Test_computeUpdatedInfraFromService(t *testing.T) {
var (
IngressLBIP = configv1.IP("196.78.125.4")
awsPlatform = configv1.PlatformStatus{
Type: configv1.AWSPlatformType,
}
awsPlatformWithDNSType = configv1.PlatformStatus{
Type: configv1.AWSPlatformType,
AWS: &configv1.AWSPlatformStatus{
CloudLoadBalancerConfig: &configv1.CloudLoadBalancerConfig{
DNSType: configv1.ClusterHostedDNSType,
},
},
}
awsPlatformWithLBIP = configv1.PlatformStatus{
Type: configv1.AWSPlatformType,
AWS: &configv1.AWSPlatformStatus{
CloudLoadBalancerConfig: &configv1.CloudLoadBalancerConfig{
DNSType: configv1.ClusterHostedDNSType,
ClusterHosted: &configv1.CloudLoadBalancerIPs{
IngressLoadBalancerIPs: []configv1.IP{
IngressLBIP,
},
},
},
},
}
azurePlatform = configv1.PlatformStatus{
Type: configv1.AzurePlatformType,
}
gcpPlatform = configv1.PlatformStatus{
Type: configv1.GCPPlatformType,
}
Expand All @@ -1631,10 +1655,6 @@ func Test_computeUpdatedInfraFromService(t *testing.T) {
},
},
}
ingresses = []corev1.LoadBalancerIngress{
{IP: "196.78.125.4"},
}
IngressLBIP = configv1.IP("196.78.125.4")
gcpPlatformWithLBIP = configv1.PlatformStatus{
Type: configv1.GCPPlatformType,
GCP: &configv1.GCPPlatformStatus{
Expand All @@ -1648,16 +1668,26 @@ func Test_computeUpdatedInfraFromService(t *testing.T) {
},
},
}
ingresses = []corev1.LoadBalancerIngress{
{IP: "196.78.125.4"},
}
ingressesWithMultipleIPs = []corev1.LoadBalancerIngress{
{IP: "196.78.125.4"},
{IP: "10.10.10.4"},
}
// Hostname is intentionally assigned an IP address for unit testing purposes since net.LookupIP simply returns the provided IP.
awsIngresses = []corev1.LoadBalancerIngress{
{Hostname: "196.78.125.4"},
}
awsUpdatedIngresses = []corev1.LoadBalancerIngress{
{Hostname: "10.10.10.4"},
}
)
testCases := []struct {
description string
platform *configv1.PlatformStatus
ingresses []corev1.LoadBalancerIngress
expectedInfra configv1.Infrastructure
expectedLBIPs []configv1.IP
expectUpdated bool
expectError bool
}{
Expand All @@ -1670,7 +1700,7 @@ func Test_computeUpdatedInfraFromService(t *testing.T) {
},
{
description: "unsupported platform should not cause an error",
platform: &awsPlatform,
platform: &azurePlatform,
ingresses: []corev1.LoadBalancerIngress{},
expectUpdated: false,
expectError: false,
Expand All @@ -1693,20 +1723,61 @@ func Test_computeUpdatedInfraFromService(t *testing.T) {
description: "gcp platform with DNSType and no LB IP in infra config, service has 1 IP",
platform: &gcpPlatformWithDNSType,
ingresses: ingresses,
expectedLBIPs: []configv1.IP{IngressLBIP},
expectUpdated: true,
expectError: false,
},
{
description: "gcp platform with no change to LB IPs",
platform: &gcpPlatformWithLBIP,
ingresses: ingresses,
expectedLBIPs: []configv1.IP{IngressLBIP},
expectUpdated: false,
expectError: false,
},
{
description: "gcp platform with DNSType and LB IP",
platform: &gcpPlatformWithLBIP,
ingresses: ingressesWithMultipleIPs,
expectedLBIPs: []configv1.IP{IngressLBIP, configv1.IP("10.10.10.4")},
expectUpdated: true,
expectError: false,
},
{
description: "aws platform without DNSType set",
platform: &awsPlatform,
ingresses: []corev1.LoadBalancerIngress{},
expectUpdated: false,
expectError: false,
},
{
description: "aws platform with DNSType and no LB IP",
platform: &awsPlatformWithDNSType,
ingresses: []corev1.LoadBalancerIngress{},
expectUpdated: false,
expectError: false,
},
{
description: "aws platform with DNSType and no LB IP in infra config, service has 1 IP",
platform: &awsPlatformWithDNSType,
ingresses: awsIngresses,
expectedLBIPs: []configv1.IP{IngressLBIP},
expectUpdated: true,
expectError: false,
},
{
description: "aws platform with no change to LB IPs",
platform: &awsPlatformWithLBIP,
ingresses: awsIngresses,
expectedLBIPs: []configv1.IP{IngressLBIP},
expectUpdated: false,
expectError: false,
},
{
description: "aws platform with 1 LB IP with change in hostname",
platform: &awsPlatformWithLBIP,
ingresses: awsUpdatedIngresses,
expectedLBIPs: []configv1.IP{configv1.IP("10.10.10.4")},
expectUpdated: true,
expectError: false,
},
Expand All @@ -1733,12 +1804,14 @@ func Test_computeUpdatedInfraFromService(t *testing.T) {
t.Errorf("expected %t, got %t", tc.expectUpdated, updated)
}
if updated {
ingressLBs := service.Status.LoadBalancer.Ingress
for _, ingress := range ingressLBs {
if len(ingress.IP) > 0 {
if !slices.Contains(infraConfig.Status.PlatformStatus.GCP.CloudLoadBalancerConfig.ClusterHosted.IngressLoadBalancerIPs, configv1.IP(ingress.IP)) {
t.Errorf("expected Infra CR to contain %s", ingress.IP)
}
switch tc.platform.Type {
case configv1.AWSPlatformType:
if !reflect.DeepEqual(infraConfig.Status.PlatformStatus.AWS.CloudLoadBalancerConfig.ClusterHosted.IngressLoadBalancerIPs, tc.expectedLBIPs) {
t.Errorf("expected Infra CR to contain %s but found %s", tc.expectedLBIPs, infraConfig.Status.PlatformStatus.AWS.CloudLoadBalancerConfig.ClusterHosted.IngressLoadBalancerIPs)
}
case configv1.GCPPlatformType:
if !reflect.DeepEqual(infraConfig.Status.PlatformStatus.GCP.CloudLoadBalancerConfig.ClusterHosted.IngressLoadBalancerIPs, tc.expectedLBIPs) {
t.Errorf("expected Infra CR to contain %s but found %s", tc.expectedLBIPs, infraConfig.Status.PlatformStatus.GCP.CloudLoadBalancerConfig.ClusterHosted.IngressLoadBalancerIPs)
}
}
}
Expand Down

0 comments on commit a8096c0

Please sign in to comment.