|
| 1 | +package process |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + |
| 9 | + mdbv1 "github.com/mongodb/mongodb-kubernetes/api/v1/mdb" |
| 10 | + "github.com/mongodb/mongodb-kubernetes/pkg/util/maputil" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + defaultMongoDBImage = "mongodb/mongodb-enterprise-server:7.0.0" |
| 15 | + defaultFCV = "7.0" |
| 16 | + defaultNamespace = "test-namespace" |
| 17 | +) |
| 18 | + |
| 19 | +func TestCreateMongodProcessesFromMongoDB(t *testing.T) { |
| 20 | + t.Run("Happy path - creates processes with correct integration", func(t *testing.T) { |
| 21 | + mdb := baseReplicaSet("test-rs", 3) |
| 22 | + processes := CreateMongodProcessesFromMongoDB( |
| 23 | + defaultMongoDBImage, |
| 24 | + false, |
| 25 | + mdb, |
| 26 | + 3, |
| 27 | + defaultFCV, |
| 28 | + "", |
| 29 | + ) |
| 30 | + |
| 31 | + assert.Len(t, processes, 3, "Should create 3 processes") |
| 32 | + |
| 33 | + // Verify basic integration - processes are created with correct names and FCV |
| 34 | + for i, process := range processes { |
| 35 | + expectedName := fmt.Sprintf("test-rs-%d", i) |
| 36 | + assert.Equal(t, expectedName, process.Name(), "Process name should be generated correctly") |
| 37 | + assert.Equal(t, defaultFCV, process.FeatureCompatibilityVersion(), "FCV should be set correctly") |
| 38 | + assert.NotEmpty(t, process.HostName(), "Hostname should be generated") |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + t.Run("Limit parameter controls process count", func(t *testing.T) { |
| 43 | + mdb := baseReplicaSet("scale-rs", 5) |
| 44 | + |
| 45 | + // Test limit less than members (scale up in progress) |
| 46 | + processesScaleUp := CreateMongodProcessesFromMongoDB( |
| 47 | + defaultMongoDBImage, |
| 48 | + false, |
| 49 | + mdb, |
| 50 | + 3, // limit |
| 51 | + defaultFCV, |
| 52 | + "", |
| 53 | + ) |
| 54 | + assert.Len(t, processesScaleUp, 3, "Limit should control process count during scale up") |
| 55 | + |
| 56 | + // Test limit greater than members (scale down in progress) |
| 57 | + processesScaleDown := CreateMongodProcessesFromMongoDB( |
| 58 | + defaultMongoDBImage, |
| 59 | + false, |
| 60 | + mdb, |
| 61 | + 7, // limit |
| 62 | + defaultFCV, |
| 63 | + "", |
| 64 | + ) |
| 65 | + assert.Len(t, processesScaleDown, 7, "Limit should control process count during scale down") |
| 66 | + |
| 67 | + // Test limit zero |
| 68 | + processesZero := CreateMongodProcessesFromMongoDB( |
| 69 | + defaultMongoDBImage, |
| 70 | + false, |
| 71 | + mdb, |
| 72 | + 0, // limit |
| 73 | + defaultFCV, |
| 74 | + "", |
| 75 | + ) |
| 76 | + assert.Empty(t, processesZero, "Zero limit should create empty process slice") |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("TLS cert path flows through to processes", func(t *testing.T) { |
| 80 | + mdb := baseReplicaSet("tls-rs", 2) |
| 81 | + mdb.Spec.Security = &mdbv1.Security{ |
| 82 | + TLSConfig: &mdbv1.TLSConfig{Enabled: true}, |
| 83 | + } |
| 84 | + |
| 85 | + tlsCertPath := "/custom/path/to/cert.pem" |
| 86 | + processes := CreateMongodProcessesFromMongoDB( |
| 87 | + defaultMongoDBImage, |
| 88 | + false, |
| 89 | + mdb, |
| 90 | + 2, |
| 91 | + defaultFCV, |
| 92 | + tlsCertPath, |
| 93 | + ) |
| 94 | + |
| 95 | + assert.Len(t, processes, 2) |
| 96 | + |
| 97 | + // Verify TLS configuration is properly integrated |
| 98 | + for i, process := range processes { |
| 99 | + tlsConfig := process.TLSConfig() |
| 100 | + assert.NotNil(t, tlsConfig, "TLS config should be set when cert path provided") |
| 101 | + assert.Equal(t, tlsCertPath, tlsConfig["certificateKeyFile"], "TLS cert path should match at index %d", i) |
| 102 | + } |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +func TestCreateMongodProcessesFromMongoDB_AdditionalConfig(t *testing.T) { |
| 107 | + config := mdbv1.NewAdditionalMongodConfig("storage.engine", "inMemory"). |
| 108 | + AddOption("replication.oplogSizeMB", 2048) |
| 109 | + |
| 110 | + mdb := mdbv1.NewReplicaSetBuilder(). |
| 111 | + SetName("config-rs"). |
| 112 | + SetNamespace(defaultNamespace). |
| 113 | + SetMembers(2). |
| 114 | + SetVersion("7.0.0"). |
| 115 | + SetFCVersion(defaultFCV). |
| 116 | + SetAdditionalConfig(config). |
| 117 | + Build() |
| 118 | + |
| 119 | + processes := CreateMongodProcessesFromMongoDB( |
| 120 | + defaultMongoDBImage, |
| 121 | + false, |
| 122 | + mdb, |
| 123 | + 2, |
| 124 | + defaultFCV, |
| 125 | + "", |
| 126 | + ) |
| 127 | + |
| 128 | + assert.Len(t, processes, 2) |
| 129 | + |
| 130 | + for i, process := range processes { |
| 131 | + assert.Equal(t, "inMemory", maputil.ReadMapValueAsInterface(process.Args(), "storage", "engine"), |
| 132 | + "Storage engine mismatch at index %d", i) |
| 133 | + assert.Equal(t, 2048, maputil.ReadMapValueAsInterface(process.Args(), "replication", "oplogSizeMB"), |
| 134 | + "OplogSizeMB mismatch at index %d", i) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func baseReplicaSet(name string, members int) *mdbv1.MongoDB { |
| 139 | + return mdbv1.NewReplicaSetBuilder(). |
| 140 | + SetName(name). |
| 141 | + SetNamespace(defaultNamespace). |
| 142 | + SetMembers(members). |
| 143 | + SetVersion("7.0.0"). |
| 144 | + SetFCVersion(defaultFCV). |
| 145 | + Build() |
| 146 | +} |
0 commit comments