From 53ff35b9ca3509b7107f313969c881834f0a34db Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 3 Dec 2018 12:47:17 -0500 Subject: [PATCH] StateFunc tests --- builtin/providers/test/provider.go | 1 + builtin/providers/test/resource_state_func.go | 74 +++++++++++++++++++ .../test/resource_state_func_test.go | 60 +++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 builtin/providers/test/resource_state_func.go create mode 100644 builtin/providers/test/resource_state_func_test.go diff --git a/builtin/providers/test/provider.go b/builtin/providers/test/provider.go index 59dd55085..155b39422 100644 --- a/builtin/providers/test/provider.go +++ b/builtin/providers/test/provider.go @@ -25,6 +25,7 @@ func Provider() terraform.ResourceProvider { "test_resource_force_new": testResourceForceNew(), "test_resource_nested": testResourceNested(), "test_resource_nested_set": testResourceNestedSet(), + "test_resource_state_func": testResourceStateFunc(), }, DataSourcesMap: map[string]*schema.Resource{ "test_data_source": testDataSource(), diff --git a/builtin/providers/test/resource_state_func.go b/builtin/providers/test/resource_state_func.go new file mode 100644 index 000000000..46c4b9b20 --- /dev/null +++ b/builtin/providers/test/resource_state_func.go @@ -0,0 +1,74 @@ +package test + +import ( + "crypto/sha1" + "encoding/hex" + "fmt" + "math/rand" + + "github.com/hashicorp/terraform/helper/schema" +) + +func testResourceStateFunc() *schema.Resource { + return &schema.Resource{ + Create: testResourceStateFuncCreate, + Read: testResourceStateFuncRead, + Update: testResourceStateFuncUpdate, + Delete: testResourceStateFuncDelete, + + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "optional": { + Type: schema.TypeString, + Optional: true, + }, + "state_func": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + StateFunc: stateFuncHash, + }, + "state_func_value": { + Type: schema.TypeString, + Optional: true, + }, + }, + } +} + +func stateFuncHash(v interface{}) string { + hash := sha1.Sum([]byte(v.(string))) + return hex.EncodeToString(hash[:]) +} + +func testResourceStateFuncCreate(d *schema.ResourceData, meta interface{}) error { + d.SetId(fmt.Sprintf("%x", rand.Int63())) + + // if we have a reference for the actual data in the state_func field, + // compare it + if data, ok := d.GetOk("state_func_value"); ok { + expected := data.(string) + got := d.Get("state_func").(string) + if expected != got { + return fmt.Errorf("expected state_func value:%q, got%q", expected, got) + } + } + + return testResourceStateFuncRead(d, meta) +} + +func testResourceStateFuncRead(d *schema.ResourceData, meta interface{}) error { + return nil +} + +func testResourceStateFuncUpdate(d *schema.ResourceData, meta interface{}) error { + return nil +} + +func testResourceStateFuncDelete(d *schema.ResourceData, meta interface{}) error { + d.SetId("") + return nil +} diff --git a/builtin/providers/test/resource_state_func_test.go b/builtin/providers/test/resource_state_func_test.go new file mode 100644 index 000000000..afdbd924e --- /dev/null +++ b/builtin/providers/test/resource_state_func_test.go @@ -0,0 +1,60 @@ +package test + +import ( + "strings" + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestResourceStateFunc_basic(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: testAccCheckResourceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_state_func" "foo" { +} + `), + Check: resource.TestCheckNoResourceAttr("test_resource_state_func.foo", "state_func"), + }, + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_state_func" "foo" { + state_func = "data" + state_func_value = "data" +} + `), + Check: resource.TestCheckResourceAttr("test_resource_state_func.foo", "state_func", stateFuncHash("data")), + }, + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_state_func" "foo" { +} + `), + Check: resource.TestCheckNoResourceAttr("test_resource_state_func.foo", "state_func"), + }, + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_state_func" "foo" { + optional = "added" + state_func = "data" + state_func_value = "data" +} + `), + Check: resource.TestCheckResourceAttr("test_resource_state_func.foo", "state_func", stateFuncHash("data")), + }, + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_state_func" "foo" { + optional = "added" + state_func = "changed" + state_func_value = "changed" +} + `), + Check: resource.TestCheckResourceAttr("test_resource_state_func.foo", "state_func", stateFuncHash("changed")), + }, + }, + }) +}