Skip to content

Commit

Permalink
Add all test changes
Browse files Browse the repository at this point in the history
  • Loading branch information
simonxmh committed Dec 19, 2024
1 parent f65dc9c commit 91a6fb5
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 15 deletions.
53 changes: 51 additions & 2 deletions internal/provider/resource_tfe_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ func TestAccTFEProject_basic(t *testing.T) {
"tfe_project.foobar", "description", "project description"),
resource.TestCheckResourceAttr(
"tfe_project.foobar", "organization", fmt.Sprintf("tst-terraform-%d", rInt)),
resource.TestCheckResourceAttr(
"tfe_project.foobar", "auto_destroy_activity_duration", "3d"),
),
},
},
Expand Down Expand Up @@ -131,6 +129,43 @@ func TestAccTFEProject_import(t *testing.T) {
})
}

func TestAccTFEProject_withAutoDestroy(t *testing.T) {
project := &tfe.Project{}
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEProject_basicWithAutoDestroy(rInt, "3d"),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEProjectExists(
"tfe_project.foobar", project),
testAccCheckTFEProjectAttributes(project),
resource.TestCheckResourceAttr(
"tfe_project.foobar", "auto_destroy_activity_duration", "3d"),
),
},
{
Config: testAccTFEProject_basicWithAutoDestroy(rInt, "10m"),
ExpectError: regexp.MustCompile(`must be 1-4 digits followed by d or h`),
},
{
Config: testAccTFEProject_basic(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEProjectExists(
"tfe_project.foobar", project),
testAccCheckTFEProjectAttributes(project),
resource.TestCheckResourceAttr(
"tfe_project.foobar", "auto_destroy_activity_duration", ""),
),
},
},
})
}

func testAccTFEProject_update(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
Expand Down Expand Up @@ -184,6 +219,20 @@ resource "tfe_project" "foobar" {
}`, rInt)
}

func testAccTFEProject_basicWithAutoDestroy(rInt int, duration string) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "[email protected]"
}
resource "tfe_project" "foobar" {
organization = tfe_organization.foobar.name
name = "projecttest"
auto_destroy_activity_duration = "%s"
}`, rInt, duration)
}

func testAccCheckTFEProjectDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(ConfiguredClient)

Expand Down
14 changes: 7 additions & 7 deletions internal/provider/resource_tfe_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func resourceTFEWorkspaceCreate(d *schema.ResourceData, meta interface{}) error
}
}

if v, ok := d.GetOk("inherits_project_auto_destroy"); ok {
if v, ok := d.GetOkExists("inherits_project_auto_destroy"); ok {
options.InheritsProjectAutoDestroy = tfe.Bool(v.(bool))
}

Expand Down Expand Up @@ -702,6 +702,12 @@ func resourceTFEWorkspaceUpdate(d *schema.ResourceData, meta interface{}) error
}
}

if d.HasChange("inherits_project_auto_destroy") {
if v, ok := d.GetOkExists("inherits_project_auto_destroy"); ok {
options.InheritsProjectAutoDestroy = tfe.Bool(v.(bool))
}
}

if hasAutoDestroyAtChange(d) {
autoDestroyAt, err := expandAutoDestroyAt(d)
if err != nil {
Expand All @@ -719,12 +725,6 @@ func resourceTFEWorkspaceUpdate(d *schema.ResourceData, meta interface{}) error
}
}

if d.HasChange("inherits_project_auto_destroy") {
if v, ok := d.GetOkExists("inherits_project_auto_destroy"); ok {
options.InheritsProjectAutoDestroy = tfe.Bool(v.(bool))
}
}

if d.HasChange("execution_mode") {
if v, ok := d.GetOk("execution_mode"); ok {
options.ExecutionMode = tfe.String(v.(string))
Expand Down
212 changes: 206 additions & 6 deletions internal/provider/resource_tfe_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2815,6 +2815,126 @@ func TestAccTFEWorkspace_validationAutoDestroyDuration(t *testing.T) {
})
}

func TestAccTFEWorkspace_createWithAutoDestroyDurationInProject(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEWorkspace_basicWithAutoDestroyDurationInProject(rInt, "1d", "3d"),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists("tfe_workspace.foobar", &tfe.Workspace{}, testAccProvider),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "auto_destroy_activity_duration", "3d"),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "inherits_project_auto_destroy", "false"),
),
},
},
})
}

func TestAccTFEWorkspace_updateWithAutoDestroyDurationInProject(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEWorkspace_basicWithAutoDestroyDurationInProject(rInt, "1d", "3d"),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists("tfe_workspace.foobar", &tfe.Workspace{}, testAccProvider),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "auto_destroy_activity_duration", "3d"),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "inherits_project_auto_destroy", "false"),
),
},
{
Config: testAccTFEWorkspace_basicWithAutoDestroyDurationInProject(rInt, "2d", "5d"),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists("tfe_workspace.foobar", &tfe.Workspace{}, testAccProvider),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "auto_destroy_activity_duration", "5d"),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "inherits_project_auto_destroy", "false"),
),
},
{
Config: testAccTFEWorkspace_basicWithAutoDestroyDuration(rInt, "1d"),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists("tfe_workspace.foobar", &tfe.Workspace{}, testAccProvider),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "auto_destroy_activity_duration", "1d"),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "inherits_project_auto_destroy", "false"),
),
},
{
Config: testAccTFEWorkspace_basicInProject(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists("tfe_workspace.foobar", &tfe.Workspace{}, testAccProvider),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "auto_destroy_activity_duration", ""),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "inherits_project_auto_destroy", "true"),
),
},
},
})
}

