github.com/clly/consul@v1.4.5/agent/consul/fsm/fsm_test.go (about)

     1  package fsm
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/consul/agent/structs"
     9  	"github.com/hashicorp/raft"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  type MockSink struct {
    14  	*bytes.Buffer
    15  	cancel bool
    16  }
    17  
    18  func (m *MockSink) ID() string {
    19  	return "Mock"
    20  }
    21  
    22  func (m *MockSink) Cancel() error {
    23  	m.cancel = true
    24  	return nil
    25  }
    26  
    27  func (m *MockSink) Close() error {
    28  	return nil
    29  }
    30  
    31  func makeLog(buf []byte) *raft.Log {
    32  	return &raft.Log{
    33  		Index: 1,
    34  		Term:  1,
    35  		Type:  raft.LogCommand,
    36  		Data:  buf,
    37  	}
    38  }
    39  
    40  func TestFSM_IgnoreUnknown(t *testing.T) {
    41  	t.Parallel()
    42  	fsm, err := New(nil, os.Stderr)
    43  	assert.Nil(t, err)
    44  
    45  	// Create a new reap request
    46  	type UnknownRequest struct {
    47  		Foo string
    48  	}
    49  	req := UnknownRequest{Foo: "bar"}
    50  	msgType := structs.IgnoreUnknownTypeFlag | 64
    51  	buf, err := structs.Encode(msgType, req)
    52  	assert.Nil(t, err)
    53  
    54  	// Apply should work, even though not supported
    55  	resp := fsm.Apply(makeLog(buf))
    56  	err, ok := resp.(error)
    57  	assert.False(t, ok, "response: %s", err)
    58  }