github.com/opentofu/opentofu@v1.7.1/internal/tofu/eval_provider_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/hashicorp/hcl/v2/hcldec"
    12  	"github.com/zclconf/go-cty/cty"
    13  
    14  	"github.com/opentofu/opentofu/internal/addrs"
    15  	"github.com/opentofu/opentofu/internal/configs"
    16  	"github.com/opentofu/opentofu/internal/configs/configschema"
    17  )
    18  
    19  func TestBuildProviderConfig(t *testing.T) {
    20  	configBody := configs.SynthBody("", map[string]cty.Value{
    21  		"set_in_config": cty.StringVal("config"),
    22  	})
    23  	providerAddr := addrs.AbsProviderConfig{
    24  		Module:   addrs.RootModule,
    25  		Provider: addrs.NewDefaultProvider("foo"),
    26  	}
    27  
    28  	ctx := &MockEvalContext{
    29  		// The input values map is expected to contain only keys that aren't
    30  		// already present in the config, since we skip prompting for
    31  		// attributes that are already set.
    32  		ProviderInputValues: map[string]cty.Value{
    33  			"set_by_input": cty.StringVal("input"),
    34  		},
    35  	}
    36  	gotBody := buildProviderConfig(ctx, providerAddr, &configs.Provider{
    37  		Name:   "foo",
    38  		Config: configBody,
    39  	})
    40  
    41  	schema := &configschema.Block{
    42  		Attributes: map[string]*configschema.Attribute{
    43  			"set_in_config": {Type: cty.String, Optional: true},
    44  			"set_by_input":  {Type: cty.String, Optional: true},
    45  		},
    46  	}
    47  	got, diags := hcldec.Decode(gotBody, schema.DecoderSpec(), nil)
    48  	if diags.HasErrors() {
    49  		t.Fatalf("body decode failed: %s", diags.Error())
    50  	}
    51  
    52  	// We expect the provider config with the added input value
    53  	want := cty.ObjectVal(map[string]cty.Value{
    54  		"set_in_config": cty.StringVal("config"),
    55  		"set_by_input":  cty.StringVal("input"),
    56  	})
    57  	if !got.RawEquals(want) {
    58  		t.Fatalf("incorrect merged config\ngot:  %#v\nwant: %#v", got, want)
    59  	}
    60  }