6
6
"testing"
7
7
8
8
providerSchema "github.com./elastic/terraform-provider-elasticstack/internal/schema"
9
+ "github.com./hashicorp/terraform-plugin-framework/types"
9
10
"github.com./hashicorp/terraform-plugin-sdk/v2/helper/schema"
10
11
"github.com./stretchr/testify/require"
11
12
)
@@ -27,11 +28,62 @@ func TestNewBaseConfigFromSDK(t *testing.T) {
27
28
})
28
29
29
30
baseCfg := newBaseConfigFromSDK (rd , "unit-testing" , "elasticsearch" )
30
- ua := buildUserAgent ( " unit-testing")
31
+ ua := "elasticstack-terraform-provider/ unit-testing"
31
32
require .Equal (t , baseConfig {
32
33
Username : "elastic" ,
33
34
Password : "changeme" ,
34
35
UserAgent : ua ,
35
36
Header : http.Header {"User-Agent" : []string {ua }},
36
37
}, baseCfg )
37
38
}
39
+
40
+ func TestNewBaseConfigFromFramework (t * testing.T ) {
41
+ os .Unsetenv ("ELASTICSEARCH_USERNAME" )
42
+ os .Unsetenv ("ELASTICSEARCH_PASSWORD" )
43
+ os .Unsetenv ("ELASTICSEARCH_API_KEY" )
44
+
45
+ expectedUA := "elasticstack-terraform-provider/unit-testing"
46
+
47
+ tests := []struct {
48
+ name string
49
+ config ProviderConfiguration
50
+ expectedBaseConfig baseConfig
51
+ }{
52
+ {
53
+ name : "with es config defined" ,
54
+ config : ProviderConfiguration {
55
+ Elasticsearch : []ElasticsearchConnection {
56
+ {
57
+ Username : types .StringValue ("elastic" ),
58
+ Password : types .StringValue ("changeme" ),
59
+ APIKey : types .StringValue ("apikey" ),
60
+ },
61
+ },
62
+ },
63
+ expectedBaseConfig : baseConfig {
64
+ Username : "elastic" ,
65
+ Password : "changeme" ,
66
+ ApiKey : "apikey" ,
67
+ UserAgent : expectedUA ,
68
+ Header : http.Header {"User-Agent" : []string {expectedUA }},
69
+ },
70
+ },
71
+ {
72
+ name : "should not set credentials if no configuration available" ,
73
+ config : ProviderConfiguration {
74
+ Elasticsearch : []ElasticsearchConnection {},
75
+ },
76
+ expectedBaseConfig : baseConfig {
77
+ UserAgent : expectedUA ,
78
+ Header : http.Header {"User-Agent" : []string {expectedUA }},
79
+ },
80
+ },
81
+ }
82
+
83
+ for _ , tt := range tests {
84
+ t .Run (tt .name , func (t * testing.T ) {
85
+ baseCfg := newBaseConfigFromFramework (tt .config , "unit-testing" )
86
+ require .Equal (t , tt .expectedBaseConfig , baseCfg )
87
+ })
88
+ }
89
+ }
0 commit comments