k8s.io/apiserver@v0.31.1/pkg/storage/etcd3/compact_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package etcd3
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	etcdrpc "go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
    24  	clientv3 "go.etcd.io/etcd/client/v3"
    25  
    26  	"k8s.io/apiserver/pkg/storage/etcd3/testserver"
    27  )
    28  
    29  func TestCompact(t *testing.T) {
    30  	client := testserver.RunEtcd(t, nil)
    31  	ctx := context.Background()
    32  
    33  	putResp, err := client.Put(ctx, "/somekey", "data")
    34  	if err != nil {
    35  		t.Fatalf("Put failed: %v", err)
    36  	}
    37  
    38  	putResp1, err := client.Put(ctx, "/somekey", "data2")
    39  	if err != nil {
    40  		t.Fatalf("Put failed: %v", err)
    41  	}
    42  
    43  	_, _, err = compact(ctx, client, 0, putResp1.Header.Revision)
    44  	if err != nil {
    45  		t.Fatalf("compact failed: %v", err)
    46  	}
    47  
    48  	obj, err := client.Get(ctx, "/somekey", clientv3.WithRev(putResp.Header.Revision))
    49  	if err != etcdrpc.ErrCompacted {
    50  		t.Errorf("Expecting ErrCompacted, but get=%v err=%v", obj, err)
    51  	}
    52  }
    53  
    54  // TestCompactConflict tests that two compactors (Let's use C1, C2) are trying to compact etcd cluster with the same
    55  // logical time.
    56  // - C1 compacts first. It will succeed.
    57  // - C2 compacts after. It will fail. But it will get latest logical time, which should be larger by one.
    58  func TestCompactConflict(t *testing.T) {
    59  	client := testserver.RunEtcd(t, nil)
    60  	ctx := context.Background()
    61  
    62  	putResp, err := client.Put(ctx, "/somekey", "data")
    63  	if err != nil {
    64  		t.Fatalf("Put failed: %v", err)
    65  	}
    66  
    67  	// Compact first. It would do the compaction and return compact time which is incremented by 1.
    68  	curTime, _, err := compact(ctx, client, 0, putResp.Header.Revision)
    69  	if err != nil {
    70  		t.Fatalf("compact failed: %v", err)
    71  	}
    72  	if curTime != 1 {
    73  		t.Errorf("Expect current logical time = 1, get = %v", curTime)
    74  	}
    75  
    76  	// Compact again with the same parameters. It won't do compaction but return the latest compact time.
    77  	curTime2, _, err := compact(ctx, client, 0, putResp.Header.Revision)
    78  	if err != nil {
    79  		t.Fatalf("compact failed: %v", err)
    80  	}
    81  	if curTime != curTime2 {
    82  		t.Errorf("Unexpected curTime (%v) != curTime2 (%v)", curTime, curTime2)
    83  	}
    84  }