Skip to content
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

test: omit envoy golden test files that differ from the latest version #9807

Merged
merged 2 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
13 changes: 7 additions & 6 deletions agent/xds/clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ func TestClustersFromSnapshot(t *testing.T) {
},
}

newestEnvoyVersion := proxysupport.EnvoyVersions[0]
for _, envoyVersion := range proxysupport.EnvoyVersions {
sf, err := determineSupportedProxyFeaturesFromString(envoyVersion)
require.NoError(t, err)
Expand Down Expand Up @@ -660,7 +661,7 @@ func TestClustersFromSnapshot(t *testing.T) {
gName = tt.overrideGoldenName
}

require.JSONEq(goldenEnvoy(t, filepath.Join("clusters", gName), envoyVersion, gotJSON), gotJSON)
require.JSONEq(goldenEnvoy(t, filepath.Join("clusters", gName), envoyVersion, newestEnvoyVersion, gotJSON), gotJSON)
})
}
})
Expand Down Expand Up @@ -818,15 +819,15 @@ func setupTLSRootsAndLeaf(t *testing.T, snap *proxycfg.ConfigSnapshot) {
if snap.Leaf() != nil {
switch snap.Kind {
case structs.ServiceKindConnectProxy:
snap.ConnectProxy.Leaf.CertPEM = golden(t, "test-leaf-cert", "", "")
snap.ConnectProxy.Leaf.PrivateKeyPEM = golden(t, "test-leaf-key", "", "")
snap.ConnectProxy.Leaf.CertPEM = golden(t, "test-leaf-cert", "", "", "")
snap.ConnectProxy.Leaf.PrivateKeyPEM = golden(t, "test-leaf-key", "", "", "")
case structs.ServiceKindIngressGateway:
snap.IngressGateway.Leaf.CertPEM = golden(t, "test-leaf-cert", "", "")
snap.IngressGateway.Leaf.PrivateKeyPEM = golden(t, "test-leaf-key", "", "")
snap.IngressGateway.Leaf.CertPEM = golden(t, "test-leaf-cert", "", "", "")
snap.IngressGateway.Leaf.PrivateKeyPEM = golden(t, "test-leaf-key", "", "", "")
}
}
if snap.Roots != nil {
snap.Roots.Roots[0].RootCert = golden(t, "test-root-cert", "", "")
snap.Roots.Roots[0].RootCert = golden(t, "test-root-cert", "", "", "")
}
}

Expand Down
3 changes: 2 additions & 1 deletion agent/xds/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ func Test_endpointsFromSnapshot(t *testing.T) {
},
}

newestEnvoyVersion := proxysupport.EnvoyVersions[0]
for _, envoyVersion := range proxysupport.EnvoyVersions {
sf, err := determineSupportedProxyFeaturesFromString(envoyVersion)
require.NoError(t, err)
Expand Down Expand Up @@ -599,7 +600,7 @@ func Test_endpointsFromSnapshot(t *testing.T) {
gName = tt.overrideGoldenName
}

require.JSONEq(goldenEnvoy(t, filepath.Join("endpoints", gName), envoyVersion, gotJSON), gotJSON)
require.JSONEq(goldenEnvoy(t, filepath.Join("endpoints", gName), envoyVersion, newestEnvoyVersion, gotJSON), gotJSON)
})
}
})
Expand Down
44 changes: 38 additions & 6 deletions agent/xds/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -20,7 +21,16 @@ var update = flag.Bool("update", false, "update golden files")

// 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 {
func goldenEnvoy(t *testing.T, name, envoyVersion, newestEnvoyVersion, got string) string {
require.NotEmpty(t, envoyVersion)

subname := goldenEnvoyVersionName(t, envoyVersion)
newestSubname := goldenEnvoyVersionName(t, newestEnvoyVersion)

return golden(t, name, subname, newestSubname, got)
}

func goldenEnvoyVersionName(t *testing.T, envoyVersion string) string {
require.NotEmpty(t, envoyVersion)

// We do version sniffing on the complete version, but only generate
Expand All @@ -29,14 +39,12 @@ func goldenEnvoy(t *testing.T, name, envoyVersion, got string) string {
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)
return fmt.Sprintf("envoy-%d-%d-x", segments[0], segments[1])
}

