2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
package fsm
|
2013-12-11 22:38:18 +00:00
|
|
|
|
|
|
|
import (
|
2013-12-16 18:47:14 +00:00
|
|
|
"bytes"
|
2013-12-11 22:38:18 +00:00
|
|
|
"testing"
|
2017-04-19 23:00:11 +00:00
|
|
|
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2014-10-09 18:54:47 +00:00
|
|
|
"github.com/hashicorp/raft"
|
2018-03-06 17:48:15 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2013-12-11 22:38:18 +00:00
|
|
|
)
|
|
|
|
|
2013-12-16 18:47:14 +00:00
|
|
|
type MockSink struct {
|
|
|
|
*bytes.Buffer
|
|
|
|
cancel bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockSink) ID() string {
|
|
|
|
return "Mock"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockSink) Cancel() error {
|
|
|
|
m.cancel = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockSink) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-06 22:18:38 +00:00
|
|
|
func makeLog(buf []byte) *raft.Log {
|
|
|
|
return &raft.Log{
|
|
|
|
Index: 1,
|
|
|
|
Term: 1,
|
|
|
|
Type: raft.LogCommand,
|
|
|
|
Data: buf,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 02:44:21 +00:00
|
|
|
func TestFSM_IgnoreUnknown(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
2020-01-28 23:50:41 +00:00
|
|
|
logger := testutil.Logger(t)
|
|
|
|
fsm, err := New(nil, logger)
|
2018-03-06 17:48:15 +00:00
|
|
|
assert.Nil(t, err)
|
2015-05-06 02:44:21 +00:00
|
|
|
|
|
|
|
// Create a new reap request
|
|
|
|
type UnknownRequest struct {
|
|
|
|
Foo string
|
|
|
|
}
|
|
|
|
req := UnknownRequest{Foo: "bar"}
|
2019-12-06 20:35:58 +00:00
|
|
|
msgType := structs.IgnoreUnknownTypeFlag | 75
|
2015-05-06 02:44:21 +00:00
|
|
|
buf, err := structs.Encode(msgType, req)
|
2018-03-06 17:48:15 +00:00
|
|
|
assert.Nil(t, err)
|
2015-05-06 02:44:21 +00:00
|
|
|
|
|
|
|
// Apply should work, even though not supported
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
2018-03-06 17:48:15 +00:00
|
|
|
err, ok := resp.(error)
|
|
|
|
assert.False(t, ok, "response: %s", err)
|
2015-05-06 02:44:21 +00:00
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
|
|
|
|
func TestFSM_NilLogger(t *testing.T) {
|
|
|
|
fsm, err := New(nil, nil)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, fsm)
|
|
|
|
}
|