This repository has been archived by the owner on Jan 8, 2021. It is now read-only.
forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprovider_test.go
315 lines (286 loc) · 9.93 KB
/
provider_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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package github
import (
"fmt"
"math/rand"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-tls/tls"
)
var testUser string = os.Getenv("GITHUB_TEST_USER")
var testCollaborator string = os.Getenv("GITHUB_TEST_COLLABORATOR")
var testOrganization string = os.Getenv("GITHUB_ORGANIZATION")
var testAccProviders map[string]terraform.ResourceProvider
var testAccProviderFactories func(providers *[]*schema.Provider) map[string]terraform.ResourceProviderFactory
var testAccProvider *schema.Provider
func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"github": testAccProvider,
"tls": tls.Provider(),
}
testAccProviderFactories = func(providers *[]*schema.Provider) map[string]terraform.ResourceProviderFactory {
return map[string]terraform.ResourceProviderFactory{
"github": func() (terraform.ResourceProvider, error) {
p := Provider()
*providers = append(*providers, p.(*schema.Provider))
return p, nil
},
}
}
}
func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}
func testAccPreCheck(t *testing.T) {
if v := os.Getenv("GITHUB_TOKEN"); v == "" {
t.Fatal("GITHUB_TOKEN must be set for acceptance tests")
}
if v := os.Getenv("GITHUB_ORGANIZATION"); v == "" {
t.Fatal("GITHUB_ORGANIZATION must be set for acceptance tests")
}
if v := os.Getenv("GITHUB_TEST_USER"); v == "" {
t.Fatal("GITHUB_TEST_USER must be set for acceptance tests")
}
if v := os.Getenv("GITHUB_TEST_COLLABORATOR"); v == "" {
t.Fatal("GITHUB_TEST_COLLABORATOR must be set for acceptance tests")
}
}
func TestProvider_individual(t *testing.T) {
username := "hashibot"
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckGithubMembershipDestroy,
Steps: []resource.TestStep{
{
// Test individual is true. Because GITHUB_ORGANIZATION should be set for these tests, we'll pass an
// empty string for `org` to unset the organization
Config: configProviderOrganization("", true) + testAccCheckGithubUserDataSourceConfig(username),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.github_user.test", "name"),
resource.TestCheckResourceAttr("data.github_user.test", "name", "HashiBot"),
),
},
{
// Test individual is true, but resource requires organization. Because GITHUB_ORGANIZATION should be
// set for these tests, we'll pass an empty string for `org` to unset the organization
Config: configProviderOrganization("", true) + testAccGithubMembershipConfig(username),
ExpectError: regexp.MustCompile("This resource requires GitHub organization to be set on the provider."),
},
{
// Test conflicting `individual` and `organization`
Config: configProviderOrganization(testOrganization, true) + testAccCheckGithubUserDataSourceConfig(username),
ExpectError: regexp.MustCompile("If `individual` is true, `organization` cannot be set."),
},
{
// Test neither `individual` or `organization` is set. Because GITHUB_ORGANIZATION should be
// set for these tests, we'll pass an empty string for `org` to unset the organization
Config: configProviderOrganization("", false) + testAccCheckGithubUserDataSourceConfig(username),
ExpectError: regexp.MustCompile("If `individual` is false, `organization` is required."),
},
},
})
}
func TestProvider_anonymous(t *testing.T) {
username := "hashibot"
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckGithubMembershipDestroy,
Steps: []resource.TestStep{
{
// Test anonymous is true. Because GITHUB_TOKEN should be set for these tests, we'll pass an
// empty string for `token` to unset the token
Config: configProviderToken("", true) + testAccCheckGithubUserDataSourceConfig(username),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.github_user.test", "name"),
resource.TestCheckResourceAttr("data.github_user.test", "name", "HashiBot"),
),
},
{
// Test conflicting `anonymous` and `token`
Config: configProviderToken(os.Getenv("GITHUB_TOKEN"), true) + testAccCheckGithubUserDataSourceConfig(username),
ExpectError: regexp.MustCompile("If `anonymous` is true, `token` cannot be set."),
},
{
// Test neither `anonymous` or `token` is set. Because GITHUB_TOKEN should be
// set for these tests, we'll pass an empty string for `token` to unset the token
Config: configProviderToken("", false) + testAccCheckGithubUserDataSourceConfig(username),
ExpectError: regexp.MustCompile("If `anonymous` is false, `token` is required."),
},
},
})
}
func TestProvider_insecure(t *testing.T) {
// Use ephemeral port range (49152–65535)
port := fmt.Sprintf("%d", 49152+rand.Intn(16382))
// Use self-signed certificate
certFile := filepath.Join("test-fixtures", "cert.pem")
keyFile := filepath.Join("test-fixtures", "key.pem")
url, closeFunc := githubTLSApiMock(port, certFile, keyFile, t)
defer func() {
err := closeFunc()
if err != nil {
t.Fatal(err)
}
}()
oldBaseUrl := os.Getenv("GITHUB_BASE_URL")
defer os.Setenv("GITHUB_BASE_URL", oldBaseUrl)
// Point provider to mock API with self-signed cert
os.Setenv("GITHUB_BASE_URL", url)
insecureProviderConfig := `provider "github" {
insecure = true
}
`
username := "hashibot"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGithubUserDataSourceConfig(username),
ExpectError: regexp.MustCompile("x509: certificate is valid for untrusted, not localhost"),
},
{
Config: insecureProviderConfig + testAccCheckGithubUserDataSourceConfig(username),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.github_user.test", "name"),
resource.TestCheckResourceAttr("data.github_user.test", "id", "1"),
resource.TestCheckResourceAttr("data.github_user.test", "name", "HashiBot"),
),
},
},
})
}
func githubTLSApiMock(port, certFile, keyFile string, t *testing.T) (string, func() error) {
mux := http.NewServeMux()
mux.HandleFunc("/users/hashibot", testRespondJson(userResponseBody))
mux.HandleFunc("/users/hashibot/gpg_keys", testRespondJson(gpgKeysResponseBody))
mux.HandleFunc("/users/hashibot/keys", testRespondJson(keysResponseBody))
server := &http.Server{
Addr: ":" + port,
Handler: mux,
}
// nolint: errcheck
go server.ListenAndServeTLS(certFile, keyFile)
return "https://localhost:" + port + "/", server.Close
}
func testRespondJson(responseBody string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write([]byte(responseBody)); err != nil {
return
}
}
}
func configProviderOrganization(org string, individual bool) string {
return fmt.Sprintf(`
provider "github" {
organization = "%s"
individual = %s
}
`, org, strconv.FormatBool(individual))
}
func configProviderToken(token string, anonymous bool) string {
return fmt.Sprintf(`
provider "github" {
token = "%s"
anonymous = %s
}
`, token, strconv.FormatBool(anonymous))
}
const userResponseBody = `{
"login": "hashibot",
"id": 1,
"node_id": "MDQ6VXNlcjE=",
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false,
"name": "HashiBot",
"company": "GitHub",
"blog": "https://github.com/blog",
"location": "San Francisco",
"email": "octocat@github.com",
"hireable": false,
"bio": "There once was...",
"public_repos": 2,
"public_gists": 1,
"followers": 20,
"following": 0,
"created_at": "2008-01-14T04:33:35Z",
"updated_at": "2008-01-14T04:33:35Z"
}`
const gpgKeysResponseBody = `[
{
"id": 3,
"primary_key_id": null,
"key_id": "3262EFF25BA0D270",
"public_key": "xsBNBFayYZ...",
"emails": [
{
"email": "mastahyeti@users.noreply.github.com",
"verified": true
}
],
"subkeys": [
{
"id": 4,
"primary_key_id": 3,
"key_id": "4A595D4C72EE49C7",
"public_key": "zsBNBFayYZ...",
"emails": [
],
"subkeys": [
],
"can_sign": false,
"can_encrypt_comms": true,
"can_encrypt_storage": true,
"can_certify": false,
"created_at": "2016-03-24T11:31:04-06:00",
"expires_at": null
}
],
"can_sign": true,
"can_encrypt_comms": false,
"can_encrypt_storage": false,
"can_certify": true,
"created_at": "2016-03-24T11:31:04-06:00",
"expires_at": null
}
]`
const keysResponseBody = `[
{
"id": 1,
"key": "ssh-rsa AAA..."
}
]`