github.com/richardmarshall/terraform@v0.9.5-0.20170429023105-15704cc6ee35/command/state_push_test.go (about)

     1  package command
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/copy"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/mitchellh/cli"
    11  )
    12  
    13  func TestStatePush_empty(t *testing.T) {
    14  	// Create a temporary working directory that is empty
    15  	td := tempDir(t)
    16  	copy.CopyDir(testFixturePath("state-push-good"), td)
    17  	defer os.RemoveAll(td)
    18  	defer testChdir(t, td)()
    19  
    20  	expected := testStateRead(t, "replace.tfstate")
    21  
    22  	p := testProvider()
    23  	ui := new(cli.MockUi)
    24  	c := &StatePushCommand{
    25  		Meta: Meta{
    26  			ContextOpts: testCtxConfig(p),
    27  			Ui:          ui,
    28  		},
    29  	}
    30  
    31  	args := []string{"replace.tfstate"}
    32  	if code := c.Run(args); code != 0 {
    33  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    34  	}
    35  
    36  	actual := testStateRead(t, "local-state.tfstate")
    37  	if !actual.Equal(expected) {
    38  		t.Fatalf("bad: %#v", actual)
    39  	}
    40  }
    41  
    42  func TestStatePush_replaceMatch(t *testing.T) {
    43  	// Create a temporary working directory that is empty
    44  	td := tempDir(t)
    45  	copy.CopyDir(testFixturePath("state-push-replace-match"), td)
    46  	defer os.RemoveAll(td)
    47  	defer testChdir(t, td)()
    48  
    49  	expected := testStateRead(t, "replace.tfstate")
    50  
    51  	p := testProvider()
    52  	ui := new(cli.MockUi)
    53  	c := &StatePushCommand{
    54  		Meta: Meta{
    55  			ContextOpts: testCtxConfig(p),
    56  			Ui:          ui,
    57  		},
    58  	}
    59  
    60  	args := []string{"replace.tfstate"}
    61  	if code := c.Run(args); code != 0 {
    62  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    63  	}
    64  
    65  	actual := testStateRead(t, "local-state.tfstate")
    66  	if !actual.Equal(expected) {
    67  		t.Fatalf("bad: %#v", actual)
    68  	}
    69  }
    70  
    71  func TestStatePush_replaceMatchStdin(t *testing.T) {
    72  	// Create a temporary working directory that is empty
    73  	td := tempDir(t)
    74  	copy.CopyDir(testFixturePath("state-push-replace-match"), td)
    75  	defer os.RemoveAll(td)
    76  	defer testChdir(t, td)()
    77  
    78  	expected := testStateRead(t, "replace.tfstate")
    79  
    80  	// Setup the replacement to come from stdin
    81  	var buf bytes.Buffer
    82  	if err := terraform.WriteState(expected, &buf); err != nil {
    83  		t.Fatalf("err: %s", err)
    84  	}
    85  	defer testStdinPipe(t, &buf)()
    86  
    87  	p := testProvider()
    88  	ui := new(cli.MockUi)
    89  	c := &StatePushCommand{
    90  		Meta: Meta{
    91  			ContextOpts: testCtxConfig(p),
    92  			Ui:          ui,
    93  		},
    94  	}
    95  
    96  	args := []string{"-"}
    97  	if code := c.Run(args); code != 0 {
    98  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    99  	}
   100  
   101  	actual := testStateRead(t, "local-state.tfstate")
   102  	if !actual.Equal(expected) {
   103  		t.Fatalf("bad: %#v", actual)
   104  	}
   105  }
   106  
   107  func TestStatePush_lineageMismatch(t *testing.T) {
   108  	// Create a temporary working directory that is empty
   109  	td := tempDir(t)
   110  	copy.CopyDir(testFixturePath("state-push-bad-lineage"), td)
   111  	defer os.RemoveAll(td)
   112  	defer testChdir(t, td)()
   113  
   114  	expected := testStateRead(t, "local-state.tfstate")
   115  
   116  	p := testProvider()
   117  	ui := new(cli.MockUi)
   118  	c := &StatePushCommand{
   119  		Meta: Meta{
   120  			ContextOpts: testCtxConfig(p),
   121  			Ui:          ui,
   122  		},
   123  	}
   124  
   125  	args := []string{"replace.tfstate"}
   126  	if code := c.Run(args); code != 1 {
   127  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   128  	}
   129  
   130  	actual := testStateRead(t, "local-state.tfstate")
   131  	if !actual.Equal(expected) {
   132  		t.Fatalf("bad: %#v", actual)
   133  	}
   134  }
   135  
   136  func TestStatePush_serialNewer(t *testing.T) {
   137  	// Create a temporary working directory that is empty
   138  	td := tempDir(t)
   139  	copy.CopyDir(testFixturePath("state-push-serial-newer"), td)
   140  	defer os.RemoveAll(td)
   141  	defer testChdir(t, td)()
   142  
   143  	expected := testStateRead(t, "local-state.tfstate")
   144  
   145  	p := testProvider()
   146  	ui := new(cli.MockUi)
   147  	c := &StatePushCommand{
   148  		Meta: Meta{
   149  			ContextOpts: testCtxConfig(p),
   150  			Ui:          ui,
   151  		},
   152  	}
   153  
   154  	args := []string{"replace.tfstate"}
   155  	if code := c.Run(args); code != 1 {
   156  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   157  	}
   158  
   159  	actual := testStateRead(t, "local-state.tfstate")
   160  	if !actual.Equal(expected) {
   161  		t.Fatalf("bad: %#v", actual)
   162  	}
   163  }
   164  
   165  func TestStatePush_serialOlder(t *testing.T) {
   166  	// Create a temporary working directory that is empty
   167  	td := tempDir(t)
   168  	copy.CopyDir(testFixturePath("state-push-serial-older"), td)
   169  	defer os.RemoveAll(td)
   170  	defer testChdir(t, td)()
   171  
   172  	expected := testStateRead(t, "replace.tfstate")
   173  
   174  	p := testProvider()
   175  	ui := new(cli.MockUi)
   176  	c := &StatePushCommand{
   177  		Meta: Meta{
   178  			ContextOpts: testCtxConfig(p),
   179  			Ui:          ui,
   180  		},
   181  	}
   182  
   183  	args := []string{"replace.tfstate"}
   184  	if code := c.Run(args); code != 0 {
   185  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   186  	}
   187  
   188  	actual := testStateRead(t, "local-state.tfstate")
   189  	if !actual.Equal(expected) {
   190  		t.Fatalf("bad: %#v", actual)
   191  	}
   192  }