Skip to content

Commit

Permalink
fix: use require instead of t.Fatal(err) in tests/e2e package
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Nov 1, 2024
1 parent 3de0018 commit 7c6e774
Show file tree
Hide file tree
Showing 30 changed files with 386 additions and 617 deletions.
109 changes: 44 additions & 65 deletions tests/e2e/ctl_v3_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,44 +131,39 @@ func authTestCertCN(cx ctlCtx) {
}

func authTestFromKeyPerm(cx ctlCtx) {
if err := authEnable(cx); err != nil {
cx.t.Fatal(err)
}
err := authEnable(cx)
require.NoError(cx.t, err)

cx.user, cx.pass = "root", "root"
authSetupTestUser(cx)

// grant keys after z to test-user
cx.user, cx.pass = "root", "root"
if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "z", "\x00", false}); err != nil {
cx.t.Fatal(err)
}
err = ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "z", "\x00", false})
require.NoError(cx.t, err)

// try the granted open ended permission
cx.user, cx.pass = "test-user", "pass"
for i := 0; i < 10; i++ {
key := fmt.Sprintf("z%d", i)
if err := ctlV3Put(cx, key, "val", ""); err != nil {
cx.t.Fatal(err)
}
err := ctlV3Put(cx, key, "val", "")
require.NoError(cx.t, err)
}
largeKey := ""
for i := 0; i < 10; i++ {
largeKey += "\xff"
if err := ctlV3Put(cx, largeKey, "val", ""); err != nil {
cx.t.Fatal(err)
}
err := ctlV3Put(cx, largeKey, "val", "")
require.NoError(cx.t, err)
}

// try a non granted key
err := ctlV3PutFailPerm(cx, "x", "baz")
err = ctlV3PutFailPerm(cx, "x", "baz")
require.ErrorContains(cx.t, err, "permission denied")

// revoke the open ended permission
cx.user, cx.pass = "root", "root"
if err := ctlV3RoleRevokePermission(cx, "test-role", "z", "", true); err != nil {
cx.t.Fatal(err)
}
err = ctlV3RoleRevokePermission(cx, "test-role", "z", "", true)
require.NoError(cx.t, err)

// try the revoked open ended permission
cx.user, cx.pass = "test-user", "pass"
Expand All @@ -180,24 +175,21 @@ func authTestFromKeyPerm(cx ctlCtx) {

// grant the entire keys
cx.user, cx.pass = "root", "root"
if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "", "\x00", false}); err != nil {
cx.t.Fatal(err)
}
err = ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "", "\x00", false})
require.NoError(cx.t, err)

// try keys, of course it must be allowed because test-role has a permission of the entire keys
cx.user, cx.pass = "test-user", "pass"
for i := 0; i < 10; i++ {
key := fmt.Sprintf("z%d", i)
if err := ctlV3Put(cx, key, "val", ""); err != nil {
cx.t.Fatal(err)
}
err := ctlV3Put(cx, key, "val", "")
require.NoError(cx.t, err)
}

// revoke the entire keys
cx.user, cx.pass = "root", "root"
if err := ctlV3RoleRevokePermission(cx, "test-role", "", "", true); err != nil {
cx.t.Fatal(err)
}
err = ctlV3RoleRevokePermission(cx, "test-role", "", "", true)
require.NoError(cx.t, err)

// try the revoked entire key permission
cx.user, cx.pass = "test-user", "pass"
Expand All @@ -209,17 +201,15 @@ func authTestFromKeyPerm(cx ctlCtx) {
}

func authTestWatch(cx ctlCtx) {
if err := authEnable(cx); err != nil {
cx.t.Fatal(err)
}
err := authEnable(cx)
require.NoError(cx.t, err)

cx.user, cx.pass = "root", "root"
authSetupTestUser(cx)

// grant a key range
if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "key", "key4", false}); err != nil {
cx.t.Fatal(err)
}
err = ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "key", "key4", false})
require.NoError(cx.t, err)

