@@ -19,12 +19,14 @@ package v1alpha4
1919import (
2020 "context"
2121 "testing"
22+ "time"
2223
2324 . "github.com/onsi/gomega"
2425
2526 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627 clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
2728 utildefaulting "sigs.k8s.io/cluster-api/util/defaulting"
29+ "sigs.k8s.io/controller-runtime/pkg/client"
2830)
2931
3032func TestAWSClusterDefault (t * testing.T ) {
@@ -36,12 +38,51 @@ func TestAWSClusterDefault(t *testing.T) {
3638}
3739
3840func TestAWSCluster_ValidateCreate (t * testing.T ) {
41+ unsupportedIncorrectScheme := ClassicELBScheme ("any-other-scheme" )
42+
3943 tests := []struct {
4044 name string
4145 cluster * AWSCluster
4246 wantErr bool
47+ expect func (t * testing.T , res * AWSLoadBalancerSpec )
4348 }{
4449 // The SSHKeyName tests were moved to sshkeyname_test.go
50+
51+ {
52+ name : "Default nil scheme to `internet-facing`" ,
53+ cluster : & AWSCluster {
54+ Spec : AWSClusterSpec {},
55+ },
56+ expect : func (t * testing.T , res * AWSLoadBalancerSpec ) {
57+ if res .Scheme .String () != ClassicELBSchemeInternetFacing .String () {
58+ t .Error ("Expected internet-facing defaulting for nil loadbalancer schemes" )
59+ }
60+ },
61+ wantErr : false ,
62+ },
63+ {
64+ name : "Internet-facing ELB scheme is defaulted to internet-facing during creation" ,
65+ cluster : & AWSCluster {
66+ Spec : AWSClusterSpec {
67+ ControlPlaneLoadBalancer : & AWSLoadBalancerSpec {Scheme : & ClassicELBSchemeIncorrectInternetFacing },
68+ },
69+ },
70+ expect : func (t * testing.T , res * AWSLoadBalancerSpec ) {
71+ if res .Scheme .String () != ClassicELBSchemeInternetFacing .String () {
72+ t .Error ("Expected internet-facing defaulting for supported incorrect scheme: Internet-facing" )
73+ }
74+ },
75+ wantErr : false ,
76+ },
77+ {
78+ name : "Supported schemes are 'internet-facing, Internet-facing, internal, or nil', rest will be rejected" ,
79+ cluster : & AWSCluster {
80+ Spec : AWSClusterSpec {
81+ ControlPlaneLoadBalancer : & AWSLoadBalancerSpec {Scheme : & unsupportedIncorrectScheme },
82+ },
83+ },
84+ wantErr : true ,
85+ },
4586 }
4687 for _ , tt := range tests {
4788 t .Run (tt .name , func (t * testing.T ) {
@@ -54,6 +95,24 @@ func TestAWSCluster_ValidateCreate(t *testing.T) {
5495 if err := testEnv .Create (ctx , cluster ); (err != nil ) != tt .wantErr {
5596 t .Errorf ("ValidateCreate() error = %v, wantErr %v" , err , tt .wantErr )
5697 }
98+
99+ if tt .wantErr {
100+ return
101+ }
102+
103+ c := & AWSCluster {}
104+ key := client.ObjectKey {
105+ Name : cluster .Name ,
106+ Namespace : "default" ,
107+ }
108+
109+ g := NewWithT (t )
110+ g .Eventually (func () bool {
111+ err := testEnv .Get (ctx , key , c )
112+ return err == nil
113+ }, 10 * time .Second ).Should (Equal (true ))
114+
115+ tt .expect (t , c .Spec .ControlPlaneLoadBalancer )
57116 })
58117 }
59118}
0 commit comments