forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Overwrite Support For
github_repository_file
(integrations#459)
- Loading branch information
Jeremy Udit
authored
Oct 3, 2020
1 parent
e58676a
commit e9f6037
Showing
3 changed files
with
150 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,47 @@ | ||
package github | ||
|
||
import ( | ||
"strings" | ||
|
||
"fmt" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform/helper/acctest" | ||
) | ||
|
||
func TestAccGithubRepositoryFile(t *testing.T) { | ||
|
||
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) | ||
|
||
t.Run("creates and updates file content without error", func(t *testing.T) { | ||
|
||
fileContent := "file_content_value" | ||
updatedFileContent := "updated_file_content_value" | ||
t.Run("creates and manages files", func(t *testing.T) { | ||
|
||
config := fmt.Sprintf(` | ||
resource "github_repository" "test" { | ||
name = "tf-acc-test-%s" | ||
auto_init = true | ||
} | ||
resource "github_repository_file" "test" { | ||
repository = github_repository.test.id | ||
file = "test" | ||
content = "%s" | ||
} | ||
`, randomID, fileContent) | ||
|
||
checks := map[string]resource.TestCheckFunc{ | ||
"before": resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"github_repository_file.test", "sha", | ||
"deee258b7c807901aad79d01da020d993739160a", | ||
), | ||
), | ||
"after": resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"github_repository_file.test", "sha", | ||
"ec9aad0ba478cdd7349faabbeac2a64e5ce72ddb", | ||
), | ||
), | ||
} | ||
|
||
testCase := func(t *testing.T, mode string) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { skipUnlessMode(t, mode) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: checks["before"], | ||
}, | ||
{ | ||
Config: strings.Replace(config, | ||
fileContent, | ||
updatedFileContent, 1), | ||
Check: checks["after"], | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
t.Run("with an anonymous account", func(t *testing.T) { | ||
t.Skip("anonymous account not supported for this operation") | ||
}) | ||
|
||
t.Run("with an individual account", func(t *testing.T) { | ||
testCase(t, individual) | ||
}) | ||
t.Run("with an organization account", func(t *testing.T) { | ||
testCase(t, organization) | ||
}) | ||
|
||
}) | ||
|
||
t.Run("manages file content for a specified branch", func(t *testing.T) { | ||
|
||
config := fmt.Sprintf(` | ||
resource "github_repository" "test" { | ||
name = "tf-acc-test-%s" | ||
name = "tf-acc-test-%s" | ||
auto_init = true | ||
} | ||
resource "github_branch" "test" { | ||
repository = github_repository.test.id | ||
branch = "tf-acc-test-%[1]s" | ||
} | ||
resource "github_repository_file" "test" { | ||
repository = github_repository.test.id | ||
branch = github_branch.test.branch | ||
file = "test" | ||
content = "test" | ||
repository = github_repository.test.name | ||
branch = "main" | ||
file = "test" | ||
content = "bar" | ||
commit_message = "Managed by Terraform" | ||
commit_author = "Terraform User" | ||
commit_email = "[email protected]" | ||
} | ||
`, randomID) | ||
|
||
check := resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"github_repository_file.test", "content", | ||
"bar", | ||
), | ||
resource.TestCheckResourceAttr( | ||
"github_repository_file.test", "sha", | ||
"30d74d258442c7c65512eafab474568dd706c430", | ||
"ba0e162e1c47469e3fe4b393a8bf8c569f302116", | ||
), | ||
) | ||
|
||
|
@@ -135,36 +72,32 @@ func TestAccGithubRepositoryFile(t *testing.T) { | |
|
||
}) | ||
|
||
t.Run("commits with custom message, author and e-mail", func(t *testing.T) { | ||
t.Run("can be configured to overwrite files on create", func(t *testing.T) { | ||
|
||
config := fmt.Sprintf(` | ||
resource "github_repository" "test" { | ||
name = "tf-acc-test-%s" | ||
auto_init = true | ||
name = "tf-acc-test-%s" | ||
auto_init = true | ||
} | ||
resource "github_repository_file" "test" { | ||
repository = github_repository.test.id | ||
file = "test" | ||
content = "test" | ||
commit_message = "Managed by Terraform" | ||
commit_author = "Terraform User" | ||
commit_email = "[email protected]" | ||
repository = github_repository.test.name | ||
branch = "main" | ||
file = "README.md" | ||
content = "overwritten" | ||
overwrite_on_create = false | ||
} | ||
`, randomID) | ||
|
||
check := resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"github_repository_file.test", "commit_message", | ||
"Managed by Terraform", | ||
), | ||
resource.TestCheckResourceAttr( | ||
"github_repository_file.test", "commit_author", | ||
"Terraform User", | ||
"github_repository_file.test", "content", | ||
"overwritten", | ||
), | ||
resource.TestCheckResourceAttr( | ||
"github_repository_file.test", "commit_email", | ||
"[email protected]", | ||
"github_repository_file.test", "sha", | ||
"67c1a95c2d9bb138aefeaebb319cca82e531736b", | ||
), | ||
) | ||
|
||
|
@@ -174,8 +107,14 @@ func TestAccGithubRepositoryFile(t *testing.T) { | |
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: check, | ||
Config: config, | ||
ExpectError: regexp.MustCompile(`Refusing to overwrite existing file`), | ||
}, | ||
{ | ||
Config: strings.Replace(config, | ||
"overwrite_on_create = false", | ||
"overwrite_on_create = true", 1), | ||
Check: check, | ||
}, | ||
}, | ||
}) | ||
|
Oops, something went wrong.