Skip to content

Commit 0f5f84f

Browse files
committed
New tests
1 parent 3286cc6 commit 0f5f84f

12 files changed

+68
-135
lines changed

docs/data-sources/project_milestone.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
page_title: "gitlab_project_milestone Data Source - terraform-provider-gitlab"
44
subcategory: ""
55
description: |-
6-
The gitlab_project_milestone data source allows details of a project milestone.
6+
The gitlab_project_milestone data source allows get details of a project milestone.
77
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/milestones.html
88
---
99

1010
# gitlab_project_milestone (Data Source)
1111

12-
The `gitlab_project_milestone` data source allows details of a project milestone.
12+
The `gitlab_project_milestone` data source allows get details of a project milestone.
1313

1414
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/milestones.html)
1515

docs/data-sources/project_milestones.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
page_title: "gitlab_project_milestones Data Source - terraform-provider-gitlab"
44
subcategory: ""
55
description: |-
6-
The gitlab_project_milestones data source allows details of a project milestones.
6+
The gitlab_project_milestones data source allows get details of a project milestones.
77
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/milestones.html
88
---
99

1010
# gitlab_project_milestones (Data Source)
1111

12-
The `gitlab_project_milestones` data source allows details of a project milestones.
12+
The `gitlab_project_milestones` data source allows get details of a project milestones.
1313

1414
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/milestones.html)
1515

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,6 @@ github.com/vmihailenco/msgpack/v4 v4.3.12 h1:07s4sz9IReOgdikxLTKNbBdqDMLsjPKXwvC
309309
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
310310
github.com/vmihailenco/tagparser v0.1.1 h1:quXMXlA39OCbd2wAdTsGDlK9RkOk6Wuw+x37wVyIuWY=
311311
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
312-
github.com/xanzy/go-gitlab v0.63.0 h1:a9fXpKWykUS6dowapFej/2Wjf4aOAEFC1q2ZIcz4IpI=
313-
github.com/xanzy/go-gitlab v0.63.0/go.mod h1:F0QEXwmqiBUxCgJm8fE9S+1veX4XC9Z4cfaAbqwk4YM=
314312
github.com/xanzy/go-gitlab v0.64.0 h1:rMgQdW9S1w3qvNAH2LYpFd2xh7KNLk+JWJd7sorNuTc=
315313
github.com/xanzy/go-gitlab v0.64.0/go.mod h1:F0QEXwmqiBUxCgJm8fE9S+1veX4XC9Z4cfaAbqwk4YM=
316314
github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
@@ -413,8 +411,6 @@ golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT
413411
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
414412
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
415413
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
416-
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 h1:bRb386wvrE+oBNdF1d/Xh9mQrfQ4ecYhW5qJ5GvTGT4=
417-
golang.org/x/net v0.0.0-20220412020605-290c469a71a5/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
418414
golang.org/x/net v0.0.0-20220421235706-1d1ef9303861 h1:yssD99+7tqHWO5Gwh81phT+67hg+KttniBr6UnEXOY8=
419415
golang.org/x/net v0.0.0-20220421235706-1d1ef9303861/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
420416
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=

