2019-04-29 16:27:57 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2020-07-09 22:04:23 +00:00
|
|
|
"fmt"
|
2019-04-29 16:27:57 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
envoy "github.com/envoyproxy/go-control-plane/envoy/api/v2"
|
2020-07-07 21:22:30 +00:00
|
|
|
"github.com/golang/protobuf/jsonpb"
|
2020-07-09 22:04:23 +00:00
|
|
|
"github.com/hashicorp/go-version"
|
2019-04-29 16:27:57 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// update allows golden files to be updated based on the current output.
|
|
|
|
var update = flag.Bool("update", false, "update golden files")
|
|
|
|
|
2020-07-09 22:04:23 +00:00
|
|
|
// goldenEnvoy is a special variant of golden() that silos each named test by
|
|
|
|
// each supported envoy version
|
|
|
|
func goldenEnvoy(t *testing.T, name, envoyVersion, got string) string {
|
|
|
|
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)
|
|
|
|
|
|
|
|
subname := fmt.Sprintf("envoy-%d-%d-x", segments[0], segments[1])
|
|
|
|
|
|
|
|
return golden(t, name, subname, got)
|
|
|
|
}
|
|
|
|
|
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.
|
2020-07-09 22:04:23 +00:00
|
|
|
func golden(t *testing.T, name, subname, got string) string {
|
2019-04-29 16:27:57 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2020-07-09 22:04:23 +00:00
|
|
|
suffix := ".golden"
|
|
|
|
if subname != "" {
|
|
|
|
suffix = fmt.Sprintf(".%s.golden", subname)
|
|
|
|
}
|
|
|
|
|
|
|
|
golden := filepath.Join("testdata", name+suffix)
|
2019-04-29 16:27:57 +00:00
|
|
|
if *update && got != "" {
|
|
|
|
err := ioutil.WriteFile(golden, []byte(got), 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected, err := ioutil.ReadFile(golden)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return string(expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func responseToJSON(t *testing.T, r *envoy.DiscoveryResponse) string {
|
|
|
|
t.Helper()
|
|
|
|
m := jsonpb.Marshaler{
|
|
|
|
Indent: " ",
|
|
|
|
}
|
|
|
|
gotJSON, err := m.MarshalToString(r)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return gotJSON
|
|
|
|
}
|