Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests to azuremanagedcontrolplane reconciler #4303

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions azure/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
Cluster: params.Cluster,
ControlPlane: params.ControlPlane,
ManagedMachinePools: params.ManagedMachinePools,
patchHelper: helper,
PatchHelper: helper,
cache: params.Cache,
VnetDescriber: params.VnetDescriber,
}, nil
Expand All @@ -128,7 +128,7 @@
// ManagedControlPlaneScope defines the basic context for an actuator to operate upon.
type ManagedControlPlaneScope struct {
Client client.Client
patchHelper *patch.Helper
PatchHelper *patch.Helper
adminKubeConfigData []byte
userKubeConfigData []byte
cache *ManagedControlPlaneCache
Expand Down Expand Up @@ -230,7 +230,7 @@

conditions.SetSummary(s.ControlPlane)

return s.patchHelper.Patch(
return s.PatchHelper.Patch(

Check warning on line 233 in azure/scope/managedcontrolplane.go

View check run for this annotation

Codecov / codecov/patch

azure/scope/managedcontrolplane.go#L233

Added line #L233 was not covered by tests
ctx,
s.ControlPlane,
patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
Expand Down
14 changes: 8 additions & 6 deletions controllers/azuremanagedcontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@
// AzureManagedControlPlaneReconciler reconciles an AzureManagedControlPlane object.
type AzureManagedControlPlaneReconciler struct {
client.Client
Recorder record.EventRecorder
ReconcileTimeout time.Duration
WatchFilterValue string
Recorder record.EventRecorder
ReconcileTimeout time.Duration
WatchFilterValue string
getNewAzureManagedControlPlaneReconciler func(scope *scope.ManagedControlPlaneScope) (*azureManagedControlPlaneService, error)
}

// SetupWithManager initializes this controller with a manager.
Expand All @@ -61,6 +62,7 @@
)
defer done()

amcpr.getNewAzureManagedControlPlaneReconciler = newAzureManagedControlPlaneReconciler
var r reconcile.Reconciler = amcpr
if options.Cache != nil {
r = coalescing.NewReconciler(amcpr, options.Cache, log)
Expand Down Expand Up @@ -238,7 +240,7 @@
}
}

svc, err := newAzureManagedControlPlaneReconciler(scope)
svc, err := amcpr.getNewAzureManagedControlPlaneReconciler(scope)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create azureManagedControlPlane service")
}
Expand Down Expand Up @@ -279,7 +281,7 @@

log.Info("Reconciling AzureManagedControlPlane pause")

svc, err := newAzureManagedControlPlaneReconciler(scope)
svc, err := amcpr.getNewAzureManagedControlPlaneReconciler(scope)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create azureManagedControlPlane service")
}
Expand All @@ -296,7 +298,7 @@

log.Info("Reconciling AzureManagedControlPlane delete")

svc, err := newAzureManagedControlPlaneReconciler(scope)
svc, err := amcpr.getNewAzureManagedControlPlaneReconciler(scope)

Check warning on line 301 in controllers/azuremanagedcontrolplane_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/azuremanagedcontrolplane_controller.go#L301

Added line #L301 was not covered by tests
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create azureManagedControlPlane service")
}
Expand Down
92 changes: 88 additions & 4 deletions controllers/azuremanagedcontrolplane_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@ import (
asocontainerservicev1 "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20230201"
asoresourcesv1 "github.com/Azure/azure-service-operator/v2/api/resources/v1api20200601"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/mock_azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/scope"
"sigs.k8s.io/cluster-api-provider-azure/internal/test"
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/patch"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
Expand Down Expand Up @@ -110,10 +115,11 @@ func TestAzureManagedControlPlaneReconcilePaused(t *testing.T) {
recorder := record.NewFakeRecorder(1)

reconciler := &AzureManagedControlPlaneReconciler{
Client: c,
Recorder: recorder,
ReconcileTimeout: reconciler.DefaultLoopTimeout,
WatchFilterValue: "",
Client: c,
Recorder: recorder,
ReconcileTimeout: reconciler.DefaultLoopTimeout,
WatchFilterValue: "",
getNewAzureManagedControlPlaneReconciler: newAzureManagedControlPlaneReconciler,
}
name := test.RandomName("paused", 10)
namespace := "default"
Expand Down Expand Up @@ -176,3 +182,81 @@ func TestAzureManagedControlPlaneReconcilePaused(t *testing.T) {
g.Expect(err).To(BeNil())
g.Expect(result.RequeueAfter).To(BeZero())
}

func TestAzureManagedControlPlaneReconcileNormal(t *testing.T) {
g := NewWithT(t)
ctx := context.Background()
cp := &infrav1.AzureManagedControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-azmp",
Namespace: "fake-ns",
},
Spec: infrav1.AzureManagedControlPlaneSpec{
AzureManagedControlPlaneClassSpec: infrav1.AzureManagedControlPlaneClassSpec{
Version: "0.0.1",
},
},
Status: infrav1.AzureManagedControlPlaneStatus{
Ready: false,
Initialized: false,
},
}
scheme, err := newScheme()
g.Expect(err).ToNot(HaveOccurred())

client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(cp).WithStatusSubresource(cp).Build()
amcpr := &AzureManagedControlPlaneReconciler{
Client: client,
}

helper, err := patch.NewHelper(cp, client)
g.Expect(err).ToNot(HaveOccurred())

scopes := &scope.ManagedControlPlaneScope{
Cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-cluster",
Namespace: "fake-ns",
},
},
Client: client,
PatchHelper: helper,
ControlPlane: cp,
}
scopes.SetAdminKubeconfigData(createFakeKubeConfig())
scopes.SetUserKubeconfigData(createFakeKubeConfig())

amcpr.getNewAzureManagedControlPlaneReconciler = func(scope *scope.ManagedControlPlaneScope) (*azureManagedControlPlaneService, error) {
ctrlr := gomock.NewController(t)
svcr := mock_azure.NewMockServiceReconciler(ctrlr)
svcr.EXPECT().Reconcile(gomock.Any()).Return(nil)

return &azureManagedControlPlaneService{
kubeclient: scope.Client,
scope: scope,
services: []azure.ServiceReconciler{
svcr,
},
}, nil
}

_, err = amcpr.reconcileNormal(ctx, scopes)
g.Expect(err).To(HaveOccurred())
}

func createFakeKubeConfig() []byte {
return []byte(`
apiVersion: v1
kind: Config
clusters:
- cluster:
certificate-authority-data: UEhPTlkK
server: https://1.1.1.1
name: production
contexts:
- context:
cluster: production
user: production
name: production
current-context: production`)
}