github.com/opentofu/opentofu@v1.7.1/internal/tofu/context_plugins_test.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package tofu
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/zclconf/go-cty/cty"
    12  
    13  	"github.com/opentofu/opentofu/internal/addrs"
    14  	"github.com/opentofu/opentofu/internal/configs/configschema"
    15  	"github.com/opentofu/opentofu/internal/providers"
    16  	"github.com/opentofu/opentofu/internal/provisioners"
    17  )
    18  
    19  // simpleMockPluginLibrary returns a plugin library pre-configured with
    20  // one provider and one provisioner, both called "test".
    21  //
    22  // The provider is built with simpleMockProvider and the provisioner with
    23  // simpleMockProvisioner, and all schemas used in both are as built by
    24  // function simpleTestSchema.
    25  //
    26  // Each call to this function produces an entirely-separate set of objects,
    27  // so the caller can feel free to modify the returned value to further
    28  // customize the mocks contained within.
    29  func simpleMockPluginLibrary() *contextPlugins {
    30  	// We create these out here, rather than in the factory functions below,
    31  	// because we want each call to the factory to return the _same_ instance,
    32  	// so that test code can customize it before passing this component
    33  	// factory into real code under test.
    34  	provider := simpleMockProvider()
    35  	provisioner := simpleMockProvisioner()
    36  	ret := &contextPlugins{
    37  		providerFactories: map[addrs.Provider]providers.Factory{
    38  			addrs.NewDefaultProvider("test"): func() (providers.Interface, error) {
    39  				return provider, nil
    40  			},
    41  		},
    42  		provisionerFactories: map[string]provisioners.Factory{
    43  			"test": func() (provisioners.Interface, error) {
    44  				return provisioner, nil
    45  			},
    46  		},
    47  	}
    48  	return ret
    49  }
    50  
    51  // simpleTestSchema returns a block schema that contains a few optional
    52  // attributes for use in tests.
    53  //
    54  // The returned schema contains the following optional attributes:
    55  //
    56  //   - test_string, of type string
    57  //   - test_number, of type number
    58  //   - test_bool, of type bool
    59  //   - test_list, of type list(string)
    60  //   - test_map, of type map(string)
    61  //
    62  // Each call to this function produces an entirely new schema instance, so
    63  // callers can feel free to modify it once returned.
    64  func simpleTestSchema() *configschema.Block {
    65  	return &configschema.Block{
    66  		Attributes: map[string]*configschema.Attribute{
    67  			"test_string": {
    68  				Type:     cty.String,
    69  				Optional: true,
    70  			},
    71  			"test_number": {
    72  				Type:     cty.Number,
    73  				Optional: true,
    74  			},
    75  			"test_bool": {
    76  				Type:     cty.Bool,
    77  				Optional: true,
    78  			},
    79  			"test_list": {
    80  				Type:     cty.List(cty.String),
    81  				Optional: true,
    82  			},
    83  			"test_map": {
    84  				Type:     cty.Map(cty.String),
    85  				Optional: true,
    86  			},
    87  		},
    88  	}
    89  }
    90  
    91  func newContextPluginsForTest(providerFactories map[addrs.Provider]providers.Factory, t *testing.T) *contextPlugins {
    92  	plugins, err := newContextPlugins(providerFactories, nil)
    93  	if err != nil {
    94  		t.Fatal(err.Error())
    95  	}
    96  	return plugins
    97  }