Skip to content

Commit

Permalink
Updates Metricbeat stack module tests to use require.* (#15993)
Browse files Browse the repository at this point in the history
* Replace assert.* with require.*

* Replace assert.* with require.*

* Fixing imports formatting

* More uses of require.*

* Fixing method call args
  • Loading branch information
ycombinator authored Feb 5, 2020
1 parent 95a7e10 commit 56512c7
Show file tree
Hide file tree
Showing 23 changed files with 150 additions and 182 deletions.
12 changes: 4 additions & 8 deletions metricbeat/module/beat/beat_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package beat_test
import (
"testing"

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

"github.com/elastic/beats/libbeat/tests/compose"
mbtest "github.com/elastic/beats/metricbeat/mb/testing"
Expand All @@ -43,10 +43,8 @@ func TestFetch(t *testing.T) {
f := mbtest.NewReportingMetricSetV2Error(t, beat.GetConfig(metricSet, service.Host()))
events, errs := mbtest.ReportingFetchV2Error(f)

assert.Empty(t, errs)
if !assert.NotEmpty(t, events) {
t.FailNow()
}
require.Empty(t, errs)
require.NotEmpty(t, events)

t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(),
events[0].BeatEvent("beat", metricSet).Fields.StringToPrint())
Expand All @@ -59,8 +57,6 @@ func TestData(t *testing.T) {
for _, metricSet := range metricSets {
f := mbtest.NewReportingMetricSetV2Error(t, beat.GetConfig(metricSet, service.Host()))
err := mbtest.WriteEventsReporterV2Error(f, t, metricSet)
if err != nil {
t.Fatal("write", err)
}
require.NoError(t, err)
}
}
14 changes: 7 additions & 7 deletions metricbeat/module/beat/state/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

"github.com/elastic/beats/metricbeat/module/beat"

mbtest "github.com/elastic/beats/metricbeat/mb/testing"

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

func TestEventMapping(t *testing.T) {

files, err := filepath.Glob("./_meta/test/state.*.json")
assert.NoError(t, err)
require.NoError(t, err)

info := beat.Info{
UUID: "1234",
Expand All @@ -43,13 +43,13 @@ func TestEventMapping(t *testing.T) {

for _, f := range files {
input, err := ioutil.ReadFile(f)
assert.NoError(t, err)
require.NoError(t, err)

reporter := &mbtest.CapturingReporterV2{}
err = eventMapping(reporter, info, input)

assert.NoError(t, err, f)
assert.True(t, len(reporter.GetEvents()) >= 1, f)
assert.Equal(t, 0, len(reporter.GetErrors()), f)
require.NoError(t, err, f)
require.True(t, len(reporter.GetEvents()) >= 1, f)
require.Equal(t, 0, len(reporter.GetErrors()), f)
}
}
14 changes: 7 additions & 7 deletions metricbeat/module/beat/stats/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

"github.com/elastic/beats/metricbeat/module/beat"

mbtest "github.com/elastic/beats/metricbeat/mb/testing"

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

func TestEventMapping(t *testing.T) {

files, err := filepath.Glob("./_meta/test/stats.*.json")
assert.NoError(t, err)
require.NoError(t, err)

info := beat.Info{
UUID: "1234",
Expand All @@ -43,13 +43,13 @@ func TestEventMapping(t *testing.T) {

for _, f := range files {
input, err := ioutil.ReadFile(f)
assert.NoError(t, err)
require.NoError(t, err)

reporter := &mbtest.CapturingReporterV2{}
err = eventMapping(reporter, info, input)

assert.NoError(t, err, f)
assert.True(t, len(reporter.GetEvents()) >= 1, f)
assert.Equal(t, 0, len(reporter.GetErrors()), f)
require.NoError(t, err, f)
require.True(t, len(reporter.GetEvents()) >= 1, f)
require.Equal(t, 0, len(reporter.GetErrors()), f)
}
}
8 changes: 4 additions & 4 deletions metricbeat/module/elasticsearch/ccr/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"io/ioutil"
"testing"

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

mbtest "github.com/elastic/beats/metricbeat/mb/testing"
"github.com/elastic/beats/metricbeat/module/elasticsearch"
Expand All @@ -40,10 +40,10 @@ func TestMapper(t *testing.T) {

func TestEmpty(t *testing.T) {
input, err := ioutil.ReadFile("./_meta/test/empty.700.json")
assert.NoError(t, err)
require.NoError(t, err)

reporter := &mbtest.CapturingReporterV2{}
eventsMapping(reporter, info, input)
assert.Equal(t, 0, len(reporter.GetErrors()))
assert.Equal(t, 0, len(reporter.GetEvents()))
require.Equal(t, 0, len(reporter.GetErrors()))
require.Equal(t, 0, len(reporter.GetEvents()))
}
20 changes: 6 additions & 14 deletions metricbeat/module/elasticsearch/elasticsearch_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (

"github.com/pkg/errors"

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

"github.com/elastic/beats/libbeat/common"
Expand Down Expand Up @@ -80,9 +79,7 @@ func TestFetch(t *testing.T) {
host := service.Host()

version, err := getElasticsearchVersion(host)
if err != nil {
t.Fatal("getting elasticsearch version", err)
}
require.NoError(t, err)

setupTest(t, host, version)

Expand All @@ -92,10 +89,9 @@ func TestFetch(t *testing.T) {
f := mbtest.NewReportingMetricSetV2Error(t, getConfig(metricSet, host))
events, errs := mbtest.ReportingFetchV2Error(f)

assert.Empty(t, errs)
if !assert.NotEmpty(t, events) {
t.FailNow()
}
require.Empty(t, errs)
require.NotEmpty(t, events)

t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(),
events[0].BeatEvent("elasticsearch", metricSet).Fields.StringToPrint())
})
Expand All @@ -107,18 +103,14 @@ func TestData(t *testing.T) {
host := service.Host()

version, err := getElasticsearchVersion(host)
if err != nil {
t.Fatal("getting elasticsearch version", err)
}
require.NoError(t, err)

for _, metricSet := range metricSets {
t.Run(metricSet, func(t *testing.T) {
checkSkip(t, metricSet, version)
f := mbtest.NewReportingMetricSetV2Error(t, getConfig(metricSet, host))
err := mbtest.WriteEventsReporterV2Error(f, t, metricSet)
if err != nil {
t.Fatal("write", err)
}
require.NoError(t, err)
})
}
}
Expand Down
8 changes: 4 additions & 4 deletions metricbeat/module/elasticsearch/enrich/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"io/ioutil"
"testing"

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

mbtest "github.com/elastic/beats/metricbeat/mb/testing"
"github.com/elastic/beats/metricbeat/module/elasticsearch"
Expand All @@ -40,10 +40,10 @@ func TestMapper(t *testing.T) {

func TestEmpty(t *testing.T) {
input, err := ioutil.ReadFile("./_meta/test/empty.750.json")
assert.NoError(t, err)
require.NoError(t, err)

reporter := &mbtest.CapturingReporterV2{}
eventsMapping(reporter, info, input)
assert.Equal(t, 0, len(reporter.GetErrors()))
assert.Equal(t, 0, len(reporter.GetEvents()))
require.Equal(t, 0, len(reporter.GetErrors()))
require.Equal(t, 0, len(reporter.GetEvents()))
}
6 changes: 3 additions & 3 deletions metricbeat/module/elasticsearch/index/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"io/ioutil"
"testing"

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

mbtest "github.com/elastic/beats/metricbeat/mb/testing"
"github.com/elastic/beats/metricbeat/module/elasticsearch"
Expand All @@ -40,9 +40,9 @@ func TestMapper(t *testing.T) {

func TestEmpty(t *testing.T) {
input, err := ioutil.ReadFile("./_meta/test/empty.512.json")
assert.NoError(t, err)
require.NoError(t, err)

reporter := &mbtest.CapturingReporterV2{}
eventsMapping(reporter, info, input)
assert.Equal(t, 0, len(reporter.GetEvents()))
require.Equal(t, 0, len(reporter.GetEvents()))
}
8 changes: 4 additions & 4 deletions metricbeat/module/elasticsearch/index_summary/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"io/ioutil"
"testing"

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

mbtest "github.com/elastic/beats/metricbeat/mb/testing"
"github.com/elastic/beats/metricbeat/module/elasticsearch"
Expand All @@ -40,10 +40,10 @@ func TestMapper(t *testing.T) {

func TestEmpty(t *testing.T) {
input, err := ioutil.ReadFile("../index/_meta/test/empty.512.json")
assert.NoError(t, err)
require.NoError(t, err)

reporter := &mbtest.CapturingReporterV2{}
eventMapping(reporter, info, input)
assert.Empty(t, reporter.GetErrors())
assert.Equal(t, 1, len(reporter.GetEvents()))
require.Empty(t, reporter.GetErrors())
require.Equal(t, 1, len(reporter.GetEvents()))
}
8 changes: 4 additions & 4 deletions metricbeat/module/elasticsearch/node/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"io/ioutil"
"testing"

mbtest "github.com/elastic/beats/metricbeat/mb/testing"
"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"
mbtest "github.com/elastic/beats/metricbeat/mb/testing"

"github.com/elastic/beats/metricbeat/module/elasticsearch"
)
Expand All @@ -43,9 +43,9 @@ func TestInvalid(t *testing.T) {
file := "./_meta/test/invalid.json"

content, err := ioutil.ReadFile(file)
assert.NoError(t, err)
require.NoError(t, err)

reporter := &mbtest.CapturingReporterV2{}
err = eventsMapping(reporter, info, content)
assert.Error(t, err)
require.Error(t, err)
}
8 changes: 4 additions & 4 deletions metricbeat/module/elasticsearch/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"path/filepath"
"testing"

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

mbtest "github.com/elastic/beats/metricbeat/mb/testing"
"github.com/elastic/beats/metricbeat/module/elasticsearch"
Expand All @@ -35,14 +35,14 @@ import (
func TestFetch(t *testing.T) {

files, err := filepath.Glob("./_meta/test/node.*.json")
assert.NoError(t, err)
require.NoError(t, err)
// Makes sure glob matches at least 1 file
assert.True(t, len(files) > 0)
require.True(t, len(files) > 0)

for _, f := range files {
t.Run(f, func(t *testing.T) {
response, err := ioutil.ReadFile(f)
assert.NoError(t, err)
require.NoError(t, err)

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
Expand Down
Loading

0 comments on commit 56512c7

Please sign in to comment.