1// Copyright 2016 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
15package e2e
16
17import (
18	"strconv"
19	"strings"
20	"testing"
21)
22
23func TestCtlV3Compact(t *testing.T)         { testCtl(t, compactTest) }
24func TestCtlV3CompactPhysical(t *testing.T) { testCtl(t, compactTest, withCompactPhysical()) }
25
26func compactTest(cx ctlCtx) {
27	compactPhysical := cx.compactPhysical
28	if err := ctlV3Compact(cx, 2, compactPhysical); err != nil {
29		if !strings.Contains(err.Error(), "required revision is a future revision") {
30			cx.t.Fatal(err)
31		}
32	} else {
33		cx.t.Fatalf("expected '...future revision' error, got <nil>")
34	}
35
36	var kvs = []kv{{"key", "val1"}, {"key", "val2"}, {"key", "val3"}}
37	for i := range kvs {
38		if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
39			cx.t.Fatalf("compactTest #%d: ctlV3Put error (%v)", i, err)
40		}
41	}
42
43	if err := ctlV3Get(cx, []string{"key", "--rev", "3"}, kvs[1:2]...); err != nil {
44		cx.t.Errorf("compactTest: ctlV3Get error (%v)", err)
45	}
46
47	if err := ctlV3Compact(cx, 4, compactPhysical); err != nil {
48		cx.t.Fatal(err)
49	}
50
51	if err := ctlV3Get(cx, []string{"key", "--rev", "3"}, kvs[1:2]...); err != nil {
52		if !strings.Contains(err.Error(), "required revision has been compacted") {
53			cx.t.Errorf("compactTest: ctlV3Get error (%v)", err)
54		}
55	} else {
56		cx.t.Fatalf("expected '...has been compacted' error, got <nil>")
57	}
58
59	if err := ctlV3Compact(cx, 2, compactPhysical); err != nil {
60		if !strings.Contains(err.Error(), "required revision has been compacted") {
61			cx.t.Fatal(err)
62		}
63	} else {
64		cx.t.Fatalf("expected '...has been compacted' error, got <nil>")
65	}
66}
67
68func ctlV3Compact(cx ctlCtx, rev int64, physical bool) error {
69	rs := strconv.FormatInt(rev, 10)
70	cmdArgs := append(cx.PrefixArgs(), "compact", rs)
71	if physical {
72		cmdArgs = append(cmdArgs, "--physical")
73	}
74	return spawnWithExpect(cmdArgs, "compacted revision "+rs)
75}
76