sigs.k8s.io/cluster-api-provider-azure@v1.14.3/util/futures/setter_test.go (about)

     1  /*
     2  Copyright 2021 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 futures
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    24  )
    25  
    26  func TestSet(t *testing.T) {
    27  	testService := "test-service"
    28  	a := fakeFuture("a", testService)
    29  	b := fakeFuture("b", testService)
    30  	newA := a
    31  	newA.Data = "new"
    32  
    33  	tests := []struct {
    34  		name   string
    35  		to     Setter
    36  		future *infrav1.Future
    37  		want   infrav1.Futures
    38  	}{
    39  		{
    40  			name:   "Set adds a future",
    41  			to:     setterWithFutures(infrav1.Futures{}),
    42  			future: &a,
    43  			want:   infrav1.Futures{a},
    44  		},
    45  		{
    46  			name:   "Set adds more futures",
    47  			to:     setterWithFutures(infrav1.Futures{a}),
    48  			future: &b,
    49  			want:   infrav1.Futures{a, b},
    50  		},
    51  		{
    52  			name:   "Set does not duplicate existing future",
    53  			to:     setterWithFutures(infrav1.Futures{a, b}),
    54  			future: &a,
    55  			want:   infrav1.Futures{a, b},
    56  		},
    57  		{
    58  			name:   "Set updates an existing future",
    59  			to:     setterWithFutures(infrav1.Futures{a, b}),
    60  			future: &newA,
    61  			want:   infrav1.Futures{newA, b},
    62  		},
    63  	}
    64  
    65  	for _, tt := range tests {
    66  		t.Run(tt.name, func(t *testing.T) {
    67  			g := NewWithT(t)
    68  
    69  			Set(tt.to, tt.future)
    70  
    71  			g.Expect(tt.to.GetFutures()).To(Equal(tt.want))
    72  		})
    73  	}
    74  }
    75  
    76  func TestDelete(t *testing.T) {
    77  	testService := "test-service"
    78  	a := fakeFuture("a", testService)
    79  	b := fakeFuture("b", testService)
    80  	c := fakeFuture("c", testService)
    81  	d := fakeFuture("d", testService)
    82  
    83  	tests := []struct {
    84  		name   string
    85  		to     Setter
    86  		future string
    87  		want   infrav1.Futures
    88  	}{
    89  		{
    90  			name:   "Delete removes a future",
    91  			to:     setterWithFutures(infrav1.Futures{a, b, c, d}),
    92  			future: "b",
    93  			want:   infrav1.Futures{a, c, d},
    94  		},
    95  		{
    96  			name:   "Delete does nothing if the future does not exist",
    97  			to:     setterWithFutures(infrav1.Futures{a}),
    98  			future: "b",
    99  			want:   infrav1.Futures{a},
   100  		},
   101  	}
   102  
   103  	for _, tt := range tests {
   104  		t.Run(tt.name, func(t *testing.T) {
   105  			g := NewWithT(t)
   106  
   107  			Delete(tt.to, tt.future, testService, fakeFutureType)
   108  
   109  			g.Expect(tt.to.GetFutures()).To(Equal(tt.want))
   110  		})
   111  	}
   112  }
   113  
   114  func setterWithFutures(futures infrav1.Futures) Setter {
   115  	obj := &infrav1.AzureCluster{}
   116  	obj.SetFutures(futures)
   117  	return obj
   118  }