github.com/pulumi/terraform@v1.4.0/pkg/command/graph_test.go (about)

     1  package command
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/mitchellh/cli"
     9  	"github.com/zclconf/go-cty/cty"
    10  
    11  	"github.com/pulumi/terraform/pkg/addrs"
    12  	"github.com/pulumi/terraform/pkg/plans"
    13  	"github.com/pulumi/terraform/pkg/states"
    14  )
    15  
    16  func TestGraph(t *testing.T) {
    17  	td := t.TempDir()
    18  	testCopyDir(t, testFixturePath("graph"), td)
    19  	defer testChdir(t, td)()
    20  
    21  	ui := new(cli.MockUi)
    22  	c := &GraphCommand{
    23  		Meta: Meta{
    24  			testingOverrides: metaOverridesForProvider(applyFixtureProvider()),
    25  			Ui:               ui,
    26  		},
    27  	}
    28  
    29  	args := []string{}
    30  	if code := c.Run(args); code != 0 {
    31  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    32  	}
    33  
    34  	output := ui.OutputWriter.String()
    35  	if !strings.Contains(output, `provider[\"registry.terraform.io/hashicorp/test\"]`) {
    36  		t.Fatalf("doesn't look like digraph: %s", output)
    37  	}
    38  }
    39  
    40  func TestGraph_multipleArgs(t *testing.T) {
    41  	ui := new(cli.MockUi)
    42  	c := &GraphCommand{
    43  		Meta: Meta{
    44  			testingOverrides: metaOverridesForProvider(applyFixtureProvider()),
    45  			Ui:               ui,
    46  		},
    47  	}
    48  
    49  	args := []string{
    50  		"bad",
    51  		"bad",
    52  	}
    53  	if code := c.Run(args); code != 1 {
    54  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
    55  	}
    56  }
    57  
    58  func TestGraph_noArgs(t *testing.T) {
    59  	td := t.TempDir()
    60  	testCopyDir(t, testFixturePath("graph"), td)
    61  	defer testChdir(t, td)()
    62  
    63  	ui := new(cli.MockUi)
    64  	c := &GraphCommand{
    65  		Meta: Meta{
    66  			testingOverrides: metaOverridesForProvider(applyFixtureProvider()),
    67  			Ui:               ui,
    68  		},
    69  	}
    70  
    71  	args := []string{}
    72  	if code := c.Run(args); code != 0 {
    73  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    74  	}
    75  
    76  	output := ui.OutputWriter.String()
    77  	if !strings.Contains(output, `provider[\"registry.terraform.io/hashicorp/test\"]`) {
    78  		t.Fatalf("doesn't look like digraph: %s", output)
    79  	}
    80  }
    81  
    82  func TestGraph_noConfig(t *testing.T) {
    83  	td := t.TempDir()
    84  	os.MkdirAll(td, 0755)
    85  	defer testChdir(t, td)()
    86  
    87  	ui := new(cli.MockUi)
    88  	c := &GraphCommand{
    89  		Meta: Meta{
    90  			testingOverrides: metaOverridesForProvider(applyFixtureProvider()),
    91  			Ui:               ui,
    92  		},
    93  	}
    94  
    95  	// Running the graph command without a config should not panic,
    96  	// but this may be an error at some point in the future.
    97  	args := []string{"-type", "apply"}
    98  	if code := c.Run(args); code != 0 {
    99  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   100  	}
   101  }
   102  
   103  func TestGraph_plan(t *testing.T) {
   104  	testCwd(t)
   105  
   106  	plan := &plans.Plan{
   107  		Changes: plans.NewChanges(),
   108  	}
   109  	plan.Changes.Resources = append(plan.Changes.Resources, &plans.ResourceInstanceChangeSrc{
   110  		Addr: addrs.Resource{
   111  			Mode: addrs.ManagedResourceMode,
   112  			Type: "test_instance",
   113  			Name: "bar",
   114  		}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
   115  		ChangeSrc: plans.ChangeSrc{
   116  			Action: plans.Delete,
   117  			Before: plans.DynamicValue(`{}`),
   118  			After:  plans.DynamicValue(`null`),
   119  		},
   120  		ProviderAddr: addrs.AbsProviderConfig{
   121  			Provider: addrs.NewDefaultProvider("test"),
   122  			Module:   addrs.RootModule,
   123  		},
   124  	})
   125  	emptyConfig, err := plans.NewDynamicValue(cty.EmptyObjectVal, cty.EmptyObject)
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  	plan.Backend = plans.Backend{
   130  		// Doesn't actually matter since we aren't going to activate the backend
   131  		// for this command anyway, but we need something here for the plan
   132  		// file writer to succeed.
   133  		Type:   "placeholder",
   134  		Config: emptyConfig,
   135  	}
   136  	_, configSnap := testModuleWithSnapshot(t, "graph")
   137  
   138  	planPath := testPlanFile(t, configSnap, states.NewState(), plan)
   139  
   140  	ui := new(cli.MockUi)
   141  	c := &GraphCommand{
   142  		Meta: Meta{
   143  			testingOverrides: metaOverridesForProvider(applyFixtureProvider()),
   144  			Ui:               ui,
   145  		},
   146  	}
   147  
   148  	args := []string{
   149  		"-plan", planPath,
   150  	}
   151  	if code := c.Run(args); code != 0 {
   152  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   153  	}
   154  
   155  	output := ui.OutputWriter.String()
   156  	if !strings.Contains(output, `provider[\"registry.terraform.io/hashicorp/test\"]`) {
   157  		t.Fatalf("doesn't look like digraph: %s", output)
   158  	}
   159  }