Skip to content

Commit

Permalink
Convert some libbeat python tests to go (#35959)
Browse files Browse the repository at this point in the history
Migrates some of the libbeat python integration tests to go

---------

Co-authored-by: Amit Kanfer <amit.kanfer@elastic.co>
  • Loading branch information
leehinman and amitkanfer authored Jul 28, 2023
1 parent b2d5017 commit 224e3e1
Show file tree
Hide file tree
Showing 30 changed files with 2,344 additions and 1,381 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestLegacyMetrics(t *testing.T) {
filebeat.WriteConfigFile(cfg)
filebeat.Start()

filebeat.WaitForLogs("Metrics endpoint listening on:", 10*time.Second, "The metric server is not running")
filebeat.WaitForLogs("Metrics endpoint listening on:", 10*time.Second)

// After starting Filebeat all counters must be zero
waitForMetrics(t,
Expand Down
2 changes: 1 addition & 1 deletion libbeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func IntegTest() {

// GoIntegTest starts the docker containers and executes the Go integration tests.
func GoIntegTest(ctx context.Context) error {
mg.Deps(Fields)
mg.Deps(Fields, devtools.BuildSystemTestBinary)
args := devtools.DefaultGoTestIntegrationFromHostArgs()
// ES_USER must be admin in order for the Go Integration tests to function because they require
// indices:data/read/search
Expand Down
209 changes: 209 additions & 0 deletions libbeat/tests/integration/base_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

//go:build integration

package integration

import (
"runtime"
"syscall"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestBase(t *testing.T) {
cfg := `
mockbeat:
name:
queue.mem:
events: 4096
flush.min_events: 8
flush.timeout: 0.1s
output.console:
code.json:
pretty: true
`
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test")
mockbeat.WriteConfigFile(cfg)
mockbeat.Start()
mockbeat.WaitForLogs("mockbeat start running.", 60*time.Second)
mockbeat.Stop()
mockbeat.WaitForLogs("mockbeat stopped.", 30*time.Second)
}

func TestSigHUP(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("sighup not supported on windows")
}
cfg := `
mockbeat:
name:
queue.mem:
events: 4096
flush.min_events: 8
flush.timeout: 0.1s
output.console:
code.json:
pretty: true
`
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test")
mockbeat.WriteConfigFile(cfg)
mockbeat.Start()
mockbeat.WaitForLogs("mockbeat start running.", 60*time.Second)
err := mockbeat.Process.Signal(syscall.SIGHUP)
require.NoErrorf(t, err, "error sending SIGHUP to mockbeat")
mockbeat.Stop()
mockbeat.WaitForLogs("mockbeat stopped.", 30*time.Second)
}

func TestNoConfig(t *testing.T) {
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test")
mockbeat.Start()
procState, err := mockbeat.Process.Wait()
require.NoError(t, err, "error waiting for mockbeat to exit")
require.Equal(t, 1, procState.ExitCode(), "incorrect exit code")
mockbeat.WaitStdErrContains("error loading config file", 10*time.Second)
}

func TestInvalidConfig(t *testing.T) {
cfg := `
test:
test were
: invalid yml
`
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test")
mockbeat.WriteConfigFile(cfg)
mockbeat.Start()
procState, err := mockbeat.Process.Wait()
require.NoError(t, err, "error waiting for mockbeat to exit")
require.Equal(t, 1, procState.ExitCode(), "incorrect exit code")
mockbeat.WaitStdErrContains("error loading config file", 10*time.Second)
}

func TestInvalidCLI(t *testing.T) {
cfg := `
mockbeat:
name:
queue.mem:
events: 4096
flush.min_events: 8
flush.timeout: 0.1s
output.console:
code.json:
pretty: true
`
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test", "-d", "config", "-E", "output.console=invalid")
mockbeat.WriteConfigFile(cfg)
mockbeat.Start()
procState, err := mockbeat.Process.Wait()
require.NoError(t, err, "error waiting for mockbeat to exit")
require.Equal(t, 1, procState.ExitCode(), "incorrect exit code")
mockbeat.WaitStdErrContains("error unpacking config data", 10*time.Second)
}

func TestConsoleOutput(t *testing.T) {
cfg := `
mockbeat:
name:
queue.mem:
events: 4096
flush.min_events: 8
flush.timeout: 0.1s
output.console:
code.json:
pretty: false
`
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test", "-e")
mockbeat.WriteConfigFile(cfg)
mockbeat.Start()
mockbeat.WaitStdErrContains("mockbeat start running.", 10*time.Second)
mockbeat.WaitStdOutContains("Mockbeat is alive", 10*time.Second)
}

func TestConsoleBulkMaxSizeOutput(t *testing.T) {
cfg := `
mockbeat:
name:
queue.mem:
events: 4096
flush.min_events: 8
flush.timeout: 0.1s
output.console:
code.json:
pretty: false
bulk_max_size: 1
`
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test", "-e")
mockbeat.WriteConfigFile(cfg)
mockbeat.Start()
mockbeat.WaitStdErrContains("mockbeat start running.", 10*time.Second)
mockbeat.WaitStdOutContains("Mockbeat is alive", 10*time.Second)
}

func TestLoggingMetrics(t *testing.T) {
cfg := `
mockbeat:
name:
queue.mem:
events: 4096
flush.min_events: 8
flush.timeout: 0.1s
output.console:
code.json:
pretty: true
logging:
metrics:
period: 0.1s
`
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test", "-e")
mockbeat.WriteConfigFile(cfg)
mockbeat.Start()
mockbeat.WaitStdErrContains("mockbeat start running.", 10*time.Second)
mockbeat.WaitStdErrContains("Non-zero metrics in the last", 10*time.Second)
mockbeat.Stop()
mockbeat.WaitStdErrContains("Total metrics", 10*time.Second)
}

func TestPersistentUuid(t *testing.T) {
cfg := `
mockbeat:
name:
queue.mem:
events: 4096
flush.min_events: 8
flush.timeout: 0.1s
output.console:
code.json:
pretty: true
`
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test", "-e")
mockbeat.WriteConfigFile(cfg)
mockbeat.Start()
mockbeat.WaitStdErrContains("mockbeat start running.", 10*time.Second)

metaFile1, err := mockbeat.LoadMeta()
require.NoError(t, err, "error opening meta.json file")
mockbeat.Stop()
mockbeat.WaitStdErrContains("Beat ID: "+metaFile1.UUID.String(), 10*time.Second)
mockbeat.Start()
mockbeat.WaitStdErrContains("mockbeat start running.", 10*time.Second)
metaFile2, err := mockbeat.LoadMeta()
require.Equal(t, metaFile1.UUID.String(), metaFile2.UUID.String())
}
89 changes: 89 additions & 0 deletions libbeat/tests/integration/ca_pinning_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

//go:build integration

package integration

import (
"fmt"
"path/filepath"
"testing"
"time"
)

func TestCAPinningGoodSHA(t *testing.T) {
EnsureESIsRunning(t)
esURL := GetESURL(t, "https")
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test")
caPath := filepath.Join(mockbeat.TempDir(), "../../../../", "testing", "environments", "docker", "elasticsearch", "pki", "ca", "ca.crt")
cfg := `
mockbeat:
name:
logging:
level: debug
queue.mem:
events: 4096
flush.min_events: 8
flush.timeout: 0.1s
output.elasticsearch:
hosts:
- %s
username: admin
password: testing
allow_older_versions: true
ssl:
verification_mode: certificate
certificate_authorities: %s
ca_sha256: FDFOtqdUyXZw74YgvAJUC+I67ED1WfcI1qK44Qy2WQM=
`
mockbeat.WriteConfigFile(fmt.Sprintf(cfg, esURL.String(), caPath))
mockbeat.Start()
mockbeat.WaitForLogs("mockbeat start running.", 60*time.Second)
mockbeat.WaitForLogs("PublishEvents: 1 events have been published", 60*time.Second)
}

func TestCAPinningBadSHA(t *testing.T) {
EnsureESIsRunning(t)
esURL := GetESURL(t, "https")
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test")
caPath := filepath.Join(mockbeat.TempDir(), "../../../../", "testing", "environments", "docker", "elasticsearch", "pki", "ca", "ca.crt")
cfg := `
mockbeat:
name:
logging:
level: debug
queue.mem:
events: 4096
flush.min_events: 8
flush.timeout: 0.1s
output.elasticsearch:
hosts:
- %s
username: admin
password: testing
allow_older_versions: true
ssl:
verification_mode: certificate
certificate_authorities: %s
ca_sha256: bad
`
mockbeat.WriteConfigFile(fmt.Sprintf(cfg, esURL.String(), caPath))
mockbeat.Start()
mockbeat.WaitForLogs("mockbeat start running.", 60*time.Second)
mockbeat.WaitForLogs("provided CA certificate pins doesn't match any of the certificate authorities used to validate the certificate", 60*time.Second)
}
55 changes: 55 additions & 0 deletions libbeat/tests/integration/cmd_completion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

//go:build integration

package integration

import (
"testing"
"time"
)

func TestShellCompletion(t *testing.T) {
cfg := `
mockbeat:
name:
queue.mem:
events: 4096
flush.min_events: 8
flush.timeout: 0.1s
output.console:
code.json:
pretty: true
`
tests := map[string]struct {
shell string
expected string
}{
"bash completion": {shell: "bash", expected: "bash completion for mockbeat"},
"zsh completion": {shell: "zsh", expected: "#compdef _mockbeat mockbeat"},
"awesomeshell": {shell: "awesomeshell", expected: "Unknown shell awesomeshell"},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test", "completion", tc.shell)
mockbeat.WriteConfigFile(cfg)
mockbeat.Start()
mockbeat.WaitStdOutContains(tc.expected, 10*time.Second)
})
}
}
Loading

0 comments on commit 224e3e1

Please sign in to comment.