@@ -512,6 +512,140 @@ func TestUiHookPreImportState(t *testing.T) {
512512 }
513513}
514514
515+ func TestUiHookPreApplyImport (t * testing.T ) {
516+ testCases := map [string ]struct {
517+ importingSrc plans.ImportingSrc
518+ want string
519+ }{
520+ "id" : {
521+ importingSrc : plans.ImportingSrc {
522+ ID : "test" ,
523+ },
524+ want : "test_instance.foo: Importing... [id=test]\n " ,
525+ },
526+ "identity" : {
527+ importingSrc : plans.ImportingSrc {
528+ Identity : mustNewDynamicValue (
529+ cty .ObjectVal (map [string ]cty.Value {
530+ "id" : cty .StringVal ("test" ),
531+ }),
532+ cty .Object (map [string ]cty.Type {
533+ "id" : cty .String ,
534+ }),
535+ ),
536+ },
537+ want : "test_instance.foo: Importing... [identity=id=test]\n " ,
538+ },
539+ "identity type error" : {
540+ importingSrc : plans.ImportingSrc {
541+ Identity : mustNewDynamicValue (
542+ cty .ObjectVal (map [string ]cty.Value {
543+ "id" : cty .StringVal ("test" ),
544+ }),
545+ cty .DynamicPseudoType ,
546+ ),
547+ },
548+ want : "test_instance.foo: Importing... [identity=(type error)]\n " ,
549+ },
550+ }
551+
552+ addr := addrs.Resource {
553+ Mode : addrs .ManagedResourceMode ,
554+ Type : "test_instance" ,
555+ Name : "foo" ,
556+ }.Instance (addrs .NoKey ).Absolute (addrs .RootModuleInstance )
557+
558+ for name , tc := range testCases {
559+ t .Run (name , func (t * testing.T ) {
560+ streams , done := terminal .StreamsForTesting (t )
561+ view := NewView (streams )
562+ h := NewUiHook (view )
563+
564+ action , err := h .PreApplyImport (testUiHookResourceID (addr ), tc .importingSrc )
565+
566+ if err != nil {
567+ t .Fatal (err )
568+ }
569+ if action != terraform .HookActionContinue {
570+ t .Fatalf ("Expected hook to continue, given: %#v" , action )
571+ }
572+ result := done (t )
573+ got := result .Stdout ()
574+
575+ if got != tc .want {
576+ t .Fatalf ("unexpected output\n got: %q\n want: %q" , got , tc .want )
577+ }
578+ })
579+ }
580+ }
581+
582+ func TestUiHookPostApplyImport (t * testing.T ) {
583+ testCases := map [string ]struct {
584+ importingSrc plans.ImportingSrc
585+ want string
586+ }{
587+ "id" : {
588+ importingSrc : plans.ImportingSrc {
589+ ID : "test" ,
590+ },
591+ want : "test_instance.foo: Import complete [id=test]\n " ,
592+ },
593+ "identity" : {
594+ importingSrc : plans.ImportingSrc {
595+ Identity : mustNewDynamicValue (
596+ cty .ObjectVal (map [string ]cty.Value {
597+ "id" : cty .StringVal ("test" ),
598+ }),
599+ cty .Object (map [string ]cty.Type {
600+ "id" : cty .String ,
601+ }),
602+ ),
603+ },
604+ want : "test_instance.foo: Import complete [identity=id=test]\n " ,
605+ },
606+ "identity type error" : {
607+ importingSrc : plans.ImportingSrc {
608+ Identity : mustNewDynamicValue (
609+ cty .ObjectVal (map [string ]cty.Value {
610+ "id" : cty .StringVal ("test" ),
611+ }),
612+ cty .DynamicPseudoType ,
613+ ),
614+ },
615+ want : "test_instance.foo: Import complete [identity=(type error)]\n " ,
616+ },
617+ }
618+
619+ addr := addrs.Resource {
620+ Mode : addrs .ManagedResourceMode ,
621+ Type : "test_instance" ,
622+ Name : "foo" ,
623+ }.Instance (addrs .NoKey ).Absolute (addrs .RootModuleInstance )
624+
625+ for name , tc := range testCases {
626+ t .Run (name , func (t * testing.T ) {
627+ streams , done := terminal .StreamsForTesting (t )
628+ view := NewView (streams )
629+ h := NewUiHook (view )
630+
631+ action , err := h .PostApplyImport (testUiHookResourceID (addr ), tc .importingSrc )
632+
633+ if err != nil {
634+ t .Fatal (err )
635+ }
636+ if action != terraform .HookActionContinue {
637+ t .Fatalf ("Expected hook to continue, given: %#v" , action )
638+ }
639+ result := done (t )
640+ got := result .Stdout ()
641+
642+ if got != tc .want {
643+ t .Fatalf ("unexpected output\n got: %q\n want: %q" , got , tc .want )
644+ }
645+ })
646+ }
647+ }
648+
515649// Test the PostImportState UI hook. Again, this hook behaviour seems odd to
516650// me (see below), so please don't consider these tests as justification for
517651// keeping this behaviour.
@@ -792,3 +926,11 @@ func TestTruncateId(t *testing.T) {
792926 })
793927 }
794928}
929+
930+ func mustNewDynamicValue (val cty.Value , ty cty.Type ) plans.DynamicValue {
931+ ret , err := plans .NewDynamicValue (val , ty )
932+ if err != nil {
933+ panic (err )
934+ }
935+ return ret
936+ }
0 commit comments