@@ -4,11 +4,14 @@ import (
44 "context"
55 "io/ioutil"
66 "os"
7+ "regexp"
78 "strconv"
9+ "strings"
810 "sync"
911 "testing"
1012
1113 "github.com/Masterminds/semver/v3"
14+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1215 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1316 "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1417)
@@ -46,11 +49,143 @@ func init() {
4649}
4750
4851func TestProvider (t * testing.T ) {
52+ IsUnitTest (t )
53+
4954 if err := Provider ("dev" )().InternalValidate (); err != nil {
5055 t .Fatalf ("err: %s" , err )
5156 }
5257}
5358
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+ }
188+
54189// testAccPreCheckEnv contains all environment variables that must be present
55190// for acceptance tests to run. These are checked in testAccPreCheck.
56191var testAccPreCheckEnv = []string {
@@ -114,6 +249,14 @@ func accTestsEnabled(t *testing.T, envVarName string) bool {
114249 return enabled
115250}
116251
252+ func IsUnitTest (t * testing.T ) {
253+ t .Helper ()
254+
255+ if accTestsEnabled (t , "TF_ACC" ) {
256+ t .Skip ("Skipping acceptance tests" )
257+ }
258+ }
259+
117260func CheckOSSTestsEnabled (t * testing.T ) {
118261 t .Helper ()
119262 if ! accTestsEnabled (t , "TF_ACC_OSS" ) {
0 commit comments