github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/builtin/provisioners/habitat/resource_provisioner_test.go (about) 1 package habitat 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/terraform/helper/schema" 7 "github.com/hashicorp/terraform/terraform" 8 ) 9 10 func TestResourceProvisioner_impl(t *testing.T) { 11 var _ terraform.ResourceProvisioner = Provisioner() 12 } 13 14 func TestProvisioner(t *testing.T) { 15 if err := Provisioner().(*schema.Provisioner).InternalValidate(); err != nil { 16 t.Fatalf("error: %s", err) 17 } 18 } 19 20 func TestResourceProvisioner_Validate_good(t *testing.T) { 21 c := testConfig(t, map[string]interface{}{ 22 "peers": []interface{}{"1.2.3.4"}, 23 "version": "0.32.0", 24 "service_type": "systemd", 25 "accept_license": false, 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 "url": "badurl", 41 }) 42 43 warn, errs := Provisioner().Validate(c) 44 if len(warn) > 0 { 45 t.Fatalf("Warnings: %v", warn) 46 } 47 // 3 errors, bad service_type, bad url, missing accept_license 48 if len(errs) != 3 { 49 t.Fatalf("Should have three errors, got %d", len(errs)) 50 } 51 } 52 53 func TestResourceProvisioner_Validate_bad_service_config(t *testing.T) { 54 c := testConfig(t, map[string]interface{}{ 55 "accept_license": true, 56 "service": []interface{}{ 57 map[string]interface{}{ 58 "name": "core/foo", 59 "strategy": "bar", 60 "topology": "baz", 61 "url": "badurl", 62 }, 63 }, 64 }) 65 66 warn, errs := Provisioner().Validate(c) 67 if len(warn) > 0 { 68 t.Fatalf("Warnings: %v", warn) 69 } 70 if len(errs) != 3 { 71 t.Fatalf("Should have three errors, got %d", len(errs)) 72 } 73 } 74 75 func TestResourceProvisioner_Validate_bad_service_definition(t *testing.T) { 76 c := testConfig(t, map[string]interface{}{ 77 "service": "core/vault", 78 }) 79 80 warn, errs := Provisioner().Validate(c) 81 if len(warn) > 0 { 82 t.Fatalf("Warnings: %v", warn) 83 } 84 if len(errs) != 3 { 85 t.Fatalf("Should have three errors, got %d", len(errs)) 86 } 87 } 88 89 func testConfig(t *testing.T, c map[string]interface{}) *terraform.ResourceConfig { 90 return terraform.NewResourceConfigRaw(c) 91 }