-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathbundle_test.go
More file actions
315 lines (250 loc) · 9.2 KB
/
Copy pathbundle_test.go
File metadata and controls
315 lines (250 loc) · 9.2 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
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 root
import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/databricks/cli/internal/testutil"
"github.com/databricks/cli/libs/cmdctx"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/logdiag"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func setupDatabricksCfg(t *testing.T) {
tempHomeDir := t.TempDir()
homeEnvVar := "HOME"
if runtime.GOOS == "windows" {
homeEnvVar = "USERPROFILE"
}
cfg := []byte("[PROFILE-1]\nhost = https://a.com\ntoken = a\n[PROFILE-2]\nhost = https://a.com\ntoken = b\n")
err := os.WriteFile(filepath.Join(tempHomeDir, ".databrickscfg"), cfg, 0o644)
assert.NoError(t, err)
t.Setenv("DATABRICKS_CONFIG_FILE", "")
t.Setenv(homeEnvVar, tempHomeDir)
}
func emptyCommand(t *testing.T) *cobra.Command {
ctx := t.Context()
ctx = cmdio.MockDiscard(ctx)
cmd := &cobra.Command{}
cmd.SetContext(ctx)
initProfileFlag(cmd)
return cmd
}
func setupWithHost(t *testing.T, cmd *cobra.Command, host string) []diag.Diagnostic {
setupDatabricksCfg(t)
rootPath := t.TempDir()
t.Chdir(rootPath)
contents := fmt.Sprintf(`
workspace:
host: %q
`, host)
err := os.WriteFile(filepath.Join(rootPath, "databricks.yml"), []byte(contents), 0o644)
require.NoError(t, err)
ctx := logdiag.InitContext(cmd.Context())
logdiag.SetCollect(ctx, true)
cmd.SetContext(ctx)
_ = MustConfigureBundle(cmd)
return logdiag.FlushCollected(ctx)
}
func setupWithProfile(t *testing.T, cmd *cobra.Command, profile string) []diag.Diagnostic {
setupDatabricksCfg(t)
rootPath := t.TempDir()
t.Chdir(rootPath)
contents := fmt.Sprintf(`
workspace:
profile: %q
`, profile)
err := os.WriteFile(filepath.Join(rootPath, "databricks.yml"), []byte(contents), 0o644)
require.NoError(t, err)
ctx := logdiag.InitContext(cmd.Context())
logdiag.SetCollect(ctx, true)
cmd.SetContext(ctx)
_ = MustConfigureBundle(cmd)
return logdiag.FlushCollected(ctx)
}
func TestBundleConfigureDefault(t *testing.T) {
testutil.CleanupEnvironment(t)
// Restrict PATH to system directories to prevent the SDK from invoking
// external tools (e.g. az) during auth resolution.
// Bundle loading requires a shell so PATH cannot be fully cleared.
if runtime.GOOS == "windows" {
t.Setenv("PATH", `C:\Windows\System32`)
} else {
t.Setenv("PATH", "/usr/bin:/bin")
}
cmd := emptyCommand(t)
diags := setupWithHost(t, cmd, "https://x.com")
require.Empty(t, diags)
assert.Equal(t, "https://x.com", cmdctx.ConfigUsed(cmd.Context()).Host)
}
func TestBundleConfigureWithMultipleMatches(t *testing.T) {
testutil.CleanupEnvironment(t)
cmd := emptyCommand(t)
diags := setupWithHost(t, cmd, "https://a.com")
require.Len(t, diags, 1)
assert.Contains(t, diags[0].Summary, "multiple profiles matched: PROFILE-1, PROFILE-2")
assert.Contains(t, diags[0].Summary, "Matching workspace profiles: PROFILE-1, PROFILE-2")
assert.Contains(t, diags[0].Summary, "DATABRICKS_CONFIG_PROFILE=PROFILE-1")
}
func TestBundleConfigureWithNonExistentProfileFlag(t *testing.T) {
testutil.CleanupEnvironment(t)
cmd := emptyCommand(t)
err := cmd.Flag("profile").Value.Set("NOEXIST")
require.NoError(t, err)
diags := setupWithHost(t, cmd, "https://x.com")
require.Len(t, diags, 1)
assert.Contains(t, diags[0].Summary, "has no NOEXIST profile configured")
}
func TestBundleConfigureWithMismatchedProfile(t *testing.T) {
testutil.CleanupEnvironment(t)
cmd := emptyCommand(t)
err := cmd.Flag("profile").Value.Set("PROFILE-1")
require.NoError(t, err)
diags := setupWithHost(t, cmd, "https://x.com")
assert.Equal(t, []diag.Diagnostic{{Summary: "cannot resolve bundle auth configuration: the host in the profile (https://a.com) doesn’t match the host configured in the bundle (https://x.com)"}}, diags)
}
func TestBundleConfigureWithCorrectProfile(t *testing.T) {
testutil.CleanupEnvironment(t)
cmd := emptyCommand(t)
err := cmd.Flag("profile").Value.Set("PROFILE-1")
require.NoError(t, err)
diags := setupWithHost(t, cmd, "https://a.com")
require.Empty(t, diags)
assert.Equal(t, "https://a.com", cmdctx.ConfigUsed(cmd.Context()).Host)
assert.Equal(t, "PROFILE-1", cmdctx.ConfigUsed(cmd.Context()).Profile)
}
func TestBundleConfigureWithMismatchedProfileEnvVariable(t *testing.T) {
testutil.CleanupEnvironment(t)
t.Setenv("DATABRICKS_CONFIG_PROFILE", "PROFILE-1")
cmd := emptyCommand(t)
diags := setupWithHost(t, cmd, "https://x.com")
assert.Equal(t, []diag.Diagnostic{{Summary: "cannot resolve bundle auth configuration: the host in the profile (https://a.com) doesn’t match the host configured in the bundle (https://x.com)"}}, diags)
}
func TestBundleConfigureWithProfileFlagAndEnvVariable(t *testing.T) {
testutil.CleanupEnvironment(t)
t.Setenv("DATABRICKS_CONFIG_PROFILE", "NOEXIST")
cmd := emptyCommand(t)
err := cmd.Flag("profile").Value.Set("PROFILE-1")
require.NoError(t, err)
diags := setupWithHost(t, cmd, "https://a.com")
require.Empty(t, diags)
assert.Equal(t, "https://a.com", cmdctx.ConfigUsed(cmd.Context()).Host)
assert.Equal(t, "PROFILE-1", cmdctx.ConfigUsed(cmd.Context()).Profile)
}
func TestBundleConfigureProfileDefault(t *testing.T) {
testutil.CleanupEnvironment(t)
// The profile in the databricks.yml file is used
cmd := emptyCommand(t)
diags := setupWithProfile(t, cmd, "PROFILE-1")
require.Empty(t, diags)
assert.Equal(t, "https://a.com", cmdctx.ConfigUsed(cmd.Context()).Host)
assert.Equal(t, "a", cmdctx.ConfigUsed(cmd.Context()).Token)
assert.Equal(t, "PROFILE-1", cmdctx.ConfigUsed(cmd.Context()).Profile)
}
func TestBundleConfigureProfileFlag(t *testing.T) {
testutil.CleanupEnvironment(t)
// The --profile flag takes precedence over the profile in the databricks.yml file
cmd := emptyCommand(t)
err := cmd.Flag("profile").Value.Set("PROFILE-2")
require.NoError(t, err)
diags := setupWithProfile(t, cmd, "PROFILE-1")
require.Empty(t, diags)
assert.Equal(t, "https://a.com", cmdctx.ConfigUsed(cmd.Context()).Host)
assert.Equal(t, "b", cmdctx.ConfigUsed(cmd.Context()).Token)
assert.Equal(t, "PROFILE-2", cmdctx.ConfigUsed(cmd.Context()).Profile)
}
func TestBundleConfigureProfileEnvVariable(t *testing.T) {
testutil.CleanupEnvironment(t)
// The DATABRICKS_CONFIG_PROFILE environment variable takes precedence over the profile in the databricks.yml file
t.Setenv("DATABRICKS_CONFIG_PROFILE", "PROFILE-2")
cmd := emptyCommand(t)
diags := setupWithProfile(t, cmd, "PROFILE-1")
require.Empty(t, diags)
assert.Equal(t, "https://a.com", cmdctx.ConfigUsed(cmd.Context()).Host)
assert.Equal(t, "b", cmdctx.ConfigUsed(cmd.Context()).Token)
assert.Equal(t, "PROFILE-2", cmdctx.ConfigUsed(cmd.Context()).Profile)
}
func TestBundleConfigureProfileFlagAndEnvVariable(t *testing.T) {
testutil.CleanupEnvironment(t)
// The --profile flag takes precedence over the DATABRICKS_CONFIG_PROFILE environment variable
t.Setenv("DATABRICKS_CONFIG_PROFILE", "NOEXIST")
cmd := emptyCommand(t)
err := cmd.Flag("profile").Value.Set("PROFILE-2")
require.NoError(t, err)
diags := setupWithProfile(t, cmd, "PROFILE-1")
require.Empty(t, diags)
assert.Equal(t, "https://a.com", cmdctx.ConfigUsed(cmd.Context()).Host)
assert.Equal(t, "b", cmdctx.ConfigUsed(cmd.Context()).Token)
assert.Equal(t, "PROFILE-2", cmdctx.ConfigUsed(cmd.Context()).Profile)
}
func TestBundleConfigureMultiMatchInteractivePromptFires(t *testing.T) {
testutil.CleanupEnvironment(t)
setupDatabricksCfg(t)
rootPath := t.TempDir()
t.Chdir(rootPath)
contents := `
workspace:
host: "https://a.com"
`
err := os.WriteFile(filepath.Join(rootPath, "databricks.yml"), []byte(contents), 0o644)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second)
defer cancel()
ctx, io := cmdio.SetupTest(ctx, cmdio.TestOptions{PromptSupported: true})
defer io.Done()
cmd := &cobra.Command{}
cmd.SetContext(ctx)
initProfileFlag(cmd)
done := make(chan struct{})
go func() {
defer close(done)
ctx := logdiag.InitContext(cmd.Context())
logdiag.SetCollect(ctx, true)
cmd.SetContext(ctx)
_ = MustConfigureBundle(cmd)
}()
// Verify the prompt fires by reading output from stderr.
// cmdio.RunSelect with StartInSearchMode writes a search cursor first.
line, _, readErr := io.Stderr.ReadLine()
if assert.NoError(t, readErr, "expected prompt output on stderr") {
assert.Contains(t, string(line), "Search:")
}
// Cancel to unblock the prompt.
cancel()
<-done
}
func TestTargetFlagFull(t *testing.T) {
cmd := emptyCommand(t)
initTargetFlag(cmd)
cmd.SetArgs([]string{"version", "--target", "development"})
ctx := t.Context()
err := Execute(ctx, cmd)
assert.NoError(t, err)
assert.Equal(t, "development", getTarget(cmd))
}
func TestTargetFlagShort(t *testing.T) {
cmd := emptyCommand(t)
initTargetFlag(cmd)
cmd.SetArgs([]string{"version", "-t", "production"})
ctx := t.Context()
err := Execute(ctx, cmd)
assert.NoError(t, err)
assert.Equal(t, "production", getTarget(cmd))
}
// TODO: remove when environment flag is fully deprecated
func TestTargetEnvironmentFlag(t *testing.T) {
cmd := emptyCommand(t)
initTargetFlag(cmd)
initEnvironmentFlag(cmd)
cmd.SetArgs([]string{"version", "--environment", "development"})
ctx := t.Context()
err := Execute(ctx, cmd)
assert.NoError(t, err)
assert.Equal(t, "development", getTarget(cmd))
}