terraform: EvalDeleteOutput and context test
This commit is contained in:
parent
2ca181d42d
commit
873f5a91bb
|
@ -4128,6 +4128,48 @@ func TestContext2Apply_nilDiff(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContext2Apply_outputOrphan(t *testing.T) {
|
||||||
|
m := testModule(t, "apply-output-orphan")
|
||||||
|
p := testProvider("aws")
|
||||||
|
p.ApplyFn = testApplyFn
|
||||||
|
p.DiffFn = testDiffFn
|
||||||
|
|
||||||
|
state := &State{
|
||||||
|
Modules: []*ModuleState{
|
||||||
|
&ModuleState{
|
||||||
|
Path: rootModulePath,
|
||||||
|
Outputs: map[string]string{
|
||||||
|
"foo": "bar",
|
||||||
|
"bar": "baz",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := testContext2(t, &ContextOpts{
|
||||||
|
Module: m,
|
||||||
|
Providers: map[string]ResourceProviderFactory{
|
||||||
|
"aws": testProviderFuncFixed(p),
|
||||||
|
},
|
||||||
|
State: state,
|
||||||
|
})
|
||||||
|
|
||||||
|
if _, err := ctx.Plan(); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
state, err := ctx.Apply()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := strings.TrimSpace(state.String())
|
||||||
|
expected := strings.TrimSpace(testTerraformApplyOutputOrphanStr)
|
||||||
|
if actual != expected {
|
||||||
|
t.Fatalf("bad: \n%s", actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestContext2Apply_Provisioner_compute(t *testing.T) {
|
func TestContext2Apply_Provisioner_compute(t *testing.T) {
|
||||||
m := testModule(t, "apply-provisioner-compute")
|
m := testModule(t, "apply-provisioner-compute")
|
||||||
p := testProvider("aws")
|
p := testProvider("aws")
|
||||||
|
|
|
@ -6,6 +6,34 @@ import (
|
||||||
"github.com/hashicorp/terraform/config"
|
"github.com/hashicorp/terraform/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// EvalDeleteOutput is an EvalNode implementation that deletes an output
|
||||||
|
// from the state.
|
||||||
|
type EvalDeleteOutput struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: test
|
||||||
|
func (n *EvalDeleteOutput) Eval(ctx EvalContext) (interface{}, error) {
|
||||||
|
state, lock := ctx.State()
|
||||||
|
if state == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a write lock so we can access this instance
|
||||||
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
|
|
||||||
|
// Look for the module state. If we don't have one, create it.
|
||||||
|
mod := state.ModuleByPath(ctx.Path())
|
||||||
|
if mod == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(mod.Outputs, n.Name)
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
// EvalWriteOutput is an EvalNode implementation that writes the output
|
// EvalWriteOutput is an EvalNode implementation that writes the output
|
||||||
// for the given name to the current state.
|
// for the given name to the current state.
|
||||||
type EvalWriteOutput struct {
|
type EvalWriteOutput struct {
|
||||||
|
|
|
@ -95,6 +95,9 @@ func (b *BuiltinGraphBuilder) Steps() []GraphTransformer {
|
||||||
Targeting: (len(b.Targets) > 0),
|
Targeting: (len(b.Targets) > 0),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Output-related transformations
|
||||||
|
&AddOutputOrphanTransformer{State: b.State},
|
||||||
|
|
||||||
// Provider-related transformations
|
// Provider-related transformations
|
||||||
&MissingProviderTransformer{Providers: b.Providers},
|
&MissingProviderTransformer{Providers: b.Providers},
|
||||||
&ProviderTransformer{},
|
&ProviderTransformer{},
|
||||||
|
|
|
@ -373,6 +373,13 @@ do_instance.foo:
|
||||||
type = do_instance
|
type = do_instance
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const testTerraformApplyOutputOrphanStr = `
|
||||||
|
<no state>
|
||||||
|
Outputs:
|
||||||
|
|
||||||
|
foo = bar
|
||||||
|
`
|
||||||
|
|
||||||
const testTerraformApplyProvisionerStr = `
|
const testTerraformApplyProvisionerStr = `
|
||||||
aws_instance.bar:
|
aws_instance.bar:
|
||||||
ID = foo
|
ID = foo
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
output "foo" { value = "bar" }
|
|
@ -57,3 +57,12 @@ type graphNodeOrphanOutput struct {
|
||||||
func (n *graphNodeOrphanOutput) Name() string {
|
func (n *graphNodeOrphanOutput) Name() string {
|
||||||
return fmt.Sprintf("output.%s (orphan)", n.OutputName)
|
return fmt.Sprintf("output.%s (orphan)", n.OutputName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *graphNodeOrphanOutput) EvalTree() EvalNode {
|
||||||
|
return &EvalOpFilter{
|
||||||
|
Ops: []walkOperation{walkApply, walkRefresh},
|
||||||
|
Node: &EvalDeleteOutput{
|
||||||
|
Name: n.OutputName,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue