-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce approvaltest module (#4112)
* Introduce new approvaltest module We move tests/approvals and tests/scripts/approvals.go intoa new module, github.com/elastic/apm-server/approvaltest. This new module will be used by both apm-server (integration tests) and the systemtest module. We also reduce the API surface area to two functions: ApproveJSON, and ApproveEventDocs. The ApproveJSON function can be used for approving arbitrary JSON-encoded values, while ApproveEventDocs is specifically intended for approving JSON-encoded Elasticsearch event documents. To enable the API reduction, we introduce beatertest.EncodeEventDoc(s), which encode events using the libbeat Elasticsearch output. In some tests we previously used the libbeat JSON codec directly, which led to @metadata being included in diffs. The output removes @metadata, hence it no longer features in our approval diffs. * Update approved files - Since we now use the Elasticsearch output, rather than directly using the libbeat JSON codec, `@metadata` is no longer part of the diff, and `@timestamp` is encoded slightly differently. - For dynamic fields like `profile.id`, we now replace the value with a known string ("dynamic") rather than ignoring the field. * .ci/docker/golang-mage: support multiple modules
- Loading branch information
Showing
71 changed files
with
806 additions
and
1,996 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,141 @@ | ||
// 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 approvaltest | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/tidwall/gjson" | ||
"github.com/tidwall/sjson" | ||
) | ||
|
||
const ( | ||
// ApprovedSuffix signals a file has been reviewed and approved. | ||
ApprovedSuffix = ".approved.json" | ||
|
||
// ReceivedSuffix signals a file has changed and not yet been approved. | ||
ReceivedSuffix = ".received.json" | ||
) | ||
|
||
// ApproveEventDocs compares the given event documents with | ||
// the contents of the file in "<name>.approved.json". | ||
// | ||
// Any specified dynamic fields (e.g. @timestamp, observer.id) | ||
// will be replaced with a static string for comparison. | ||
// | ||
// If the events differ, then the test will fail. | ||
func ApproveEventDocs(t testing.TB, name string, eventDocs [][]byte, dynamic ...string) { | ||
t.Helper() | ||
|
||
// Rewrite all dynamic fields to have a known value, | ||
// so dynamic fields don't affect diffs. | ||
events := make([]interface{}, len(eventDocs)) | ||
for i, doc := range eventDocs { | ||
for _, field := range dynamic { | ||
existing := gjson.GetBytes(doc, field) | ||
if !existing.Exists() { | ||
continue | ||
} | ||
|
||
var err error | ||
doc, err = sjson.SetBytes(doc, field, "dynamic") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
var event map[string]interface{} | ||
if err := json.Unmarshal(doc, &event); err != nil { | ||
t.Fatal(err) | ||
} | ||
events[i] = event | ||
} | ||
|
||
received := map[string]interface{}{"events": events} | ||
approve(t, name, received) | ||
} | ||
|
||
// ApproveJSON compares the given JSON-encoded value with the | ||
// contents of the file "<name>.approved.json", by decoding the | ||
// value and passing it to Approve. | ||
// | ||
// If the value differs, then the test will fail. | ||
func ApproveJSON(t testing.TB, name string, encoded []byte) { | ||
t.Helper() | ||
|
||
var decoded interface{} | ||
if err := json.Unmarshal(encoded, &decoded); err != nil { | ||
t.Error(err) | ||
} | ||
approve(t, name, decoded) | ||
} | ||
|
||
// approve compares the given value with the contents of the file | ||
// "<name>.approved.json". | ||
// | ||
// If the value differs, then the test will fail. | ||
func approve(t testing.TB, name string, received interface{}) { | ||
t.Helper() | ||
|
||
var approved interface{} | ||
readApproved(name, &approved) | ||
if diff := cmp.Diff(approved, received); diff != "" { | ||
writeReceived(name, received) | ||
t.Fatalf("%s\n%s\n\n", diff, | ||
"Run `make update check-approvals` to verify the diff", | ||
) | ||
} else { | ||
// Remove an old *.received.json file if it exists. | ||
removeReceived(name) | ||
} | ||
} | ||
|
||
func readApproved(name string, approved interface{}) { | ||
path := name + ApprovedSuffix | ||
f, err := os.Open(path) | ||
if os.IsNotExist(err) { | ||
return | ||
} else if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
if err := json.NewDecoder(f).Decode(&approved); err != nil { | ||
panic(fmt.Errorf("cannot unmarshal file %q: %w", path, err)) | ||
} | ||
} | ||
|
||
func removeReceived(name string) { | ||
os.Remove(name + ReceivedSuffix) | ||
} | ||
|
||
func writeReceived(name string, received interface{}) { | ||
f, err := os.Create(name + ReceivedSuffix) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
enc := json.NewEncoder(f) | ||
enc.SetIndent("", " ") | ||
if err := enc.Encode(received); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains 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 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,9 @@ | ||
module github.com/elastic/apm-server/approvaltest | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/google/go-cmp v0.5.2 | ||
github.com/tidwall/gjson v1.6.0 | ||
github.com/tidwall/sjson v1.1.1 | ||
) |
This file contains 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,12 @@ | ||
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= | ||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc= | ||
github.com/tidwall/gjson v1.6.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls= | ||
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc= | ||
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E= | ||
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= | ||
github.com/tidwall/pretty v1.0.1 h1:WE4RBSZ1x6McVVC8S/Md+Qse8YUv6HRObAx6ke00NY8= | ||
github.com/tidwall/pretty v1.0.1/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= | ||
github.com/tidwall/sjson v1.1.1 h1:7h1vk049Jnd5EH9NyzNiEuwYW4b5qgreBbqRC19AS3U= | ||
github.com/tidwall/sjson v1.1.1/go.mod h1:yvVuSnpEQv5cYIrO+AT6kw4QVfd5SDZoGIS7/5+fZFs= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
This file contains 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 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 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.