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