-
Notifications
You must be signed in to change notification settings - Fork 7
/
serviceaccount_test.go
260 lines (229 loc) · 8.01 KB
/
serviceaccount_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package tests
import (
"context"
"fmt"
"regexp"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/pkg/errors"
rhoasAPI "github.com/redhat-developer/terraform-provider-rhoas/rhoas/api"
"github.com/redhat-developer/terraform-provider-rhoas/rhoas/utils"
"github.com/stretchr/testify/assert"
saclient "github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt/apiv1/client"
)
const (
serviceAccountID = "test_service_account"
serviceAccountPath = "rhoas_service_account.test_service_account"
)
// TestAccRHOASServiceAccount_Basic checks that this provider is able to create a
// service account and then destroy it.
func TestAccRHOASServiceAccount_Basic(t *testing.T) {
var serviceAccount saclient.ServiceAccountData
randomName := fmt.Sprintf("test-%s", randomString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckServiceAccountDestroy,
Steps: []resource.TestStep{
{
Config: testAccServiceAccountBasic(serviceAccountID, randomName),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceAccountExists(
&serviceAccount),
resource.TestCheckResourceAttr(
serviceAccountPath, "name", randomName),
resource.TestCheckResourceAttr(
serviceAccountPath, "description", ""),
resource.TestCheckResourceAttrSet(serviceAccountPath, "client_id"),
resource.TestCheckResourceAttrSet(serviceAccountPath, "client_secret"),
resource.TestCheckResourceAttrSet(serviceAccountPath, "created_by"),
resource.TestCheckResourceAttrSet(serviceAccountPath, "created_at"),
),
},
},
})
}
// TestAccRHOASServiceAccount_Update checks that this provider is able create a
// service account cluster and then update it. Finally, it destroys the resource.
func TestAccRHOASServiceAccount_Update(t *testing.T) {
// TODO: FIXME
t.Skip("FIXME")
randomName := fmt.Sprintf("test-%s", randomString(10))
preName := fmt.Sprintf("%s-pre", randomName)
postName := fmt.Sprintf("%s-post", randomName)
var (
// Used to compare the pre and post IDs
preServiceAccount saclient.ServiceAccountData
postServiceAccount saclient.ServiceAccountData
)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckServiceAccountDestroy,
Steps: []resource.TestStep{
{
Config: testAccServiceAccountBasic(serviceAccountID, preName),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceAccountExists(
&preServiceAccount),
resource.TestCheckResourceAttr(
serviceAccountPath, "name", preName),
),
},
{
Config: testAccServiceAccountBasic(serviceAccountID, postName),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceAccountExists(
&postServiceAccount),
resource.TestCheckResourceAttr(
serviceAccountPath, "name", postName),
testCheckServiceAccountPreAndPostIDs(&preServiceAccount, &postServiceAccount),
),
},
},
})
}
// TestAccRHOASServiceAccount_Error checks that this provider returns an error if
// some field is misconfigured
func TestAccRHOASServiceAccount_Error(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckServiceAccountDestroy,
Steps: []resource.TestStep{
{
Config: testAccServiceAccountBasic(
serviceAccountID, ""),
ExpectError: regexp.MustCompile(
"Request failed field validation",
),
},
},
})
}
// TestAccRHOASServiceAccount_DataSource checks the rhoas_service_account data source behavior
func TestAccRHOASServiceAccount_DataSource(t *testing.T) {
var serviceAccount saclient.ServiceAccountData
randomName := fmt.Sprintf("test-%s", randomString(10))
config := fmt.Sprintf(`
resource "rhoas_service_account" "%[1]s" {
name = "%[2]s"
}
data "rhoas_service_account" "test" {
id = rhoas_service_account.%[1]s.id
}`, serviceAccountID, randomName)
dataSourcePath := "data.rhoas_service_account.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckServiceAccountDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceAccountExists(&serviceAccount),
resource.TestCheckResourceAttrPair(
serviceAccountPath, "name",
dataSourcePath, "name",
),
resource.TestCheckResourceAttrPair(
serviceAccountPath, "client_id",
dataSourcePath, "client_id",
),
resource.TestCheckResourceAttrPair(
serviceAccountPath, "description",
dataSourcePath, "description",
),
resource.TestCheckResourceAttrPair(
serviceAccountPath, "id",
dataSourcePath, "id",
),
// The secret is only retrieved in the creation, not in a data source
resource.TestCheckResourceAttrSet(serviceAccountPath, "client_secret"),
resource.TestCheckResourceAttrPair(
serviceAccountPath, "created_by",
dataSourcePath, "created_by",
),
resource.TestCheckResourceAttrPair(
serviceAccountPath, "created_at",
dataSourcePath, "created_at",
),
),
},
},
})
}
// testAccCheckServiceAccountDestroy verifies the service account has been destroyed
func testAccCheckServiceAccountDestroy(s *terraform.State) error {
// retrieve the connection established in Provider configuration
factory, ok := testAccRHOAS.Meta().(rhoasAPI.Factory)
if !ok {
return errors.Errorf("unable to cast %v to rhoasAPI.Factory)", testAccRHOAS.Meta())
}
// loop through the resources in state, verifying each widget of type rhoas_kafka is destroyed
for _, rs := range s.RootModule().Resources {
if rs.Type != "rhoas_service_account" {
continue
}
// Retrieve the service account struct by referencing it's state ID for API lookup
serviceAccount, resp, err := factory.ServiceAccountMgmt().GetServiceAccount(context.Background(), rs.Primary.ID).Execute()
if resp.StatusCode == http.StatusNotFound {
return nil
}
if apiErr := utils.GetAPIError(factory, resp, err); apiErr != nil {
return apiErr
}
return errors.Errorf("expected a 404 but found a service account: %v", serviceAccount)
}
return nil
}
// testAccCheckServiceAccountDestroy verifies the service account at "rhoas_service_account.test_service_account" exists
func testAccCheckServiceAccountExists(serviceAccount *saclient.ServiceAccountData) resource.TestCheckFunc {
return func(state *terraform.State) error {
rs, ok := state.RootModule().Resources[serviceAccountPath]
if !ok {
return fmt.Errorf("Not found: %s", serviceAccountPath)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Record ID is set")
}
api, ok := testAccRHOAS.Meta().(rhoasAPI.Factory)
if !ok {
return errors.Errorf("unable to cast %v to rhoasAPI.Factory)", testAccRHOAS.Meta())
}
gotServiceAccount, resp, err := api.ServiceAccountMgmt().GetServiceAccount(context.Background(), rs.Primary.ID).Execute()
if apiErr := utils.GetAPIError(nil, resp, err); apiErr != nil {
return apiErr
}
*serviceAccount = gotServiceAccount
return nil
}
}
// needed in order to pass linting until we unskip the test that uses this function
var _ = testCheckServiceAccountPreAndPostIDs
func testCheckServiceAccountPreAndPostIDs(pre, post *saclient.ServiceAccountData) resource.TestCheckFunc {
return func(state *terraform.State) error {
if *pre.Id != *post.Id {
return errors.Errorf("expected the id to be the same - before update the id was %s, after it was %s)", *pre.Id, *post.Id)
}
return nil
}
}
func testAccServiceAccountBasic(id, name string) string {
return fmt.Sprintf(`
resource "rhoas_service_account" "%s" {
name = "%s"
}
`, id, name)
}
func Test_testAccServiceAccountBasic(t *testing.T) {
assert.Equal(
t, `
resource "rhoas_service_account" "test_id" {
name = "test_name"
}
`,
testAccServiceAccountBasic("test_id", "test_name"),
)
}