-
Notifications
You must be signed in to change notification settings - Fork 5k
[Metricbeat] Simplify testing http Metricbeat modules #10648
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
Merged
ruflin
merged 6 commits into
elastic:master
from
ruflin:metricbeat-module-data-json-generation
Mar 5, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
059917e
[Metricbeat] Simplify testing http Metricbeat modules
ruflin 06f0d1f
add missing files and headers
ruflin bf7b30d
apply review feedback
ruflin 976e12c
fix typo
ruflin 243a617
debug windows
ruflin e17d977
use path seperator
ruflin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
|
jsoriano marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| 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.
Outdated
|
||
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.