github.com/lfch/etcd-io/tests/v3@v3.0.0-20221004140520-eac99acd3e9d/common/defrag_test.go (about)

     1  // Copyright 2022 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package common
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	clientv3 "github.com/lfch/etcd-io/client/v3"
    23  	"github.com/lfch/etcd-io/tests/v3/framework"
    24  	"github.com/lfch/etcd-io/tests/v3/framework/config"
    25  	"github.com/lfch/etcd-io/tests/v3/framework/testutils"
    26  )
    27  
    28  func TestDefragOnline(t *testing.T) {
    29  	testRunner.BeforeTest(t)
    30  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    31  	defer cancel()
    32  	options := config.DefragOption{Timeout: 10 * time.Second}
    33  	clus := testRunner.NewCluster(ctx, t, config.ClusterConfig{ClusterSize: 3})
    34  	cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))
    35  	testutils.ExecuteUntil(ctx, t, func() {
    36  		defer clus.Close()
    37  		var kvs = []testutils.KV{{Key: "key", Val: "val1"}, {Key: "key", Val: "val2"}, {Key: "key", Val: "val3"}}
    38  		for i := range kvs {
    39  			if err := cc.Put(ctx, kvs[i].Key, kvs[i].Val, config.PutOptions{}); err != nil {
    40  				t.Fatalf("compactTest #%d: put kv error (%v)", i, err)
    41  			}
    42  		}
    43  		_, err := cc.Compact(ctx, 4, config.CompactOption{Physical: true, Timeout: 10 * time.Second})
    44  		if err != nil {
    45  			t.Fatalf("defrag_test: compact with revision error (%v)", err)
    46  		}
    47  
    48  		if err = cc.Defragment(ctx, options); err != nil {
    49  			t.Fatalf("defrag_test: defrag error (%v)", err)
    50  		}
    51  	})
    52  }