Skip to content
Snippets Groups Projects
Commit bebd0c2f authored by Patrick Rice's avatar Patrick Rice :evergreen_tree:
Browse files

Merge branch 'embed-test-config-batch-one' into 'main'

Embed config inside tests

See merge request !2297
parents b83fa7c4 777ac643
No related branches found
No related tags found
1 merge request!2297Embed config inside tests
Pipeline #1652194642 passed
Showing
with 334 additions and 338 deletions
......@@ -14,12 +14,29 @@ import (
func TestAccDataGitlabProjectProtectedBranches_search(t *testing.T) {
projectName := fmt.Sprintf("tf-%s", acctest.RandString(5))
//lintignore:AT001 // Data sources don't need check destroy in their tests
// lintignore:AT001 // Data sources don't need check destroy in their tests
resource.ParallelTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6MuxProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataGitlabProjectProtectedBranchesConfigGetProjectSearch(projectName),
Config: fmt.Sprintf(`
resource "gitlab_project" "test" {
name = "%s"
path = "%s"
default_branch = "main"
}
resource "gitlab_branch_protection" "test" {
project = gitlab_project.test.id
branch = "main"
push_access_level = "maintainer"
merge_access_level = "developer"
}
data "gitlab_project_protected_branches" "test" {
project_id = gitlab_branch_protection.test.project # This expresses the dependency of the data source on the protected branch having first been configured
}
`, projectName, projectName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"data.gitlab_project_protected_branches.test",
......@@ -36,24 +53,3 @@ func TestAccDataGitlabProjectProtectedBranches_search(t *testing.T) {
},
})
}
func testAccDataGitlabProjectProtectedBranchesConfigGetProjectSearch(projectName string) string {
return fmt.Sprintf(`
resource "gitlab_project" "test" {
name = "%s"
path = "%s"
default_branch = "main"
}
resource "gitlab_branch_protection" "test" {
project = gitlab_project.test.id
branch = "main"
push_access_level = "maintainer"
merge_access_level = "developer"
}
data "gitlab_project_protected_branches" "test" {
project_id = gitlab_branch_protection.test.project # This expresses the dependency of the data source on the protected branch having first been configured
}
`, projectName, projectName)
}
......@@ -78,7 +78,22 @@ func TestAccGitlabGroupVariable_basic(t *testing.T) {
Steps: []resource.TestStep{
// Create a group and variable with default options
{
Config: testAccGitlabGroupVariableConfig(rString),
Config: fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo%v"
path = "foo%v"
}
resource "gitlab_group_variable" "foo" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "value-%s"
variable_type = "file"
masked = false
description = "description-%s"
protected = false
}
`, rString, rString, rString, rString, rString),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupVariableExists("gitlab_group_variable.foo", &groupVariable),
testAccCheckGitlabGroupVariableAttributes(&groupVariable, &testAccGitlabGroupVariableExpectedAttributes{
......@@ -91,7 +106,21 @@ func TestAccGitlabGroupVariable_basic(t *testing.T) {
},
// Update the group variable to toggle all the values to their inverse
{
Config: testAccGitlabGroupVariableUpdateConfig(rString),
Config: fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo%v"
path = "foo%v"
}
resource "gitlab_group_variable" "foo" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "value-inverse-%s"
protected = true
masked = false
description = "description-inverse-%s"
}
`, rString, rString, rString, rString, rString),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupVariableExists("gitlab_group_variable.foo", &groupVariable),
testAccCheckGitlabGroupVariableAttributes(&groupVariable, &testAccGitlabGroupVariableExpectedAttributes{
......@@ -105,7 +134,22 @@ func TestAccGitlabGroupVariable_basic(t *testing.T) {
},
// // Update the group variable to toggle the options back
{
Config: testAccGitlabGroupVariableConfig(rString),
Config: fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo%v"
path = "foo%v"
}
resource "gitlab_group_variable" "foo" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "value-%s"
variable_type = "file"
masked = false
description = "description-%s"
protected = false
}
`, rString, rString, rString, rString, rString),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupVariableExists("gitlab_group_variable.foo", &groupVariable),
testAccCheckGitlabGroupVariableAttributes(&groupVariable, &testAccGitlabGroupVariableExpectedAttributes{
......@@ -120,7 +164,23 @@ func TestAccGitlabGroupVariable_basic(t *testing.T) {
// Update the group variable to enable "masked" for a value that does not meet masking requirements, and expect an error with no state change.
// ref: https://docs.gitlab.com/ce/ci/variables/README.html#masked-variable-requirements
{
Config: testAccGitlabGroupVariableUpdateConfigMaskedBad(rString),
Config: fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo%v"
path = "foo%v"
}
resource "gitlab_group_variable" "foo" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = <<EOF
value-%s"
i am multiline
EOF
variable_type = "env_var"
masked = true
}
`, rString, rString, rString, rString),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupVariableExists("gitlab_group_variable.foo", &groupVariable),
testAccCheckGitlabGroupVariableAttributes(&groupVariable, &testAccGitlabGroupVariableExpectedAttributes{
......@@ -136,7 +196,20 @@ func TestAccGitlabGroupVariable_basic(t *testing.T) {
// Update the group variable to to enable "masked" and meet masking requirements
// ref: https://docs.gitlab.com/ce/ci/variables/README.html#masked-variable-requirements
{
Config: testAccGitlabGroupVariableUpdateConfigMaskedGood(rString),
Config: fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo%v"
path = "foo%v"
}
resource "gitlab_group_variable" "foo" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "value-%s"
variable_type = "env_var"
masked = true
}
`, rString, rString, rString, rString),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupVariableExists("gitlab_group_variable.foo", &groupVariable),
testAccCheckGitlabGroupVariableAttributes(&groupVariable, &testAccGitlabGroupVariableExpectedAttributes{
......@@ -149,7 +222,22 @@ func TestAccGitlabGroupVariable_basic(t *testing.T) {
},
// Update the group variable to toggle the options back
{
Config: testAccGitlabGroupVariableConfig(rString),
Config: fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo%v"
path = "foo%v"
}
resource "gitlab_group_variable" "foo" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "value-%s"
variable_type = "file"
masked = false
description = "description-%s"
protected = false
}
`, rString, rString, rString, rString, rString),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupVariableExists("gitlab_group_variable.foo", &groupVariable),
testAccCheckGitlabGroupVariableAttributes(&groupVariable, &testAccGitlabGroupVariableExpectedAttributes{
......@@ -258,7 +346,26 @@ func TestAccGitlabGroupVariable_scope(t *testing.T) {
Steps: []resource.TestStep{
// Create a group and variables with same keys, different scopes
{
Config: testAccGitlabGroupVariableScopeConfig(rString, "*", "review/*", defaultValueA, defaultValueB),
Config: fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo%v"
path = "foo%v"
}
resource "gitlab_group_variable" "a" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "%s"
environment_scope = "*"
}
resource "gitlab_group_variable" "b" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "%s"
environment_scope = "review/*"
}
`, rString, rString, rString, defaultValueA, rString, defaultValueB),
SkipFunc: testutil.IsRunningInCE,
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupVariableExists("gitlab_group_variable.a", &groupVariableA),
......@@ -277,7 +384,26 @@ func TestAccGitlabGroupVariable_scope(t *testing.T) {
},
// Change a variable's scope
{
Config: testAccGitlabGroupVariableScopeConfig(rString, "my-new-scope", "review/*", defaultValueA, defaultValueB),
Config: fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo%v"
path = "foo%v"
}
resource "gitlab_group_variable" "a" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "%s"
environment_scope = "my-new-scope"
}
resource "gitlab_group_variable" "b" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "%s"
environment_scope = "review/*"
}
`, rString, rString, rString, defaultValueA, rString, defaultValueB),
SkipFunc: testutil.IsRunningInCE,
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupVariableExists("gitlab_group_variable.a", &groupVariableA),
......@@ -296,7 +422,26 @@ func TestAccGitlabGroupVariable_scope(t *testing.T) {
},
// Change both variables scopes at the same time
{
Config: testAccGitlabGroupVariableScopeConfig(rString, "my-new-new-scope", "review/hello-world", defaultValueA, defaultValueB),
Config: fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo%v"
path = "foo%v"
}
resource "gitlab_group_variable" "a" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "%s"
environment_scope = "my-new-new-scope"
}
resource "gitlab_group_variable" "b" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "%s"
environment_scope = "review/hello-world"
}
`, rString, rString, rString, defaultValueA, rString, defaultValueB),
SkipFunc: testutil.IsRunningInCE,
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupVariableExists("gitlab_group_variable.a", &groupVariableA),
......@@ -315,7 +460,26 @@ func TestAccGitlabGroupVariable_scope(t *testing.T) {
},
// Change value of one variable
{
Config: testAccGitlabGroupVariableScopeConfig(rString, "my-new-new-scope", "review/hello-world", defaultValueA, fmt.Sprintf("new-value-for-b-%s", rString)),
Config: fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo%v"
path = "foo%v"
}
resource "gitlab_group_variable" "a" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "%s"
environment_scope = "my-new-new-scope"
}
resource "gitlab_group_variable" "b" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "%s"
environment_scope = "%s"
}
`, rString, rString, rString, defaultValueA, rString, fmt.Sprintf("new-value-for-b-%s", rString), "review/hello-world"),
// SkipFunc: IsRunningInCE,
// NOTE(TF): this test sporadically fails because of this: https://gitlab.com/gitlab-org/gitlab/-/issues/333296
SkipFunc: func() (bool, error) { return true, nil },
......@@ -337,6 +501,7 @@ func TestAccGitlabGroupVariable_scope(t *testing.T) {
},
})
}
func testAccCheckGitlabGroupVariableExists(n string, groupVariable *gitlab.GroupVariable) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
......@@ -420,100 +585,3 @@ func testAccCheckGitlabGroupVariableDestroy(s *terraform.State) error {
}
return nil
}
func testAccGitlabGroupVariableConfig(rString string) string {
return fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo%v"
path = "foo%v"
}
resource "gitlab_group_variable" "foo" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "value-%s"
variable_type = "file"
masked = false
description = "description-%s"
protected = false
}
`, rString, rString, rString, rString, rString)
}
func testAccGitlabGroupVariableUpdateConfig(rString string) string {
return fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo%v"
path = "foo%v"
}
resource "gitlab_group_variable" "foo" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "value-inverse-%s"
protected = true
masked = false
description = "description-inverse-%s"
}
`, rString, rString, rString, rString, rString)
}
func testAccGitlabGroupVariableScopeConfig(rString, scopeA, scopeB string, valueA, valueB string) string {
return fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo%v"
path = "foo%v"
}
resource "gitlab_group_variable" "a" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "%s"
environment_scope = "%s"
}
resource "gitlab_group_variable" "b" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "%s"
environment_scope = "%s"
}
`, rString, rString, rString, valueA, scopeA, rString, valueB, scopeB)
}
func testAccGitlabGroupVariableUpdateConfigMaskedBad(rString string) string {
return fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo%v"
path = "foo%v"
}
resource "gitlab_group_variable" "foo" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = <<EOF
value-%s"
i am multiline
EOF
variable_type = "env_var"
masked = true
}
`, rString, rString, rString, rString)
}
func testAccGitlabGroupVariableUpdateConfigMaskedGood(rString string) string {
return fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo%v"
path = "foo%v"
}
resource "gitlab_group_variable" "foo" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "value-%s"
variable_type = "env_var"
masked = true
}
`, rString, rString, rString, rString)
}
......@@ -21,7 +21,18 @@ func TestAccDataGitlabBranch_basic(t *testing.T) {
ProtoV6ProviderFactories: providerFactoriesV6,
Steps: []resource.TestStep{
{
Config: testAccDataGitlabBranch(rInt, project.PathWithNamespace),
Config: fmt.Sprintf(`
resource "gitlab_branch" "foo" {
name = "testbranch-%[1]d"
ref = "main"
project = "%s"
}
data "gitlab_branch" "foo" {
name = "${gitlab_branch.foo.name}"
project = "%s"
}
`, rInt, project.PathWithNamespace, project.PathWithNamespace),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceGitlabBranch("gitlab_branch.foo", "data.gitlab_branch.foo"),
),
......@@ -32,7 +43,6 @@ func TestAccDataGitlabBranch_basic(t *testing.T) {
func testAccDataSourceGitlabBranch(src, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
branch := s.RootModule().Resources[src]
branchAttr := branch.Primary.Attributes
......@@ -62,24 +72,3 @@ func testAccDataSourceGitlabBranch(src, n string) resource.TestCheckFunc {
return nil
}
}
func testAccDataGitlabBranch(rInt int, project string) string {
return fmt.Sprintf(`
%s
data "gitlab_branch" "foo" {
name = "${gitlab_branch.foo.name}"
project = "%s"
}
`, testAccDataGitlabBranchSetup(rInt, project), project)
}
func testAccDataGitlabBranchSetup(rInt int, project string) string {
return fmt.Sprintf(`
resource "gitlab_branch" "foo" {
name = "testbranch-%[1]d"
ref = "main"
project = "%s"
}
`, rInt, project)
}
......@@ -28,7 +28,13 @@ func TestAccDataSourceGitlabGroups_basic(t *testing.T) {
ProtoV6ProviderFactories: providerFactoriesV6,
Steps: []resource.TestStep{
{
Config: testAccDataSourceGitlabGroupsConfigSearchSort(prefixFoo),
Config: `
data "gitlab_groups" "foos" {
sort = "asc"
search = "acctest-group-foo"
order_by = "id"
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.gitlab_groups.foos", "groups.#", "2"),
resource.TestCheckResourceAttr("data.gitlab_groups.foos", "groups.0.group_id", fmt.Sprint(groupsFoo[0].ID)),
......@@ -51,19 +57,33 @@ func TestAccDataSourceGitlabGroups_basic(t *testing.T) {
),
},
{
Config: testAccDataSourceGitlabLotsOfGroupsSearch(prefixLotsOf),
Config: `
data "gitlab_groups" "lotsof" {
search = "acctest-group-lotsof"
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.gitlab_groups.lotsof", "groups.#", "42"),
),
},
{
Config: testAccDataSourceGitlabWithTopLevelOnly(prefixParent),
Config: `
data "gitlab_groups" "toplevel" {
top_level_only = true
search = "acctest-group-parent"
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.gitlab_groups.toplevel", "groups.#", "1"),
),
},
{
Config: testAccDataSourceGitlabWithoutTopLevelOnly(prefixParent),
Config: `
data "gitlab_groups" "sublevel" {
top_level_only = false
search = "acctest-group-parent"
}
`,
Check: resource.ComposeTestCheckFunc(
// check if all subgroups are returned
resource.TestCheckResourceAttr("data.gitlab_groups.sublevel", "groups.#", "2"),
......@@ -77,39 +97,3 @@ func TestAccDataSourceGitlabGroups_basic(t *testing.T) {
},
})
}
func testAccDataSourceGitlabGroupsConfigSearchSort(prefix string) string {
return fmt.Sprintf(`
data "gitlab_groups" "foos" {
sort = "asc"
search = "%s"
order_by = "id"
}
`, prefix)
}
func testAccDataSourceGitlabLotsOfGroupsSearch(prefix string) string {
return fmt.Sprintf(`
data "gitlab_groups" "lotsof" {
search = "%s"
}
`, prefix)
}
func testAccDataSourceGitlabWithTopLevelOnly(prefix string) string {
return fmt.Sprintf(`
data "gitlab_groups" "toplevel" {
top_level_only = true
search = "%s"
}
`, prefix)
}
func testAccDataSourceGitlabWithoutTopLevelOnly(prefix string) string {
return fmt.Sprintf(`
data "gitlab_groups" "sublevel" {
top_level_only = false
search = "%s"
}
`, prefix)
}
......@@ -20,7 +20,19 @@ func TestAccDataSourceGitlabProjectIssue_basic(t *testing.T) {
ProtoV6ProviderFactories: providerFactoriesV6,
Steps: []resource.TestStep{
{
Config: testAccDataGitlabProjectIssueConfig(testProject.ID),
Config: fmt.Sprintf(`
resource "gitlab_project_issue" "this" {
project = %d
title = "Terraform acceptance tests"
description = "Some description"
due_date = "1994-02-21"
}
data "gitlab_project_issue" "this" {
project = %d
iid = gitlab_project_issue.this.iid
}
`, testProject.ID, testProject.ID),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceGitlabProjectIssue("gitlab_project_issue.this", "data.gitlab_project_issue.this"),
),
......@@ -31,7 +43,6 @@ func TestAccDataSourceGitlabProjectIssue_basic(t *testing.T) {
func testAccDataSourceGitlabProjectIssue(src, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
resource := s.RootModule().Resources[src]
resourceAttributes := resource.Primary.Attributes
......@@ -49,19 +60,3 @@ func testAccDataSourceGitlabProjectIssue(src, n string) resource.TestCheckFunc {
return nil
}
}
func testAccDataGitlabProjectIssueConfig(projectID int) string {
return fmt.Sprintf(`
resource "gitlab_project_issue" "this" {
project = %d
title = "Terraform acceptance tests"
description = "Some description"
due_date = "1994-02-21"
}
data "gitlab_project_issue" "this" {
project = %d
iid = gitlab_project_issue.this.iid
}
`, projectID, projectID)
}
......@@ -20,7 +20,15 @@ func TestAccDataSourceGitlabProjectIssues_basic(t *testing.T) {
ProtoV6ProviderFactories: providerFactoriesV6,
Steps: []resource.TestStep{
{
Config: testAccDataGitlabProjectIssuesConfig(testProject.ID),
Config: fmt.Sprintf(`
data "gitlab_project_issues" "this" {
project = %d
// only for determinism
order_by = "relative_position"
sort = "asc"
}
`, testProject.ID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.gitlab_project_issues.this", "issues.#", fmt.Sprintf("%d", len(testIssues))),
resource.TestCheckResourceAttr("data.gitlab_project_issues.this", "issues.0.iid", "1"),
......@@ -34,15 +42,3 @@ func TestAccDataSourceGitlabProjectIssues_basic(t *testing.T) {
},
})
}
func testAccDataGitlabProjectIssuesConfig(projectID int) string {
return fmt.Sprintf(`
data "gitlab_project_issues" "this" {
project = %d
// only for determinism
order_by = "relative_position"
sort = "asc"
}
`, projectID)
}
......@@ -13,7 +13,6 @@ import (
)
func TestAccDataSourceGitlabProjectMembership_basic(t *testing.T) {
project := testutil.CreateProject(t)
users := testutil.CreateUsers(t, 1)
testutil.AddProjectMembers(t, project.ID, users)
......@@ -22,7 +21,11 @@ func TestAccDataSourceGitlabProjectMembership_basic(t *testing.T) {
ProtoV6ProviderFactories: providerFactoriesV6,
Steps: []resource.TestStep{
{
Config: testAccDataSourceGitlabProjectMembership(project.ID),
Config: fmt.Sprintf(`
data "gitlab_project_membership" "foo" {
project_id = "%d"
}
`, project.ID),
Check: resource.ComposeTestCheckFunc(
// Members is 2 because the user owning the token is always added to the project
resource.TestCheckResourceAttr("data.gitlab_project_membership.foo", "members.#", "2"),
......@@ -45,7 +48,11 @@ func TestAccDataSourceGitlabProjectMembership_pagination(t *testing.T) {
ProtoV6ProviderFactories: providerFactoriesV6,
Steps: []resource.TestStep{
{
Config: testAccDataSourceGitlabProjectMembership(project.ID),
Config: fmt.Sprintf(`
data "gitlab_project_membership" "foo" {
project_id = "%d"
}
`, project.ID),
// one more for the user owning the token, which is always added to the project.
Check: resource.TestCheckResourceAttr("data.gitlab_project_membership.foo", "members.#", fmt.Sprintf("%d", userCount+1)),
},
......@@ -76,10 +83,3 @@ func TestAccDataSourceGitlabProjectMembership_ByUserID(t *testing.T) {
},
})
}
func testAccDataSourceGitlabProjectMembership(projectID int) string {
return fmt.Sprintf(`
data "gitlab_project_membership" "foo" {
project_id = "%d"
}`, projectID)
}
......@@ -22,7 +22,18 @@ func TestAccDataGitlabProjectTag_basic(t *testing.T) {
ProtoV6ProviderFactories: providerFactoriesV6,
Steps: []resource.TestStep{
{
Config: testAccDataGitlabProjectTag(rInt, project.PathWithNamespace),
Config: fmt.Sprintf(`
resource "gitlab_project_tag" "foo" {
name = "tag-%[1]d"
ref = "main"
project = "%s"
}
data "gitlab_project_tag" "foo" {
name = "${gitlab_project_tag.foo.name}"
project = "%s"
}
`, rInt, project.PathWithNamespace, project.PathWithNamespace),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceGitlabProjectTag("gitlab_project_tag.foo", "data.gitlab_project_tag.foo"),
),
......@@ -33,7 +44,6 @@ func TestAccDataGitlabProjectTag_basic(t *testing.T) {
func testAccDataSourceGitlabProjectTag(src, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
tag := s.RootModule().Resources[src]
tagAttr := tag.Primary.Attributes
......@@ -59,23 +69,3 @@ func testAccDataSourceGitlabProjectTag(src, n string) resource.TestCheckFunc {
return nil
}
}
func testAccDataGitlabProjectTag(rInt int, project string) string {
return fmt.Sprintf(`
%s
data "gitlab_project_tag" "foo" {
name = "${gitlab_project_tag.foo.name}"
project = "%s"
}
`, testAccDataGitlabProjectTagSetup(rInt, project), project)
}
func testAccDataGitlabProjectTagSetup(rInt int, project string) string {
return fmt.Sprintf(`
resource "gitlab_project_tag" "foo" {
name = "tag-%[1]d"
ref = "main"
project = "%s"
}
`, rInt, project)
}
......@@ -21,7 +21,26 @@ func TestAccDataGitlabProjectTags_basic(t *testing.T) {
ProtoV6ProviderFactories: providerFactoriesV6,
Steps: []resource.TestStep{
{
Config: testAccDataGitlabProjectTags(countTags, project.PathWithNamespace),
Config: fmt.Sprintf(`
resource "gitlab_project_tag" "foo" {
count = "%[1]d"
name = "${count.index}"
ref = "main"
project = "%s"
message = "Tag ${count.index}"
}
data "gitlab_project_tags" "foo" {
project = "%s"
order_by = "name"
sort = "asc"
depends_on = [
gitlab_project_tag.foo,
]
}
`, countTags, project.PathWithNamespace, project.PathWithNamespace),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceGitlabProjectTags("gitlab_project_tag.foo", "data.gitlab_project_tags.foo", countTags),
),
......@@ -60,31 +79,3 @@ func testAccDataSourceGitlabProjectTags(src string, n string, countTags int) res
return nil
}
}
func testAccDataGitlabProjectTags(countTags int, project string) string {
return fmt.Sprintf(`
%s
data "gitlab_project_tags" "foo" {
project = "%s"
order_by = "name"
sort = "asc"
depends_on = [
gitlab_project_tag.foo,
]
}
`, testAccDataGitlabProjectTagsSetup(countTags, project), project)
}
func testAccDataGitlabProjectTagsSetup(countTags int, project string) string {
return fmt.Sprintf(`
resource "gitlab_project_tag" "foo" {
count = "%[1]d"
name = "${count.index}"
ref = "main"
project = "%s"
message = "Tag ${count.index}"
}
`, countTags, project)
}
......@@ -11,8 +11,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"gitlab.com/gitlab-org/api/client-go"
gitlab "gitlab.com/gitlab-org/api/client-go"
"gitlab.com/gitlab-org/terraform-provider-gitlab/internal/provider/testutil"
)
......@@ -25,18 +25,54 @@ func TestAccDataGitlabProject_basic(t *testing.T) {
ProtoV6ProviderFactories: providerFactoriesV6,
Steps: []resource.TestStep{
{
Config: testAccDataGitlabProjectConfigByPathWithNamespace(projectname),
Config: fmt.Sprintf(`
resource "gitlab_project" "test"{
name = "%s"
path = "%s"
description = "Terraform acceptance tests"
visibility_level = "public"
}
data "gitlab_project" "foo" {
path_with_namespace = gitlab_project.test.path_with_namespace
}
`, projectname, projectname),
Check: testAccDataSourceGitlabProject("gitlab_project.test", "data.gitlab_project.foo",
[]string{"id", "name", "path", "visibility", "description"}),
},
{
Config: testAccDataGitlabProjectConfig(projectname),
Config: fmt.Sprintf(`
resource "gitlab_project" "test"{
name = "%s"
path = "%s"
description = "Terraform acceptance tests"
visibility_level = "public"
}
data "gitlab_project" "foo" {
id = "${gitlab_project.test.id}"
}
`, projectname, projectname),
Check: testAccDataSourceGitlabProject("gitlab_project.test", "data.gitlab_project.foo",
[]string{"id", "name", "path", "visibility", "description"}),
},
{
SkipFunc: testutil.IsRunningInCE,
Config: testAccDataGitlabProjectConfigPushRules(projectname),
Config: fmt.Sprintf(`
resource "gitlab_project" "test"{
name = "%[1]s"
path = "%[1]s"
description = "Terraform acceptance tests"
visibility_level = "public"
push_rules {
author_email_regex = "foo"
}
}
data "gitlab_project" "foo" {
id = gitlab_project.test.id
}
`, projectname),
Check: testAccDataSourceGitlabProject("gitlab_project.test", "data.gitlab_project.foo",
[]string{"push_rules.0.author_email_regex"}),
},
......@@ -223,7 +259,6 @@ func TestAccDataGitlabProject_CIPipelineVariablesMinimumOverrideRole(t *testing.
func testAccDataSourceGitlabProject(resourceName, dataSourceName string, testAttributes []string) resource.TestCheckFunc {
return func(s *terraform.State) error {
project := s.RootModule().Resources[resourceName]
projectResource := project.Primary.Attributes
......@@ -242,51 +277,3 @@ func testAccDataSourceGitlabProject(resourceName, dataSourceName string, testAtt
return nil
}
}
func testAccDataGitlabProjectConfig(projectname string) string {
return fmt.Sprintf(`
resource "gitlab_project" "test"{
name = "%s"
path = "%s"
description = "Terraform acceptance tests"
visibility_level = "public"
}
data "gitlab_project" "foo" {
id = "${gitlab_project.test.id}"
}
`, projectname, projectname)
}
func testAccDataGitlabProjectConfigByPathWithNamespace(projectname string) string {
return fmt.Sprintf(`
resource "gitlab_project" "test"{
name = "%s"
path = "%s"
description = "Terraform acceptance tests"
visibility_level = "public"
}
data "gitlab_project" "foo" {
path_with_namespace = gitlab_project.test.path_with_namespace
}
`, projectname, projectname)
}
func testAccDataGitlabProjectConfigPushRules(projectName string) string {
return fmt.Sprintf(`
resource "gitlab_project" "test"{
name = "%[1]s"
path = "%[1]s"
description = "Terraform acceptance tests"
visibility_level = "public"
push_rules {
author_email_regex = "foo"
}
}
data "gitlab_project" "foo" {
id = gitlab_project.test.id
}
`, projectName)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment