-
Notifications
You must be signed in to change notification settings - Fork 353
/
resource_databricks_group_azure_test.go
158 lines (148 loc) · 5.63 KB
/
resource_databricks_group_azure_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
package databricks
import (
"errors"
"fmt"
"github.com/databrickslabs/databricks-terraform/client/model"
"github.com/databrickslabs/databricks-terraform/client/service"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/stretchr/testify/assert"
"testing"
)
func TestAccAzureGroupResource(t *testing.T) {
var Group model.Group
// generate a random name for each tokenInfo test run, to avoid
// collisions from multiple concurrent tests.
// the acctest package includes many helpers such as RandStringFromCharSet
// See https://godoc.org/github.com/hashicorp/terraform-plugin-sdk/helper/acctest
//scope := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
randomStr := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
displayName := fmt.Sprintf("tf group test %s", randomStr)
newDisplayName := fmt.Sprintf("new tf group test %s", randomStr)
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAzureGroupResourceDestroy,
Steps: []resource.TestStep{
{
// use a dynamic configuration with the random name from above
Config: testAzureDatabricksGroup(displayName),
// compose a basic test, checking both remote and local values
Check: resource.ComposeTestCheckFunc(
// query the API to retrieve the tokenInfo object
testAzureGroupResourceExists("databricks_group.my_group", &Group, t),
// verify remote values
testAzureGroupValues(t, &Group, displayName),
// verify local values
resource.TestCheckResourceAttr("databricks_group.my_group", "display_name", displayName),
),
Destroy: false,
},
{
// use a dynamic configuration with the random name from above
Config: testAzureDatabricksGroup(newDisplayName),
// test to see if new resource is attempted to be planned
PlanOnly: true,
ExpectNonEmptyPlan: true,
Destroy: false,
},
},
})
}
func TestAccAzureGroupResource_verify_entitlements(t *testing.T) {
//var secretScope model.Secre
var Group model.Group
// generate a random name for each tokenInfo test run, to avoid
// collisions from multiple concurrent tests.
// the acctest package includes many helpers such as RandStringFromCharSet
// See https://godoc.org/github.com/hashicorp/terraform-plugin-sdk/helper/acctest
//scope := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
randomStr := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
displayName := fmt.Sprintf("tf group test %s", randomStr)
newDisplayName := fmt.Sprintf("new tf group test %s", randomStr)
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAzureGroupResourceDestroy,
Steps: []resource.TestStep{
{
// use a dynamic configuration with the random name from above
Config: testAzureDatabricksGroupEntitlements(displayName, "true", "true"),
// compose a basic test, checking both remote and local values
Check: resource.ComposeTestCheckFunc(
// query the API to retrieve the tokenInfo object
testAzureGroupResourceExists("databricks_group.my_group", &Group, t),
// verify remote values
testAzureGroupValues(t, &Group, displayName),
// verify local values
resource.TestCheckResourceAttr("databricks_group.my_group", "allow_cluster_create", "true"),
resource.TestCheckResourceAttr("databricks_group.my_group", "allow_instance_pool_create", "true"),
),
Destroy: false,
},
// Remove entitlements and expect a non empty plan
{
// use a dynamic configuration with the random name from above
Config: testAzureDatabricksGroup(newDisplayName),
// test to see if new resource is attempted to be planned
PlanOnly: true,
ExpectNonEmptyPlan: true,
Destroy: false,
},
},
})
}
func testAzureGroupResourceDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*service.DBApiClient)
for _, rs := range s.RootModule().Resources {
if rs.Type != "databricks_group" {
continue
}
_, err := client.Users().Read(rs.Primary.ID)
if err != nil {
return nil
}
return errors.New("resource Group is not cleaned up")
}
return nil
}
func testAzureGroupValues(t *testing.T, group *model.Group, displayName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
assert.True(t, group.DisplayName == displayName)
return nil
}
}
// testAccCheckTokenResourceExists queries the API and retrieves the matching Widget.
func testAzureGroupResourceExists(n string, group *model.Group, t *testing.T) resource.TestCheckFunc {
return func(s *terraform.State) error {
// find the corresponding state object
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
// retrieve the configured client from the test setup
conn := testAccProvider.Meta().(*service.DBApiClient)
resp, err := conn.Groups().Read(rs.Primary.ID)
if err != nil {
return err
}
// If no error, assign the response Widget attribute to the widget pointer
*group = resp
return nil
}
}
func testAzureDatabricksGroup(groupName string) string {
return fmt.Sprintf(`
resource "databricks_group" "my_group" {
display_name = "%s"
}
`, groupName)
}
func testAzureDatabricksGroupEntitlements(groupName, allowClusterCreate, allowPoolCreate string) string {
return fmt.Sprintf(`
resource "databricks_group" "my_group" {
display_name = "%s"
allow_cluster_create = %s
allow_instance_pool_create = %s
}
`, groupName, allowClusterCreate, allowPoolCreate)
}