open-consul/agent/consul/fsm/snapshot_oss_test.go

64 lines
1.5 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2022-10-24 17:39:42 +00:00
//go:build !consulent
// +build !consulent
package fsm
import (
"bytes"
"testing"
2022-10-24 17:39:42 +00:00
"github.com/stretchr/testify/require"
2023-04-04 16:30:06 +00:00
"github.com/hashicorp/consul-net-rpc/go-msgpack/codec"
"github.com/hashicorp/consul/agent/consul/state"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/sdk/testutil"
)
2022-10-24 17:39:42 +00:00
func TestRestoreFromEnterprise(t *testing.T) {
logger := testutil.Logger(t)
2023-04-04 16:30:06 +00:00
handle := &testRaftHandle{}
storageBackend := newStorageBackend(t, handle)
handle.apply = func(buf []byte) (any, error) { return storageBackend.Apply(buf, 123), nil }
fsm := NewFromDeps(Deps{
Logger: logger,
NewStateStore: func() *state.Store {
return state.NewStateStore(nil)
},
StorageBackend: storageBackend,
})
// To verify if a proper message is displayed when Consul OSS tries to
// unsuccessfully restore entries from a Consul Ent snapshot.
2022-10-24 17:39:42 +00:00
buf := bytes.NewBuffer(nil)
sink := &MockSink{buf, false}
type EntMock struct {
ID int
Type string
}
entMockEntry := EntMock{
ID: 65,
Type: "A Consul Ent Log Type",
}
// Write the header
header := SnapshotHeader{
LastIndex: 0,
}
2022-10-24 17:39:42 +00:00
encoder := codec.NewEncoder(sink, structs.MsgpackHandle)
encoder.Encode(&header)
sink.Write([]byte{byte(structs.MessageType(entMockEntry.ID))})
encoder.Encode(entMockEntry)
require.EqualError(t, fsm.Restore(sink), "msg type <65> is a Consul Enterprise log entry. Consul OSS cannot restore it")
sink.Cancel()
}