Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 9bd961c

Browse files
committed
terraform: test that depends_on is used for destroy ordering
1 parent 4b72349 commit 9bd961c

5 files changed

Lines changed: 186 additions & 0 deletions

File tree

terraform/context_apply_test.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,156 @@ func TestContext2Apply_destroyComputed(t *testing.T) {
508508
}
509509
}
510510

511+
// Test that the destroy operation uses depends_on as a source of ordering.
512+
func TestContext2Apply_destroyDependsOn(t *testing.T) {
513+
// It is possible for this to be racy, so we loop a number of times
514+
// just to check.
515+
for i := 0; i < 10; i++ {
516+
testContext2Apply_destroyDependsOn(t)
517+
}
518+
}
519+
520+
func testContext2Apply_destroyDependsOn(t *testing.T) {
521+
m := testModule(t, "apply-destroy-depends-on")
522+
p := testProvider("aws")
523+
p.ApplyFn = testApplyFn
524+
p.DiffFn = testDiffFn
525+
state := &State{
526+
Modules: []*ModuleState{
527+
&ModuleState{
528+
Path: rootModulePath,
529+
Resources: map[string]*ResourceState{
530+
"aws_instance.foo": &ResourceState{
531+
Type: "aws_instance",
532+
Primary: &InstanceState{
533+
ID: "foo",
534+
Attributes: map[string]string{},
535+
},
536+
},
537+
538+
"aws_instance.bar": &ResourceState{
539+
Type: "aws_instance",
540+
Primary: &InstanceState{
541+
ID: "bar",
542+
Attributes: map[string]string{},
543+
},
544+
},
545+
},
546+
},
547+
},
548+
}
549+
550+
// Record the order we see Apply
551+
var actual []string
552+
var actualLock sync.Mutex
553+
p.ApplyFn = func(
554+
info *InstanceInfo, _ *InstanceState, _ *InstanceDiff) (*InstanceState, error) {
555+
actualLock.Lock()
556+
defer actualLock.Unlock()
557+
actual = append(actual, info.Id)
558+
return nil, nil
559+
}
560+
561+
ctx := testContext2(t, &ContextOpts{
562+
Module: m,
563+
Providers: map[string]ResourceProviderFactory{
564+
"aws": testProviderFuncFixed(p),
565+
},
566+
State: state,
567+
Destroy: true,
568+
Parallelism: 1, // To check ordering
569+
})
570+
571+
if _, err := ctx.Plan(); err != nil {
572+
t.Fatalf("err: %s", err)
573+
}
574+
575+
if _, err := ctx.Apply(); err != nil {
576+
t.Fatalf("err: %s", err)
577+
}
578+
579+
expected := []string{"aws_instance.foo", "aws_instance.bar"}
580+
if !reflect.DeepEqual(actual, expected) {
581+
t.Fatalf("bad: %#v", actual)
582+
}
583+
}
584+
585+
// Test that destroy ordering is correct with dependencies only
586+
// in the state.
587+
func TestContext2Apply_destroyDependsOnStateOnly(t *testing.T) {
588+
// It is possible for this to be racy, so we loop a number of times
589+
// just to check.
590+
for i := 0; i < 10; i++ {
591+
testContext2Apply_destroyDependsOnStateOnly(t)
592+
}
593+
}
594+
595+
func testContext2Apply_destroyDependsOnStateOnly(t *testing.T) {
596+
m := testModule(t, "empty")
597+
p := testProvider("aws")
598+
p.ApplyFn = testApplyFn
599+
p.DiffFn = testDiffFn
600+
state := &State{
601+
Modules: []*ModuleState{
602+
&ModuleState{
603+
Path: rootModulePath,
604+
Resources: map[string]*ResourceState{
605+
"aws_instance.foo": &ResourceState{
606+
Type: "aws_instance",
607+
Primary: &InstanceState{
608+
ID: "foo",
609+
Attributes: map[string]string{},
610+
},
611+
},
612+
613+
"aws_instance.bar": &ResourceState{
614+
Type: "aws_instance",
615+
Primary: &InstanceState{
616+
ID: "bar",
617+
Attributes: map[string]string{},
618+
},
619+
Dependencies: []string{"aws_instance.foo"},
620+
},
621+
},
622+
},
623+
},
624+
}
625+
626+
// Record the order we see Apply
627+
var actual []string
628+
var actualLock sync.Mutex
629+
p.ApplyFn = func(
630+
info *InstanceInfo, _ *InstanceState, _ *InstanceDiff) (*InstanceState, error) {
631+
actualLock.Lock()
632+
defer actualLock.Unlock()
633+
actual = append(actual, info.Id)
634+
return nil, nil
635+
}
636+
637+
ctx := testContext2(t, &ContextOpts{
638+
Module: m,
639+
Providers: map[string]ResourceProviderFactory{
640+
"aws": testProviderFuncFixed(p),
641+
},
642+
State: state,
643+
Destroy: true,
644+
Parallelism: 1, // To check ordering
645+
})
646+
647+
if _, err := ctx.Plan(); err != nil {
648+
t.Fatalf("err: %s", err)
649+
}
650+
651+
if _, err := ctx.Apply(); err != nil {
652+
t.Fatalf("err: %s", err)
653+
}
654+
655+
expected := []string{"aws_instance.bar", "aws_instance.foo"}
656+
if !reflect.DeepEqual(actual, expected) {
657+
t.Fatalf("bad: %#v", actual)
658+
}
659+
}
660+
511661
func TestContext2Apply_dataBasic(t *testing.T) {
512662
m := testModule(t, "apply-data-basic")
513663
p := testProvider("null")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resource "aws_instance" "foo" {
2+
depends_on = ["aws_instance.bar"]
3+
}
4+
5+
resource "aws_instance" "bar" {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Empty, use this for any test that requires a module but no config.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resource "aws_instance" "foo" {}
2+
3+
resource "aws_instance" "bar" {
4+
depends_on = ["aws_instance.foo"]
5+
}

terraform/transform_destroy_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,31 @@ func TestDestroyTransformer(t *testing.T) {
3030
}
3131
}
3232

33+
func TestDestroyTransformer_dependsOn(t *testing.T) {
34+
mod := testModule(t, "transform-destroy-depends-on")
35+
36+
g := Graph{Path: RootModulePath}
37+
{
38+
tf := &ConfigTransformer{Module: mod}
39+
if err := tf.Transform(&g); err != nil {
40+
t.Fatalf("err: %s", err)
41+
}
42+
}
43+
44+
{
45+
tf := &DestroyTransformer{}
46+
if err := tf.Transform(&g); err != nil {
47+
t.Fatalf("err: %s", err)
48+
}
49+
}
50+
51+
actual := strings.TrimSpace(g.String())
52+
expected := strings.TrimSpace(testTransformDestroyBasicStr)
53+
if actual != expected {
54+
t.Fatalf("bad:\n\n%s", actual)
55+
}
56+
}
57+
3358
func TestCreateBeforeDestroyTransformer(t *testing.T) {
3459
mod := testModule(t, "transform-create-before-destroy-basic")
3560

0 commit comments

Comments
 (0)