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

[Metricbeat] Simplify testing http Metricbeat modules #10648

Merged
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: 0 additions & 5 deletions metricbeat/mb/testing/data_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,6 @@ func StandardizeEvent(ms mb.MetricSet, e mb.Event, modifiers ...mb.EventModifier

fullEvent := e.BeatEvent(ms.Module().Name(), ms.Name(), modifiers...)

fullEvent.Fields["agent"] = common.MapStr{
"name": "host.example.com",
"hostname": "host.example.com",
}

return fullEvent
}

Expand Down
198 changes: 198 additions & 0 deletions metricbeat/mb/testing/data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
// 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.

package testing

import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"

// TODO: generate include file for these tests automatically moving forward
_ "github.com/elastic/beats/metricbeat/module/kibana/status"
_ "github.com/elastic/beats/metricbeat/module/rabbitmq/connection"
_ "github.com/elastic/beats/metricbeat/module/traefik/health"
)

const (
expectedExtension = "-expected.json"
)

var (
// Use `go test -generate` to update files.
generateFlag = flag.Bool("generate", false, "Write golden files")
)

type Config struct {
Type string
URL string
}

func TestAll(t *testing.T) {

configFiles, _ := filepath.Glob(getModulesPath() + "/*/*/_meta/testdata/config.yml")

for _, f := range configFiles {
// get module and metricset name from path
s := strings.Split(f, string(os.PathSeparator))
moduleName := s[3]
metricSetName := s[4]

configFile, err := ioutil.ReadFile(f)
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
var config Config
err = yaml.Unmarshal(configFile, &config)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}

getTestdataFiles(t, config.URL, moduleName, metricSetName)
}
}

func getTestdataFiles(t *testing.T, url, module, metricSet string) {

ff, _ := filepath.Glob(getMetricsetPath(module, metricSet) + "/_meta/testdata/*.json")
var files []string
for _, f := range ff {
// Exclude all the expected files
if strings.HasSuffix(f, expectedExtension) {
continue
}
files = append(files, f)
}

for _, f := range files {
t.Run(f, func(t *testing.T) {
runTest(t, f, module, metricSet, url)
})
}
}

func runTest(t *testing.T, file string, module, metricSetName, url string) {

// starts a server serving the given file under the given url
s := server(t, file, url)
defer s.Close()

metricSet := NewReportingMetricSetV2(t, getConfig(module, metricSetName, s.URL))
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
events, errs := ReportingFetchV2(metricSet)

// Gather errors to build also error events
for _, e := range errs {
// TODO: for errors strip out and standardise the URL error as it would create a different diff every time
events = append(events, mb.Event{Error: e})
}

var data []common.MapStr

for _, e := range events {
beatEvent := StandardizeEvent(metricSet, e, mb.AddMetricSetInfo)
// Overwrite service.address as the port changes every time
beatEvent.Fields.Put("service.address", "127.0.0.1:55555")
data = append(data, beatEvent.Fields)
}

output, err := json.MarshalIndent(&data, "", " ")
if err != nil {
t.Fatal(err)
}

// Overwrites the golden files if run with -generate
if *generateFlag {
if err = ioutil.WriteFile(file+expectedExtension, output, 0644); err != nil {
t.Fatal(err)
}
}

// Read expected file
expected, err := ioutil.ReadFile(file + expectedExtension)
if err != nil {
t.Fatalf("could not read file: %s", err)
}

assert.Equal(t, string(expected), string(output))

if strings.HasSuffix(file, "docs.json") {
writeDataJSON(t, data[0], module, metricSetName)
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
}
}

func writeDataJSON(t *testing.T, data common.MapStr, module, metricSet string) {
// Add hardcoded timestamp
data.Put("@timestamp", "2019-03-01T08:05:34.853Z")
output, err := json.MarshalIndent(&data, "", " ")
if err = ioutil.WriteFile(getMetricsetPath(module, metricSet)+"/_meta/data.json", output, 0644); err != nil {
t.Fatal(err)
}
}

// GetConfig returns config for elasticsearch module
func getConfig(module, metricSet, url string) map[string]interface{} {
return map[string]interface{}{
"module": module,
"metricsets": []string{metricSet},
"hosts": []string{url},
}
}

// server starts a server with a mock output
func server(t *testing.T, path string, url string) *httptest.Server {

body, err := ioutil.ReadFile(path)
if err != nil {
t.Fatalf("could not read file: %s", err)
}

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == url {
w.Header().Set("Content-Type", "application/json;")
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
w.WriteHeader(200)
w.Write(body)
} else {
w.WriteHeader(404)
}
}))
return server
}

func getModulesPath() string {
return "../../module"
}

func getModulePath(module string) string {
return getModulesPath() + "/" + module
}

func getMetricsetPath(module, metricSet string) string {
return getModulePath(module) + "/" + metricSet
}
18 changes: 7 additions & 11 deletions metricbeat/module/kibana/status/_meta/data.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{
"@timestamp": "2017-10-12T08:05:34.853Z",
"agent": {
"hostname": "host.example.com",
"name": "host.example.com"
},
"@timestamp": "2019-03-01T08:05:34.853Z",
"event": {
"dataset": "kibana.status",
"duration": 115000,
Expand All @@ -12,13 +8,13 @@
"kibana": {
"status": {
"metrics": {
"concurrent_connections": 0,
"concurrent_connections": 12,
"requests": {
"disconnects": 0,
"total": 0
"disconnects": 3,
"total": 241
}
},
"name": "Shaunaks-MBP-2",
"name": "ruflin",
"status": {
"overall": {
"state": "green"
Expand All @@ -30,10 +26,10 @@
"name": "status"
},
"service": {
"address": "127.0.0.1:5601",
"address": "127.0.0.1:55555",
"id": "5b2de169-2785-441b-ae8c-186a1936b17d",
"name": "kibana",
"type": "kibana",
"version": "7.0.0"
"version": "6.0.0-alpha1"
}
}
Loading