Skip to content

Commit

Permalink
[Metricbeat] Simplify testing http Metricbeat modules (#10648)
Browse files Browse the repository at this point in the history
Currently most modules are tested against a docker container. This leads to long setup times and potentially flakyness. Also it requires additional setup to test actual changes on a module without running CI. The goal of this PR is to reduce this overhead, make it possible to easily test new data sets without having to write go code. Expected files were added to verify that changes had no effect on the generated data. The tests with the environment are still needed but should become less critical during development.

The structure and logic is inspired by the testing of the Filebeat modules. So far 3 metricsets were convert to test the implementation. It's all based on conventions:

* Tests outputs from a JSON endpoint must go int `_meta/testdata`
* A `testdata/config.yml` file must exists to specify url under which the testdata should be served
* A golden files is generated by adding `-expected.json`.

For a metricset to be converted it must have the reporter interface, be http and json based and only have 1 endpoint requested at the time. All metricsets should be converted to the reporter interface.

As there is now a more global view on the testing of a metricset, this code can potentially also take over the check to make sure that all fields are documented or at least the generated files can be used to do these checks.

To support metricsets which generate one or multiple events the output is always an array of JSON objects. These arrays can also contain errors, meaning also invalid data can be tested.

The `data.json` we had so far was hard to update and changed every time it was updated because it was pulled from a life instance. For the metricsets that are switched over to this testing, it's not the case anymore. The `data.json` is generated from the first event in the `docs.json`. This is by convention and allows to have a `docs.json` with a specially interesting event. This should make condition checks for which event should be shown also partially obsolete.

Future work:

* Support multiple endpoints: Elasticsearch metricsets do not work with the above model yet as they need multiple endpoints to be available at the same time. Config options for this could be introduced.
* Support more then .json: Currently only .json is supported. More config options could be added to support other data formats for example for the apache module
* Support other protocols then http: Not all modules are http based, 2-3 other comments protocols could be added.
* Extend with additional config options: Some metricsets need additional config options to be set for testing. It should be possible to pass these as part of the config.yml file.
* Generate the includes automatically: Currently if a new directory with testdata is added to a metricset, it will be discovered by the tests but then throws and error because the metricset is not registered. The metricset then has to be manually added to the `data_test.go` file. This works for now but potentially should be automated.

The overall goal of all the above is to have Metricbeat modules more and more config based instead of golang code based.
  • Loading branch information
ruflin authored Mar 5, 2019
1 parent 54fe095 commit cca36b2
Show file tree
Hide file tree
Showing 19 changed files with 1,035 additions and 66 deletions.
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))
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)
}
}

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;")
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

0 comments on commit cca36b2

Please sign in to comment.