Skip to content

Commit dfe4966

Browse files
add unit test
Signed-off-by: Mayank Shah <mayank.shah@percona.com>
1 parent 6775fcd commit dfe4966

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

pkg/psmdb/logcollector/container_test.go

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
1111
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/config"
12+
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/logcollector/logrotate"
1213
"github.com/percona/percona-server-mongodb-operator/pkg/version"
1314
)
1415

@@ -25,8 +26,16 @@ func TestContainers(t *testing.T) {
2526
},
2627
},
2728
}
29+
testLogRotate := &api.LogRotateSpec{
30+
Configuration: "my-logrotate-config",
31+
ExtraConfig: corev1.LocalObjectReference{
32+
Name: "my-logrotate-extra-config",
33+
},
34+
Schedule: "0 0 * * *",
35+
}
2836
tests := map[string]struct {
2937
logCollector *api.LogCollectorSpec
38+
logRotate *api.LogRotateSpec
3039
secrets *corev1.Secret
3140
expectedContainerNames []string
3241
expectedContainers []corev1.Container
@@ -47,7 +56,7 @@ func TestContainers(t *testing.T) {
4756
ImagePullPolicy: corev1.PullIfNotPresent,
4857
},
4958
expectedContainerNames: []string{"logs", "logrotate"},
50-
expectedContainers: expectedContainers("", nil, nil),
59+
expectedContainers: expectedContainers("", nil, nil, nil),
5160
},
5261
"logcollector enabled with configuration": {
5362
logCollector: &api.LogCollectorSpec{
@@ -57,7 +66,7 @@ func TestContainers(t *testing.T) {
5766
Configuration: "my-config",
5867
},
5968
expectedContainerNames: []string{"logs", "logrotate"},
60-
expectedContainers: expectedContainers("my-config", nil, nil),
69+
expectedContainers: expectedContainers("my-config", nil, nil, nil),
6170
},
6271
"logcollector enabled with env variable": {
6372
logCollector: &api.LogCollectorSpec{
@@ -69,11 +78,25 @@ func TestContainers(t *testing.T) {
6978
EnvFrom: testEnvFrom,
7079
},
7180
expectedContainerNames: []string{"logs", "logrotate"},
72-
expectedContainers: expectedContainers("my-config", testEnvVar, testEnvFrom),
81+
expectedContainers: expectedContainers("my-config", testEnvVar, testEnvFrom, nil),
82+
},
83+
"logcollector enabled with logrotate": {
84+
logCollector: &api.LogCollectorSpec{
85+
Enabled: true,
86+
Image: "log-test-image",
87+
ImagePullPolicy: corev1.PullIfNotPresent,
88+
},
89+
logRotate: testLogRotate,
90+
expectedContainerNames: []string{"logs", "logrotate"},
91+
expectedContainers: expectedContainers("", nil, nil, testLogRotate),
7392
},
7493
}
7594

7695
for name, tt := range tests {
96+
logColl := tt.logCollector
97+
if tt.logRotate != nil {
98+
logColl.LogRotate = tt.logRotate
99+
}
77100
t.Run(name, func(t *testing.T) {
78101
cr := &api.PerconaServerMongoDB{
79102
ObjectMeta: metav1.ObjectMeta{
@@ -82,7 +105,7 @@ func TestContainers(t *testing.T) {
82105
},
83106
Spec: api.PerconaServerMongoDBSpec{
84107
CRVersion: version.Version(),
85-
LogCollector: tt.logCollector,
108+
LogCollector: logColl,
86109
Secrets: &api.SecretsSpec{
87110
Users: "users-secret",
88111
},
@@ -109,7 +132,12 @@ func TestContainers(t *testing.T) {
109132
}
110133
}
111134

112-
func expectedContainers(configuration string, envVars []corev1.EnvVar, envFrom []corev1.EnvFromSource) []corev1.Container {
135+
func expectedContainers(
136+
configuration string,
137+
envVars []corev1.EnvVar,
138+
envFrom []corev1.EnvFromSource,
139+
logrotateConfig *api.LogRotateSpec,
140+
) []corev1.Container {
113141
envs := []corev1.EnvVar{
114142
{Name: "LOG_DATA_DIR", Value: config.MongodContainerDataLogsDir},
115143
{
@@ -193,5 +221,20 @@ func expectedContainers(configuration string, envVars []corev1.EnvVar, envFrom [
193221
},
194222
}
195223

224+
if logrotateConfig != nil {
225+
if logrotateConfig.Configuration != "" || logrotateConfig.ExtraConfig.Name != "" {
226+
logRotateC.VolumeMounts = append(logRotateC.VolumeMounts, corev1.VolumeMount{
227+
Name: logrotate.VolumeName,
228+
MountPath: "/opt/percona/logcollector/logrotate/conf.d",
229+
})
230+
}
231+
if logrotateConfig.Schedule != "" {
232+
logRotateC.Env = append(logRotateC.Env, corev1.EnvVar{
233+
Name: "LOGROTATE_SCHEDULE",
234+
Value: logrotateConfig.Schedule,
235+
})
236+
}
237+
}
238+
196239
return []corev1.Container{logsC, logRotateC}
197240
}

0 commit comments

Comments
 (0)