github.com/lfch/etcd-io/tests/v3@v3.0.0-20221004140520-eac99acd3e9d/common/compact_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  	"strings"
    20  	"testing"
    21  	"time"
    22  
    23  	clientv3 "github.com/lfch/etcd-io/client/v3"
    24  	"github.com/lfch/etcd-io/tests/v3/framework"
    25  	"github.com/lfch/etcd-io/tests/v3/framework/config"
    26  	"github.com/lfch/etcd-io/tests/v3/framework/testutils"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestCompact(t *testing.T) {
    31  
    32  	testRunner.BeforeTest(t)
    33  	tcs := []struct {
    34  		name    string
    35  		options config.CompactOption
    36  	}{
    37  		{
    38  			name:    "NoPhysical",
    39  			options: config.CompactOption{Physical: false, Timeout: 10 * time.Second},
    40  		},
    41  		{
    42  			name:    "Physical",
    43  			options: config.CompactOption{Physical: true, Timeout: 10 * time.Second},
    44  		},
    45  	}
    46  	for _, tc := range tcs {
    47  		t.Run(tc.name, func(t *testing.T) {
    48  			ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    49  			defer cancel()
    50  			clus := testRunner.NewCluster(ctx, t, config.ClusterConfig{ClusterSize: 3})
    51  			defer clus.Close()
    52  			cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))
    53  			testutils.ExecuteUntil(ctx, t, func() {
    54  				var kvs = []testutils.KV{{Key: "key", Val: "val1"}, {Key: "key", Val: "val2"}, {Key: "key", Val: "val3"}}
    55  				for i := range kvs {
    56  					if err := cc.Put(ctx, kvs[i].Key, kvs[i].Val, config.PutOptions{}); err != nil {
    57  						t.Fatalf("compactTest #%d: put kv error (%v)", i, err)
    58  					}
    59  				}
    60  				get, err := cc.Get(ctx, "key", config.GetOptions{Revision: 3})
    61  				if err != nil {
    62  					t.Fatalf("compactTest: Get kv by revision error (%v)", err)
    63  				}
    64  
    65  				getkvs := testutils.KeyValuesFromGetResponse(get)
    66  				assert.Equal(t, kvs[1:2], getkvs)
    67  
    68  				_, err = cc.Compact(ctx, 4, tc.options)
    69  				if err != nil {
    70  					t.Fatalf("compactTest: Compact error (%v)", err)
    71  				}
    72  
    73  				get, err = cc.Get(ctx, "key", config.GetOptions{Revision: 3})
    74  				if err != nil {
    75  					if !strings.Contains(err.Error(), "required revision has been compacted") {
    76  						t.Fatalf("compactTest: Get compact key error (%v)", err)
    77  					}
    78  				} else {
    79  					t.Fatalf("expected '...has been compacted' error, got <nil>")
    80  				}
    81  
    82  				_, err = cc.Compact(ctx, 2, tc.options)
    83  				if err != nil {
    84  					if !strings.Contains(err.Error(), "required revision has been compacted") {
    85  						t.Fatal(err)
    86  					}
    87  				} else {
    88  					t.Fatalf("expected '...has been compacted' error, got <nil>")
    89  				}
    90  			})
    91  		})
    92  	}
    93  }