@@ -22,11 +22,14 @@ import (
2222 "github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2022-03-01/containerservice"
2323 . "github.com/onsi/gomega"
2424 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+ "k8s.io/apimachinery/pkg/runtime"
2526 utilfeature "k8s.io/component-base/featuregate/testing"
2627 "k8s.io/utils/pointer"
2728 "sigs.k8s.io/cluster-api-provider-azure/feature"
29+ clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2830 capifeature "sigs.k8s.io/cluster-api/feature"
2931 "sigs.k8s.io/controller-runtime/pkg/client"
32+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
3033)
3134
3235func TestAzureManagedMachinePoolDefaultingWebhook (t * testing.T ) {
@@ -882,6 +885,87 @@ func TestAzureManagedMachinePool_ValidateCreateFailure(t *testing.T) {
882885 }
883886}
884887
888+ func TestAzureManagedMachinePool_validateLastSystemNodePool (t * testing.T ) {
889+ deletionTime := metav1 .Now ()
890+ systemMachinePool := getManagedMachinePoolWithSystemMode ()
891+ tests := []struct {
892+ name string
893+ ammp * AzureManagedMachinePool
894+ cluster * clusterv1.Cluster
895+ wantErr bool
896+ }{
897+ {
898+ name : "Test with paused cluster without deletion timestamp having one system pool node(valid delete:move operation)" ,
899+ ammp : systemMachinePool ,
900+ cluster : & clusterv1.Cluster {
901+ ObjectMeta : metav1.ObjectMeta {
902+ Name : systemMachinePool .GetLabels ()[clusterv1 .ClusterLabelName ],
903+ Namespace : systemMachinePool .Namespace ,
904+ DeletionTimestamp : & deletionTime ,
905+ },
906+ Spec : clusterv1.ClusterSpec {
907+ Paused : true ,
908+ },
909+ },
910+ wantErr : false ,
911+ },
912+ {
913+ name : "Test with paused cluster with deletion timestamp having one system pool node(valid delete)" ,
914+ ammp : systemMachinePool ,
915+ cluster : & clusterv1.Cluster {
916+ ObjectMeta : metav1.ObjectMeta {
917+ Name : systemMachinePool .GetLabels ()[clusterv1 .ClusterLabelName ],
918+ Namespace : systemMachinePool .Namespace ,
919+ DeletionTimestamp : & deletionTime ,
920+ },
921+ Spec : clusterv1.ClusterSpec {
922+ Paused : true ,
923+ },
924+ },
925+ wantErr : false ,
926+ },
927+ {
928+ name : "Test with running cluster without deletion timestamp having one system pool node(invalid delete)" ,
929+ ammp : systemMachinePool ,
930+ cluster : & clusterv1.Cluster {
931+ ObjectMeta : metav1.ObjectMeta {
932+ Name : systemMachinePool .GetLabels ()[clusterv1 .ClusterLabelName ],
933+ Namespace : systemMachinePool .Namespace ,
934+ },
935+ },
936+ wantErr : true ,
937+ },
938+ {
939+ name : "Test with running cluster with deletion timestamp having one system pool node(valid delete)" ,
940+ ammp : systemMachinePool ,
941+ cluster : & clusterv1.Cluster {
942+ ObjectMeta : metav1.ObjectMeta {
943+ Name : systemMachinePool .GetLabels ()[clusterv1 .ClusterLabelName ],
944+ Namespace : systemMachinePool .Namespace ,
945+ DeletionTimestamp : & deletionTime ,
946+ },
947+ },
948+ wantErr : false ,
949+ },
950+ }
951+
952+ for _ , tc := range tests {
953+ t .Run (tc .name , func (t * testing.T ) {
954+ g := NewWithT (t )
955+ scheme := runtime .NewScheme ()
956+ _ = AddToScheme (scheme )
957+ _ = clusterv1 .AddToScheme (scheme )
958+ fakeClient := fake .NewClientBuilder ().WithScheme (scheme ).WithRuntimeObjects (tc .cluster , tc .ammp ).Build ()
959+ err := tc .ammp .validateLastSystemNodePool (fakeClient )
960+ if tc .wantErr {
961+ g .Expect (err ).To (HaveOccurred ())
962+ } else {
963+ g .Expect (err ).NotTo (HaveOccurred ())
964+ }
965+ })
966+ }
967+ }
968+
885969func getKnownValidAzureManagedMachinePool () * AzureManagedMachinePool {
886970 return & AzureManagedMachinePool {
887971 Spec : AzureManagedMachinePoolSpec {
@@ -890,3 +974,15 @@ func getKnownValidAzureManagedMachinePool() *AzureManagedMachinePool {
890974 },
891975 }
892976}
977+
978+ func getManagedMachinePoolWithSystemMode () * AzureManagedMachinePool {
979+ return & AzureManagedMachinePool {
980+ ObjectMeta : metav1.ObjectMeta {
981+ Namespace : metav1 .NamespaceDefault ,
982+ Labels : map [string ]string {
983+ clusterv1 .ClusterLabelName : "test-cluster" ,
984+ LabelAgentPoolMode : string (NodePoolModeSystem ),
985+ },
986+ },
987+ }
988+ }
0 commit comments