|
| 1 | +package replicaset |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "k8s.io/utils/ptr" |
| 8 | + |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + |
| 11 | + mdbv1 "github.com/mongodb/mongodb-kubernetes/api/v1/mdb" |
| 12 | + "github.com/mongodb/mongodb-kubernetes/controllers/om" |
| 13 | + "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/automationconfig" |
| 14 | +) |
| 15 | + |
| 16 | +// This test focuses on the integration/glue logic, not re-testing components. |
| 17 | +func TestBuildFromMongoDBWithReplicas(t *testing.T) { |
| 18 | + memberOptions := []automationconfig.MemberOptions{ |
| 19 | + {Votes: ptr.To(1), Priority: ptr.To("1.0")}, |
| 20 | + {Votes: ptr.To(1), Priority: ptr.To("0.5")}, |
| 21 | + {Votes: ptr.To(0), Priority: ptr.To("0")}, // Non-voting member |
| 22 | + } |
| 23 | + |
| 24 | + mdb := &mdbv1.MongoDB{ |
| 25 | + ObjectMeta: metav1.ObjectMeta{ |
| 26 | + Name: "test-rs", |
| 27 | + Namespace: "test-namespace", |
| 28 | + }, |
| 29 | + Spec: mdbv1.MongoDbSpec{ |
| 30 | + DbCommonSpec: mdbv1.DbCommonSpec{ |
| 31 | + Version: "7.0.5", |
| 32 | + Security: &mdbv1.Security{ |
| 33 | + TLSConfig: &mdbv1.TLSConfig{}, |
| 34 | + Authentication: &mdbv1.Authentication{}, |
| 35 | + }, |
| 36 | + Connectivity: &mdbv1.MongoDBConnectivity{ |
| 37 | + ReplicaSetHorizons: []mdbv1.MongoDBHorizonConfig{}, |
| 38 | + }, |
| 39 | + }, |
| 40 | + Members: 5, // Spec (target) is 5 members |
| 41 | + MemberConfig: memberOptions, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + // 3 replicas is less than spec.Members, scale up scenario |
| 46 | + replicas := 3 |
| 47 | + rsWithProcesses := BuildFromMongoDBWithReplicas( |
| 48 | + "mongodb/mongodb-enterprise-server:7.0.5", |
| 49 | + false, |
| 50 | + mdb, |
| 51 | + replicas, |
| 52 | + "7.0", |
| 53 | + "", |
| 54 | + ) |
| 55 | + |
| 56 | + // Assert: ReplicaSet structure |
| 57 | + assert.Equal(t, "test-rs", rsWithProcesses.Rs.Name(), "ReplicaSet ID should match MongoDB name") |
| 58 | + assert.Equal(t, "1", rsWithProcesses.Rs["protocolVersion"], "Protocol version should be set to 1 for this MongoDB version") |
| 59 | + |
| 60 | + // Assert: Member count is controlled by replicas parameter, NOT mdb.Spec.Members |
| 61 | + members := rsWithProcesses.Rs["members"].([]om.ReplicaSetMember) |
| 62 | + assert.Len(t, members, replicas, "Member count should match replicas parameter (3), not mdb.Spec.Members (5)") |
| 63 | + assert.Equal(t, 3, len(members), "Should have exactly 3 members") |
| 64 | + |
| 65 | + // Assert: Processes are created correctly |
| 66 | + assert.Len(t, rsWithProcesses.Processes, replicas, "Process count should match replicas parameter") |
| 67 | + expectedProcessNames := []string{"test-rs-0", "test-rs-1", "test-rs-2"} |
| 68 | + expectedHostnames := []string{ |
| 69 | + "test-rs-0.test-rs-svc.test-namespace.svc.cluster.local", |
| 70 | + "test-rs-1.test-rs-svc.test-namespace.svc.cluster.local", |
| 71 | + "test-rs-2.test-rs-svc.test-namespace.svc.cluster.local", |
| 72 | + } |
| 73 | + |
| 74 | + for i := 0; i < replicas; i++ { |
| 75 | + assert.Equal(t, expectedProcessNames[i], rsWithProcesses.Processes[i].Name(), |
| 76 | + "Process name mismatch at index %d", i) |
| 77 | + assert.Equal(t, expectedHostnames[i], rsWithProcesses.Processes[i].HostName(), |
| 78 | + "Process hostname mismatch at index %d", i) |
| 79 | + } |
| 80 | + |
| 81 | + // Assert: Member options are propagated |
| 82 | + assert.Equal(t, 1, members[0].Votes(), "Member 0 should have 1 vote") |
| 83 | + assert.Equal(t, float32(1.0), members[0].Priority(), "Member 0 should have priority 1.0") |
| 84 | + assert.Equal(t, 1, members[1].Votes(), "Member 1 should have 1 vote") |
| 85 | + assert.Equal(t, float32(0.5), members[1].Priority(), "Member 1 should have priority 0.5") |
| 86 | + assert.Equal(t, 0, members[2].Votes(), "Member 2 should have 0 votes (non-voting)") |
| 87 | + assert.Equal(t, float32(0), members[2].Priority(), "Member 2 should have priority 0") |
| 88 | + |
| 89 | + // Assert: Member host field contains process name (not full hostname) |
| 90 | + // Note: ReplicaSetMember["host"] is the process name, not the full hostname |
| 91 | + for i := 0; i < replicas; i++ { |
| 92 | + assert.Equal(t, expectedProcessNames[i], members[i].Name(), |
| 93 | + "Member host should match process name at index %d", i) |
| 94 | + } |
| 95 | +} |
0 commit comments