github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/agent/consul/config_entries_testing.go (about)

     1  package consul
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/hashicorp/consul/api"
     7  	"github.com/hashicorp/go-hclog"
     8  )
     9  
    10  var _ ConfigAPI = (*MockConfigsAPI)(nil)
    11  
    12  type MockConfigsAPI struct {
    13  	logger hclog.Logger
    14  
    15  	lock  sync.Mutex
    16  	state struct {
    17  		error   error
    18  		entries map[string]api.ConfigEntry
    19  	}
    20  }
    21  
    22  func NewMockConfigsAPI(l hclog.Logger) *MockConfigsAPI {
    23  	return &MockConfigsAPI{
    24  		logger: l.Named("mock_consul"),
    25  		state: struct {
    26  			error   error
    27  			entries map[string]api.ConfigEntry
    28  		}{entries: make(map[string]api.ConfigEntry)},
    29  	}
    30  }
    31  
    32  // Set is a mock of ConfigAPI.Set
    33  func (m *MockConfigsAPI) Set(entry api.ConfigEntry, w *api.WriteOptions) (bool, *api.WriteMeta, error) {
    34  	m.lock.Lock()
    35  	defer m.lock.Unlock()
    36  
    37  	if m.state.error != nil {
    38  		return false, nil, m.state.error
    39  	}
    40  
    41  	m.state.entries[entry.GetName()] = entry
    42  
    43  	return true, &api.WriteMeta{
    44  		RequestTime: 1,
    45  	}, nil
    46  }
    47  
    48  // SetError is a helper method for configuring an error that will be returned
    49  // on future calls to mocked methods.
    50  func (m *MockConfigsAPI) SetError(err error) {
    51  	m.lock.Lock()
    52  	defer m.lock.Unlock()
    53  
    54  	m.state.error = err
    55  }