github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/terraform/eval_context_mock.go (about)

     1  package terraform
     2  
     3  import (
     4  	"github.com/hashicorp/hcl/v2"
     5  	"github.com/hashicorp/hcl/v2/hcldec"
     6  	"github.com/muratcelep/terraform/not-internal/addrs"
     7  	"github.com/muratcelep/terraform/not-internal/configs/configschema"
     8  	"github.com/muratcelep/terraform/not-internal/instances"
     9  	"github.com/muratcelep/terraform/not-internal/lang"
    10  	"github.com/muratcelep/terraform/not-internal/plans"
    11  	"github.com/muratcelep/terraform/not-internal/providers"
    12  	"github.com/muratcelep/terraform/not-internal/provisioners"
    13  	"github.com/muratcelep/terraform/not-internal/refactoring"
    14  	"github.com/muratcelep/terraform/not-internal/states"
    15  	"github.com/muratcelep/terraform/not-internal/tfdiags"
    16  	"github.com/zclconf/go-cty/cty"
    17  	"github.com/zclconf/go-cty/cty/convert"
    18  )
    19  
    20  // MockEvalContext is a mock version of EvalContext that can be used
    21  // for tests.
    22  type MockEvalContext struct {
    23  	StoppedCalled bool
    24  	StoppedValue  <-chan struct{}
    25  
    26  	HookCalled bool
    27  	HookHook   Hook
    28  	HookError  error
    29  
    30  	InputCalled bool
    31  	InputInput  UIInput
    32  
    33  	InitProviderCalled   bool
    34  	InitProviderType     string
    35  	InitProviderAddr     addrs.AbsProviderConfig
    36  	InitProviderProvider providers.Interface
    37  	InitProviderError    error
    38  
    39  	ProviderCalled   bool
    40  	ProviderAddr     addrs.AbsProviderConfig
    41  	ProviderProvider providers.Interface
    42  
    43  	ProviderSchemaCalled bool
    44  	ProviderSchemaAddr   addrs.AbsProviderConfig
    45  	ProviderSchemaSchema *ProviderSchema
    46  	ProviderSchemaError  error
    47  
    48  	CloseProviderCalled   bool
    49  	CloseProviderAddr     addrs.AbsProviderConfig
    50  	CloseProviderProvider providers.Interface
    51  
    52  	ProviderInputCalled bool
    53  	ProviderInputAddr   addrs.AbsProviderConfig
    54  	ProviderInputValues map[string]cty.Value
    55  
    56  	SetProviderInputCalled bool
    57  	SetProviderInputAddr   addrs.AbsProviderConfig
    58  	SetProviderInputValues map[string]cty.Value
    59  
    60  	ConfigureProviderFn func(
    61  		addr addrs.AbsProviderConfig,
    62  		cfg cty.Value) tfdiags.Diagnostics // overrides the other values below, if set
    63  	ConfigureProviderCalled bool
    64  	ConfigureProviderAddr   addrs.AbsProviderConfig
    65  	ConfigureProviderConfig cty.Value
    66  	ConfigureProviderDiags  tfdiags.Diagnostics
    67  
    68  	ProvisionerCalled      bool
    69  	ProvisionerName        string
    70  	ProvisionerProvisioner provisioners.Interface
    71  
    72  	ProvisionerSchemaCalled bool
    73  	ProvisionerSchemaName   string
    74  	ProvisionerSchemaSchema *configschema.Block
    75  	ProvisionerSchemaError  error
    76  
    77  	CloseProvisionersCalled bool
    78  
    79  	EvaluateBlockCalled     bool
    80  	EvaluateBlockBody       hcl.Body
    81  	EvaluateBlockSchema     *configschema.Block
    82  	EvaluateBlockSelf       addrs.Referenceable
    83  	EvaluateBlockKeyData    InstanceKeyEvalData
    84  	EvaluateBlockResultFunc func(
    85  		body hcl.Body,
    86  		schema *configschema.Block,
    87  		self addrs.Referenceable,
    88  		keyData InstanceKeyEvalData,
    89  	) (cty.Value, hcl.Body, tfdiags.Diagnostics) // overrides the other values below, if set
    90  	EvaluateBlockResult       cty.Value
    91  	EvaluateBlockExpandedBody hcl.Body
    92  	EvaluateBlockDiags        tfdiags.Diagnostics
    93  
    94  	EvaluateExprCalled     bool
    95  	EvaluateExprExpr       hcl.Expression
    96  	EvaluateExprWantType   cty.Type
    97  	EvaluateExprSelf       addrs.Referenceable
    98  	EvaluateExprResultFunc func(
    99  		expr hcl.Expression,
   100  		wantType cty.Type,
   101  		self addrs.Referenceable,
   102  	) (cty.Value, tfdiags.Diagnostics) // overrides the other values below, if set
   103  	EvaluateExprResult cty.Value
   104  	EvaluateExprDiags  tfdiags.Diagnostics
   105  
   106  	EvaluationScopeCalled  bool
   107  	EvaluationScopeSelf    addrs.Referenceable
   108  	EvaluationScopeKeyData InstanceKeyEvalData
   109  	EvaluationScopeScope   *lang.Scope
   110  
   111  	PathCalled bool
   112  	PathPath   addrs.ModuleInstance
   113  
   114  	SetModuleCallArgumentsCalled bool
   115  	SetModuleCallArgumentsModule addrs.ModuleCallInstance
   116  	SetModuleCallArgumentsValues map[string]cty.Value
   117  
   118  	GetVariableValueCalled bool
   119  	GetVariableValueAddr   addrs.AbsInputVariableInstance
   120  	GetVariableValueValue  cty.Value
   121  
   122  	ChangesCalled  bool
   123  	ChangesChanges *plans.ChangesSync
   124  
   125  	StateCalled bool
   126  	StateState  *states.SyncState
   127  
   128  	RefreshStateCalled bool
   129  	RefreshStateState  *states.SyncState
   130  
   131  	PrevRunStateCalled bool
   132  	PrevRunStateState  *states.SyncState
   133  
   134  	MoveResultsCalled  bool
   135  	MoveResultsResults refactoring.MoveResults
   136  
   137  	InstanceExpanderCalled   bool
   138  	InstanceExpanderExpander *instances.Expander
   139  }
   140  
   141  // MockEvalContext implements EvalContext
   142  var _ EvalContext = (*MockEvalContext)(nil)
   143  
   144  func (c *MockEvalContext) Stopped() <-chan struct{} {
   145  	c.StoppedCalled = true
   146  	return c.StoppedValue
   147  }
   148  
   149  func (c *MockEvalContext) Hook(fn func(Hook) (HookAction, error)) error {
   150  	c.HookCalled = true
   151  	if c.HookHook != nil {
   152  		if _, err := fn(c.HookHook); err != nil {
   153  			return err
   154  		}
   155  	}
   156  
   157  	return c.HookError
   158  }
   159  
   160  func (c *MockEvalContext) Input() UIInput {
   161  	c.InputCalled = true
   162  	return c.InputInput
   163  }
   164  
   165  func (c *MockEvalContext) InitProvider(addr addrs.AbsProviderConfig) (providers.Interface, error) {
   166  	c.InitProviderCalled = true
   167  	c.InitProviderType = addr.String()
   168  	c.InitProviderAddr = addr
   169  	return c.InitProviderProvider, c.InitProviderError
   170  }
   171  
   172  func (c *MockEvalContext) Provider(addr addrs.AbsProviderConfig) providers.Interface {
   173  	c.ProviderCalled = true
   174  	c.ProviderAddr = addr
   175  	return c.ProviderProvider
   176  }
   177  
   178  func (c *MockEvalContext) ProviderSchema(addr addrs.AbsProviderConfig) (*ProviderSchema, error) {
   179  	c.ProviderSchemaCalled = true
   180  	c.ProviderSchemaAddr = addr
   181  	return c.ProviderSchemaSchema, c.ProviderSchemaError
   182  }
   183  
   184  func (c *MockEvalContext) CloseProvider(addr addrs.AbsProviderConfig) error {
   185  	c.CloseProviderCalled = true
   186  	c.CloseProviderAddr = addr
   187  	return nil
   188  }
   189  
   190  func (c *MockEvalContext) ConfigureProvider(addr addrs.AbsProviderConfig, cfg cty.Value) tfdiags.Diagnostics {
   191  
   192  	c.ConfigureProviderCalled = true
   193  	c.ConfigureProviderAddr = addr
   194  	c.ConfigureProviderConfig = cfg
   195  	if c.ConfigureProviderFn != nil {
   196  		return c.ConfigureProviderFn(addr, cfg)
   197  	}
   198  	return c.ConfigureProviderDiags
   199  }
   200  
   201  func (c *MockEvalContext) ProviderInput(addr addrs.AbsProviderConfig) map[string]cty.Value {
   202  	c.ProviderInputCalled = true
   203  	c.ProviderInputAddr = addr
   204  	return c.ProviderInputValues
   205  }
   206  
   207  func (c *MockEvalContext) SetProviderInput(addr addrs.AbsProviderConfig, vals map[string]cty.Value) {
   208  	c.SetProviderInputCalled = true
   209  	c.SetProviderInputAddr = addr
   210  	c.SetProviderInputValues = vals
   211  }
   212  
   213  func (c *MockEvalContext) Provisioner(n string) (provisioners.Interface, error) {
   214  	c.ProvisionerCalled = true
   215  	c.ProvisionerName = n
   216  	return c.ProvisionerProvisioner, nil
   217  }
   218  
   219  func (c *MockEvalContext) ProvisionerSchema(n string) (*configschema.Block, error) {
   220  	c.ProvisionerSchemaCalled = true
   221  	c.ProvisionerSchemaName = n
   222  	return c.ProvisionerSchemaSchema, c.ProvisionerSchemaError
   223  }
   224  
   225  func (c *MockEvalContext) CloseProvisioners() error {
   226  	c.CloseProvisionersCalled = true
   227  	return nil
   228  }
   229  
   230  func (c *MockEvalContext) EvaluateBlock(body hcl.Body, schema *configschema.Block, self addrs.Referenceable, keyData InstanceKeyEvalData) (cty.Value, hcl.Body, tfdiags.Diagnostics) {
   231  	c.EvaluateBlockCalled = true
   232  	c.EvaluateBlockBody = body
   233  	c.EvaluateBlockSchema = schema
   234  	c.EvaluateBlockSelf = self
   235  	c.EvaluateBlockKeyData = keyData
   236  	if c.EvaluateBlockResultFunc != nil {
   237  		return c.EvaluateBlockResultFunc(body, schema, self, keyData)
   238  	}
   239  	return c.EvaluateBlockResult, c.EvaluateBlockExpandedBody, c.EvaluateBlockDiags
   240  }
   241  
   242  func (c *MockEvalContext) EvaluateExpr(expr hcl.Expression, wantType cty.Type, self addrs.Referenceable) (cty.Value, tfdiags.Diagnostics) {
   243  	c.EvaluateExprCalled = true
   244  	c.EvaluateExprExpr = expr
   245  	c.EvaluateExprWantType = wantType
   246  	c.EvaluateExprSelf = self
   247  	if c.EvaluateExprResultFunc != nil {
   248  		return c.EvaluateExprResultFunc(expr, wantType, self)
   249  	}
   250  	return c.EvaluateExprResult, c.EvaluateExprDiags
   251  }
   252  
   253  // installSimpleEval is a helper to install a simple mock implementation of
   254  // both EvaluateBlock and EvaluateExpr into the receiver.
   255  //
   256  // These default implementations will either evaluate the given input against
   257  // the scope in field EvaluationScopeScope or, if it is nil, with no eval
   258  // context at all so that only constant values may be used.
   259  //
   260  // This function overwrites any existing functions installed in fields
   261  // EvaluateBlockResultFunc and EvaluateExprResultFunc.
   262  func (c *MockEvalContext) installSimpleEval() {
   263  	c.EvaluateBlockResultFunc = func(body hcl.Body, schema *configschema.Block, self addrs.Referenceable, keyData InstanceKeyEvalData) (cty.Value, hcl.Body, tfdiags.Diagnostics) {
   264  		if scope := c.EvaluationScopeScope; scope != nil {
   265  			// Fully-functional codepath.
   266  			var diags tfdiags.Diagnostics
   267  			body, diags = scope.ExpandBlock(body, schema)
   268  			if diags.HasErrors() {
   269  				return cty.DynamicVal, body, diags
   270  			}
   271  			val, evalDiags := c.EvaluationScopeScope.EvalBlock(body, schema)
   272  			diags = diags.Append(evalDiags)
   273  			if evalDiags.HasErrors() {
   274  				return cty.DynamicVal, body, diags
   275  			}
   276  			return val, body, diags
   277  		}
   278  
   279  		// Fallback codepath supporting constant values only.
   280  		val, hclDiags := hcldec.Decode(body, schema.DecoderSpec(), nil)
   281  		return val, body, tfdiags.Diagnostics(nil).Append(hclDiags)
   282  	}
   283  	c.EvaluateExprResultFunc = func(expr hcl.Expression, wantType cty.Type, self addrs.Referenceable) (cty.Value, tfdiags.Diagnostics) {
   284  		if scope := c.EvaluationScopeScope; scope != nil {
   285  			// Fully-functional codepath.
   286  			return scope.EvalExpr(expr, wantType)
   287  		}
   288  
   289  		// Fallback codepath supporting constant values only.
   290  		var diags tfdiags.Diagnostics
   291  		val, hclDiags := expr.Value(nil)
   292  		diags = diags.Append(hclDiags)
   293  		if hclDiags.HasErrors() {
   294  			return cty.DynamicVal, diags
   295  		}
   296  		var err error
   297  		val, err = convert.Convert(val, wantType)
   298  		if err != nil {
   299  			diags = diags.Append(err)
   300  			return cty.DynamicVal, diags
   301  		}
   302  		return val, diags
   303  	}
   304  }
   305  
   306  func (c *MockEvalContext) EvaluationScope(self addrs.Referenceable, keyData InstanceKeyEvalData) *lang.Scope {
   307  	c.EvaluationScopeCalled = true
   308  	c.EvaluationScopeSelf = self
   309  	c.EvaluationScopeKeyData = keyData
   310  	return c.EvaluationScopeScope
   311  }
   312  
   313  func (c *MockEvalContext) WithPath(path addrs.ModuleInstance) EvalContext {
   314  	newC := *c
   315  	newC.PathPath = path
   316  	return &newC
   317  }
   318  
   319  func (c *MockEvalContext) Path() addrs.ModuleInstance {
   320  	c.PathCalled = true
   321  	return c.PathPath
   322  }
   323  
   324  func (c *MockEvalContext) SetModuleCallArguments(n addrs.ModuleCallInstance, values map[string]cty.Value) {
   325  	c.SetModuleCallArgumentsCalled = true
   326  	c.SetModuleCallArgumentsModule = n
   327  	c.SetModuleCallArgumentsValues = values
   328  }
   329  
   330  func (c *MockEvalContext) GetVariableValue(addr addrs.AbsInputVariableInstance) cty.Value {
   331  	c.GetVariableValueCalled = true
   332  	c.GetVariableValueAddr = addr
   333  	return c.GetVariableValueValue
   334  }
   335  
   336  func (c *MockEvalContext) Changes() *plans.ChangesSync {
   337  	c.ChangesCalled = true
   338  	return c.ChangesChanges
   339  }
   340  
   341  func (c *MockEvalContext) State() *states.SyncState {
   342  	c.StateCalled = true
   343  	return c.StateState
   344  }
   345  
   346  func (c *MockEvalContext) RefreshState() *states.SyncState {
   347  	c.RefreshStateCalled = true
   348  	return c.RefreshStateState
   349  }
   350  
   351  func (c *MockEvalContext) PrevRunState() *states.SyncState {
   352  	c.PrevRunStateCalled = true
   353  	return c.PrevRunStateState
   354  }
   355  
   356  func (c *MockEvalContext) MoveResults() refactoring.MoveResults {
   357  	c.MoveResultsCalled = true
   358  	return c.MoveResultsResults
   359  }
   360  
   361  func (c *MockEvalContext) InstanceExpander() *instances.Expander {
   362  	c.InstanceExpanderCalled = true
   363  	return c.InstanceExpanderExpander
   364  }