github.com/magodo/terraform@v0.11.12-beta1/builtin/provisioners/habitat/resource_provisioner_test.go (about) 1 package habitat 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/terraform/config" 7 "github.com/hashicorp/terraform/helper/schema" 8 "github.com/hashicorp/terraform/terraform" 9 ) 10 11 func TestResourceProvisioner_impl(t *testing.T) { 12 var _ terraform.ResourceProvisioner = Provisioner() 13 } 14 15 func TestProvisioner(t *testing.T) { 16 if err := Provisioner().(*schema.Provisioner).InternalValidate(); err != nil { 17 t.Fatalf("error: %s", err) 18 } 19 } 20 21 func TestResourceProvisioner_Validate_good(t *testing.T) { 22 c := testConfig(t, map[string]interface{}{ 23 "peer": "1.2.3.4", 24 "version": "0.32.0", 25 "service_type": "systemd", 26 }) 27 28 warn, errs := Provisioner().Validate(c) 29 if len(warn) > 0 { 30 t.Fatalf("Warnings: %v", warn) 31 } 32 if len(errs) > 0 { 33 t.Fatalf("Errors: %v", errs) 34 } 35 } 36 37 func TestResourceProvisioner_Validate_bad(t *testing.T) { 38 c := testConfig(t, map[string]interface{}{ 39 "service_type": "invalidtype", 40 }) 41 42 warn, errs := Provisioner().Validate(c) 43 if len(warn) > 0 { 44 t.Fatalf("Warnings: %v", warn) 45 } 46 if len(errs) != 1 { 47 t.Fatalf("Should have one error") 48 } 49 } 50 51 func TestResourceProvisioner_Validate_bad_service_config(t *testing.T) { 52 c := testConfig(t, map[string]interface{}{ 53 "service": []map[string]interface{}{ 54 map[string]interface{}{"name": "core/foo", "strategy": "bar", "topology": "baz", "url": "badurl"}, 55 }, 56 }) 57 58 warn, errs := Provisioner().Validate(c) 59 if len(warn) > 0 { 60 t.Fatalf("Warnings: %v", warn) 61 } 62 if len(errs) != 3 { 63 t.Fatalf("Should have three errors") 64 } 65 } 66 67 func testConfig(t *testing.T, c map[string]interface{}) *terraform.ResourceConfig { 68 r, err := config.NewRawConfig(c) 69 if err != nil { 70 t.Fatalf("config error: %s", err) 71 } 72 73 return terraform.NewResourceConfig(r) 74 }