Skip to content

Commit 3a0d5f7

Browse files
committed
e2e: reduce subtest interdependencies
The jsonpatch change for bm-tcb-specs surfaced some issues where we implicitly depended on unrelated test runs. This commit aims to reduce these dependencies by not sharing as much state between tests, or by using an entirely fresh ContrastTest instance where feasible.
1 parent bda8dc8 commit 3a0d5f7

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed

e2e/policy/deterministic_test.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package policy
77

88
import (
99
"encoding/json"
10+
"fmt"
1011
"os"
1112
"path"
1213
"testing"
@@ -22,7 +23,6 @@ func TestDeterminsticPolicyGeneration(t *testing.T) {
2223
require := require.New(t)
2324
platform, err := platforms.FromString(contrasttest.Flags.PlatformStr)
2425
require.NoError(err)
25-
ct := contrasttest.New(t)
2626

2727
// create K8s resources
2828
runtimeHandler, err := manifest.RuntimeHandler(platform)
@@ -33,26 +33,37 @@ func TestDeterminsticPolicyGeneration(t *testing.T) {
3333
resources = kuberesource.PatchRuntimeHandlers(resources, runtimeHandler)
3434
unstructuredResources, err := kuberesource.ResourcesToUnstructured(resources)
3535
require.NoError(err)
36-
buf, err := kuberesource.EncodeUnstructured(unstructuredResources)
36+
resourcesBytes, err := kuberesource.EncodeUnstructured(unstructuredResources)
3737
require.NoError(err)
3838

3939
// generate policy 5 times and check if the policy hash is the same
4040
var expectedPolicies map[manifest.HexString]manifest.PolicyEntry
4141
for i := range 5 {
42-
t.Log("Generate run", i)
43-
require.NoError(os.WriteFile(path.Join(ct.WorkDir, "resources.yml"), buf, 0o644)) // reset file for each run
44-
require.True(t.Run("generate", ct.Generate), "contrast generate needs to succeed for subsequent tests")
45-
manifestBytes, err := os.ReadFile(ct.ManifestPath())
46-
require.NoError(err)
47-
48-
// verify that policies are deterministic
49-
var m manifest.Manifest
50-
require.NoError(json.Unmarshal(manifestBytes, &m))
51-
if expectedPolicies != nil {
52-
require.Equal(expectedPolicies, m.Policies, "expected deterministic policy generation")
53-
} else {
54-
expectedPolicies = m.Policies // only set policies on the first run
55-
}
42+
t.Run(fmt.Sprint(i), func(t *testing.T) {
43+
policies := runGenerate(t, resourcesBytes)
44+
45+
// verify that policies are deterministic
46+
if expectedPolicies != nil {
47+
require.Equal(expectedPolicies, policies, "expected deterministic policy generation")
48+
} else {
49+
expectedPolicies = policies // only set policies on the first run
50+
}
51+
})
5652
}
5753
t.Log("Policies are deterministic")
5854
}
55+
56+
func runGenerate(t *testing.T, resources []byte) map[manifest.HexString]manifest.PolicyEntry {
57+
require := require.New(t)
58+
ct := contrasttest.New(t)
59+
60+
require.NoError(os.WriteFile(path.Join(ct.WorkDir, "resources.yml"), resources, 0o644)) // reset file for each run
61+
require.True(t.Run("generate", ct.Generate), "contrast generate needs to succeed for subsequent tests")
62+
manifestBytes, err := os.ReadFile(ct.ManifestPath())
63+
require.NoError(err)
64+
65+
var m manifest.Manifest
66+
require.NoError(json.Unmarshal(manifestBytes, &m))
67+
68+
return m.Policies
69+
}

e2e/proxy/proxy_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,6 @@ func TestHTTPProxy(t *testing.T) {
4242

4343
runtimeHandler, err := manifest.RuntimeHandler(platform)
4444
require.NoError(t, err)
45-
46-
ct := contrasttest.New(t)
47-
48-
resources := kuberesource.CoordinatorBundle()
49-
resources = kuberesource.PatchRuntimeHandlers(resources, runtimeHandler)
50-
resources = kuberesource.AddPortForwarders(resources)
51-
52-
ct.Init(t, resources)
53-
5445
// Start a proxy server
5546

5647
proxy := goproxy.NewProxyHttpServer()
@@ -116,6 +107,13 @@ func TestHTTPProxy(t *testing.T) {
116107
assert := assert.New(t)
117108
require := require.New(t)
118109

110+
resources := kuberesource.CoordinatorBundle()
111+
resources = kuberesource.PatchRuntimeHandlers(resources, runtimeHandler)
112+
resources = kuberesource.AddPortForwarders(resources)
113+
114+
ct := contrasttest.New(t)
115+
ct.Init(t, resources)
116+
119117
if tc.wantErrMsg != "" {
120118
// Run generate and apply in this process so that they don't use the bad proxy.
121119
require.True(t.Run("generate", ct.Generate))

e2e/regression/regression_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"context"
1515
"errors"
1616
"flag"
17+
"io/fs"
1718
"os"
1819
"path"
1920
"path/filepath"
@@ -68,6 +69,12 @@ func TestRegression(t *testing.T) {
6869
t.Run(dir.Name(), func(t *testing.T) {
6970
require := require.New(t)
7071

72+
// Make sure we start with a fresh manifest, not the one of the previous test or the
73+
// outer generate.
74+
if err := os.Remove(ct.ManifestPath()); err != nil {
75+
require.ErrorIs(err, fs.ErrNotExist)
76+
}
77+
7178
c := kubeclient.NewForTest(t)
7279

7380
pattern := filepath.Join(dataDir, dir.Name(), "*.yml")

0 commit comments

Comments
 (0)