@@ -4,11 +4,14 @@ import (
4
4
"context"
5
5
"io/ioutil"
6
6
"os"
7
+ "regexp"
7
8
"strconv"
9
+ "strings"
8
10
"sync"
9
11
"testing"
10
12
11
13
"github.com/Masterminds/semver/v3"
14
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12
15
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13
16
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
14
17
)
@@ -46,11 +49,142 @@ func init() {
46
49
}
47
50
48
51
func TestProvider (t * testing.T ) {
52
+ IsUnitTest (t )
53
+
49
54
if err := Provider ("dev" )().InternalValidate (); err != nil {
50
55
t .Fatalf ("err: %s" , err )
51
56
}
52
57
}
53
58
59
+ func TestProviderConfigure (t * testing.T ) {
60
+ IsUnitTest (t )
61
+
62
+ // Helper for header tests
63
+ checkHeaders := func (t * testing.T , provider * schema.Provider ) {
64
+ gotHeaders := provider .Meta ().(* client ).gapiConfig .HTTPHeaders
65
+ if len (gotHeaders ) != 2 {
66
+ t .Errorf ("expected 2 HTTP header, got %d" , len (gotHeaders ))
67
+ }
68
+ if gotHeaders ["Authorization" ] != "Bearer test" {
69
+ t .Errorf ("expected HTTP header Authorization to be \" Bearer test\" , got %q" , gotHeaders ["Authorization" ])
70
+ }
71
+ if gotHeaders ["X-Custom-Header" ] != "custom-value" {
72
+ t .Errorf ("expected HTTP header X-Custom-Header to be \" custom-value\" , got %q" , gotHeaders ["X-Custom-Header" ])
73
+ }
74
+ }
75
+
76
+ envBackup := os .Environ ()
77
+ defer func () {
78
+ os .Clearenv ()
79
+ for _ , v := range envBackup {
80
+ kv := strings .SplitN (v , "=" , 2 )
81
+ os .Setenv (kv [0 ], kv [1 ])
82
+ }
83
+ }()
84
+
85
+ cases := []struct {
86
+ name string
87
+ config map [string ]interface {}
88
+ env map [string ]string
89
+ expectedErr string
90
+ check func (t * testing.T , provider * schema.Provider )
91
+ }{
92
+ {
93
+ name : "no config" ,
94
+ env : map [string ]string {},
95
+ expectedErr : "\" auth\" : one of `auth,cloud_api_key,sm_access_token` must be specified" ,
96
+ },
97
+ {
98
+ name : "grafana config from env" ,
99
+ env : map [string ]string {
100
+ "GRAFANA_AUTH" : "admin:admin" ,
101
+ "GRAFANA_URL" : "https://test.com" ,
102
+ },
103
+ },
104
+ {
105
+ name : "header config" ,
106
+ env : map [string ]string {
107
+ "GRAFANA_AUTH" : "admin:admin" ,
108
+ "GRAFANA_URL" : "https://test.com" ,
109
+ },
110
+ config : map [string ]interface {}{
111
+ "http_headers" : map [string ]interface {}{
112
+ "Authorization" : "Bearer test" ,
113
+ "X-Custom-Header" : "custom-value" ,
114
+ },
115
+ },
116
+ check : checkHeaders ,
117
+ },
118
+ {
119
+ name : "header config from env" ,
120
+ env : map [string ]string {
121
+ "GRAFANA_AUTH" : "admin:admin" ,
122
+ "GRAFANA_URL" : "https://test.com" ,
123
+ "GRAFANA_HTTP_HEADERS" : `{"X-Custom-Header": "custom-value", "Authorization": "Bearer test"}` ,
124
+ },
125
+ check : checkHeaders ,
126
+ },
127
+ {
128
+ name : "invalid header" ,
129
+ env : map [string ]string {
130
+ "GRAFANA_AUTH" : "admin:admin" ,
131
+ "GRAFANA_URL" : "https://test.com" ,
132
+ "GRAFANA_HTTP_HEADERS" : `blabla` ,
133
+ },
134
+ expectedErr : "invalid http_headers config: invalid character 'b' looking for beginning of value" ,
135
+ },
136
+ {
137
+ name : "grafana cloud config from env" ,
138
+ env : map [string ]string {
139
+ "GRAFANA_CLOUD_API_KEY" : "testtest" ,
140
+ },
141
+ },
142
+ {
143
+ name : "grafana sm config from env" ,
144
+ env : map [string ]string {
145
+ "GRAFANA_SM_ACCESS_TOKEN" : "testtest" ,
146
+ },
147
+ },
148
+ }
149
+
150
+ for _ , tc := range cases {
151
+ t .Run (tc .name , func (t * testing.T ) {
152
+ os .Clearenv ()
153
+ for k , v := range tc .env {
154
+ os .Setenv (k , v )
155
+ }
156
+
157
+ test := resource.TestStep {
158
+ // Resource is irrelevant, it's just there to test the provider being configured
159
+ // Terraform will "validate" the provider, but not actually use it when planning
160
+ PlanOnly : true ,
161
+ ExpectNonEmptyPlan : true ,
162
+ Config : `resource "grafana_folder" "test" {
163
+ title = "test"
164
+ }` ,
165
+ }
166
+
167
+ if tc .expectedErr != "" {
168
+ test .ExpectError = regexp .MustCompile (tc .expectedErr )
169
+ }
170
+
171
+ // Configure the provider and check it
172
+ provider := Provider ("dev" )()
173
+ provider .Configure (context .Background (), terraform .NewResourceConfigRaw (tc .config ))
174
+ if tc .check != nil {
175
+ tc .check (t , provider )
176
+ }
177
+ // Run the plan to check for validation errors
178
+ resource .UnitTest (t , resource.TestCase {
179
+ Providers : map [string ]* schema.Provider {
180
+ "grafana" : provider ,
181
+ },
182
+ Steps : []resource.TestStep {test },
183
+ })
184
+ })
185
+ }
186
+ }
187
+
54
188
// testAccPreCheckEnv contains all environment variables that must be present
55
189
// for acceptance tests to run. These are checked in testAccPreCheck.
56
190
var testAccPreCheckEnv = []string {
@@ -114,6 +248,14 @@ func accTestsEnabled(t *testing.T, envVarName string) bool {
114
248
return enabled
115
249
}
116
250
251
+ func IsUnitTest (t * testing.T ) {
252
+ t .Helper ()
253
+
254
+ if accTestsEnabled (t , "TF_ACC" ) {
255
+ t .Skip ("Skipping acceptance tests" )
256
+ }
257
+ }
258
+
117
259
func CheckOSSTestsEnabled (t * testing.T ) {
118
260
t .Helper ()
119
261
if ! accTestsEnabled (t , "TF_ACC_OSS" ) {
0 commit comments