From 121c9c127ff465b9f229d45a29261cb6bc8a81ec Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 30 Oct 2018 12:58:29 -0400 Subject: [PATCH] add timeout tests to the test provider --- builtin/providers/test/provider.go | 1 + builtin/providers/test/resource_timeout.go | 120 ++++++++++++++++++ .../providers/test/resource_timeout_test.go | 91 +++++++++++++ 3 files changed, 212 insertions(+) create mode 100644 builtin/providers/test/resource_timeout.go create mode 100644 builtin/providers/test/resource_timeout_test.go diff --git a/builtin/providers/test/provider.go b/builtin/providers/test/provider.go index 6c71bfb18..1f005b24f 100644 --- a/builtin/providers/test/provider.go +++ b/builtin/providers/test/provider.go @@ -20,6 +20,7 @@ func Provider() terraform.ResourceProvider { "test_resource": testResource(), "test_resource_gh12183": testResourceGH12183(), "test_resource_with_custom_diff": testResourceCustomDiff(), + "test_resource_timeout": testResourceTimeout(), }, DataSourcesMap: map[string]*schema.Resource{ "test_data_source": testDataSource(), diff --git a/builtin/providers/test/resource_timeout.go b/builtin/providers/test/resource_timeout.go new file mode 100644 index 000000000..cf32bcaf5 --- /dev/null +++ b/builtin/providers/test/resource_timeout.go @@ -0,0 +1,120 @@ +package test + +import ( + "fmt" + "time" + + "github.com/hashicorp/terraform/helper/schema" +) + +func testResourceTimeout() *schema.Resource { + return &schema.Resource{ + Create: testResourceTimeoutCreate, + Read: testResourceTimeoutRead, + Update: testResourceTimeoutUpdate, + Delete: testResourceTimeoutDelete, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(time.Second), + Update: schema.DefaultTimeout(time.Second), + Delete: schema.DefaultTimeout(time.Second), + }, + + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "create_delay": { + Type: schema.TypeString, + Optional: true, + }, + "read_delay": { + Type: schema.TypeString, + Optional: true, + }, + "update_delay": { + Type: schema.TypeString, + Optional: true, + }, + "delete_delay": { + Type: schema.TypeString, + Optional: true, + }, + }, + } +} + +func testResourceTimeoutCreate(d *schema.ResourceData, meta interface{}) error { + delayString := d.Get("create_delay").(string) + var delay time.Duration + var err error + if delayString != "" { + delay, err = time.ParseDuration(delayString) + if err != nil { + return err + } + } + + if delay > d.Timeout(schema.TimeoutCreate) { + return fmt.Errorf("timeout while creating resource") + } + + d.SetId("testId") + + return testResourceRead(d, meta) +} + +func testResourceTimeoutRead(d *schema.ResourceData, meta interface{}) error { + delayString := d.Get("read_delay").(string) + var delay time.Duration + var err error + if delayString != "" { + delay, err = time.ParseDuration(delayString) + if err != nil { + return err + } + } + + if delay > d.Timeout(schema.TimeoutRead) { + return fmt.Errorf("timeout while reading resource") + } + + return nil +} + +func testResourceTimeoutUpdate(d *schema.ResourceData, meta interface{}) error { + delayString := d.Get("update_delay").(string) + var delay time.Duration + var err error + if delayString != "" { + delay, err = time.ParseDuration(delayString) + if err != nil { + return err + } + } + + if delay > d.Timeout(schema.TimeoutUpdate) { + return fmt.Errorf("timeout while updating resource") + } + return nil +} + +func testResourceTimeoutDelete(d *schema.ResourceData, meta interface{}) error { + delayString := d.Get("delete_delay").(string) + var delay time.Duration + var err error + if delayString != "" { + delay, err = time.ParseDuration(delayString) + if err != nil { + return err + } + } + + if delay > d.Timeout(schema.TimeoutDelete) { + return fmt.Errorf("timeout while deleting resource") + } + + d.SetId("") + return nil +} diff --git a/builtin/providers/test/resource_timeout_test.go b/builtin/providers/test/resource_timeout_test.go new file mode 100644 index 000000000..9edbba89a --- /dev/null +++ b/builtin/providers/test/resource_timeout_test.go @@ -0,0 +1,91 @@ +package test + +import ( + "regexp" + "strings" + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestResourceTimeout_create(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: testAccCheckResourceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_timeout" "foo" { + create_delay = "2s" + timeouts { + create = "1s" + } +} + `), + ExpectError: regexp.MustCompile("timeout while creating resource"), + }, + }, + }) +} +func TestResourceTimeout_update(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: testAccCheckResourceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_timeout" "foo" { + update_delay = "1s" + timeouts { + update = "1s" + } +} + `), + }, + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_timeout" "foo" { + update_delay = "2s" + timeouts { + update = "1s" + } +} + `), + ExpectError: regexp.MustCompile("timeout while updating resource"), + }, + }, + }) +} + +func TestResourceTimeout_read(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: testAccCheckResourceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_timeout" "foo" { +} + `), + }, + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_timeout" "foo" { + read_delay = "30m" +} + `), + ExpectError: regexp.MustCompile("timeout while reading resource"), + }, + // we need to remove the read_delay so that the resource can be + // destroyed in the final step, but expect an error here from the + // pre-existing delay. + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_timeout" "foo" { +} + `), + ExpectError: regexp.MustCompile("timeout while reading resource"), + }, + }, + }) +}