sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/groups/groups_test.go (about)

     1  /*
     2  Copyright 2023 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 groups
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	asoresourcesv1 "github.com/Azure/azure-service-operator/v2/api/resources/v1api20200601"
    24  	asoannotations "github.com/Azure/azure-service-operator/v2/pkg/common/annotations"
    25  	. "github.com/onsi/gomega"
    26  	"go.uber.org/mock/gomock"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/runtime"
    29  	"k8s.io/utils/ptr"
    30  	"sigs.k8s.io/cluster-api-provider-azure/azure"
    31  	"sigs.k8s.io/cluster-api-provider-azure/azure/services/groups/mock_groups"
    32  	"sigs.k8s.io/controller-runtime/pkg/client"
    33  	"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
    34  	fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
    35  )
    36  
    37  func TestIsManaged(t *testing.T) {
    38  	newOwner := func() *asoresourcesv1.ResourceGroup {
    39  		return &asoresourcesv1.ResourceGroup{
    40  			ObjectMeta: metav1.ObjectMeta{
    41  				Namespace: "namespace",
    42  			},
    43  		}
    44  	}
    45  
    46  	newOwnerRefs := func() []metav1.OwnerReference {
    47  		s := runtime.NewScheme()
    48  		if err := asoresourcesv1.AddToScheme(s); err != nil {
    49  			t.Fatal(err.Error())
    50  		}
    51  		gvk, _ := apiutil.GVKForObject(&asoresourcesv1.ResourceGroup{}, s)
    52  		return []metav1.OwnerReference{
    53  			{
    54  				APIVersion:         gvk.GroupVersion().String(),
    55  				Kind:               gvk.Kind,
    56  				Controller:         ptr.To(true),
    57  				BlockOwnerDeletion: ptr.To(true),
    58  			},
    59  		}
    60  	}
    61  
    62  	tests := []struct {
    63  		name          string
    64  		objects       []client.Object
    65  		expect        func(s *mock_groups.MockGroupScopeMockRecorder)
    66  		expected      bool
    67  		expectedError bool
    68  	}{
    69  		{
    70  			name:    "error checking if group is managed",
    71  			objects: nil,
    72  			expect: func(s *mock_groups.MockGroupScopeMockRecorder) {
    73  				s.GroupSpecs().Return([]azure.ASOResourceSpecGetter[*asoresourcesv1.ResourceGroup]{&GroupSpec{}}).AnyTimes()
    74  				s.ClusterName().Return("").AnyTimes()
    75  			},
    76  			expectedError: true,
    77  		},
    78  		{
    79  			name: "group is unmanaged",
    80  			objects: []client.Object{
    81  				&asoresourcesv1.ResourceGroup{
    82  					ObjectMeta: metav1.ObjectMeta{
    83  						Name:      "name",
    84  						Namespace: "namespace",
    85  						OwnerReferences: []metav1.OwnerReference{
    86  							{
    87  								APIVersion: "wrong version",
    88  							},
    89  						},
    90  					},
    91  				},
    92  			},
    93  			expect: func(s *mock_groups.MockGroupScopeMockRecorder) {
    94  				s.GroupSpecs().Return([]azure.ASOResourceSpecGetter[*asoresourcesv1.ResourceGroup]{
    95  					&GroupSpec{
    96  						Name: "name",
    97  					},
    98  				}).AnyTimes()
    99  				s.ClusterName().Return("cluster").AnyTimes()
   100  			},
   101  			expected: false,
   102  		},
   103  		{
   104  			name: "group is managed and has reconcile policy skip",
   105  			objects: []client.Object{
   106  				&asoresourcesv1.ResourceGroup{
   107  					ObjectMeta: metav1.ObjectMeta{
   108  						Name:            "name",
   109  						Namespace:       "namespace",
   110  						OwnerReferences: newOwnerRefs(),
   111  						Annotations: map[string]string{
   112  							asoannotations.ReconcilePolicy: string(asoannotations.ReconcilePolicySkip),
   113  						},
   114  					},
   115  				},
   116  			},
   117  			expect: func(s *mock_groups.MockGroupScopeMockRecorder) {
   118  				s.GroupSpecs().Return([]azure.ASOResourceSpecGetter[*asoresourcesv1.ResourceGroup]{
   119  					&GroupSpec{
   120  						Name: "name",
   121  					},
   122  				}).AnyTimes()
   123  				s.ClusterName().Return("cluster").AnyTimes()
   124  			},
   125  			expected: false,
   126  		},
   127  		{
   128  			name: "group is managed and has reconcile policy manage",
   129  			objects: []client.Object{
   130  				&asoresourcesv1.ResourceGroup{
   131  					ObjectMeta: metav1.ObjectMeta{
   132  						Name:            "name",
   133  						Namespace:       "namespace",
   134  						OwnerReferences: newOwnerRefs(),
   135  						Annotations: map[string]string{
   136  							asoannotations.ReconcilePolicy: string(asoannotations.ReconcilePolicyManage),
   137  						},
   138  					},
   139  				},
   140  			},
   141  			expect: func(s *mock_groups.MockGroupScopeMockRecorder) {
   142  				s.GroupSpecs().Return([]azure.ASOResourceSpecGetter[*asoresourcesv1.ResourceGroup]{
   143  					&GroupSpec{
   144  						Name: "name",
   145  					},
   146  				}).AnyTimes()
   147  				s.ClusterName().Return("cluster").AnyTimes()
   148  			},
   149  			expected: true,
   150  		},
   151  	}
   152  
   153  	for _, test := range tests {
   154  		t.Run(test.name, func(t *testing.T) {
   155  			g := NewWithT(t)
   156  
   157  			mockCtrl := gomock.NewController(t)
   158  			defer mockCtrl.Finish()
   159  			scopeMock := mock_groups.NewMockGroupScope(mockCtrl)
   160  
   161  			scheme := runtime.NewScheme()
   162  			g.Expect(asoresourcesv1.AddToScheme(scheme)).To(Succeed())
   163  			ctrlClient := fakeclient.NewClientBuilder().
   164  				WithScheme(scheme).
   165  				WithObjects(test.objects...).
   166  				Build()
   167  			scopeMock.EXPECT().GetClient().Return(ctrlClient).AnyTimes()
   168  			scopeMock.EXPECT().ASOOwner().Return(newOwner()).AnyTimes()
   169  			test.expect(scopeMock.EXPECT())
   170  
   171  			actual, err := New(scopeMock).IsManaged(context.Background())
   172  			if test.expectedError {
   173  				g.Expect(err).To(HaveOccurred())
   174  			} else {
   175  				g.Expect(actual).To(Equal(test.expected))
   176  			}
   177  		})
   178  	}
   179  }