Skip to content

Commit

Permalink
add logstore as test parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
dhiaayachi authored and jmurret committed Mar 18, 2023
1 parent a8525f8 commit a2d5605
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
17 changes: 17 additions & 0 deletions test/integration/consul-container/libs/cluster/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ const (
ConsulCACertKey = "consul-agent-ca-key.pem"
)

type LogStore string

const (
LogStore_WAL LogStore = "wal"
LogStore_BoltDB LogStore = "boltdb"
)

// BuildContext provides a reusable object meant to share common configuration settings
// between agent configuration builders.
type BuildContext struct {
Expand All @@ -41,6 +48,7 @@ type BuildContext struct {
tlsCertIndex int // keeps track of the certificates issued for naming purposes

aclEnabled bool
logStore LogStore
}

func (c *BuildContext) DockerImage() string {
Expand Down Expand Up @@ -89,6 +97,9 @@ type BuildOptions struct {

// ACLEnabled configures acl in agent configuration
ACLEnabled bool

//StoreLog define which LogStore to use
LogStore LogStore
}

func NewBuildContext(t *testing.T, opts BuildOptions) *BuildContext {
Expand All @@ -103,6 +114,7 @@ func NewBuildContext(t *testing.T, opts BuildOptions) *BuildContext {
useAPIWithTLS: opts.UseAPIWithTLS,
useGRPCWithTLS: opts.UseGRPCWithTLS,
aclEnabled: opts.ACLEnabled,
logStore: opts.LogStore,
}

if ctx.consulImageName == "" {
Expand Down Expand Up @@ -201,6 +213,11 @@ func NewConfigBuilder(ctx *BuildContext) *Builder {
b.conf.Set("acl.default_policy", "deny")
b.conf.Set("acl.enable_token_persistence", true)
}
ls := string(ctx.logStore)
if ls != string(LogStore_WAL) && ls != string(LogStore_BoltDB) {
ls = string(LogStore_BoltDB)
}
b.conf.Set("raft_logstore.backend", ls)

return b
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ func NewCluster(
AllowHTTPAnyway: true,
ConsulVersion: config.BuildOpts.ConsulVersion,
ACLEnabled: config.BuildOpts.ACLEnabled,
LogStore: config.BuildOpts.LogStore,
}
ctx := libcluster.NewBuildContext(t, opts)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package snapshot
import (
"fmt"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/sdk/testutil"
libcluster "github.com/hashicorp/consul/test/integration/consul-container/libs/cluster"
libtopology "github.com/hashicorp/consul/test/integration/consul-container/libs/topology"
"github.com/hashicorp/consul/test/integration/consul-container/libs/utils"
Expand All @@ -13,6 +12,18 @@ import (
)

func TestSnapshotRestore(t *testing.T) {

cases := []libcluster.LogStore{libcluster.LogStore_WAL, libcluster.LogStore_BoltDB}

for _, c := range cases {
t.Run(fmt.Sprintf("test log store: %s", c), func(t *testing.T) {
testSnapShotRestoreForLogStore(t, c)
})
}
}

func testSnapShotRestoreForLogStore(t *testing.T, logStore libcluster.LogStore) {

const (
numServers = 3
)
Expand All @@ -25,6 +36,7 @@ func TestSnapshotRestore(t *testing.T) {
Datacenter: "dc1",
ConsulImageName: utils.LatestImageName,
ConsulVersion: utils.LatestVersion,
LogStore: logStore,
},
ApplyDefaultProxySettings: true,
})
Expand All @@ -33,21 +45,17 @@ func TestSnapshotRestore(t *testing.T) {
libcluster.WaitForLeader(t, cluster, client)
libcluster.WaitForMembers(t, client, 3)

testutil.RunStep(t, "Create Data", func(t *testing.T) {
for i := 0; i < 100; i++ {
_, err := client.KV().Put(&api.KVPair{Key: fmt.Sprintf("key-%d", i), Value: []byte(fmt.Sprintf("value-%d", i))}, nil)
require.NoError(t, err)
}
})
for i := 0; i < 100; i++ {
_, err := client.KV().Put(&api.KVPair{Key: fmt.Sprintf("key-%d", i), Value: []byte(fmt.Sprintf("value-%d", i))}, nil)
require.NoError(t, err)
}

var snapshot io.ReadCloser
testutil.RunStep(t, "Save snapshot", func(t *testing.T) {
var err error
snapshot, _, err = client.Snapshot().Save(nil)
require.NoError(t, err)
})
var err error
snapshot, _, err = client.Snapshot().Save(nil)
require.NoError(t, err)

err := cluster.Terminate()
err = cluster.Terminate()
require.NoError(t, err)
// Create a fresh cluster from scratch
cluster2, _, _ := libtopology.NewCluster(t, &libtopology.ClusterConfig{
Expand All @@ -57,26 +65,25 @@ func TestSnapshotRestore(t *testing.T) {
Datacenter: "dc1",
ConsulImageName: utils.LatestImageName,
ConsulVersion: utils.LatestVersion,
LogStore: logStore,
},
ApplyDefaultProxySettings: true,
})
client2 := cluster2.APIClient(0)

testutil.RunStep(t, "Restore saved snapshot", func(t *testing.T) {
libcluster.WaitForLeader(t, cluster2, client2)
libcluster.WaitForMembers(t, client2, 3)

// Restore the saved snapshot
require.NoError(t, client2.Snapshot().Restore(nil, snapshot))
libcluster.WaitForLeader(t, cluster2, client2)
libcluster.WaitForMembers(t, client2, 3)

libcluster.WaitForLeader(t, cluster2, client2)
// Restore the saved snapshot
require.NoError(t, client2.Snapshot().Restore(nil, snapshot))

})
libcluster.WaitForLeader(t, cluster2, client2)

for i := 0; i < 100; i++ {
kv, _, err := client2.KV().Get(fmt.Sprintf("key-%d", i), nil)
kv, _, err := client2.KV().Get(fmt.Sprintf("key-%d", i), &api.QueryOptions{AllowStale: true})
require.NoError(t, err)
require.Equal(t, kv.Key, fmt.Sprintf("key-%d", i))
require.Equal(t, kv.Value, []byte(fmt.Sprintf("value-%d", i)))
}

}

0 comments on commit a2d5605

Please sign in to comment.