sigs.k8s.io/cluster-api@v1.7.1/exp/ipam/internal/webhooks/ipaddressclaim_test.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package webhooks
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	. "github.com/onsi/gomega"
    24  	corev1 "k8s.io/api/core/v1"
    25  	"k8s.io/utils/ptr"
    26  
    27  	ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1beta1"
    28  )
    29  
    30  func TestIPAddressClaimValidateCreate(t *testing.T) {
    31  	getClaim := func(fn func(addr *ipamv1.IPAddressClaim)) ipamv1.IPAddressClaim {
    32  		claim := ipamv1.IPAddressClaim{
    33  			Spec: ipamv1.IPAddressClaimSpec{
    34  				PoolRef: corev1.TypedLocalObjectReference{
    35  					Name:     "identical",
    36  					Kind:     "TestPool",
    37  					APIGroup: ptr.To("ipam.cluster.x-k8s.io"),
    38  				},
    39  			},
    40  		}
    41  		fn(&claim)
    42  		return claim
    43  	}
    44  
    45  	tests := []struct {
    46  		name      string
    47  		claim     ipamv1.IPAddressClaim
    48  		expectErr bool
    49  	}{
    50  		{
    51  			name:      "should accept a valid claim",
    52  			claim:     getClaim(func(*ipamv1.IPAddressClaim) {}),
    53  			expectErr: false,
    54  		},
    55  		{
    56  			name: "should reject a pool reference without a group",
    57  			claim: getClaim(func(addr *ipamv1.IPAddressClaim) {
    58  				addr.Spec.PoolRef.APIGroup = nil
    59  			}),
    60  			expectErr: true,
    61  		},
    62  	}
    63  
    64  	for i := range tests {
    65  		tt := tests[i]
    66  		t.Run(tt.name, func(t *testing.T) {
    67  			g := NewWithT(t)
    68  			wh := IPAddressClaim{}
    69  			warnings, err := wh.ValidateCreate(context.Background(), &tt.claim)
    70  			if tt.expectErr {
    71  				g.Expect(err).To(HaveOccurred())
    72  			} else {
    73  				g.Expect(err).ToNot(HaveOccurred())
    74  			}
    75  			g.Expect(warnings).To(BeEmpty())
    76  		})
    77  	}
    78  }
    79  
    80  func TestIPAddressClaimValidateUpdate(t *testing.T) {
    81  	getClaim := func(fn func(addr *ipamv1.IPAddressClaim)) ipamv1.IPAddressClaim {
    82  		claim := ipamv1.IPAddressClaim{
    83  			Spec: ipamv1.IPAddressClaimSpec{
    84  				PoolRef: corev1.TypedLocalObjectReference{
    85  					Name: "identical",
    86  				},
    87  			},
    88  		}
    89  		fn(&claim)
    90  		return claim
    91  	}
    92  
    93  	tests := []struct {
    94  		name      string
    95  		oldClaim  ipamv1.IPAddressClaim
    96  		newClaim  ipamv1.IPAddressClaim
    97  		expectErr bool
    98  	}{
    99  		{
   100  			name:      "should accept objects with identical spec",
   101  			oldClaim:  getClaim(func(*ipamv1.IPAddressClaim) {}),
   102  			newClaim:  getClaim(func(*ipamv1.IPAddressClaim) {}),
   103  			expectErr: false,
   104  		},
   105  		{
   106  			name:     "should reject objects with different spec",
   107  			oldClaim: getClaim(func(*ipamv1.IPAddressClaim) {}),
   108  			newClaim: getClaim(func(addr *ipamv1.IPAddressClaim) {
   109  				addr.Spec.PoolRef.Name = "different"
   110  			}),
   111  			expectErr: true,
   112  		},
   113  	}
   114  
   115  	for i := range tests {
   116  		tt := tests[i]
   117  		t.Run(tt.name, func(t *testing.T) {
   118  			g := NewWithT(t)
   119  			wh := IPAddressClaim{}
   120  			warnings, err := wh.ValidateUpdate(context.Background(), &tt.oldClaim, &tt.newClaim)
   121  			if tt.expectErr {
   122  				g.Expect(err).To(HaveOccurred())
   123  			} else {
   124  				g.Expect(err).ToNot(HaveOccurred())
   125  			}
   126  			g.Expect(warnings).To(BeEmpty())
   127  		})
   128  	}
   129  }