-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathconfig_attributes_test.go
More file actions
166 lines (158 loc) · 3.94 KB
/
config_attributes_test.go
File metadata and controls
166 lines (158 loc) · 3.94 KB
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
package config
import (
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
)
// TestConfigFile_Configure_ListParsing tests that comma-separated list values
// in configuration files are correctly parsed into slices.
func TestConfigFile_Configure_ListParsing(t *testing.T) {
testCases := []struct {
name string
profile string
want []string
}{
{
name: "single item",
profile: "single-item",
want: []string{"clusters"},
},
{
name: "multiple items",
profile: "multiple-items",
want: []string{"alpha", "beta", "gamma"},
},
{
name: "whitespace around items is trimmed",
profile: "whitespace-around-items",
want: []string{"alpha", "beta", "gamma"},
},
{
name: "empty items are filtered out",
profile: "empty-items-filtered",
want: []string{"alpha", "beta"},
},
{
name: "whitespace-only items are filtered out",
profile: "whitespace-only-items-filtered",
want: []string{"alpha", "beta"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
withMockEnv(t, map[string]string{})
cfg := &Config{
Profile: tc.profile,
ConfigFile: "testdata/list-parsing/.databrickscfg",
}
err := ConfigFile.Configure(cfg)
if err != nil {
t.Fatalf("Configure failed: %v", err)
}
if diff := cmp.Diff(tc.want, cfg.Scopes); diff != "" {
t.Errorf("list mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestConfigAttribute_GetString(t *testing.T) {
findAttr := func(name string) *ConfigAttribute {
for i := range ConfigAttributes {
if ConfigAttributes[i].Name == name {
return &ConfigAttributes[i]
}
}
t.Fatalf("attribute %q not found", name)
return nil
}
testCases := []struct {
name string
attrName string
wantKind reflect.Kind
cfg *Config
want string
}{
{
name: "string type returns string value",
attrName: "host",
wantKind: reflect.String,
cfg: &Config{Host: "https://example.databricks.com"},
want: "https://example.databricks.com",
},
{
name: "string type returns empty string when not set",
attrName: "host",
wantKind: reflect.String,
cfg: &Config{},
want: "",
},
{
name: "bool type returns true",
attrName: "skip_verify",
wantKind: reflect.Bool,
cfg: &Config{InsecureSkipVerify: true},
want: "true",
},
{
name: "bool type returns false",
attrName: "skip_verify",
wantKind: reflect.Bool,
cfg: &Config{InsecureSkipVerify: false},
want: "false",
},
{
name: "int type returns numeric string",
attrName: "http_timeout_seconds",
wantKind: reflect.Int,
cfg: &Config{HTTPTimeoutSeconds: 120},
want: "120",
},
{
name: "int type returns zero",
attrName: "http_timeout_seconds",
wantKind: reflect.Int,
cfg: &Config{HTTPTimeoutSeconds: 0},
want: "0",
},
{
name: "slice type returns comma-joined string",
attrName: "scopes",
wantKind: reflect.Slice,
cfg: &Config{Scopes: []string{"alpha", "beta", "gamma"}},
want: "alpha,beta,gamma",
},
{
name: "slice type returns single item",
attrName: "scopes",
wantKind: reflect.Slice,
cfg: &Config{Scopes: []string{"all-apis"}},
want: "all-apis",
},
{
name: "slice type returns empty string for nil slice",
attrName: "scopes",
wantKind: reflect.Slice,
cfg: &Config{Scopes: nil},
want: "",
},
{
name: "slice type returns empty string for empty slice",
attrName: "scopes",
wantKind: reflect.Slice,
cfg: &Config{Scopes: []string{}},
want: "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
attr := findAttr(tc.attrName)
if attr.Kind != tc.wantKind {
t.Fatalf("expected %v kind, got %v", tc.wantKind, attr.Kind)
}
got := attr.GetString(tc.cfg)
if got != tc.want {
t.Errorf("GetString() = %q, want %q", got, tc.want)
}
})
}
}