-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathresource_agent_pool_test.go
172 lines (153 loc) · 5.3 KB
/
resource_agent_pool_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
//go:build (all || resource_agentpool) && !exclude_resource_agentpool
// +build all resource_agentpool
// +build !exclude_resource_agentpool
package acceptancetests
import (
"fmt"
"regexp"
"strconv"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/microsoft/azure-devops-go-api/azuredevops/v7/taskagent"
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/acceptancetests/testutils"
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/client"
)
func TestAccAgentPool_basic(t *testing.T) {
poolName := testutils.GenerateResourceName()
tfNode := "azuredevops_agent_pool.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, nil) },
Providers: testutils.GetProviders(),
CheckDestroy: checkAgentPoolDestroyed,
Steps: []resource.TestStep{
{
Config: hclAgentPoolBasic(poolName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(tfNode, "name", poolName),
resource.TestCheckResourceAttr(tfNode, "auto_provision", "false"),
resource.TestCheckResourceAttr(tfNode, "auto_update", "false"),
resource.TestCheckResourceAttr(tfNode, "pool_type", "automation"),
),
},
{
ResourceName: tfNode,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccAgentPool_update(t *testing.T) {
poolName := testutils.GenerateResourceName()
tfNode := "azuredevops_agent_pool.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, nil) },
Providers: testutils.GetProviders(),
CheckDestroy: checkAgentPoolDestroyed,
Steps: []resource.TestStep{
{
Config: hclAgentPoolBasic(poolName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(tfNode, "name", poolName),
resource.TestCheckResourceAttr(tfNode, "auto_provision", "false"),
resource.TestCheckResourceAttr(tfNode, "auto_update", "false"),
resource.TestCheckResourceAttr(tfNode, "pool_type", "automation"),
),
},
{
ResourceName: tfNode,
ImportState: true,
ImportStateVerify: true,
},
{
Config: hclAgentPoolUpdate(poolName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(tfNode, "name", poolName),
resource.TestCheckResourceAttr(tfNode, "auto_provision", "true"),
resource.TestCheckResourceAttr(tfNode, "auto_update", "true"),
resource.TestCheckResourceAttr(tfNode, "pool_type", "automation"),
),
},
{
ResourceName: tfNode,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccAgentPool_requiresImportErrorStep(t *testing.T) {
poolName := testutils.GenerateResourceName()
tfNode := "azuredevops_agent_pool.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, nil) },
Providers: testutils.GetProviders(),
CheckDestroy: checkAgentPoolDestroyed,
Steps: []resource.TestStep{
{
Config: hclAgentPoolBasic(poolName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(tfNode, "name", poolName),
resource.TestCheckResourceAttr(tfNode, "auto_provision", "false"),
resource.TestCheckResourceAttr(tfNode, "auto_update", "false"),
resource.TestCheckResourceAttr(tfNode, "pool_type", "automation"),
),
},
{
Config: hclAgentPoolResourceRequiresImport(poolName),
ExpectError: requiresImportError(poolName),
},
},
})
}
func checkAgentPoolDestroyed(s *terraform.State) error {
clients := testutils.GetProvider().Meta().(*client.AggregatedClient)
// verify that every agent pool referenced in the state does not exist in AzDO
for _, resource := range s.RootModule().Resources {
if resource.Type != "azuredevops_agent_pool" {
continue
}
id, err := strconv.Atoi(resource.Primary.ID)
if err != nil {
return fmt.Errorf("Agent Pool ID=%d cannot be parsed!. Error=%v", id, err)
}
// indicates the agent pool still exists - this should fail the test
if _, err := clients.TaskAgentClient.GetAgentPool(clients.Ctx, taskagent.GetAgentPoolArgs{PoolId: &id}); err == nil {
return fmt.Errorf("Agent Pool ID %d should not exist", id)
}
}
return nil
}
func requiresImportError(resourceName string) *regexp.Regexp {
message := "creating agent pool in Azure DevOps: Agent pool %[1]s already exists."
return regexp.MustCompile(fmt.Sprintf(message, resourceName))
}
func hclAgentPoolBasic(name string) string {
return fmt.Sprintf(`
resource "azuredevops_agent_pool" "test" {
name = "%s"
auto_provision = false
auto_update = false
pool_type = "automation"
}`, name)
}
func hclAgentPoolUpdate(name string) string {
return fmt.Sprintf(`
resource "azuredevops_agent_pool" "test" {
name = "%s"
auto_provision = true
auto_update = true
pool_type = "automation"
}`, name)
}
func hclAgentPoolResourceRequiresImport(name string) string {
return fmt.Sprintf(`
%s
resource "azuredevops_agent_pool" "import" {
name = azuredevops_agent_pool.test.name
auto_provision = azuredevops_agent_pool.test.auto_provision
auto_update = azuredevops_agent_pool.test.auto_update
pool_type = azuredevops_agent_pool.test.pool_type
}`, hclAgentPoolBasic(name))
}