func TestAccTFEWorkspace_createWithAutoDestroyAtInProject(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEWorkspace_basicWithAutoDestroyAtInProject(rInt, "1d"),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists("tfe_workspace.foobar", &tfe.Workspace{}, testAccProvider),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "auto_destroy_at", "2100-01-01T00:00:00Z"),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "inherits_project_auto_destroy", "false"),
),
},
},
})
}

func TestAccTFEWorkspace_updateWithAutoDestroyAtInProject(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEWorkspace_basicWithAutoDestroyAtInProject(rInt, "1d"),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists("tfe_workspace.foobar", &tfe.Workspace{}, testAccProvider),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "auto_destroy_at", "2100-01-01T00:00:00Z"),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "inherits_project_auto_destroy", "false"),
),
},
{
Config: testAccTFEWorkspace_basicWithAutoDestroyAt(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists("tfe_workspace.foobar", &tfe.Workspace{}, testAccProvider),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "auto_destroy_at", "2100-01-01T00:00:00Z"),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "inherits_project_auto_destroy", "false"),
),
},
{
Config: testAccTFEWorkspace_basicInProject(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists("tfe_workspace.foobar", &tfe.Workspace{}, testAccProvider),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "auto_destroy_at", ""),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "inherits_project_auto_destroy", "true"),
),
},
},
})
}

func TestAccTFEWorkspace_createWithSourceURL(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

Expand Down Expand Up @@ -3136,31 +3256,111 @@ resource "tfe_organization" "foobar" {
email = "[email protected]"
}
resource "tfe_project" "new_project" {
name = "testproject"
organization = tfe_organization.foobar.id
}
resource "tfe_workspace" "foobar" {
name = "workspace-test"
organization = tfe_organization.foobar.id
auto_apply = true
file_triggers_enabled = false
auto_destroy_at = "2100-01-01T00:00:00Z"
name = "workspace-test"
organization = tfe_organization.foobar.id
project_id = tfe_project.new_project.id
auto_apply = true
file_triggers_enabled = false
auto_destroy_at = "2100-01-01T00:00:00Z"
inherits_project_auto_destroy = false
}`, rInt)
}

func testAccTFEWorkspace_basicWithAutoDestroyAtInProject(rInt int, projectDuration string) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "[email protected]"
}
resource "tfe_project" "new_project" {
name = "testproject"
organization = tfe_organization.foobar.id
auto_destroy_activity_duration = "%s"
}
resource "tfe_workspace" "foobar" {
name = "workspace-test"
organization = tfe_organization.foobar.id
project_id = tfe_project.new_project.id
auto_apply = true
auto_destroy_at = "2100-01-01T00:00:00Z"
inherits_project_auto_destroy = false
}`, rInt, projectDuration)
}

func testAccTFEWorkspace_basicWithAutoDestroyDuration(rInt int, value string) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "[email protected]"
}
resource "tfe_project" "new_project" {
name = "testproject"
organization = tfe_organization.foobar.id
}
resource "tfe_workspace" "foobar" {
name = "workspace-test"
organization = tfe_organization.foobar.id
project_id = tfe_project.new_project.id
auto_apply = true
file_triggers_enabled = false
file_triggers_enabled = false
auto_destroy_activity_duration = "%s"
inherits_project_auto_destroy = false
}`, rInt, value)
}

func testAccTFEWorkspace_basicWithAutoDestroyDurationInProject(rInt int, projectDuration string, workspaceDuration string) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "[email protected]"
}
resource "tfe_project" "new_project" {
name = "testproject"
organization = tfe_organization.foobar.id
auto_destroy_activity_duration = "%s"
}
resource "tfe_workspace" "foobar" {
name = "workspace-test"
organization = tfe_organization.foobar.id
project_id = tfe_project.new_project.id
auto_apply = true
auto_destroy_activity_duration = "%s"
inherits_project_auto_destroy = false
}`, rInt, projectDuration, workspaceDuration)
}

func testAccTFEWorkspace_basicInProject(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "[email protected]"
}
resource "tfe_project" "new_project" {
name = "testproject"
organization = tfe_organization.foobar.id
}
resource "tfe_workspace" "foobar" {
name = "workspace-test"
project_id = tfe_project.new_project.id
organization = tfe_organization.foobar.id
auto_apply = true
}`, rInt)
}

func testAccTFEWorkspace_operationsTrue(organization string) string {
return fmt.Sprintf(`
resource "tfe_workspace" "foobar" {
Expand Down

0 comments on commit 91a6fb5

Please sign in to comment.