mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
updates to the context refresh tests
Add comparers for go-cmp to compare deep cty.Values. Fix a number of Context2Refresh fixtures.
This commit is contained in:
parent
57ca9e3c0a
commit
d50956bdfc
@ -8,68 +8,84 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/zclconf/go-cty/cty"
|
"github.com/zclconf/go-cty/cty"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/addrs"
|
"github.com/hashicorp/terraform/addrs"
|
||||||
|
"github.com/hashicorp/terraform/config/hcl2shim"
|
||||||
"github.com/hashicorp/terraform/configs/configschema"
|
"github.com/hashicorp/terraform/configs/configschema"
|
||||||
"github.com/hashicorp/terraform/providers"
|
"github.com/hashicorp/terraform/providers"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestContext2Refresh(t *testing.T) {
|
func TestContext2Refresh(t *testing.T) {
|
||||||
t.Fatalf("not yet updated for new provider interface")
|
p := testProvider("aws")
|
||||||
/*
|
m := testModule(t, "refresh-basic")
|
||||||
p := testProvider("aws")
|
|
||||||
m := testModule(t, "refresh-basic")
|
startingState := mustShimLegacyState(&State{
|
||||||
ctx := testContext2(t, &ContextOpts{
|
Modules: []*ModuleState{
|
||||||
Config: m,
|
&ModuleState{
|
||||||
ProviderResolver: providers.ResolverFixed(
|
Path: rootModulePath,
|
||||||
map[string]providers.Factory{
|
Resources: map[string]*ResourceState{
|
||||||
"aws": testProviderFuncFixed(p),
|
"aws_instance.web": &ResourceState{
|
||||||
},
|
Type: "aws_instance",
|
||||||
),
|
Primary: &InstanceState{
|
||||||
State: mustShimLegacyState(&State{
|
ID: "foo",
|
||||||
Modules: []*ModuleState{
|
Attributes: map[string]string{
|
||||||
&ModuleState{
|
"id": "foo",
|
||||||
Path: rootModulePath,
|
"foo": "bar",
|
||||||
Resources: map[string]*ResourceState{
|
|
||||||
"aws_instance.web": &ResourceState{
|
|
||||||
Type: "aws_instance",
|
|
||||||
Primary: &InstanceState{
|
|
||||||
ID: "foo",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}),
|
},
|
||||||
})
|
},
|
||||||
|
})
|
||||||
|
|
||||||
p.RefreshFn = nil
|
ctx := testContext2(t, &ContextOpts{
|
||||||
p.RefreshReturn = &InstanceState{
|
Config: m,
|
||||||
ID: "foo",
|
ProviderResolver: providers.ResolverFixed(
|
||||||
}
|
map[string]providers.Factory{
|
||||||
|
"aws": testProviderFuncFixed(p),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
State: startingState,
|
||||||
|
})
|
||||||
|
|
||||||
s, err := ctx.Refresh()
|
schema := p.GetSchemaReturn.ResourceTypes["aws_instance"]
|
||||||
mod := s.RootModule()
|
ty := schema.ImpliedType()
|
||||||
if err != nil {
|
readState, err := hcl2shim.HCL2ValueFromFlatmap(map[string]string{"id": "foo", "foo": "baz"}, ty)
|
||||||
t.Fatalf("err: %s", err)
|
if err != nil {
|
||||||
}
|
t.Fatal(err)
|
||||||
if !p.RefreshCalled {
|
}
|
||||||
t.Fatal("refresh should be called")
|
|
||||||
}
|
|
||||||
if p.RefreshState.ID != "foo" {
|
|
||||||
t.Fatalf("bad: %#v", p.RefreshState)
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(mod.Resources["aws_instance.web"].Primary, p.RefreshReturn) {
|
|
||||||
t.Fatalf("bad: %#v %#v", mod.Resources["aws_instance.web"], p.RefreshReturn)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, r := range mod.Resources {
|
p.ReadResourceFn = nil
|
||||||
if r.Type == "" {
|
p.ReadResourceResponse = providers.ReadResourceResponse{
|
||||||
t.Fatalf("no type: %#v", r)
|
NewState: readState,
|
||||||
}
|
}
|
||||||
}
|
|
||||||
*/
|
s, diags := ctx.Refresh()
|
||||||
|
if diags.HasErrors() {
|
||||||
|
t.Fatal(diags.Err())
|
||||||
|
}
|
||||||
|
|
||||||
|
if !p.ReadResourceCalled {
|
||||||
|
t.Fatal("ReadResource should be called")
|
||||||
|
}
|
||||||
|
|
||||||
|
mod := s.RootModule()
|
||||||
|
fromState, err := mod.Resources["aws_instance.web"].Instances[addrs.NoKey].Current.Decode(ty)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
newState, err := schema.CoerceValue(fromState.Value)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !cmp.Equal(readState, newState, valueComparer) {
|
||||||
|
t.Fatal(cmp.Diff(readState, newState, valueComparer, equateEmpty))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext2Refresh_dataComputedModuleVar(t *testing.T) {
|
func TestContext2Refresh_dataComputedModuleVar(t *testing.T) {
|
||||||
@ -126,8 +142,7 @@ func TestContext2Refresh_dataComputedModuleVar(t *testing.T) {
|
|||||||
|
|
||||||
checkStateString(t, s, `
|
checkStateString(t, s, `
|
||||||
<no state>
|
<no state>
|
||||||
module.child:
|
`)
|
||||||
<no state>`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext2Refresh_targeted(t *testing.T) {
|
func TestContext2Refresh_targeted(t *testing.T) {
|
||||||
@ -207,7 +222,7 @@ func TestContext2Refresh_targeted(t *testing.T) {
|
|||||||
t.Fatalf("refresh errors: %s", diags.Err())
|
t.Fatalf("refresh errors: %s", diags.Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := []string{"aws_vpc.metoo", "aws_instance.me"}
|
expected := []string{"vpc-abc123", "i-abc123"}
|
||||||
if !reflect.DeepEqual(refreshedResources, expected) {
|
if !reflect.DeepEqual(refreshedResources, expected) {
|
||||||
t.Fatalf("expected: %#v, got: %#v", expected, refreshedResources)
|
t.Fatalf("expected: %#v, got: %#v", expected, refreshedResources)
|
||||||
}
|
}
|
||||||
@ -294,10 +309,10 @@ func TestContext2Refresh_targetedCount(t *testing.T) {
|
|||||||
|
|
||||||
// Target didn't specify index, so we should get all our instances
|
// Target didn't specify index, so we should get all our instances
|
||||||
expected := []string{
|
expected := []string{
|
||||||
"aws_vpc.metoo",
|
"vpc-abc123",
|
||||||
"aws_instance.me.0",
|
"i-abc123",
|
||||||
"aws_instance.me.1",
|
"i-cde567",
|
||||||
"aws_instance.me.2",
|
"i-cde789",
|
||||||
}
|
}
|
||||||
sort.Strings(expected)
|
sort.Strings(expected)
|
||||||
sort.Strings(refreshedResources)
|
sort.Strings(refreshedResources)
|
||||||
@ -385,7 +400,7 @@ func TestContext2Refresh_targetedCountIndex(t *testing.T) {
|
|||||||
t.Fatalf("refresh errors: %s", diags.Err())
|
t.Fatalf("refresh errors: %s", diags.Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := []string{"aws_vpc.metoo", "aws_instance.me.0"}
|
expected := []string{"vpc-abc123", "i-abc123"}
|
||||||
if !reflect.DeepEqual(refreshedResources, expected) {
|
if !reflect.DeepEqual(refreshedResources, expected) {
|
||||||
t.Fatalf("wrong result\ngot: %#v\nwant: %#v", refreshedResources, expected)
|
t.Fatalf("wrong result\ngot: %#v\nwant: %#v", refreshedResources, expected)
|
||||||
}
|
}
|
||||||
@ -456,7 +471,9 @@ func TestContext2Refresh_delete(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
p.ReadResourceFn = nil
|
p.ReadResourceFn = nil
|
||||||
p.ReadResourceResponse = providers.ReadResourceResponse{}
|
p.ReadResourceResponse = providers.ReadResourceResponse{
|
||||||
|
NewState: cty.NullVal(p.GetSchemaReturn.ResourceTypes["aws_instance"].ImpliedType()),
|
||||||
|
}
|
||||||
|
|
||||||
s, diags := ctx.Refresh()
|
s, diags := ctx.Refresh()
|
||||||
if diags.HasErrors() {
|
if diags.HasErrors() {
|
||||||
|
@ -12,6 +12,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"github.com/google/go-cmp/cmp/cmpopts"
|
||||||
|
|
||||||
"github.com/hashicorp/go-version"
|
"github.com/hashicorp/go-version"
|
||||||
"github.com/hashicorp/hil"
|
"github.com/hashicorp/hil"
|
||||||
"github.com/zclconf/go-cty/cty"
|
"github.com/zclconf/go-cty/cty"
|
||||||
@ -31,6 +34,12 @@ import (
|
|||||||
tfversion "github.com/hashicorp/terraform/version"
|
tfversion "github.com/hashicorp/terraform/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
equateEmpty = cmpopts.EquateEmpty()
|
||||||
|
typeComparer = cmp.Comparer(cty.Type.Equals)
|
||||||
|
valueComparer = cmp.Comparer(cty.Value.RawEquals)
|
||||||
|
)
|
||||||
|
|
||||||
func TestNewContextRequiredVersion(t *testing.T) {
|
func TestNewContextRequiredVersion(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
Name string
|
Name string
|
||||||
|
Loading…
Reference in New Issue
Block a user