-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathpsmdb_controller_test.go
More file actions
210 lines (183 loc) · 5.25 KB
/
psmdb_controller_test.go
File metadata and controls
210 lines (183 loc) · 5.25 KB
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
package perconaservermongodb
import (
"context"
"os"
"testing"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
psmdbv1 "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
)
func TestGetReconcileInterval(t *testing.T) {
tests := []struct {
name string
envValue string
setEnv bool
want time.Duration
}{
{
name: "unset",
setEnv: false,
want: 5 * time.Second,
},
{
name: "valid duration",
envValue: "30s",
setEnv: true,
want: 30 * time.Second,
},
{
name: "invalid duration falls back to default",
envValue: "invalid",
setEnv: true,
want: 5 * time.Second,
},
{
name: "zero duration falls back to default",
envValue: "0s",
setEnv: true,
want: 5 * time.Second,
},
{
name: "negative duration falls back to default",
envValue: "-5s",
setEnv: true,
want: 5 * time.Second,
},
{
name: "duration less than 5s falls back to default",
envValue: "1s",
setEnv: true,
want: 5 * time.Second,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
err := os.Unsetenv("RECONCILE_INTERVAL")
require.NoError(t, err)
}()
if tt.setEnv {
err := os.Setenv("RECONCILE_INTERVAL", tt.envValue)
require.NoError(t, err)
}
got := getReconcileInterval()
assert.Equal(t, tt.want, got)
})
}
}
var _ = Describe("PerconaServerMongoDB", Ordered, func() {
ctx := context.Background()
const ns = "psmdb"
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: ns,
Namespace: ns,
},
}
crName := ns + "-reconciler"
crNamespacedName := types.NamespacedName{Name: crName, Namespace: ns}
BeforeAll(func() {
By("Creating the Namespace to perform the tests")
err := k8sClient.Create(ctx, namespace)
Expect(err).To(Not(HaveOccurred()))
})
AfterAll(func() {
By("Deleting the Namespace to perform the tests")
_ = k8sClient.Delete(ctx, namespace)
})
Context("Create PerconaServerMongoDB", func() {
cr, err := readDefaultCR(crName, ns)
It("should read defautl cr.yaml", func() {
Expect(err).NotTo(HaveOccurred())
})
It("Should create PerconaServerMongoDB", func() {
Expect(k8sClient.Create(ctx, cr)).Should(Succeed())
})
})
It("Should reconcile PerconaServerMongoDB", func() {
_, err := reconciler().Reconcile(ctx, reconcile.Request{
NamespacedName: crNamespacedName,
})
Expect(err).To(Succeed())
})
})
var _ = Describe("PerconaServerMongoDB CRD Validation", Ordered, func() {
ctx := context.Background()
const ns = "psmdb-validation"
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: ns,
Namespace: ns,
},
}
BeforeAll(func() {
By("Creating the Namespace to perform the tests")
err := k8sClient.Create(ctx, namespace)
Expect(err).To(Not(HaveOccurred()))
})
AfterAll(func() {
By("Deleting the Namespace to perform the tests")
_ = k8sClient.Delete(ctx, namespace)
})
Context("StorageScaling validation", func() {
It("should reject autoscaling enabled when enableVolumeScaling is disabled", func() {
cr, err := readDefaultCR("psmdb-invalid-autoscaling", ns)
Expect(err).NotTo(HaveOccurred())
cr.Spec.StorageScaling = &psmdbv1.StorageScalingSpec{
EnableVolumeScaling: false,
Autoscaling: &psmdbv1.AutoscalingSpec{
Enabled: true,
},
}
err = k8sClient.Create(ctx, cr)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("autoscaling cannot be enabled when enableVolumeScaling is disabled"))
})
It("should allow autoscaling enabled when enableVolumeScaling is enabled", func() {
cr, err := readDefaultCR("psmdb-valid-autoscaling", ns)
Expect(err).NotTo(HaveOccurred())
cr.Spec.StorageScaling = &psmdbv1.StorageScalingSpec{
EnableVolumeScaling: true,
Autoscaling: &psmdbv1.AutoscalingSpec{
Enabled: true,
},
}
err = k8sClient.Create(ctx, cr)
Expect(err).NotTo(HaveOccurred())
})
It("should reject autoscaling enabled when enableExternalAutoscaling is enabled", func() {
cr, err := readDefaultCR("psmdb-invalid-external-autoscaling", ns)
Expect(err).NotTo(HaveOccurred())
cr.Spec.StorageScaling = &psmdbv1.StorageScalingSpec{
EnableVolumeScaling: true,
EnableExternalAutoscaling: true,
Autoscaling: &psmdbv1.AutoscalingSpec{
Enabled: true,
},
}
err = k8sClient.Create(ctx, cr)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("autoscaling cannot be enabled when enableExternalAutoscaling is enabled"))
})
It("should allow autoscaling enabled when enableExternalAutoscaling is disabled", func() {
cr, err := readDefaultCR("psmdb-valid-external-autoscaling", ns)
Expect(err).NotTo(HaveOccurred())
cr.Spec.StorageScaling = &psmdbv1.StorageScalingSpec{
EnableVolumeScaling: true,
EnableExternalAutoscaling: false,
Autoscaling: &psmdbv1.AutoscalingSpec{
Enabled: true,
},
}
err = k8sClient.Create(ctx, cr)
Expect(err).NotTo(HaveOccurred())
})
})
})