tests := []struct {
puts []kv
Expand Down Expand Up @@ -286,9 +276,8 @@ func authTestWatch(cx ctlCtx) {
func authTestSnapshot(cx ctlCtx) {
maintenanceInitKeys(cx)

if err := authEnable(cx); err != nil {
cx.t.Fatal(err)
}
err := authEnable(cx)
require.NoError(cx.t, err)

cx.user, cx.pass = "root", "root"
authSetupTestUser(cx)
Expand All @@ -298,9 +287,8 @@ func authTestSnapshot(cx ctlCtx) {

// ordinary user cannot save a snapshot
cx.user, cx.pass = "test-user", "pass"
if err := ctlV3SnapshotSave(cx, fpath); err == nil {
cx.t.Fatal("ordinary user should not be able to save a snapshot")
}
err = ctlV3SnapshotSave(cx, fpath)
require.Error(cx.t, err, "ordinary user should not be able to save a snapshot")

// root can save a snapshot
cx.user, cx.pass = "root", "root"
Expand All @@ -321,9 +309,8 @@ func authTestSnapshot(cx ctlCtx) {
}

func authTestEndpointHealth(cx ctlCtx) {
if err := authEnable(cx); err != nil {
cx.t.Fatal(err)
}
err := authEnable(cx)
require.NoError(cx.t, err)

cx.user, cx.pass = "root", "root"
authSetupTestUser(cx)
Expand All @@ -340,48 +327,40 @@ func authTestEndpointHealth(cx ctlCtx) {

// succeed if permissions granted for ordinary user
cx.user, cx.pass = "root", "root"
if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "health", "", false}); err != nil {
cx.t.Fatal(err)
}
err = ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "health", "", false})
require.NoError(cx.t, err)
cx.user, cx.pass = "test-user", "pass"
if err := ctlV3EndpointHealth(cx); err != nil {
cx.t.Fatalf("endpointStatusTest ctlV3EndpointHealth error (%v)", err)
}
}

func certCNAndUsername(cx ctlCtx, noPassword bool) {
if err := authEnable(cx); err != nil {
cx.t.Fatal(err)
}
err := authEnable(cx)
require.NoError(cx.t, err)

cx.user, cx.pass = "root", "root"
authSetupTestUser(cx)

if noPassword {
if err := ctlV3User(cx, []string{"add", "example.com", "--no-password"}, "User example.com created", []string{""}); err != nil {
cx.t.Fatal(err)
}
err := ctlV3User(cx, []string{"add", "example.com", "--no-password"}, "User example.com created", []string{""})
require.NoError(cx.t, err)
} else {
if err := ctlV3User(cx, []string{"add", "example.com", "--interactive=false"}, "User example.com created", []string{""}); err != nil {
cx.t.Fatal(err)
}
}
if err := e2e.SpawnWithExpectWithEnv(append(cx.PrefixArgs(), "role", "add", "test-role-cn"), cx.envMap, expect.ExpectedResponse{Value: "Role test-role-cn created"}); err != nil {
cx.t.Fatal(err)
}
if err := ctlV3User(cx, []string{"grant-role", "example.com", "test-role-cn"}, "Role test-role-cn is granted to user example.com", nil); err != nil {
cx.t.Fatal(err)
err := ctlV3User(cx, []string{"add", "example.com", "--interactive=false"}, "User example.com created", []string{""})
require.NoError(cx.t, err)
}
err = e2e.SpawnWithExpectWithEnv(append(cx.PrefixArgs(), "role", "add", "test-role-cn"), cx.envMap, expect.ExpectedResponse{Value: "Role test-role-cn created"})
require.NoError(cx.t, err)
err = ctlV3User(cx, []string{"grant-role", "example.com", "test-role-cn"}, "Role test-role-cn is granted to user example.com", nil)
require.NoError(cx.t, err)

// grant a new key for CN based user
if err := ctlV3RoleGrantPermission(cx, "test-role-cn", grantingPerm{true, true, "hoo", "", false}); err != nil {
cx.t.Fatal(err)
}
err = ctlV3RoleGrantPermission(cx, "test-role-cn", grantingPerm{true, true, "hoo", "", false})
require.NoError(cx.t, err)

// grant a new key for username based user
if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "bar", "", false}); err != nil {
cx.t.Fatal(err)
}
err = ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "bar", "", false})
require.NoError(cx.t, err)

