github.com/nevins-b/terraform@v0.3.8-0.20170215184714-bbae22007d5a/command/state_push_test.go (about)

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