internal/provider/data_source_gitlab_project_milestone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func dataSourceGitlabProjectMilestoneRead(ctx context.Context, d *schema.Resourc
2828
if err != nil {
2929
return diag.FromErr(err)
3030
}
31-
d.SetId(resourceGitLabProjectMilestoneBuildId(project, milestoneID))
31+
d.SetId(buildTwoPartIDInterface(project, milestoneID))
3232
stateMap := gitlabProjectMilestoneToStateMap(milestone)
3333

3434
if err := setStateMapInResourceData(stateMap, d); err != nil {

internal/provider/data_source_gitlab_project_milestone_test.go

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,35 @@ import (
55
"testing"
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8-
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
98
)
109

1110
func TestAccDataSourceGitlabProjectMilestone_basic(t *testing.T) {
1211
testAccCheck(t)
1312

1413
testProject := testAccCreateProject(t)
14+
testMilestone := testAccAddProjectMilestones(t, testProject, 1)[0]
1515

1616
resource.Test(t, resource.TestCase{
1717
PreCheck: func() { testAccPreCheck(t) },
1818
ProviderFactories: providerFactories,
1919
Steps: []resource.TestStep{
2020
{
21-
Config: testAccDataGitlabProjectMilestoneConfig(testProject.ID),
21+
Config: testAccDataGitlabProjectMilestoneConfig(testProject.ID, testMilestone.ID),
2222
Check: resource.ComposeTestCheckFunc(
23-
testAccDataSourceGitlabProjectMilestone("gitlab_project_milestone.this", "data.gitlab_project_milestone.this"),
23+
resource.TestCheckResourceAttr("data.gitlab_project_milestone.this", "milestone_id", fmt.Sprintf("%v", testMilestone.ID)),
24+
resource.TestCheckResourceAttr("data.gitlab_project_milestone.this", "title", testMilestone.Title),
25+
resource.TestCheckResourceAttr("data.gitlab_project_milestone.this", "description", testMilestone.Description),
2426
),
2527
},
2628
},
2729
})
2830
}
2931

30-
func testAccDataSourceGitlabProjectMilestone(src, n string) resource.TestCheckFunc {
31-
return func(s *terraform.State) error {
32-
33-
resource := s.RootModule().Resources[src]
34-
resourceAttributes := resource.Primary.Attributes
35-
36-
datasource := s.RootModule().Resources[n]
37-
datasourceAttributes := datasource.Primary.Attributes
38-
39-
testAttributes := attributeNamesFromSchema(gitlabProjectMilestoneGetSchema())
40-
41-
for _, attribute := range testAttributes {
42-
if datasourceAttributes[attribute] != resourceAttributes[attribute] {
43-
return fmt.Errorf("Expected issue's attribute `%s` to be: %s, but got: `%s`", attribute, resourceAttributes[attribute], datasourceAttributes[attribute])
44-
}
45-
}
46-
47-
return nil
48-
}
49-
}
50-
51-
func testAccDataGitlabProjectMilestoneConfig(projectID int) string {
32+
func testAccDataGitlabProjectMilestoneConfig(projectID int, milestoneID int) string {
5233
return fmt.Sprintf(`
53-
resource "gitlab_project_milestone" "this" {
54-
project_id = %[1]d
55-
title = "Terraform acceptance tests"
56-
description = "Some description"
57-
start_date = "1994-02-21"
58-
due_date = "1994-02-25"
59-
}
6034
data "gitlab_project_milestone" "this" {
61-
project_id = %[1]d
62-
milestone_id = gitlab_project_milestone.this.milestone_id
35+
project_id = "%d"
36+
milestone_id = "%d"
6337
}
64-
`, projectID)
38+
`, projectID, milestoneID)
6539
}

internal/provider/data_source_gitlab_project_milestones.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package provider
22

33
import (
44
"context"
5-
"fmt"
65
"log"
76

87
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -114,7 +113,7 @@ func dataSourceGitlabProjectMilestonesRead(ctx context.Context, d *schema.Resour
114113
}
115114

116115
log.Printf("[DEBUG] get gitlab milestones from project: %s", project)
117-
d.SetId(fmt.Sprintf("%s:%d", project, optionsHash))
116+
d.SetId(buildTwoPartIDInterface(project, optionsHash))
118117
if err = d.Set("milestones", flattenGitlabProjectMilestones(milestones)); err != nil {
119118
return diag.Errorf("Failed to set milestones to state: %v", err)
120119
}

internal/provider/data_source_gitlab_project_milestones_test.go

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,74 +5,36 @@ import (
55
"testing"
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8-
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
98
)
109

1110
func TestAccDataGitlabProjectMilestones_basic(t *testing.T) {
1211
testAccCheck(t)
13-
countMilestones := 2
14-
project := testAccCreateProject(t)
12+
13+
testProject := testAccCreateProject(t)
14+
testMilestones := testAccAddProjectMilestones(t, testProject, 2)
1515

1616
resource.Test(t, resource.TestCase{
1717
PreCheck: func() { testAccPreCheck(t) },
1818
ProviderFactories: providerFactories,
1919
Steps: []resource.TestStep{
2020
{
21-
Config: testAccDataGitlabProjectMilestones(countMilestones, project.ID),
21+
Config: testAccDataGitlabProjectMilestonesConfig(testProject.ID),
2222
Check: resource.ComposeTestCheckFunc(
23-
testAccDataSourceGitlabProjectMilestones("gitlab_project_milestone.this", "data.gitlab_project_milestones.this", countMilestones),
23+
resource.TestCheckResourceAttr("data.gitlab_project_milestones.this", "milestones.#", fmt.Sprintf("%d", len(testMilestones))),
24+
resource.TestCheckResourceAttr("data.gitlab_project_milestones.this", "milestones.0.title", testMilestones[1].Title),
25+
resource.TestCheckResourceAttr("data.gitlab_project_milestones.this", "milestones.0.description", testMilestones[1].Description),
26+
resource.TestCheckResourceAttr("data.gitlab_project_milestones.this", "milestones.1.title", testMilestones[0].Title),
27+
resource.TestCheckResourceAttr("data.gitlab_project_milestones.this", "milestones.1.description", testMilestones[0].Description),
2428
),
2529
},
2630
},
2731
})
2832
}
2933

30-
func testAccDataSourceGitlabProjectMilestones(src string, n string, countMilestones int) resource.TestCheckFunc {
31-
return func(s *terraform.State) error {
32-
testAttributes := attributeNamesFromSchema(gitlabProjectMilestoneGetSchema())
33-
34-
for numberMilestone := range make([]int, countMilestones) {
35-
search := s.RootModule().Resources[n]
36-
searchAttrs := search.Primary.Attributes
37-
38-
milestone := s.RootModule().Resources[fmt.Sprintf("%s.%d", src, numberMilestone)]
39-
milestoneAttrs := milestone.Primary.Attributes
40-
41-
for _, attribute := range testAttributes {
42-
milestoneAttr := milestoneAttrs[attribute]
43-
searchAttr := searchAttrs[fmt.Sprintf("milestones.%d.%s", numberMilestone, attribute)]
44-
if searchAttr != milestoneAttr {
45-
return fmt.Errorf("Expected the milestone `%s` with parameter `%s` to be: `%s`, but got: `%s`", milestoneAttrs["id"], attribute, milestoneAttr, searchAttr)
46-
}
47-
}
48-
}
49-
50-
return nil
51-
}
52-
}
53-
54-
func testAccDataGitlabProjectMilestones(countTags int, project int) string {
34+
func testAccDataGitlabProjectMilestonesConfig(projectID int) string {
5535
return fmt.Sprintf(`
56-
%s
5736
data "gitlab_project_milestones" "this" {
58-
project_id = "%d"
59-
state = "active"
60-
search = "test"
61-
depends_on = [
62-
gitlab_project_milestone.this,
63-
]
64-
}
65-
`, testAccDataGitlabProjectMilestonesSetup(countTags, project), project)
66-
}
67-
68-
func testAccDataGitlabProjectMilestonesSetup(countTags int, project int) string {
69-
return fmt.Sprintf(`
70-
resource "gitlab_project_milestone" "this" {
71-
count = "%d"
72-
73-
project_id = "%d"
74-
title = "test-${count.index}"
75-
description = "test-${count.index}"
37+
project_id = "%d"
7638
}
77-
`, countTags, project)
39+
`, projectID)
7840
}

internal/provider/helper_test.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -309,20 +309,25 @@ func testAccAddGroupMembers(t *testing.T, gid interface{}, users []*gitlab.User)
309309
}
310310
}
311311

