|
| 1 | +package operator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + mdbv1 "github.com/mongodb/mongodb-kubernetes/api/v1/mdb" |
| 6 | + "github.com/mongodb/mongodb-kubernetes/controllers/om" |
| 7 | + "github.com/mongodb/mongodb-kubernetes/controllers/operator/mock" |
| 8 | + kubernetesClient "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/kube/client" |
| 9 | + "github.com/mongodb/mongodb-kubernetes/pkg/images" |
| 10 | + "github.com/mongodb/mongodb-kubernetes/pkg/kube" |
| 11 | + "github.com/mongodb/mongodb-kubernetes/pkg/util" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 15 | + "testing" |
| 16 | +) |
| 17 | + |
| 18 | +// NedDefaultMultiReplicaSetBuilder |
| 19 | + |
| 20 | +func TestCreateMultiClusterReplicaSet(t *testing.T) { |
| 21 | + ctx := context.Background() |
| 22 | + rs := mdbv1.NewDefaultMultiReplicaSetBuilder().Build() |
| 23 | + |
| 24 | + reconciler, client, _, _ := defaultMultiClusterReplicaSetReconciler(ctx, nil, "", "", mrs) |
| 25 | + checkMultiReplicaSetReconcileSuccessful(ctx, t, reconciler, rs, client, false) |
| 26 | +} |
| 27 | + |
| 28 | +func checkMultiReplicaSetReconcileSuccessful(ctx context.Context, t *testing.T, reconciler reconcile.Reconciler, m *mdbv1.MongoDB, client client.Client, shouldRequeue bool) { |
| 29 | + err := client.Update(ctx, m) |
| 30 | + assert.NoError(t, err) |
| 31 | + |
| 32 | + result, e := reconciler.Reconcile(ctx, requestFromObject(m)) |
| 33 | + assert.NoError(t, e) |
| 34 | + if shouldRequeue { |
| 35 | + assert.True(t, result.Requeue || result.RequeueAfter > 0) |
| 36 | + } else { |
| 37 | + assert.Equal(t, reconcile.Result{RequeueAfter: util.TWENTY_FOUR_HOURS}, result) |
| 38 | + } |
| 39 | + |
| 40 | + // fetch the last updates as the reconciliation loop can update the mdb resource. |
| 41 | + err = client.Get(ctx, kube.ObjectKey(m.Namespace, m.Name), m) |
| 42 | + assert.NoError(t, err) |
| 43 | +} |
| 44 | + |
| 45 | +func multiClusterReplicaSetReconciler(ctx context.Context, imageUrls images.ImageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion string, m *mdbv1.MongoDB) (*ReconcileMongoDbReplicaSet, kubernetesClient.Client, map[string]client.Client, *om.CachedOMConnectionFactory) { |
| 46 | + kubeClient, omConnectionFactory := mock.NewDefaultFakeClient(m) |
| 47 | + memberClusterMap := getFakeMultiClusterMap(omConnectionFactory) |
| 48 | + return newReplicaSetReconciler(ctx, kubeClient, imageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion, false, false, memberClusterMap, omConnectionFactory.GetConnectionFunc), kubeClient, memberClusterMap, omConnectionFactory |
| 49 | +} |
| 50 | + |
| 51 | +func defaultMultiClusterReplicaSetReconciler(ctx context.Context, imageUrls images.ImageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion string, rs *mdbv1.MongoDB) (*ReconcileMongoDbReplicaSet, kubernetesClient.Client, map[string]client.Client, *om.CachedOMConnectionFactory) { |
| 52 | + multiReplicaSetController, client, clusterMap, omConnectionFactory := multiClusterReplicaSetReconciler(ctx, imageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion, rs) |
| 53 | + omConnectionFactory.SetPostCreateHook(func(connection om.Connection) { |
| 54 | + connection.(*om.MockedOmConnection).Hostnames = calculateHostNamesForExternalDomains(rs) |
| 55 | + }) |
| 56 | + |
| 57 | + return multiReplicaSetController, client, clusterMap, omConnectionFactory |
| 58 | +} |
0 commit comments