-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathbundle_helpers_test.go
More file actions
240 lines (203 loc) · 6.95 KB
/
bundle_helpers_test.go
File metadata and controls
240 lines (203 loc) · 6.95 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
package apps
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
"github.com/databricks/databricks-sdk-go/service/apps"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)
func TestIsIdempotencyError(t *testing.T) {
t.Run("returns true when error contains keyword", func(t *testing.T) {
err := errors.New("app is already in ACTIVE state")
assert.True(t, isIdempotencyError(err, "ACTIVE state"))
})
t.Run("returns true when error contains any keyword", func(t *testing.T) {
err := errors.New("already running")
assert.True(t, isIdempotencyError(err, "ACTIVE state", "already"))
})
t.Run("returns false when error does not contain keywords", func(t *testing.T) {
err := errors.New("something went wrong")
assert.False(t, isIdempotencyError(err, "ACTIVE state", "already"))
})
t.Run("returns false for nil error", func(t *testing.T) {
assert.False(t, isIdempotencyError(nil, "ACTIVE state"))
})
t.Run("matches partial strings", func(t *testing.T) {
err := errors.New("error: ACTIVE state detected")
assert.True(t, isIdempotencyError(err, "ACTIVE state"))
})
}
func TestFormatAppStatusMessage(t *testing.T) {
t.Run("handles nil appInfo", func(t *testing.T) {
msg := formatAppStatusMessage(nil, "test-app", "started")
assert.Equal(t, "✔ App 'test-app' status: unknown", msg)
})
t.Run("handles unavailable app state", func(t *testing.T) {
appInfo := &apps.App{
AppStatus: &apps.ApplicationStatus{
State: apps.ApplicationStateUnavailable,
},
ComputeStatus: &apps.ComputeStatus{
State: apps.ComputeStateActive,
},
}
msg := formatAppStatusMessage(appInfo, "test-app", "started")
assert.Contains(t, msg, "unavailable")
assert.Contains(t, msg, "ACTIVE")
})
t.Run("formats active state with 'is deployed' verb", func(t *testing.T) {
appInfo := &apps.App{
ComputeStatus: &apps.ComputeStatus{
State: apps.ComputeStateActive,
},
}
msg := formatAppStatusMessage(appInfo, "test-app", "is deployed")
assert.Contains(t, msg, "already running")
assert.Contains(t, msg, "ACTIVE")
})
t.Run("formats active state with 'started' verb", func(t *testing.T) {
appInfo := &apps.App{
ComputeStatus: &apps.ComputeStatus{
State: apps.ComputeStateActive,
},
}
msg := formatAppStatusMessage(appInfo, "test-app", "started")
assert.Contains(t, msg, "started successfully")
assert.Contains(t, msg, "ACTIVE")
})
t.Run("formats starting state", func(t *testing.T) {
appInfo := &apps.App{
ComputeStatus: &apps.ComputeStatus{
State: apps.ComputeStateStarting,
},
}
msg := formatAppStatusMessage(appInfo, "test-app", "started")
assert.Contains(t, msg, "already starting")
assert.Contains(t, msg, "STARTING")
})
t.Run("formats other compute states", func(t *testing.T) {
appInfo := &apps.App{
ComputeStatus: &apps.ComputeStatus{
State: apps.ComputeStateStopped,
},
}
msg := formatAppStatusMessage(appInfo, "test-app", "stopped")
assert.Contains(t, msg, "status: STOPPED")
})
t.Run("handles nil compute status", func(t *testing.T) {
appInfo := &apps.App{}
msg := formatAppStatusMessage(appInfo, "test-app", "started")
assert.Equal(t, "✔ App 'test-app' status: unknown", msg)
})
}
func TestInferAppNameHint(t *testing.T) {
t.Run("returns empty when no app config exists", func(t *testing.T) {
t.Chdir(t.TempDir())
assert.Equal(t, "", inferAppNameHint())
})
t.Run("returns dir name when app.yml exists", func(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
err := os.WriteFile(filepath.Join(dir, "app.yml"), []byte("command: [\"python\"]"), 0o644)
assert.NoError(t, err)
assert.Equal(t, filepath.Base(dir), inferAppNameHint())
})
t.Run("returns dir name when app.yaml exists", func(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
err := os.WriteFile(filepath.Join(dir, "app.yaml"), []byte("command: [\"python\"]"), 0o644)
assert.NoError(t, err)
assert.Equal(t, filepath.Base(dir), inferAppNameHint())
})
t.Run("returns empty when cwd has been deleted", func(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
os.Remove(dir)
assert.Equal(t, "", inferAppNameHint())
})
}
func TestMissingAppNameError(t *testing.T) {
t.Run("includes APP_NAME and usage info", func(t *testing.T) {
t.Chdir(t.TempDir())
err := missingAppNameError()
assert.Contains(t, err.Error(), "APP_NAME")
assert.Contains(t, err.Error(), "databricks.yml")
assert.NotContains(t, err.Error(), "Did you mean")
})
t.Run("includes hint when app.yml exists", func(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
writeErr := os.WriteFile(filepath.Join(dir, "app.yml"), []byte("command: [\"python\"]"), 0o644)
assert.NoError(t, writeErr)
err := missingAppNameError()
assert.Contains(t, err.Error(), "Did you mean")
assert.Contains(t, err.Error(), filepath.Base(dir))
})
t.Run("gracefully handles deleted cwd", func(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
os.Remove(dir)
err := missingAppNameError()
assert.Contains(t, err.Error(), "APP_NAME")
assert.NotContains(t, err.Error(), "Did you mean")
})
}
func TestMakeArgsOptionalWithBundle(t *testing.T) {
t.Run("updates command usage", func(t *testing.T) {
cmd := &cobra.Command{}
makeArgsOptionalWithBundle(cmd, "test [NAME]")
assert.Equal(t, "test [NAME]", cmd.Use)
})
t.Run("sets Args validator", func(t *testing.T) {
cmd := &cobra.Command{}
makeArgsOptionalWithBundle(cmd, "test [NAME]")
assert.NotNil(t, cmd.Args)
})
}
func TestGetAppNameFromArgs(t *testing.T) {
t.Run("returns arg when provided", func(t *testing.T) {
cmd := &cobra.Command{}
name, fromBundle, err := getAppNameFromArgs(cmd, []string{"my-app"})
assert.NoError(t, err)
assert.Equal(t, "my-app", name)
assert.False(t, fromBundle)
})
}
func TestUpdateCommandHelp(t *testing.T) {
t.Run("sets Long help text", func(t *testing.T) {
cmd := &cobra.Command{}
updateCommandHelp(cmd, "Start", "start")
assert.NotEmpty(t, cmd.Long)
})
t.Run("includes verb in help text", func(t *testing.T) {
cmd := &cobra.Command{}
updateCommandHelp(cmd, "Start", "start")
assert.Contains(t, cmd.Long, "Start an app")
})
t.Run("includes command name in examples", func(t *testing.T) {
cmd := &cobra.Command{}
updateCommandHelp(cmd, "Stop", "stop")
assert.Contains(t, cmd.Long, "databricks apps stop")
})
t.Run("includes all example scenarios", func(t *testing.T) {
cmd := &cobra.Command{}
updateCommandHelp(cmd, "Start", "start")
assert.Contains(t, cmd.Long, "from a project directory")
assert.Contains(t, cmd.Long, "--target prod")
assert.Contains(t, cmd.Long, "my-app")
})
}
func TestHandleAlreadyInStateError(t *testing.T) {
t.Run("returns false when not an idempotency error", func(t *testing.T) {
err := errors.New("some other error")
cmd := &cobra.Command{}
mockWrapper := func(cmd *cobra.Command, appName string, err error) error {
return err
}
handled, _ := handleAlreadyInStateError(context.Background(), cmd, err, "test-app", []string{"ACTIVE"}, "is deployed", mockWrapper)
assert.False(t, handled)
})
}