github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/views/plan_test.go (about)

     1  package views
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform/internal/addrs"
     7  	"github.com/hashicorp/terraform/internal/command/arguments"
     8  	"github.com/hashicorp/terraform/internal/configs/configschema"
     9  	"github.com/hashicorp/terraform/internal/plans"
    10  	"github.com/hashicorp/terraform/internal/providers"
    11  	"github.com/hashicorp/terraform/internal/terminal"
    12  	"github.com/hashicorp/terraform/internal/terraform"
    13  	"github.com/zclconf/go-cty/cty"
    14  )
    15  
    16  // Ensure that the correct view type and in-automation settings propagate to the
    17  // Operation view.
    18  func TestPlanHuman_operation(t *testing.T) {
    19  	streams, done := terminal.StreamsForTesting(t)
    20  	defer done(t)
    21  	v := NewPlan(arguments.ViewHuman, NewView(streams).SetRunningInAutomation(true)).Operation()
    22  	if hv, ok := v.(*OperationHuman); !ok {
    23  		t.Fatalf("unexpected return type %t", v)
    24  	} else if hv.inAutomation != true {
    25  		t.Fatalf("unexpected inAutomation value on Operation view")
    26  	}
    27  }
    28  
    29  // Verify that Hooks includes a UI hook
    30  func TestPlanHuman_hooks(t *testing.T) {
    31  	streams, done := terminal.StreamsForTesting(t)
    32  	defer done(t)
    33  	v := NewPlan(arguments.ViewHuman, NewView(streams).SetRunningInAutomation((true)))
    34  	hooks := v.Hooks()
    35  
    36  	var uiHook *UiHook
    37  	for _, hook := range hooks {
    38  		if ch, ok := hook.(*UiHook); ok {
    39  			uiHook = ch
    40  		}
    41  	}
    42  	if uiHook == nil {
    43  		t.Fatalf("expected Hooks to include a UiHook: %#v", hooks)
    44  	}
    45  }
    46  
    47  // Helper functions to build a trivial test plan, to exercise the plan
    48  // renderer.
    49  func testPlan(t *testing.T) *plans.Plan {
    50  	t.Helper()
    51  
    52  	plannedVal := cty.ObjectVal(map[string]cty.Value{
    53  		"id":  cty.UnknownVal(cty.String),
    54  		"foo": cty.StringVal("bar"),
    55  	})
    56  	priorValRaw, err := plans.NewDynamicValue(cty.NullVal(plannedVal.Type()), plannedVal.Type())
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	plannedValRaw, err := plans.NewDynamicValue(plannedVal, plannedVal.Type())
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  
    65  	changes := plans.NewChanges()
    66  	addr := addrs.Resource{
    67  		Mode: addrs.ManagedResourceMode,
    68  		Type: "test_resource",
    69  		Name: "foo",
    70  	}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
    71  
    72  	changes.SyncWrapper().AppendResourceInstanceChange(&plans.ResourceInstanceChangeSrc{
    73  		Addr:        addr,
    74  		PrevRunAddr: addr,
    75  		ProviderAddr: addrs.AbsProviderConfig{
    76  			Provider: addrs.NewDefaultProvider("test"),
    77  			Module:   addrs.RootModule,
    78  		},
    79  		ChangeSrc: plans.ChangeSrc{
    80  			Action: plans.Create,
    81  			Before: priorValRaw,
    82  			After:  plannedValRaw,
    83  		},
    84  	})
    85  
    86  	return &plans.Plan{
    87  		Changes: changes,
    88  	}
    89  }
    90  
    91  func testPlanWithDatasource(t *testing.T) *plans.Plan {
    92  	plan := testPlan(t)
    93  
    94  	addr := addrs.Resource{
    95  		Mode: addrs.DataResourceMode,
    96  		Type: "test_data_source",
    97  		Name: "bar",
    98  	}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
    99  
   100  	dataVal := cty.ObjectVal(map[string]cty.Value{
   101  		"id":  cty.StringVal("C6743020-40BD-4591-81E6-CD08494341D3"),
   102  		"bar": cty.StringVal("foo"),
   103  	})
   104  	priorValRaw, err := plans.NewDynamicValue(cty.NullVal(dataVal.Type()), dataVal.Type())
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	plannedValRaw, err := plans.NewDynamicValue(dataVal, dataVal.Type())
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	plan.Changes.SyncWrapper().AppendResourceInstanceChange(&plans.ResourceInstanceChangeSrc{
   114  		Addr:        addr,
   115  		PrevRunAddr: addr,
   116  		ProviderAddr: addrs.AbsProviderConfig{
   117  			Provider: addrs.NewDefaultProvider("test"),
   118  			Module:   addrs.RootModule,
   119  		},
   120  		ChangeSrc: plans.ChangeSrc{
   121  			Action: plans.Read,
   122  			Before: priorValRaw,
   123  			After:  plannedValRaw,
   124  		},
   125  	})
   126  
   127  	return plan
   128  }
   129  
   130  func testSchemas() *terraform.Schemas {
   131  	provider := testProvider()
   132  	return &terraform.Schemas{
   133  		Providers: map[addrs.Provider]*terraform.ProviderSchema{
   134  			addrs.NewDefaultProvider("test"): provider.ProviderSchema(),
   135  		},
   136  	}
   137  }
   138  
   139  func testProvider() *terraform.MockProvider {
   140  	p := new(terraform.MockProvider)
   141  	p.ReadResourceFn = func(req providers.ReadResourceRequest) providers.ReadResourceResponse {
   142  		return providers.ReadResourceResponse{NewState: req.PriorState}
   143  	}
   144  
   145  	p.GetProviderSchemaResponse = testProviderSchema()
   146  
   147  	return p
   148  }
   149  
   150  func testProviderSchema() *providers.GetProviderSchemaResponse {
   151  	return &providers.GetProviderSchemaResponse{
   152  		Provider: providers.Schema{
   153  			Block: &configschema.Block{},
   154  		},
   155  		ResourceTypes: map[string]providers.Schema{
   156  			"test_resource": {
   157  				Block: &configschema.Block{
   158  					Attributes: map[string]*configschema.Attribute{
   159  						"id":  {Type: cty.String, Computed: true},
   160  						"foo": {Type: cty.String, Optional: true},
   161  					},
   162  				},
   163  			},
   164  		},
   165  		DataSources: map[string]providers.Schema{
   166  			"test_data_source": {
   167  				Block: &configschema.Block{
   168  					Attributes: map[string]*configschema.Attribute{
   169  						"id":  {Type: cty.String, Required: true},
   170  						"bar": {Type: cty.String, Optional: true},
   171  					},
   172  				},
   173  			},
   174  		},
   175  	}
   176  }