Repository: etcd-io/etcd
File: tests/e2e/reproduce_18667_test.go (Go)

Code before:

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

	"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
	clientv3 "go.etcd.io/etcd/client/v3"
	"go.etcd.io/etcd/tests/v3/framework/e2e"
)

// TestReproduce18667 reproduces the issue: https://github.com/etcd-io/etcd/issues/18667.
func TestReproduce18667(t *testing.T) {
	e2e.BeforeTest(t)

	ctx := t.Context()
	clus, cerr := e2e.NewEtcdProcessCluster(ctx, t,
		e2e.WithClusterSize(3),
		e2e.WithSnapshotCount(1000),
	)
	require.NoError(t, cerr)

	t.Cleanup(func() { clus.Stop() })

	targetIdx := rand.Intn(3)
	cli := newClient(t, clus.Procs[targetIdx].EndpointsGRPC(), e2e.ClientConfig{})

	// Put key-value [k1...k20]
	var revision int64
	for i := 1; i <= 20; i++ {
		resp, err := cli.Put(ctx, fmt.Sprintf("k%d", i), fmt.Sprintf("v%d", i))
		require.NoError(t, err)
		revision = resp.Header.Revision
	}

	// Compact on the latest revision
	_, err := cli.Compact(ctx, revision)
	require.NoError(t, err)

	// Txn to make data inconsistent among etcdservers
	cmp := clientv3.Compare(clientv3.Value("k1"), "=", "v1")
	then := []clientv3.Op{
		clientv3.OpPut("k2", "foo"),
		clientv3.OpGet("k1", clientv3.WithRev(revision-1)),
	}
	_, err = cli.Txn(ctx).If(cmp).Then(then...).Commit()
	require.Error(t, err)
	require.Contains(t, err.Error(), rpctypes.ErrCompacted.Error())

	// k2 equals to v2 in the etcdserver which the Txn was sent to
	kv2, err := cli.Get(ctx, "k2")
	require.NoError(t, err)
	assert.Equal(t, "v2", string(kv2.Kvs[0].Value))

Review comment (suggestion):
```suggestion
	t.Log("Put key-value [k1...k20]")
```
Recommend using logs instead of comments to mark sections of the test.

Code after:

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

	"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
	clientv3 "go.etcd.io/etcd/client/v3"
	"go.etcd.io/etcd/tests/v3/framework/e2e"
)

// TestReproduce18667 reproduces the issue: https://github.com/etcd-io/etcd/issues/18667.
func TestReproduce18667(t *testing.T) {
	e2e.BeforeTest(t)

	ctx := t.Context()
	clus, cerr := e2e.NewEtcdProcessCluster(ctx, t,
		e2e.WithClusterSize(3),
		e2e.WithSnapshotCount(1000),
	)
	require.NoError(t, cerr)

	t.Cleanup(func() { clus.Stop() })

	targetIdx := rand.Intn(3)
	cli := newClient(t, clus.Procs[targetIdx].EndpointsGRPC(), e2e.ClientConfig{})

	t.Log("Put key-value [k1...k20]")
	var revision int64
	for i := 1; i <= 20; i++ {
		resp, err := cli.Put(ctx, fmt.Sprintf("k%d", i), fmt.Sprintf("v%d", i))
		require.NoError(t, err)
		revision = resp.Header.Revision
	}

	t.Logf("Compact on the latest revision %d", revision)
	_, err := cli.Compact(ctx, revision)
	require.NoError(t, err)

	t.Log("Send txn to make data inconsistent among etcdservers")
	cmp := clientv3.Compare(clientv3.Value("k1"), "=", "v1")
	then := []clientv3.Op{
		clientv3.OpPut("k2", "foo"),
		clientv3.OpGet("k1", clientv3.WithRev(revision-1)),
	}
	_, err = cli.Txn(ctx).If(cmp).Then(then...).Commit()
	require.Error(t, err)
	require.Contains(t, err.Error(), rpctypes.ErrCompacted.Error())

	t.Log("Verify all members have consistent data on key 'k2'")
	for i := 0; i < clus.Cfg.ClusterSize; i++ {
		idx := (targetIdx + i) % clus.Cfg.ClusterSize
		cli = newClient(t, clus.Procs[idx].EndpointsGRPC(), e2e.ClientConfig{})