2015-02-21 21:25:10 +01:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2016-11-20 18:37:51 +01:00
|
|
|
"fmt"
|
2015-02-21 21:25:10 +01:00
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2016-06-24 21:33:03 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-02-21 21:25:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCacheState(t *testing.T) {
|
|
|
|
cache := testLocalState(t)
|
|
|
|
durable := testLocalState(t)
|
|
|
|
defer os.Remove(cache.Path)
|
|
|
|
defer os.Remove(durable.Path)
|
|
|
|
|
|
|
|
TestState(t, &CacheState{
|
|
|
|
Cache: cache,
|
|
|
|
Durable: durable,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCacheState_persistDurable(t *testing.T) {
|
|
|
|
cache := testLocalState(t)
|
|
|
|
durable := testLocalState(t)
|
|
|
|
defer os.Remove(cache.Path)
|
|
|
|
defer os.Remove(durable.Path)
|
|
|
|
|
|
|
|
cs := &CacheState{
|
|
|
|
Cache: cache,
|
|
|
|
Durable: durable,
|
|
|
|
}
|
|
|
|
|
|
|
|
state := cache.State()
|
|
|
|
state.Modules = nil
|
|
|
|
if err := cs.WriteState(state); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.DeepEqual(cache.State(), durable.State()) {
|
|
|
|
t.Fatal("cache and durable should not be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cs.PersistState(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(cache.State(), durable.State()) {
|
|
|
|
t.Fatalf(
|
|
|
|
"cache and durable should be the same\n\n%#v\n\n%#v",
|
|
|
|
cache.State(), durable.State())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 21:33:03 +02:00
|
|
|
func TestCacheState_RefreshState(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
cacheModules []*terraform.ModuleState
|
|
|
|
expected CacheRefreshResult
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cacheModules: nil,
|
|
|
|
expected: CacheRefreshUpdateLocal,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cacheModules: []*terraform.ModuleState{},
|
|
|
|
expected: CacheRefreshUpdateLocal,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cacheModules: []*terraform.ModuleState{
|
|
|
|
&terraform.ModuleState{
|
2016-09-08 04:17:56 +02:00
|
|
|
Path: terraform.RootModulePath,
|
2016-06-24 21:33:03 +02:00
|
|
|
Resources: map[string]*terraform.ResourceState{
|
|
|
|
"foo.foo": &terraform.ResourceState{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: CacheRefreshLocalNewer,
|
|
|
|
},
|
|
|
|
} {
|
2016-11-20 18:37:51 +01:00
|
|
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
|
|
|
cache := testLocalState(t)
|
|
|
|
durable := testLocalState(t)
|
|
|
|
defer os.Remove(cache.Path)
|
|
|
|
defer os.Remove(durable.Path)
|
|
|
|
|
|
|
|
cs := &CacheState{
|
|
|
|
Cache: cache,
|
|
|
|
Durable: durable,
|
|
|
|
}
|
|
|
|
|
|
|
|
state := cache.State()
|
|
|
|
state.Modules = test.cacheModules
|
|
|
|
if err := cs.WriteState(state); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cs.RefreshState(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cs.RefreshResult() != test.expected {
|
|
|
|
t.Fatalf("bad %d: %v", i, cs.RefreshResult())
|
|
|
|
}
|
|
|
|
})
|
2016-06-24 21:33:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-21 21:25:10 +01:00
|
|
|
func TestCacheState_impl(t *testing.T) {
|
|
|
|
var _ StateReader = new(CacheState)
|
|
|
|
var _ StateWriter = new(CacheState)
|
|
|
|
var _ StatePersister = new(CacheState)
|
|
|
|
var _ StateRefresher = new(CacheState)
|
|
|
|
}
|