@@ -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+
511661func TestContext2Apply_dataBasic (t * testing.T ) {
512662 m := testModule (t , "apply-data-basic" )
513663 p := testProvider ("null" )
0 commit comments