// golden reads and optionally writes the expected data to the golden file,
// returning the contents as a string.
func golden(t *testing.T, name, subname, got string) string {
func golden(t *testing.T, name, subname, newestSubname, got string) string {
t.Helper()

suffix := ".golden"
Expand All @@ -45,14 +53,38 @@ func golden(t *testing.T, name, subname, got string) string {
}

golden := filepath.Join("testdata", name+suffix)

// To trim down PRs, we only create per-version golden files if they differ
// from the latest version.
newestExpected := ""
if newestSubname != "" && subname != newestSubname {
newestGolden := filepath.Join("testdata", fmt.Sprintf("%s.%s.golden", name, newestSubname))
expected, err := ioutil.ReadFile(newestGolden)
require.NoError(t, err)
rboyer marked this conversation as resolved.
Show resolved Hide resolved

if string(expected) == got {
if *update && got != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't figure out what was going on here; the original optional writes the file, then returns the contents, which seems straightforward, and provides a path to regenerate the golden files. But I'm not sure why we delete the golden file, but don't do anything with the newest golden, or why this is guarded by the match with expected.

Perhaps a comment explaining the new workflow, in particular if the version under test isn't the newest.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'll add a bunch of comments.

The gist is that when you run the tests with the -update flag they will erase any golden test files for prior versions that are identical to the golden test file for the latest version of envoy.

During comparison mode, we check for a specific version file if one exists and use that for comparison, but if that does not exist we use the one from the latest version of envoy instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markan check the latest commit in the PR for the restructured function that should hopefully be way clearer.

err := os.Remove(golden)
if err != nil && !os.IsNotExist(err) {
require.NoError(t, err)
}
}
return string(expected)
}

newestExpected = string(expected)
}

if *update && got != "" {
err := ioutil.WriteFile(golden, []byte(got), 0644)
require.NoError(t, err)
}

expected, err := ioutil.ReadFile(golden)
if newestExpected != "" && os.IsNotExist(err) {
return newestExpected
}
require.NoError(t, err)

return string(expected)
}

Expand Down
3 changes: 2 additions & 1 deletion agent/xds/listeners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ func TestListenersFromSnapshot(t *testing.T) {
},
}

newestEnvoyVersion := proxysupport.EnvoyVersions[0]
for _, envoyVersion := range proxysupport.EnvoyVersions {
sf, err := determineSupportedProxyFeaturesFromString(envoyVersion)
require.NoError(t, err)
Expand Down Expand Up @@ -527,7 +528,7 @@ func TestListenersFromSnapshot(t *testing.T) {
gName = tt.overrideGoldenName
}

require.JSONEq(goldenEnvoy(t, filepath.Join("listeners", gName), envoyVersion, gotJSON), gotJSON)
require.JSONEq(goldenEnvoy(t, filepath.Join("listeners", gName), envoyVersion, newestEnvoyVersion, gotJSON), gotJSON)
})
}
})
Expand Down
4 changes: 2 additions & 2 deletions agent/xds/rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,15 @@ func TestMakeRBACNetworkAndHTTPFilters(t *testing.T) {

gotJSON := protoToJSON(t, filter)

require.JSONEq(t, golden(t, filepath.Join("rbac", name), "", gotJSON), gotJSON)
require.JSONEq(t, golden(t, filepath.Join("rbac", name), "", "", gotJSON), gotJSON)
})
t.Run("http filter", func(t *testing.T) {
filter, err := makeRBACHTTPFilter(tt.intentions, tt.intentionDefaultAllow)
require.NoError(t, err)

gotJSON := protoToJSON(t, filter)

require.JSONEq(t, golden(t, filepath.Join("rbac", name+"--httpfilter"), "", gotJSON), gotJSON)
require.JSONEq(t, golden(t, filepath.Join("rbac", name+"--httpfilter"), "", "", gotJSON), gotJSON)
})
})
}
Expand Down
3 changes: 2 additions & 1 deletion agent/xds/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func TestRoutesFromSnapshot(t *testing.T) {
},
}

newestEnvoyVersion := proxysupport.EnvoyVersions[0]
for _, envoyVersion := range proxysupport.EnvoyVersions {
sf, err := determineSupportedProxyFeaturesFromString(envoyVersion)
require.NoError(t, err)
Expand Down Expand Up @@ -278,7 +279,7 @@ func TestRoutesFromSnapshot(t *testing.T) {
gName = tt.overrideGoldenName
}

require.JSONEq(goldenEnvoy(t, filepath.Join("routes", gName), envoyVersion, gotJSON), gotJSON)
require.JSONEq(goldenEnvoy(t, filepath.Join("routes", gName), envoyVersion, newestEnvoyVersion, gotJSON), gotJSON)
})
}
})
Expand Down

This file was deleted.

Loading