-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathcephconfig_test.go
234 lines (212 loc) · 6.84 KB
/
cephconfig_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package storagecluster
import (
"context"
"testing"
configv1 "github.com/openshift/api/config/v1"
api "github.com/red-hat-storage/ocs-operator/api/v4/v1"
rookCephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
ini "gopkg.in/ini.v1"
"gotest.tools/v3/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
func TestDualStack(t *testing.T) {
testTable := []struct {
label string
// the networkspec in the storagecluster
rookCephNetworkSpec rookCephv1.NetworkSpec
// status of the configv1.Network object for the cluster
networkStatus configv1.NetworkStatus
// whether to expect public network in the configmap
expectPublicNetwork bool
expectedPublicNetworkValue string
}{
{
label: "Case #1: DualStack Enabled with both IPV4 CIDR and No network provider",
rookCephNetworkSpec: rookCephv1.NetworkSpec{
DualStack: true,
},
networkStatus: configv1.NetworkStatus{
ClusterNetwork: []configv1.ClusterNetworkEntry{
{CIDR: "191118.12.3.4/16"},
{CIDR: "123.45676.12.1/12"},
},
},
expectPublicNetwork: true,
expectedPublicNetworkValue: "191118.12.3.4/16,123.45676.12.1/12",
},
{
label: "Case #2: DualStack Enabled with IPV4 and IPV6 CIDR and No network provider",
rookCephNetworkSpec: rookCephv1.NetworkSpec{
DualStack: true,
},
networkStatus: configv1.NetworkStatus{
ClusterNetwork: []configv1.ClusterNetworkEntry{
{CIDR: "191118.12.3.4/17"},
{CIDR: "fd01::/48"},
},
},
expectPublicNetwork: true,
expectedPublicNetworkValue: "191118.12.3.4/17,fd01::/48",
},
{
label: "Case #3: DualStack Disabled and No network provider ",
rookCephNetworkSpec: rookCephv1.NetworkSpec{
DualStack: false,
},
networkStatus: configv1.NetworkStatus{
ClusterNetwork: []configv1.ClusterNetworkEntry{
{CIDR: "198.1v2.3.4/16"},
{CIDR: "123.45676.12.1/12"},
},
},
expectPublicNetwork: false,
},
{
label: "Case #4: DualStack Enabled and network provider is multus",
rookCephNetworkSpec: rookCephv1.NetworkSpec{
DualStack: true,
Provider: "multus",
},
networkStatus: configv1.NetworkStatus{
ClusterNetwork: []configv1.ClusterNetworkEntry{
{CIDR: "198.1v2.3.4/16"},
{CIDR: "123.45676.12.1/12"},
},
},
expectPublicNetwork: false,
},
{
label: "Case #5: DualStack Enabled with some other network provider",
rookCephNetworkSpec: rookCephv1.NetworkSpec{
DualStack: true,
Provider: "xyz",
},
networkStatus: configv1.NetworkStatus{
ClusterNetwork: []configv1.ClusterNetworkEntry{
{CIDR: "198.1v2.3.4/16"},
{CIDR: "123.45676.12.1/12"},
},
},
expectPublicNetwork: false,
},
{
label: "Case #6: DualStack Disabled and network provider is multus",
rookCephNetworkSpec: rookCephv1.NetworkSpec{
DualStack: false,
Provider: "multus",
},
networkStatus: configv1.NetworkStatus{
ClusterNetwork: []configv1.ClusterNetworkEntry{
{CIDR: "198.1v2.3.4/16"},
{CIDR: "123.45676.12.1/12"},
},
},
expectPublicNetwork: false,
},
}
for i, testCase := range testTable {
t.Logf("Case #%+v", i+1)
// setup the mocks
networkConfig := &configv1.Network{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster",
Namespace: "",
},
Status: testCase.networkStatus,
}
r := createFakeStorageClusterReconciler(t, networkConfig)
configMap := &corev1.ConfigMap{}
sc := &api.StorageCluster{
ObjectMeta: metav1.ObjectMeta{Namespace: "test"},
Spec: api.StorageClusterSpec{Network: &testCase.rookCephNetworkSpec},
}
cephConfigReconciler := &ocsCephConfig{}
_, err := cephConfigReconciler.ensureCreated(&r, sc)
if err != nil {
assert.NilError(t, err, "ensure created failed")
}
// get the output
err = r.Client.Get(context.TODO(), types.NamespacedName{Name: rookOverrideConfigMapName, Namespace: "test"}, configMap)
assert.NilError(t, err, "expected to find configmap %q: %+v", rookOverrideConfigMapName, err)
// compare with the expected results
cfg, err := ini.Load([]byte(configMap.Data["config"]))
assert.NilError(t, err, "expected ini string to load")
sect, err := cfg.GetSection(globalSectionKey)
assert.NilError(t, err, "expected section to exist")
// check if "public_network" key exists
keyFound := sect.HasKey(publicNetworkKey)
assert.Equal(t, keyFound, testCase.expectPublicNetwork)
// check that the keys are the same
if testCase.expectPublicNetwork && keyFound {
assert.Equal(t, sect.Key(publicNetworkKey).Value(), testCase.expectedPublicNetworkValue, "expected public network value to match")
}
}
}
func TestMonTargetPGPerOSD(t *testing.T) {
testTable := []struct {
label string
existingConfig string
reconcileCount int
expectedValue string
shouldExist bool
}{
{
label: "Fresh install - should set mon_target_pg_per_osd=200",
existingConfig: "",
reconcileCount: 2,
expectedValue: "200",
shouldExist: true,
},
{
label: "Existing cluster - should not add mon_target_pg_per_osd",
existingConfig: `[global]
some_other_key = value
`,
reconcileCount: 1,
expectedValue: "",
shouldExist: false,
},
}
for i, testCase := range testTable {
t.Logf("Case #%+v: %s", i+1, testCase.label)
r := createFakeStorageClusterReconciler(t)
sc := &api.StorageCluster{
ObjectMeta: metav1.ObjectMeta{Namespace: "test"},
}
// Create existing ConfigMap if specified
if testCase.existingConfig != "" {
existingCM := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: rookOverrideConfigMapName,
Namespace: sc.Namespace,
CreationTimestamp: metav1.Now(),
},
Data: map[string]string{
"config": testCase.existingConfig,
},
}
err := r.Client.Create(context.TODO(), existingCM)
assert.NilError(t, err, "failed to create existing configmap")
}
cephConfigReconciler := &ocsCephConfig{}
for j := 0; j < testCase.reconcileCount; j++ {
_, err := cephConfigReconciler.ensureCreated(&r, sc)
assert.NilError(t, err, "reconcile %d failed", j+1)
// Verify ConfigMap after each reconcile
configMap := &corev1.ConfigMap{}
err = r.Client.Get(context.TODO(), types.NamespacedName{Name: rookOverrideConfigMapName, Namespace: sc.Namespace}, configMap)
assert.NilError(t, err, "expected to find configmap")
cfg, err := ini.Load([]byte(configMap.Data["config"]))
assert.NilError(t, err, "expected ini string to load")
sect, err := cfg.GetSection(globalSectionKey)
assert.NilError(t, err, "expected section to exist")
keyFound := sect.HasKey(targetPgPerOSDKey)
assert.Equal(t, keyFound, testCase.shouldExist, "mon_target_pg_per_osd key existence mismatch")
if testCase.shouldExist {
assert.Equal(t, sect.Key(targetPgPerOSDKey).Value(), testCase.expectedValue, "mon_target_pg_per_osd value mismatch")
}
}
}
}