2019-04-29 16:27:57 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
2023-02-23 16:32:32 +00:00
|
|
|
"encoding/json"
|
2019-04-29 16:27:57 +00:00
|
|
|
"flag"
|
2020-07-09 22:04:51 +00:00
|
|
|
"fmt"
|
2021-02-24 20:04:31 +00:00
|
|
|
"os"
|
2019-04-29 16:27:57 +00:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2020-07-09 22:04:51 +00:00
|
|
|
"github.com/hashicorp/go-version"
|
2023-02-23 16:32:32 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-04-29 16:27:57 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-01-11 14:39:10 +00:00
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
"google.golang.org/protobuf/proto"
|
2019-04-29 16:27:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// update allows golden files to be updated based on the current output.
|
|
|
|
var update = flag.Bool("update", false, "update golden files")
|
|
|
|
|
2021-02-24 20:04:31 +00:00
|
|
|
// goldenSimple is just for read/write access to a golden file that is not
|
|
|
|
// envoy specific.
|
|
|
|
func goldenSimple(t *testing.T, name, got string) string {
|
|
|
|
return golden(t, name, "", "", got)
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:04:51 +00:00
|
|
|
// goldenEnvoy is a special variant of golden() that silos each named test by
|
|
|
|
// each supported envoy version
|
2021-02-24 20:04:31 +00:00
|
|
|
func goldenEnvoy(t *testing.T, name, envoyVersion, latestEnvoyVersion, got string) string {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
require.NotEmpty(t, envoyVersion)
|
|
|
|
|
|
|
|
// We'll need both the name of this golden file for the requested version
|
|
|
|
// of envoy AND the latest version of envoy due to how the golden file
|
|
|
|
// coalescing works below when there is no xDS generated skew across envoy
|
|
|
|
// versions.
|
|
|
|
subname := goldenEnvoyVersionName(t, envoyVersion)
|
2022-04-14 17:44:42 +00:00
|
|
|
|
|
|
|
latestSubname := "latest"
|
|
|
|
if envoyVersion == latestEnvoyVersion {
|
|
|
|
subname = "latest"
|
|
|
|
}
|
2021-02-24 20:04:31 +00:00
|
|
|
|
|
|
|
return golden(t, name, subname, latestSubname, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func goldenEnvoyVersionName(t *testing.T, envoyVersion string) string {
|
|
|
|
t.Helper()
|
|
|
|
|
2020-07-09 22:04:51 +00:00
|
|
|
require.NotEmpty(t, envoyVersion)
|
|
|
|
|
|
|
|
// We do version sniffing on the complete version, but only generate
|
|
|
|
// golden files ignoring the patch portion
|
|
|
|
version := version.Must(version.NewVersion(envoyVersion))
|
|
|
|
segments := version.Segments()
|
|
|
|
require.Len(t, segments, 3)
|
|
|
|
|
2021-02-24 20:04:31 +00:00
|
|
|
return fmt.Sprintf("envoy-%d-%d-x", segments[0], segments[1])
|
2020-07-09 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 16:27:57 +00:00
|
|
|
// golden reads and optionally writes the expected data to the golden file,
|
|
|
|
// returning the contents as a string.
|
2021-02-24 20:04:31 +00:00
|
|
|
//
|
|
|
|
// The golden file is named with two components the "name" and the "subname".
|
|
|
|
// In the common case of xDS tests the "name" component is the logical name of
|
|
|
|
// the test itself, and the "subname" is derived from the envoy major version.
|
|
|
|
//
|
|
|
|
// If latestSubname is specified we use that as a fallback source of comparison
|
|
|
|
// if the specific golden file referred to by subname is absent.
|
|
|
|
//
|
|
|
|
// If the -update flag is passed when executing the tests then the contents of
|
|
|
|
// the "got" argument are written to the golden file on disk. If the
|
|
|
|
// latestSubname argument is specified in this mode and the generated content
|
|
|
|
// matches that of the latest generated content then the specific golden file
|
|
|
|
// referred to by 'subname' is deleted to avoid unnecessary duplication in the
|
|
|
|
// testdata directory.
|
|
|
|
func golden(t *testing.T, name, subname, latestSubname, got string) string {
|
2019-04-29 16:27:57 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2020-07-09 22:04:51 +00:00
|
|
|
suffix := ".golden"
|
|
|
|
if subname != "" {
|
|
|
|
suffix = fmt.Sprintf(".%s.golden", subname)
|
|
|
|
}
|
|
|
|
|
|
|
|
golden := filepath.Join("testdata", name+suffix)
|
2021-02-24 20:04:31 +00:00
|
|
|
|
2023-02-23 16:32:32 +00:00
|
|
|
var latestGoldenPath, latestExpected string
|
|
|
|
isLatest := subname == latestSubname
|
|
|
|
// Include latestSubname in the latest golden path if it exists.
|
|
|
|
if latestSubname == "" {
|
|
|
|
latestGoldenPath = filepath.Join("testdata", fmt.Sprintf("%s.golden", name))
|
|
|
|
} else {
|
|
|
|
latestGoldenPath = filepath.Join("testdata", fmt.Sprintf("%s.%s.golden", name, latestSubname))
|
|
|
|
}
|
|
|
|
|
|
|
|
if raw, err := os.ReadFile(latestGoldenPath); err == nil {
|
2021-02-24 20:04:31 +00:00
|
|
|
latestExpected = string(raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle easy updates to the golden files in the agent/xds/testdata
|
|
|
|
// directory.
|
|
|
|
//
|
|
|
|
// To trim down PRs, we only create per-version golden files if they differ
|
|
|
|
// from the latest version.
|
2023-02-23 16:32:32 +00:00
|
|
|
|
2021-02-24 20:04:31 +00:00
|
|
|
if *update && got != "" {
|
2023-02-23 16:32:32 +00:00
|
|
|
var gotInterface, latestExpectedInterface interface{}
|
|
|
|
json.Unmarshal([]byte(got), &gotInterface)
|
|
|
|
json.Unmarshal([]byte(latestExpected), &latestExpectedInterface)
|
|
|
|
|
|
|
|
// Remove non-latest golden files if they are the same as the latest one.
|
|
|
|
if !isLatest && assert.ObjectsAreEqualValues(gotInterface, latestExpectedInterface) {
|
2021-02-24 20:04:31 +00:00
|
|
|
// In update mode we erase a golden file if it is identical to
|
|
|
|
// the golden file corresponding to the latest version of
|
|
|
|
// envoy.
|
|
|
|
err := os.Remove(golden)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
return got
|
|
|
|
}
|
|
|
|
|
2023-02-23 16:32:32 +00:00
|
|
|
// We use require.JSONEq to compare values and ObjectsAreEqualValues is used
|
|
|
|
// internally by that function to compare the string JSON values. This only
|
|
|
|
// writes updates the golden file when they actually change.
|
|
|
|
if !assert.ObjectsAreEqualValues(gotInterface, latestExpectedInterface) {
|
|
|
|
require.NoError(t, os.WriteFile(golden, []byte(got), 0644))
|
|
|
|
}
|
2021-02-24 20:04:31 +00:00
|
|
|
return got
|
2019-04-29 16:27:57 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
expected, err := os.ReadFile(golden)
|
2021-02-24 20:04:31 +00:00
|
|
|
if latestExpected != "" && os.IsNotExist(err) {
|
|
|
|
// In readonly mode if a specific golden file isn't found, we fallback
|
|
|
|
// on the latest one.
|
|
|
|
return latestExpected
|
|
|
|
}
|
2019-04-29 16:27:57 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-24 20:04:31 +00:00
|
|
|
return string(expected)
|
|
|
|
}
|
2019-04-29 16:27:57 +00:00
|
|
|
|
2020-08-27 17:20:58 +00:00
|
|
|
func protoToJSON(t *testing.T, pb proto.Message) string {
|
2019-04-29 16:27:57 +00:00
|
|
|
t.Helper()
|
2023-01-11 14:39:10 +00:00
|
|
|
m := protojson.MarshalOptions{
|
2019-04-29 16:27:57 +00:00
|
|
|
Indent: " ",
|
|
|
|
}
|
2023-01-11 14:39:10 +00:00
|
|
|
gotJSON, err := m.Marshal(pb)
|
2019-04-29 16:27:57 +00:00
|
|
|
require.NoError(t, err)
|
2023-01-11 14:39:10 +00:00
|
|
|
return string(gotJSON)
|
2019-04-29 16:27:57 +00:00
|
|
|
}
|