github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/states/statemgr/statemgr_fake.go (about)

     1  package statemgr
     2  
     3  import (
     4  	"errors"
     5  	"sync"
     6  
     7  	"github.com/hashicorp/terraform/internal/states"
     8  	"github.com/hashicorp/terraform/internal/terraform"
     9  )
    10  
    11  // NewFullFake returns a full state manager that really only supports transient
    12  // snapshots. This is primarily intended for testing and is not suitable for
    13  // general use.
    14  //
    15  // The persistent part of the interface is stubbed out as an in-memory store,
    16  // and so its snapshots are effectively also transient.
    17  //
    18  // The given Transient implementation is used to implement the transient
    19  // portion of the interface. If nil is given, NewTransientInMemory is
    20  // automatically called to create an in-memory transient manager with no
    21  // initial transient snapshot.
    22  //
    23  // If the given initial state is non-nil then a copy of it will be used as
    24  // the initial persistent snapshot.
    25  //
    26  // The Locker portion of the returned manager uses a local mutex to simulate
    27  // mutually-exclusive access to the fake persistent portion of the object.
    28  func NewFullFake(t Transient, initial *states.State) Full {
    29  	if t == nil {
    30  		t = NewTransientInMemory(nil)
    31  	}
    32  
    33  	// The "persistent" part of our manager is actually just another in-memory
    34  	// transient used to fake a secondary storage layer.
    35  	fakeP := NewTransientInMemory(initial.DeepCopy())
    36  
    37  	return &fakeFull{
    38  		t:     t,
    39  		fakeP: fakeP,
    40  	}
    41  }
    42  
    43  type fakeFull struct {
    44  	t     Transient
    45  	fakeP Transient
    46  
    47  	lockLock sync.Mutex
    48  	locked   bool
    49  }
    50  
    51  var _ Full = (*fakeFull)(nil)
    52  
    53  func (m *fakeFull) State() *states.State {
    54  	return m.t.State()
    55  }
    56  
    57  func (m *fakeFull) WriteState(s *states.State) error {
    58  	return m.t.WriteState(s)
    59  }
    60  
    61  func (m *fakeFull) RefreshState() error {
    62  	return m.t.WriteState(m.fakeP.State())
    63  }
    64  
    65  func (m *fakeFull) PersistState(schemas *terraform.Schemas) error {
    66  	return m.fakeP.WriteState(m.t.State())
    67  }
    68  
    69  func (m *fakeFull) GetRootOutputValues() (map[string]*states.OutputValue, error) {
    70  	return m.State().RootModule().OutputValues, nil
    71  }
    72  
    73  func (m *fakeFull) Lock(info *LockInfo) (string, error) {
    74  	m.lockLock.Lock()
    75  	defer m.lockLock.Unlock()
    76  
    77  	if m.locked {
    78  		return "", &LockError{
    79  			Err:  errors.New("fake state manager is locked"),
    80  			Info: info,
    81  		}
    82  	}
    83  
    84  	m.locked = true
    85  	return "placeholder", nil
    86  }
    87  
    88  func (m *fakeFull) Unlock(id string) error {
    89  	m.lockLock.Lock()
    90  	defer m.lockLock.Unlock()
    91  
    92  	if !m.locked {
    93  		return errors.New("fake state manager is not locked")
    94  	}
    95  	if id != "placeholder" {
    96  		return errors.New("wrong lock id for fake state manager")
    97  	}
    98  
    99  	m.locked = false
   100  	return nil
   101  }
   102  
   103  // NewUnlockErrorFull returns a state manager that is useful for testing errors
   104  // (mostly Unlock errors) when used with the clistate.Locker interface. Lock()
   105  // does not return an error because clistate.Locker Lock()s the state at the
   106  // start of Unlock(), so Lock() must succeeded for Unlock() to get called.
   107  func NewUnlockErrorFull(t Transient, initial *states.State) Full {
   108  	return &fakeErrorFull{}
   109  }
   110  
   111  type fakeErrorFull struct{}
   112  
   113  var _ Full = (*fakeErrorFull)(nil)
   114  
   115  func (m *fakeErrorFull) State() *states.State {
   116  	return nil
   117  }
   118  
   119  func (m *fakeErrorFull) GetRootOutputValues() (map[string]*states.OutputValue, error) {
   120  	return nil, errors.New("fake state manager error")
   121  }
   122  
   123  func (m *fakeErrorFull) WriteState(s *states.State) error {
   124  	return errors.New("fake state manager error")
   125  }
   126  
   127  func (m *fakeErrorFull) RefreshState() error {
   128  	return errors.New("fake state manager error")
   129  }
   130  
   131  func (m *fakeErrorFull) PersistState(schemas *terraform.Schemas) error {
   132  	return errors.New("fake state manager error")
   133  }
   134  
   135  func (m *fakeErrorFull) Lock(info *LockInfo) (string, error) {
   136  	return "placeholder", nil
   137  }
   138  
   139  func (m *fakeErrorFull) Unlock(id string) error {
   140  	return errors.New("fake state manager error")
   141  }