diff --git a/controllers/factory_test.go b/controllers/factory_test.go index 2aa8c1eb0e86..2618558bff28 100644 --- a/controllers/factory_test.go +++ b/controllers/factory_test.go @@ -229,3 +229,63 @@ func TestFactoryWithNutanixDatacenterReconciler(t *testing.T) { g.Expect(err).NotTo(HaveOccurred()) g.Expect(reconcilers.NutanixDatacenterReconciler).NotTo(BeNil()) } + +func TestFactoryWithNodeUpgradeReconciler(t *testing.T) { + g := NewWithT(t) + ctx := context.Background() + logger := nullLog() + ctrl := gomock.NewController(t) + manager := mocks.NewMockManager(ctrl) + manager.EXPECT().GetClient().AnyTimes() + manager.EXPECT().GetScheme().AnyTimes() + + f := controllers.NewFactory(logger, manager). + WithNodeUpgradeReconciler() + + // testing idempotence + f.WithNodeUpgradeReconciler() + + reconcilers, err := f.Build(ctx) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(reconcilers.NodeUpgradeReconciler).NotTo(BeNil()) +} + +func TestFactoryWithControlPlaneUpgradeReconciler(t *testing.T) { + g := NewWithT(t) + ctx := context.Background() + logger := nullLog() + ctrl := gomock.NewController(t) + manager := mocks.NewMockManager(ctrl) + manager.EXPECT().GetClient().AnyTimes() + manager.EXPECT().GetScheme().AnyTimes() + + f := controllers.NewFactory(logger, manager). + WithControlPlaneUpgradeReconciler() + + // testing idempotence + f.WithControlPlaneUpgradeReconciler() + + reconcilers, err := f.Build(ctx) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(reconcilers.ControlPlaneUpgradeReconciler).NotTo(BeNil()) +} + +func TestFactoryWithMachineDeploymentUpgradeReconciler(t *testing.T) { + g := NewWithT(t) + ctx := context.Background() + logger := nullLog() + ctrl := gomock.NewController(t) + manager := mocks.NewMockManager(ctrl) + manager.EXPECT().GetClient().AnyTimes() + manager.EXPECT().GetScheme().AnyTimes() + + f := controllers.NewFactory(logger, manager). + WithMachineDeploymentUpgradeReconciler() + + // testing idempotence + f.WithMachineDeploymentUpgradeReconciler() + + reconcilers, err := f.Build(ctx) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(reconcilers.MachineDeploymentUpgradeReconciler).NotTo(BeNil()) +} diff --git a/controllers/nodeupgrade_controller_test.go b/controllers/nodeupgrade_controller_test.go index 172be0ebdf66..d9cd87eaee94 100644 --- a/controllers/nodeupgrade_controller_test.go +++ b/controllers/nodeupgrade_controller_test.go @@ -20,7 +20,7 @@ import ( "github.com/aws/eks-anywhere/pkg/utils/ptr" ) -func TestNodeUpgradeReconciler(t *testing.T) { +func TestNodeUpgradeReconcilerReconcileCreate(t *testing.T) { g := NewWithT(t) ctx := context.Background() ctrl := gomock.NewController(t) @@ -34,11 +34,42 @@ func TestNodeUpgradeReconciler(t *testing.T) { r := controllers.NewNodeUpgradeReconciler(client, clientRegistry) req := nodeUpgradeRequest(nodeUpgrade) _, err := r.Reconcile(ctx, req) - g.Expect(err).To(BeNil()) + g.Expect(err).ToNot(HaveOccurred()) pod := &corev1.Pod{} err = client.Get(ctx, types.NamespacedName{Name: upgrader.PodName(node.Name), Namespace: "eksa-system"}, pod) - g.Expect(err).To(BeNil()) + g.Expect(err).ToNot(HaveOccurred()) +} + +func TestNodeUpgradeReconcilerReconcileDelete(t *testing.T) { + g := NewWithT(t) + ctx := context.Background() + ctrl := gomock.NewController(t) + clientRegistry := mocks.NewMockRemoteClientRegistry(ctrl) + + cluster, machine, node, nodeUpgrade := getObjectsForNodeUpgradeTest() + client := fake.NewClientBuilder().WithRuntimeObjects(cluster, machine, node, nodeUpgrade).Build() + + clientRegistry.EXPECT().GetClient(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}).Return(client, nil).Times(2) + + r := controllers.NewNodeUpgradeReconciler(client, clientRegistry) + req := nodeUpgradeRequest(nodeUpgrade) + _, err := r.Reconcile(ctx, req) + g.Expect(err).ToNot(HaveOccurred()) + + pod := &corev1.Pod{} + err = client.Get(ctx, types.NamespacedName{Name: upgrader.PodName(node.Name), Namespace: "eksa-system"}, pod) + g.Expect(err).ToNot(HaveOccurred()) + + err = client.Delete(ctx, nodeUpgrade) + g.Expect(err).ToNot(HaveOccurred()) + + _, err = r.Reconcile(ctx, req) + g.Expect(err).ToNot(HaveOccurred()) + + pod = &corev1.Pod{} + err = client.Get(ctx, types.NamespacedName{Name: upgrader.PodName(node.Name), Namespace: "eksa-system"}, pod) + g.Expect(err).To(MatchError("pods \"node01-node-upgrader\" not found")) } func getObjectsForNodeUpgradeTest() (*clusterv1.Cluster, *clusterv1.Machine, *corev1.Node, *anywherev1.NodeUpgrade) {