github.com/hernad/nomad@v1.6.112/command/agent/consul/config_entries_testing.go (about)

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