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

Conversation

robbavey
Copy link
Member

This commit adds extra unit tests for the Logstash Controller, including tests for config, controller, pods and services

@botelastic botelastic bot added the triage label Mar 23, 2023
@thbkrkr thbkrkr added the >test Related to unit/integration/e2e tests label Mar 24, 2023
@botelastic botelastic bot removed the triage label Mar 24, 2023
@thbkrkr thbkrkr changed the title Controller tests Logstash controller unit and integration tests Mar 24, 2023
@robbavey robbavey requested review from barkbay and kaisecheng April 3, 2023 13:24
Copy link
Contributor

@kaisecheng kaisecheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for adding the tests. Mostly looks good to me. I left three comments for you to consider.

wantErr: false,
},
{
name: "configRef takes precedence",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be "config takes precedence"


// GetLogstashContainer returns the Logstash container from the given podSpec.
func GetLogstashContainer(podSpec corev1.PodSpec) *corev1.Container {
return pod.ContainerByName(podSpec, "logstash")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},
},
}
for _, tt := range tests {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add test coverage for custom ports, annotation and readinessProbe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (I think...)

Copy link
Contributor

@barkbay barkbay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

pkg/controller/logstash/logstash_controller_test.go Outdated Show resolved Hide resolved
pkg/controller/logstash/pod_test.go Outdated Show resolved Hide resolved
pkg/controller/logstash/logstash_controller_test.go Outdated Show resolved Hide resolved
pkg/controller/logstash/services_test.go Outdated Show resolved Hide resolved
test/e2e/test/logstash/http_client.go Outdated Show resolved Hide resolved
pkg/controller/logstash/pod.go Outdated Show resolved Hide resolved
Also updated to take account of pipelines
@robbavey
Copy link
Member Author

robbavey commented Apr 6, 2023

@barkbay Ready for another round

@barkbay
Copy link
Contributor

barkbay commented Apr 11, 2023

@elasticmachine run elasticsearch-ci/docs

var port = network.HTTPPort
for _, service := range logstash.Spec.Services {
if service.Name == LogstashAPIServiceName {
port = int(service.Service.Spec.Ports[0].Port)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed it during the first review but .Service.Spec.Ports can be empty. It is possible for a user to crash the operator by creating a Service with an empty slice, for example:

apiVersion: logstash.k8s.elastic.co/v1alpha1
kind: Logstash
metadata:
  name: logstash-sample
spec:
  count: 2
  version: 8.6.1
  config:
    log.level: info
    api.http.host: "0.0.0.0"
    api.http.port: 9601
    queue.type: memory
  services:
    - name: api
      service:
        spec:
          type: ExternalName
          externalName: foo
          ports: []

The slice length must be checked before accessing the index.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Fixed

var port = network.HTTPPort
for _, service := range logstash.Spec.Services {
if service.Name == ls.LogstashAPIServiceName {
port = int(service.Service.Spec.Ports[0].Port)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, we should check the length of the slice before accessing the index 0

@robbavey
Copy link
Member Author

@barkbay Nice find! I've pushed a fix and added a test to verify

Copy link
Contributor

@barkbay barkbay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@robbavey robbavey merged commit 797f1c9 into elastic:feature/logstash Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:logstash >test Related to unit/integration/e2e tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants