Skip to content

Commit d55cc0d

Browse files
authored
simulators/ethereum/engine: Fix time drift auth tests, break tests into separate suites (#612)
* simulators/ethereum/engine: Retry auth time drift tests * simulators/ethereum/engine: Break tests into separate suites
1 parent f619b16 commit d55cc0d

File tree

2 files changed

+64
-30
lines changed

2 files changed

+64
-30
lines changed

simulators/ethereum/engine/authtests.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type AuthTestSpec struct {
1212
TimeDriftSeconds int64
1313
CustomAuthSecretBytes []byte
1414
AuthOk bool
15+
RetryAttempts int64
1516
}
1617

1718
var authTestSpecs = []AuthTestSpec{
@@ -38,24 +39,28 @@ var authTestSpecs = []AuthTestSpec{
3839
TimeDriftSeconds: -1 - maxTimeDriftSeconds,
3940
CustomAuthSecretBytes: nil,
4041
AuthOk: false,
42+
RetryAttempts: 5,
4143
},
4244
{
4345
Name: "JWT Authentication: Negative time drift, within limit, correct secret",
4446
TimeDriftSeconds: 1 - maxTimeDriftSeconds,
4547
CustomAuthSecretBytes: nil,
4648
AuthOk: true,
49+
RetryAttempts: 5,
4750
},
4851
{
4952
Name: "JWT Authentication: Positive time drift, exceeding limit, correct secret",
5053
TimeDriftSeconds: maxTimeDriftSeconds + 1,
5154
CustomAuthSecretBytes: nil,
5255
AuthOk: false,
56+
RetryAttempts: 5,
5357
},
5458
{
5559
Name: "JWT Authentication: Positive time drift, within limit, correct secret",
5660
TimeDriftSeconds: maxTimeDriftSeconds - 1,
5761
CustomAuthSecretBytes: nil,
5862
AuthOk: true,
63+
RetryAttempts: 5,
5964
},
6065
}
6166

@@ -78,26 +83,38 @@ func GenerateAuthTestSpec(authTestSpec AuthTestSpec) TestSpec {
7883
TerminalBlockNumber: 0,
7984
}
8085
testSecret = authTestSpec.CustomAuthSecretBytes
81-
testTime = time.Now()
86+
// Time drift test cases are reattempted in order to mitigate false negatives
87+
retryAttemptsLeft = authTestSpec.RetryAttempts
8288
)
83-
if testSecret == nil {
84-
testSecret = defaultJwtTokenSecretBytes
85-
}
86-
if authTestSpec.TimeDriftSeconds != 0 {
87-
testTime = testTime.Add(time.Second * time.Duration(authTestSpec.TimeDriftSeconds))
88-
}
89-
if err := t.Engine.PrepareAuthCallToken(testSecret, testTime); err != nil {
90-
t.Fatalf("FAIL (%s): Unable to prepare the auth token: %v", t.TestName, err)
91-
}
92-
err := t.Engine.c.CallContext(t.Engine.Ctx(), &tConf, "engine_exchangeTransitionConfigurationV1", tConf)
93-
if authTestSpec.AuthOk {
94-
if err != nil {
95-
t.Fatalf("FAIL (%s): Authentication was supposed to pass authentication but failed: %v", t.TestName, err)
89+
90+
for {
91+
var testTime = time.Now()
92+
if testSecret == nil {
93+
testSecret = defaultJwtTokenSecretBytes
94+
}
95+
if authTestSpec.TimeDriftSeconds != 0 {
96+
testTime = testTime.Add(time.Second * time.Duration(authTestSpec.TimeDriftSeconds))
97+
}
98+
if err := t.Engine.PrepareAuthCallToken(testSecret, testTime); err != nil {
99+
t.Fatalf("FAIL (%s): Unable to prepare the auth token: %v", t.TestName, err)
100+
}
101+
err := t.Engine.c.CallContext(t.Engine.Ctx(), &tConf, "engine_exchangeTransitionConfigurationV1", tConf)
102+
if (authTestSpec.AuthOk && err == nil) || (!authTestSpec.AuthOk && err != nil) {
103+
// Test passed
104+
return
96105
}
97-
} else {
98-
if err == nil {
99-
t.Fatalf("FAIL (%s): Authentication was supposed to fail authentication but passed", t.TestName)
106+
if retryAttemptsLeft == 0 {
107+
if err != nil {
108+
// Test failed because unexpected error
109+
t.Fatalf("FAIL (%s): Authentication was supposed to pass authentication but failed: %v", t.TestName, err)
110+
} else {
111+
// Test failed because unexpected success
112+
t.Fatalf("FAIL (%s): Authentication was supposed to fail authentication but passed", t.TestName)
113+
}
100114
}
115+
retryAttemptsLeft--
116+
// Wait at least a second before trying again
117+
time.Sleep(time.Second)
101118
}
102119
}
103120
return TestSpec{

simulators/ethereum/engine/main.go

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,40 @@ type TestSpec struct {
9696
ChainFile string
9797
}
9898

99-
var allTests = append(
100-
engineTests,
101-
append(
102-
mergeTests,
103-
authTests...,
104-
)...,
105-
)
106-
10799
func main() {
108-
suite := hivesim.Suite{
100+
engine := hivesim.Suite{
109101
Name: "engine",
110102
Description: `
111103
Test Engine API tests using CL mocker to inject commands into clients after they
112104
have reached the Terminal Total Difficulty.`[1:],
113105
}
114-
for _, currentTest := range allTests {
106+
addTestsToSuite(&engine, engineTests)
107+
108+
transition := hivesim.Suite{
109+
Name: "transition",
110+
Description: `
111+
Test Engine API tests using CL mocker to inject commands into clients and drive
112+
them through the merge.`[1:],
113+
}
114+
addTestsToSuite(&transition, mergeTests)
115+
116+
auth := hivesim.Suite{
117+
Name: "auth",
118+
Description: `
119+
Test Engine API authentication features.`[1:],
120+
}
121+
addTestsToSuite(&auth, authTests)
122+
123+
// Mark suites for execution
124+
simulator := hivesim.New()
125+
hivesim.MustRunSuite(simulator, engine)
126+
hivesim.MustRunSuite(simulator, transition)
127+
hivesim.MustRunSuite(simulator, auth)
128+
}
129+
130+
// Add test cases to a given test suite
131+
func addTestsToSuite(suite *hivesim.Suite, tests []TestSpec) {
132+
for _, currentTest := range tests {
115133
currentTest := currentTest
116134
genesisPath := "./init/genesis.json"
117135
// If the TestSpec specified a custom genesis file, use that instead.
@@ -137,9 +155,9 @@ have reached the Terminal Total Difficulty.`[1:],
137155
Parameters: newParams,
138156
Files: testFiles,
139157
Run: func(t *hivesim.T, c *hivesim.Client) {
140-
t.Logf("Start test: %s", currentTest.Name)
158+
t.Logf("Start test (%s): %s", c.Type, currentTest.Name)
141159
defer func() {
142-
t.Logf("End test: %s", currentTest.Name)
160+
t.Logf("End test (%s): %s", c.Type, currentTest.Name)
143161
}()
144162
timeout := DefaultTestCaseTimeout
145163
// If a TestSpec specifies a timeout, use that instead
@@ -151,7 +169,6 @@ have reached the Terminal Total Difficulty.`[1:],
151169
},
152170
})
153171
}
154-
hivesim.MustRunSuite(hivesim.New(), suite)
155172
}
156173

157174
// TTD is the value specified in the TestSpec + Genesis.Difficulty

0 commit comments

Comments
 (0)