-
Notifications
You must be signed in to change notification settings - Fork 423
/
Copy pathmounts_test.go
368 lines (334 loc) · 10.5 KB
/
mounts_test.go
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package storage
import (
"context"
"fmt"
"testing"
"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/databricks-sdk-go/client"
"github.com/databricks/databricks-sdk-go/config"
"github.com/databricks/databricks-sdk-go/service/compute"
"github.com/databricks/terraform-provider-databricks/clusters"
"github.com/databricks/terraform-provider-databricks/commands"
"github.com/databricks/terraform-provider-databricks/qa"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/stretchr/testify/assert"
)
func TestValidateMountDirectory(t *testing.T) {
testCases := []struct {
directory string
errorCount int
}{
{"", 0},
{"/directory", 0},
{"directory", 1},
}
for _, tc := range testCases {
_, errs := ValidateMountDirectory(tc.directory, "key")
assert.Lenf(t, errs, tc.errorCount, "directory '%s' does not generate the expected error count", tc.directory)
}
}
const expectedCommandResp = "done"
func testMountFuncHelper(t *testing.T, mountFunc func(mp MountPoint, mount Mount) (string, error), mount Mount,
mountName, expectedCommand string) {
c := common.DatabricksClient{
DatabricksClient: &client.DatabricksClient{
Config: &config.Config{
Host: ".",
Token: ".",
},
},
}
var called bool
c.WithCommandMock(func(commandStr string) common.CommandResults {
called = true
assert.Equal(t, commands.TrimLeadingWhitespace(expectedCommand), commands.TrimLeadingWhitespace(commandStr))
return common.CommandResults{
ResultType: "text",
Data: expectedCommandResp,
}
})
ctx := context.Background()
mp := MountPoint{
Exec: c.CommandExecutor(ctx),
ClusterID: "random_cluster_id",
Name: mountName,
}
resp, err := mountFunc(mp, mount)
assert.NoError(t, err)
assert.True(t, called, "mocked command was not invoked")
assert.Equal(t, expectedCommandResp, resp)
}
type mockMount struct{}
func (t mockMount) Source(_ *common.DatabricksClient) string { return "fake-mount" }
func (t mockMount) Name() string { return "fake-mount" }
func (t mockMount) Config(client *common.DatabricksClient) map[string]string {
return map[string]string{"fake-key": "fake-value"}
}
func (m mockMount) ValidateAndApplyDefaults(d *schema.ResourceData, client *common.DatabricksClient) error {
return nil
}
func TestMountPoint_Mount(t *testing.T) {
client := common.DatabricksClient{
DatabricksClient: &client.DatabricksClient{
Config: &config.Config{
Host: ".",
Token: ".",
},
},
}
mount := mockMount{}
expectedMountSource := "fake-mount"
expectedMountConfig := `{"fake-key":"fake-value"}`
mountName := "this_mount"
expectedCommand := fmt.Sprintf(`
def check_path(path_string):
fs = sc._jvm.org.apache.hadoop.fs.FileSystem.get(sc._jsc.hadoopConfiguration())
path = spark.sparkContext._jvm.org.apache.hadoop.fs.Path(f"dbfs:{path_string}")
fs.exists(path)
def safe_mount(mount_point, mount_source, configs, encryptionType):
for mount in dbutils.fs.mounts():
if mount.mountPoint == mount_point and mount.source == mount_source:
return
try:
dbutils.fs.mount(mount_source, mount_point, extra_configs=configs, encryption_type=encryptionType)
dbutils.fs.refreshMounts()
check_path(mount_point)
return mount_source
except Exception as e:
try:
dbutils.fs.unmount(mount_point)
except Exception as e2:
print("Failed to unmount", e2)
raise e
mount_source = safe_mount("/mnt/%s", %q, %s, "")
dbutils.notebook.exit(mount_source)
`, mountName, expectedMountSource, expectedMountConfig)
testMountFuncHelper(t, func(mp MountPoint, mount Mount) (s string, e error) {
return mp.Mount(mount, &client)
}, mount, mountName, expectedCommand)
}
func TestMountPoint_Source(t *testing.T) {
mountName := "this_mount"
expectedCommand := fmt.Sprintf(`
dbutils.fs.refreshMounts()
for mount in dbutils.fs.mounts():
if mount.mountPoint == "/mnt/%s":
dbutils.notebook.exit(mount.source)
raise Exception("Mount not found")
`, mountName)
testMountFuncHelper(t, func(mp MountPoint, mount Mount) (s string, e error) {
return mp.Source()
}, nil, mountName, expectedCommand)
}
func TestMountPoint_Delete(t *testing.T) {
mountName := "this_mount"
expectedCommand := fmt.Sprintf(`
found = False
mount_point = "/mnt/%s"
dbutils.fs.refreshMounts()
for mount in dbutils.fs.mounts():
if mount.mountPoint == mount_point:
found = True
if not found:
dbutils.notebook.exit("success")
dbutils.fs.unmount(mount_point)
dbutils.fs.refreshMounts()
for mount in dbutils.fs.mounts():
if mount.mountPoint == mount_point:
raise Exception("Failed to unmount")
dbutils.notebook.exit("success")
`, mountName)
testMountFuncHelper(t, func(mp MountPoint, mount Mount) (s string, e error) {
return expectedCommandResp, mp.Delete()
}, nil, mountName, expectedCommand)
}
func TestDeletedMountClusterRecreates(t *testing.T) {
qa.HTTPFixturesApply(t, []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.0/clusters/get?cluster_id=abc",
Status: 404,
},
{
Method: "GET",
ReuseRequest: true,
Resource: "/api/2.0/clusters/list",
Response: map[string]any{},
},
{
Method: "GET",
ReuseRequest: true,
Resource: "/api/2.1/clusters/spark-versions",
Response: compute.GetSparkVersionsResponse{
Versions: []compute.SparkVersion{
{
Key: "7.1.x-cpu-ml-scala2.12",
Name: "7.1 ML (includes Apache Spark 3.0.0, Scala 2.12)",
},
},
},
},
{
Method: "GET",
ReuseRequest: true,
Resource: "/api/2.1/clusters/list-node-types",
Response: compute.ListNodeTypesResponse{
NodeTypes: []compute.NodeType{
{
NodeTypeId: "Standard_F4s",
InstanceTypeId: "Standard_F4s",
MemoryMb: 8192,
NumCores: 4,
NodeInstanceType: &compute.NodeInstanceType{
LocalDisks: 1,
InstanceTypeId: "Standard_F4s",
LocalDiskSizeGb: 16,
LocalNvmeDisks: 0,
},
},
},
},
},
{
Method: "POST",
ReuseRequest: true,
Resource: "/api/2.0/clusters/create",
ExpectedRequest: clusters.Cluster{
AutoterminationMinutes: 10,
ClusterName: "terraform-mount",
NodeTypeID: "Standard_F4s",
SparkVersion: "11.3.x-scala2.12",
CustomTags: map[string]string{
"ResourceClass": "SingleNode",
},
SparkConf: map[string]string{
"spark.databricks.cluster.profile": "singleNode",
"spark.master": "local[*]",
"spark.scheduler.mode": "FIFO",
},
},
Response: clusters.ClusterID{
ClusterID: "bcd",
},
},
{
Method: "GET",
ReuseRequest: true,
Resource: "/api/2.0/clusters/get?cluster_id=bcd",
Response: clusters.ClusterInfo{
ClusterID: "bcd",
State: "RUNNING",
SparkConf: map[string]string{
"spark.databricks.acl.dfAclsEnabled": "true",
"spark.databricks.cluster.profile": "singleNode",
"spark.scheduler.mode": "FIFO",
},
},
},
}, func(ctx context.Context, client *common.DatabricksClient) {
clusterID, err := getMountingClusterID(ctx, client, "abc")
assert.NoError(t, err)
assert.Equal(t, "bcd", clusterID)
})
}
func TestOldMountImplementations(t *testing.T) {
n := "test"
m1 := AzureADLSGen2Mount{ContainerName: n}
assert.Equal(t, m1.Name(), n)
assert.Nil(t, m1.ValidateAndApplyDefaults(nil, nil))
m2 := AzureBlobMount{ContainerName: n}
assert.Equal(t, m2.Name(), n)
assert.Nil(t, m2.ValidateAndApplyDefaults(nil, nil))
m3 := AzureADLSGen1Mount{StorageResource: n}
assert.Equal(t, m3.Name(), n)
assert.Nil(t, m3.ValidateAndApplyDefaults(nil, nil))
m4 := AWSIamMount{S3BucketName: n}
assert.Equal(t, m4.Name(), n)
assert.Nil(t, m4.ValidateAndApplyDefaults(nil, nil))
}
type sampleCommand string
func (s sampleCommand) Execute(clusterID, language, commandStr string) common.CommandResults {
return common.CommandResults{
ResultType: "text",
Data: string(s),
}
}
func TestNewMountPoint(t *testing.T) {
mp := NewMountPoint(sampleCommand("abc"), "abc", "bcd")
r := mp.Exec.Execute("a", "b", "c")
assert.Equal(t, "abc", (&r).Text())
}
func TestGetMountingClusterID_Failures(t *testing.T) {
qa.HTTPFixturesApply(t, []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.0/clusters/get?cluster_id=def",
Status: 518,
Response: apierr.APIError{
Message: "😤",
},
},
{
Method: "GET",
Resource: "/api/2.0/clusters/get?cluster_id=terminated",
Response: clusters.ClusterInfo{
ClusterID: "failing-id",
State: clusters.ClusterStateTerminated,
},
},
{
MatchAny: true,
ReuseRequest: true,
Status: 404,
Response: &apierr.APIError{
ErrorCode: "NOT_FOUND",
StatusCode: 404,
Message: "nope",
},
},
}, func(ctx context.Context, client *common.DatabricksClient) {
// no mounting cluster given, try creating it
_, err := getMountingClusterID(ctx, client, "")
assert.EqualError(t, err, "failed to get mounting cluster: nope")
// mounting cluster given, but it's removed already
_, err = getMountingClusterID(ctx, client, "bcd")
assert.EqualError(t, err, "failed to get mounting cluster: nope")
// some other error happens
_, err = getMountingClusterID(ctx, client, "def")
assert.EqualError(t, err, "failed to re-create mounting cluster: 😤")
_, err = getMountingClusterID(ctx, client, "terminated")
assert.EqualError(t, err, "failed to start mounting cluster: nope")
})
}
func TestMountCRD(t *testing.T) {
qa.HTTPFixturesApply(t, []qa.HTTPFixture{
{
MatchAny: true,
ReuseRequest: true,
Status: 404,
Response: &apierr.APIError{
ErrorCode: "NOT_FOUND",
StatusCode: 404,
Message: "nope",
},
},
}, func(ctx context.Context, client *common.DatabricksClient) {
r := ResourceMount().ToResource()
d := r.TestResourceData()
d.Set("name", "a name")
d.Set("uri", "a uri")
client.WithCommandMock(func(commandStr string) common.CommandResults {
return common.CommandResults{}
})
diags := r.CreateContext(ctx, d, client)
assert.True(t, diags.HasError())
assert.Equal(t, "failed to get mounting cluster: nope", diags[0].Summary)
diags = r.ReadContext(ctx, d, client)
assert.True(t, diags.HasError())
assert.Equal(t, "failed to get mounting cluster: nope", diags[0].Summary)
diags = r.DeleteContext(ctx, d, client)
assert.True(t, diags.HasError())
assert.Equal(t, "failed to get mounting cluster: nope", diags[0].Summary)
})
}