/
resource_tfe_organization_run_task_test.go
183 lines (159 loc) · 5.75 KB
/
resource_tfe_organization_run_task_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package tfe
import (
"fmt"
"math/rand"
"regexp"
"testing"
"time"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func TestAccTFEOrganizationRunTask_validateSchemaAttributeUrl(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccTFEOrganizationRunTask_basic("org", 1, ""),
ExpectError: regexp.MustCompile(`url to not be empty`),
},
{
Config: testAccTFEOrganizationRunTask_basic("org", 1, "https://"),
ExpectError: regexp.MustCompile(`to have a host`),
},
{
Config: testAccTFEOrganizationRunTask_basic("org", 1, "ftp://a.valid.url/path"),
ExpectError: regexp.MustCompile(`to have a url with schema of: "http,https"`),
},
},
})
}
func TestAccTFEOrganizationRunTask_create(t *testing.T) {
skipUnlessRunTasksDefined(t)
skipIfFreeOnly(t) // Run Tasks requires TFE or a TFC paid/trial subscription
runTask := &tfe.RunTask{}
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
orgName := fmt.Sprintf("tst-terraform-%d", rInt)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEOrganizationRunTaskDestroy,
Steps: []resource.TestStep{
testCheckCreateOrgWithRunTasks(orgName),
{
Config: testAccTFEOrganizationRunTask_basic(orgName, rInt, runTasksURL()),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEOrganizationRunTaskExists("tfe_organization_run_task.foobar", runTask),
resource.TestCheckResourceAttr("tfe_organization_run_task.foobar", "name", fmt.Sprintf("foobar-task-%d", rInt)),
resource.TestCheckResourceAttr("tfe_organization_run_task.foobar", "url", runTasksURL()),
resource.TestCheckResourceAttr("tfe_organization_run_task.foobar", "category", "task"),
resource.TestCheckResourceAttr("tfe_organization_run_task.foobar", "hmac_key", ""),
resource.TestCheckResourceAttr("tfe_organization_run_task.foobar", "enabled", "false"),
resource.TestCheckResourceAttr("tfe_organization_run_task.foobar", "description", ""),
),
},
{
Config: testAccTFEOrganizationRunTask_update(orgName, rInt, runTasksURL()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("tfe_organization_run_task.foobar", "name", fmt.Sprintf("foobar-task-%d-new", rInt)),
resource.TestCheckResourceAttr("tfe_organization_run_task.foobar", "url", runTasksURL()),
resource.TestCheckResourceAttr("tfe_organization_run_task.foobar", "category", "task"),
resource.TestCheckResourceAttr("tfe_organization_run_task.foobar", "hmac_key", "somepassword"),
resource.TestCheckResourceAttr("tfe_organization_run_task.foobar", "enabled", "true"),
resource.TestCheckResourceAttr("tfe_organization_run_task.foobar", "description", "a description"),
),
},
},
})
}
func TestAccTFEOrganizationRunTask_import(t *testing.T) {
skipUnlessRunTasksDefined(t)
skipIfFreeOnly(t) // Run Tasks requires TFE or a TFC paid/trial subscription
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
orgName := fmt.Sprintf("tst-terraform-%d", rInt)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFETeamAccessDestroy,
Steps: []resource.TestStep{
testCheckCreateOrgWithRunTasks(orgName),
{
Config: testAccTFEOrganizationRunTask_basic(orgName, rInt, runTasksURL()),
},
{
ResourceName: "tfe_organization_run_task.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("tst-terraform-%d/foobar-task-%d", rInt, rInt),
ImportStateVerify: true,
},
},
})
}
func testAccCheckTFEOrganizationRunTaskExists(n string, runTask *tfe.RunTask) resource.TestCheckFunc {
return func(s *terraform.State) error {
tfeClient := testAccProvider.Meta().(*tfe.Client)
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No instance ID is set")
}
rt, err := tfeClient.RunTasks.Read(ctx, rs.Primary.ID)
if err != nil {
return fmt.Errorf("error reading Run Task: %w", err)
}
if rt == nil {
return fmt.Errorf("Organization Run Task not found")
}
*runTask = *rt
return nil
}
}
func testAccCheckTFEOrganizationRunTaskDestroy(s *terraform.State) error {
tfeClient := testAccProvider.Meta().(*tfe.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "tfe_organization_run_task" {
continue
}
if rs.Primary.ID == "" {
return fmt.Errorf("No instance ID is set")
}
_, err := tfeClient.RunTasks.Read(ctx, rs.Primary.ID)
if err == nil {
return fmt.Errorf("Organization Run Task %s still exists", rs.Primary.ID)
}
}
return nil
}
func testAccTFEOrganizationRunTask_basic(orgName string, rInt int, runTaskURL string) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "%s"
email = "admin@company.com"
}
resource "tfe_organization_run_task" "foobar" {
organization = tfe_organization.foobar.id
url = "%s"
name = "foobar-task-%d"
enabled = false
}
`, orgName, runTaskURL, rInt)
}
func testAccTFEOrganizationRunTask_update(orgName string, rInt int, runTaskURL string) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "%s"
email = "admin@company.com"
}
resource "tfe_organization_run_task" "foobar" {
organization = tfe_organization.foobar.id
url = "%s"
name = "foobar-task-%d-new"
enabled = true
hmac_key = "somepassword"
description = "a description"
}
`, orgName, runTaskURL, rInt)
}