Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logstash controller unit and integration tests #6575

Merged
merged 11 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions config/samples/logstash/logstash_svc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ spec:
config:
log.level: info
api.http.host: "0.0.0.0"
api.http.port: 9601
queue.type: memory
services:
- name: api
service:
spec:
type: ClusterIP
ports:
- port: 9600
- port: 9601
name: "api"
protocol: TCP
targetPort: 9600
targetPort: 9601
- name: beats
service:
spec:
Expand Down
150 changes: 150 additions & 0 deletions pkg/controller/logstash/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.

package logstash

import (
"context"
"testing"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"

commonv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/common/v1"
"github.com/elastic/cloud-on-k8s/v2/pkg/apis/logstash/v1alpha1"
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/watches"
"github.com/elastic/cloud-on-k8s/v2/pkg/utils/k8s"
)

func Test_newConfig(t *testing.T) {
type args struct {
runtimeObjs []runtime.Object
logstash v1alpha1.Logstash
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "no user config",
args: args{
runtimeObjs: nil,
logstash: v1alpha1.Logstash{},
},
want: `api:
http:
host: 0.0.0.0
`,
wantErr: false,
},
{
name: "inline user config",
args: args{
runtimeObjs: nil,
logstash: v1alpha1.Logstash{
Spec: v1alpha1.LogstashSpec{Config: &commonv1.Config{Data: map[string]interface{}{
"log.level": "debug",
}}},
},
},
want: `api:
http:
host: 0.0.0.0
log:
level: debug
`,
wantErr: false,
},
{
name: "with configRef",
args: args{
runtimeObjs: []runtime.Object{secretWithConfig("cfg", []byte("log.level: debug"))},
logstash: logstashWithConfigRef("cfg", nil),
},
want: `api:
http:
host: 0.0.0.0
log:
level: debug
`,
wantErr: false,
},
{
name: "config takes precedence",
args: args{
runtimeObjs: []runtime.Object{secretWithConfig("cfg", []byte("log.level: debug"))},
logstash: logstashWithConfigRef("cfg", &commonv1.Config{Data: map[string]interface{}{
"log.level": "warn",
}}),
},
want: `api:
http:
host: 0.0.0.0
log:
level: warn
`,
wantErr: false,
},
{
name: "non existing configRef",
args: args{
logstash: logstashWithConfigRef("cfg", nil),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
params := Params{
Context: context.Background(),
Client: k8s.NewFakeClient(tt.args.runtimeObjs...),
EventRecorder: record.NewFakeRecorder(10),
Watches: watches.NewDynamicWatches(),
Logstash: tt.args.logstash,
}

got, err := buildConfig(params)
if (err != nil) != tt.wantErr {
t.Errorf("newConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
return // no point in checking the config contents
}
require.NoError(t, err)
if string(got) != tt.want {
t.Errorf("newConfig() got = \n%v\n, want \n%v\n", string(got), tt.want)
}
})
}
}

func secretWithConfig(name string, cfg []byte) *corev1.Secret {
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: name,
},
Data: map[string][]byte{
LogstashConfigFileName: cfg,
},
}
}

func logstashWithConfigRef(name string, cfg *commonv1.Config) v1alpha1.Logstash {
return v1alpha1.Logstash{
ObjectMeta: metav1.ObjectMeta{
Name: "ls",
Namespace: "ns",
},
Spec: v1alpha1.LogstashSpec{
Config: cfg,
ConfigRef: &commonv1.ConfigSource{SecretRef: commonv1.SecretRef{SecretName: name}}},
}
}
Loading