github.com/ojiry/terraform@v0.8.2-0.20161218223921-e50cec712c4a/state/cache_test.go (about)

     1  package state
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestCacheState(t *testing.T) {
    13  	cache := testLocalState(t)
    14  	durable := testLocalState(t)
    15  	defer os.Remove(cache.Path)
    16  	defer os.Remove(durable.Path)
    17  
    18  	TestState(t, &CacheState{
    19  		Cache:   cache,
    20  		Durable: durable,
    21  	})
    22  }
    23  
    24  func TestCacheState_persistDurable(t *testing.T) {
    25  	cache := testLocalState(t)
    26  	durable := testLocalState(t)
    27  	defer os.Remove(cache.Path)
    28  	defer os.Remove(durable.Path)
    29  
    30  	cs := &CacheState{
    31  		Cache:   cache,
    32  		Durable: durable,
    33  	}
    34  
    35  	state := cache.State()
    36  	state.Modules = nil
    37  	if err := cs.WriteState(state); err != nil {
    38  		t.Fatalf("err: %s", err)
    39  	}
    40  
    41  	if reflect.DeepEqual(cache.State(), durable.State()) {
    42  		t.Fatal("cache and durable should not be the same")
    43  	}
    44  
    45  	if err := cs.PersistState(); err != nil {
    46  		t.Fatalf("err: %s", err)
    47  	}
    48  
    49  	if !reflect.DeepEqual(cache.State(), durable.State()) {
    50  		t.Fatalf(
    51  			"cache and durable should be the same\n\n%#v\n\n%#v",
    52  			cache.State(), durable.State())
    53  	}
    54  }
    55  
    56  func TestCacheState_RefreshState(t *testing.T) {
    57  	for i, test := range []struct {
    58  		cacheModules []*terraform.ModuleState
    59  		expected     CacheRefreshResult
    60  	}{
    61  		{
    62  			cacheModules: nil,
    63  			expected:     CacheRefreshUpdateLocal,
    64  		},
    65  		{
    66  			cacheModules: []*terraform.ModuleState{},
    67  			expected:     CacheRefreshUpdateLocal,
    68  		},
    69  		{
    70  			cacheModules: []*terraform.ModuleState{
    71  				&terraform.ModuleState{
    72  					Path: terraform.RootModulePath,
    73  					Resources: map[string]*terraform.ResourceState{
    74  						"foo.foo": &terraform.ResourceState{
    75  							Primary: &terraform.InstanceState{
    76  								ID: "ID",
    77  							},
    78  						},
    79  					},
    80  				},
    81  			},
    82  			expected: CacheRefreshLocalNewer,
    83  		},
    84  	} {
    85  		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
    86  			cache := testLocalState(t)
    87  			durable := testLocalState(t)
    88  			defer os.Remove(cache.Path)
    89  			defer os.Remove(durable.Path)
    90  
    91  			cs := &CacheState{
    92  				Cache:   cache,
    93  				Durable: durable,
    94  			}
    95  
    96  			state := cache.State()
    97  			state.Modules = test.cacheModules
    98  			if err := cs.WriteState(state); err != nil {
    99  				t.Fatalf("err: %s", err)
   100  			}
   101  
   102  			if err := cs.RefreshState(); err != nil {
   103  				t.Fatalf("err: %s", err)
   104  			}
   105  
   106  			if cs.RefreshResult() != test.expected {
   107  				t.Fatalf("bad %d: %v", i, cs.RefreshResult())
   108  			}
   109  		})
   110  	}
   111  }
   112  
   113  func TestCacheState_impl(t *testing.T) {
   114  	var _ StateReader = new(CacheState)
   115  	var _ StateWriter = new(CacheState)
   116  	var _ StatePersister = new(CacheState)
   117  	var _ StateRefresher = new(CacheState)
   118  }