312-
func testAccAddProjectMilestone(t *testing.T, pid interface{}) *gitlab.Milestone {
312+
// testAccAddProjectMilestones is a test helper for adding milestones to project.
313+
// It assumes the group will be destroyed at the end of the test and will not cleanup milestones.
314+
func testAccAddProjectMilestones(t *testing.T, project *gitlab.Project, n int) []*gitlab.Milestone {
313315
t.Helper()
314316

315-
milestone, _, err := testGitlabClient.Milestones.CreateMilestone(pid, &gitlab.CreateMilestoneOptions{Title: gitlab.String("Test Milestone")})
316-
if err != nil {
317-
t.Fatalf("failed to create milestone during test for project %v: %v", pid, err)
318-
}
319-
t.Cleanup(func() {
320-
_, err := testGitlabClient.Milestones.DeleteMilestone(pid, milestone.ID)
317+
milestones := make([]*gitlab.Milestone, n)
318+
319+
for i := range milestones {
320+
var err error
321+
milestones[i], _, err = testGitlabClient.Milestones.CreateMilestone(project.ID, &gitlab.CreateMilestoneOptions{
322+
Title: gitlab.String(fmt.Sprintf("Milestone %d", i)),
323+
Description: gitlab.String(fmt.Sprintf("Description %d", i)),
324+
})
321325
if err != nil {
322-
t.Fatalf("failed to delete milestone %d during test for project %v: %v", milestone.ID, pid, err)
326+
t.Fatalf("Could not create test milestones: %v", err)
323327
}
324-
})
325-
return milestone
328+
}
329+
330+
return milestones
326331
}
327332

328333
func testAccCreateDeployKey(t *testing.T, projectID int, options *gitlab.AddDeployKeyOptions) *gitlab.ProjectDeployKey {

internal/provider/resource_gitlab_project_issue_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestAccGitlabProjectIssue_basic(t *testing.T) {
2020
testProject := testAccCreateProject(t)
2121
testUser := testAccCreateUsers(t, 1)[0]
2222
testAccAddProjectMembers(t, testProject.ID, []*gitlab.User{testUser})
23-
testMilestone := testAccAddProjectMilestone(t, testProject.ID)
23+
testMilestone := testAccAddProjectMilestones(t, testProject, 1)[0]
2424

2525
currentUser, _, err := testGitlabClient.Users.CurrentUser()
2626
if err != nil {
@@ -136,7 +136,7 @@ func TestAccGitlabProjectIssue_basicEE(t *testing.T) {
136136
testProject := testAccCreateProject(t)
137137
testUser := testAccCreateUsers(t, 1)[0]
138138
testAccAddProjectMembers(t, testProject.ID, []*gitlab.User{testUser})
139-
testMilestone := testAccAddProjectMilestone(t, testProject.ID)
139+
testMilestone := testAccAddProjectMilestones(t, testProject, 1)[0]
140140

141141
resource.Test(t, resource.TestCase{
142142
PreCheck: func() { testAccPreCheck(t) },

internal/provider/resource_gitlab_project_milestone.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package provider
22

33
import (
44
"context"
5-
"fmt"
65
"log"
7-
"strconv"
86

97
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
108
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -67,7 +65,7 @@ func resourceGitlabProjectMilestoneCreate(ctx context.Context, d *schema.Resourc
6765
log.Printf("[WARN] failed to create gitlab milestone in project %s with title %s (response %v)", project, title, resp)
6866
return diag.FromErr(err)
6967
}
70-
d.SetId(resourceGitLabProjectMilestoneBuildId(project, milestone.ID))
68+
d.SetId(buildTwoPartIDInterface(project, milestone.ID))
7169

7270
updateOptions := gitlab.UpdateMilestoneOptions{}
7371
if stateEvent, ok := d.GetOk("state"); ok {
@@ -85,7 +83,7 @@ func resourceGitlabProjectMilestoneCreate(ctx context.Context, d *schema.Resourc
8583

8684
func resourceGitlabProjectMilestoneRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
8785
client := meta.(*gitlab.Client)
88-
project, milestoneID, err := resourceGitLabProjectIssueParseId(d.Id())
86+
project, milestoneID, err := parseTwoPartIDInt(d.Id())
8987
if err != nil {
9088
return diag.FromErr(err)
9189
}
@@ -111,7 +109,7 @@ func resourceGitlabProjectMilestoneRead(ctx context.Context, d *schema.ResourceD
111109

112110
func resourceGitlabProjectMilestoneUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
113111
client := meta.(*gitlab.Client)
114-
project, milestoneID, err := resourceGitLabProjectIssueParseId(d.Id())
112+
project, milestoneID, err := parseTwoPartIDInt(d.Id())
115113
if err != nil {
116114
return diag.FromErr(err)
117115
}
@@ -155,7 +153,7 @@ func resourceGitlabProjectMilestoneUpdate(ctx context.Context, d *schema.Resourc
155153

156154
func resourceGitlabProjectMilestoneDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
157155
client := meta.(*gitlab.Client)
158-
project, milestoneID, err := resourceGitLabProjectIssueParseId(d.Id())
156+
project, milestoneID, err := parseTwoPartIDInt(d.Id())
159157
if err != nil {
160158
return diag.FromErr(err)
161159
}
@@ -168,22 +166,3 @@ func resourceGitlabProjectMilestoneDelete(ctx context.Context, d *schema.Resourc
168166
}
169167
return nil
170168
}
171-
172-
func resourceGitLabProjectMilestoneBuildId(project string, milestoneID int) string {
173-
stringMilestoneID := fmt.Sprintf("%d", milestoneID)
174-
return buildTwoPartID(&project, &stringMilestoneID)
175-
}
176-
177-
func resourceGitLabProjectMilestoneParseId(id string) (string, int, error) {
178-
project, milestone, err := parseTwoPartID(id)
179-
if err != nil {
180-
return "", 0, err
181-
}
182-
183-
milestoneID, err := strconv.Atoi(milestone)
184-
if err != nil {
185-
return "", 0, err
186-
}
187-
188-
return project, milestoneID, nil
189-
}

0 commit comments

Comments
 (0)