// try a granted key for CN based user
cx.user, cx.pass = "", ""
Expand All @@ -397,7 +376,7 @@ func certCNAndUsername(cx ctlCtx, noPassword bool) {

// try a non-granted key for both of them
cx.user, cx.pass = "", ""
err := ctlV3PutFailPerm(cx, "baz", "bar")
err = ctlV3PutFailPerm(cx, "baz", "bar")
require.ErrorContains(cx.t, err, "permission denied")

cx.user, cx.pass = "test-user", "pass"
Expand Down
7 changes: 4 additions & 3 deletions tests/e2e/ctl_v3_defrag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package e2e
import (
"testing"

"github.com/stretchr/testify/require"

"go.etcd.io/etcd/pkg/v3/expect"
"go.etcd.io/etcd/tests/v3/framework/e2e"
)
Expand All @@ -28,9 +30,8 @@ func TestCtlV3DefragOffline(t *testing.T) {
func maintenanceInitKeys(cx ctlCtx) {
var kvs = []kv{{"key", "val1"}, {"key", "val2"}, {"key", "val3"}}
for i := range kvs {
if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
cx.t.Fatal(err)
}
err := ctlV3Put(cx, kvs[i].key, kvs[i].val, "")
require.NoError(cx.t, err)
}
}

Expand Down
27 changes: 9 additions & 18 deletions tests/e2e/ctl_v3_elect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ func testElect(cx ctlCtx) {
name := "a"

holder, ch, err := ctlV3Elect(cx, name, "p1", false)
if err != nil {
cx.t.Fatal(err)
}
require.NoError(cx.t, err)

l1 := ""
select {
Expand All @@ -51,9 +49,7 @@ func testElect(cx ctlCtx) {

// blocked process that won't win the election
blocked, ch, err := ctlV3Elect(cx, name, "p2", true)
if err != nil {
cx.t.Fatal(err)
}
require.NoError(cx.t, err)
select {
case <-time.After(100 * time.Millisecond):
case <-ch:
Expand All @@ -62,9 +58,7 @@ func testElect(cx ctlCtx) {

// overlap with a blocker that will win the election
blockAcquire, ch, err := ctlV3Elect(cx, name, "p2", false)
if err != nil {
cx.t.Fatal(err)
}
require.NoError(cx.t, err)
defer func(blockAcquire *expect.ExpectProcess) {
err = blockAcquire.Stop()
require.NoError(cx.t, err)
Expand All @@ -78,22 +72,19 @@ func testElect(cx ctlCtx) {
}

// kill blocked process with clean shutdown
if err = blocked.Signal(os.Interrupt); err != nil {
cx.t.Fatal(err)
}
err = blocked.Signal(os.Interrupt)
require.NoError(cx.t, err)
err = e2e.CloseWithTimeout(blocked, time.Second)
if err != nil {
// due to being blocked, this can potentially get killed and thus exit non-zero sometimes
require.ErrorContains(cx.t, err, "unexpected exit code")
}

// kill the holder with clean shutdown
if err = holder.Signal(os.Interrupt); err != nil {
cx.t.Fatal(err)
}
if err = e2e.CloseWithTimeout(holder, time.Second); err != nil {
cx.t.Fatal(err)
}
err = holder.Signal(os.Interrupt)
require.NoError(cx.t, err)
err = e2e.CloseWithTimeout(holder, time.Second)
require.NoError(cx.t, err)

// blockAcquire should win the election
select {
Expand Down
9 changes: 3 additions & 6 deletions tests/e2e/ctl_v3_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.etcd.io/etcd/pkg/v3/expect"
"go.etcd.io/etcd/tests/v3/framework/config"
Expand Down Expand Up @@ -135,9 +136,7 @@ func TestAuthority(t *testing.T) {
assert.NoError(t, err)
for i := 0; i < 100; i++ {
err = client.Put(ctx, "foo", "bar", config.PutOptions{})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
}

testutils.ExecuteWithTimeout(t, 5*time.Second, func() {
Expand Down Expand Up @@ -166,9 +165,7 @@ func assertAuthority(t *testing.T, expectAuthorityPattern string, clus *e2e.Etcd
line = strings.TrimSuffix(line, "\r")

u, err := url.Parse(clus.Procs[i].EndpointsGRPC()[0])
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
expectAuthority := strings.ReplaceAll(expectAuthorityPattern, "${MEMBER_PORT}", u.Port())
expectLine := fmt.Sprintf(`http2: decoded hpack field header field ":authority" = %q`, expectAuthority)
assert.Truef(t, strings.HasSuffix(line, expectLine), "Got %q expected suffix %q", line, expectLine)
Expand Down
Loading

0 comments on commit 7c6e774

Please sign in to comment.