github.com/wikibal01/hashicorp-terraform@v0.11.12-beta1/helper/customdiff/validate_test.go (about) 1 package customdiff 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 ) 9 10 func TestValidateChange(t *testing.T) { 11 var called bool 12 var gotOld, gotNew string 13 14 provider := testProvider( 15 map[string]*schema.Schema{ 16 "foo": { 17 Type: schema.TypeString, 18 Optional: true, 19 }, 20 }, 21 ValidateChange("foo", func(old, new, meta interface{}) error { 22 called = true 23 gotOld = old.(string) 24 gotNew = new.(string) 25 return errors.New("bad") 26 }), 27 ) 28 29 _, err := testDiff( 30 provider, 31 map[string]string{ 32 "foo": "bar", 33 }, 34 map[string]string{ 35 "foo": "baz", 36 }, 37 ) 38 39 if err == nil { 40 t.Fatalf("Diff succeeded; want error") 41 } 42 if got, want := err.Error(), "bad"; got != want { 43 t.Fatalf("wrong error message %q; want %q", got, want) 44 } 45 46 if !called { 47 t.Fatal("ValidateChange callback was not called") 48 } 49 if got, want := gotOld, "bar"; got != want { 50 t.Errorf("wrong old value %q; want %q", got, want) 51 } 52 if got, want := gotNew, "baz"; got != want { 53 t.Errorf("wrong new value %q; want %q", got, want) 54 } 55 } 56 57 func TestValidateValue(t *testing.T) { 58 var called bool 59 var gotValue string 60 61 provider := testProvider( 62 map[string]*schema.Schema{ 63 "foo": { 64 Type: schema.TypeString, 65 Optional: true, 66 }, 67 }, 68 ValidateValue("foo", func(value, meta interface{}) error { 69 called = true 70 gotValue = value.(string) 71 return errors.New("bad") 72 }), 73 ) 74 75 _, err := testDiff( 76 provider, 77 map[string]string{ 78 "foo": "bar", 79 }, 80 map[string]string{ 81 "foo": "baz", 82 }, 83 ) 84 85 if err == nil { 86 t.Fatalf("Diff succeeded; want error") 87 } 88 if got, want := err.Error(), "bad"; got != want { 89 t.Fatalf("wrong error message %q; want %q", got, want) 90 } 91 92 if !called { 93 t.Fatal("ValidateValue callback was not called") 94 } 95 if got, want := gotValue, "baz"; got != want { 96 t.Errorf("wrong value %q; want %q", got, want) 97 } 98 }