-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlink_write_soft_test.go
More file actions
322 lines (280 loc) · 8.37 KB
/
link_write_soft_test.go
File metadata and controls
322 lines (280 loc) · 8.37 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
316
317
318
319
320
321
322
package hdf5
import (
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestCreateSoftLink_BasicCreation tests basic soft link creation API.
func TestCreateSoftLink_BasicCreation(t *testing.T) {
tempDir := t.TempDir()
filename := filepath.Join(tempDir, "test_softlink_basic.h5")
fw, err := CreateForWrite(filename, CreateTruncate)
require.NoError(t, err)
defer fw.Close()
// Create /data group
_, err = fw.CreateGroup("/data")
require.NoError(t, err)
// Create a dataset
ds, err := fw.CreateDataset("/data/temperature", Float64, []uint64{5})
require.NoError(t, err)
require.NotNil(t, ds)
data := []float64{1.0, 2.0, 3.0, 4.0, 5.0}
err = ds.Write(data)
require.NoError(t, err)
// Create soft link to dataset
err = fw.CreateSoftLink("/data/temp_link", "/data/temperature")
assert.NoError(t, err, "Soft link creation should succeed")
// Verify file was written
err = fw.Close()
require.NoError(t, err)
// Verify file can be opened (round-trip test)
f, err := Open(filename)
require.NoError(t, err)
defer f.Close()
// Verify root group exists (soft link written successfully)
root := f.Root()
require.NotNil(t, root)
// Verify groups exist in structure
children := root.Children()
require.NotNil(t, children, "root should have children")
assert.Greater(t, len(children), 0, "root should have at least one child (data group)")
}
// TestCreateSoftLink_ToGroup tests soft link to a group.
func TestCreateSoftLink_ToGroup(t *testing.T) {
tempDir := t.TempDir()
filename := filepath.Join(tempDir, "test_softlink_group.h5")
fw, err := CreateForWrite(filename, CreateTruncate)
require.NoError(t, err)
defer fw.Close()
// Create groups
_, err = fw.CreateGroup("/group1")
require.NoError(t, err)
_, err = fw.CreateGroup("/links")
require.NoError(t, err)
// Create soft link to group
err = fw.CreateSoftLink("/links/group_link", "/group1")
assert.NoError(t, err, "Soft link to group should succeed")
// Verify file was written
err = fw.Close()
require.NoError(t, err)
// Verify file can be opened
f, err := Open(filename)
require.NoError(t, err)
defer f.Close()
// Verify groups exist
root := f.Root()
require.NotNil(t, root)
children := root.Children()
require.NotNil(t, children)
assert.GreaterOrEqual(t, len(children), 2, "should have group1 and links groups")
}
// TestCreateSoftLink_DanglingLink tests creating a soft link to a non-existent target.
// This should be allowed in HDF5 - the target can be created later.
func TestCreateSoftLink_DanglingLink(t *testing.T) {
tempDir := t.TempDir()
filename := filepath.Join(tempDir, "test_softlink_dangling.h5")
fw, err := CreateForWrite(filename, CreateTruncate)
require.NoError(t, err)
defer fw.Close()
// Create soft link to non-existent target (dangling link allowed)
err = fw.CreateSoftLink("/link_to_future", "/data/future_dataset")
assert.NoError(t, err, "Dangling soft links should be allowed")
// Verify file was written
err = fw.Close()
require.NoError(t, err)
// Verify file can be opened
f, err := Open(filename)
require.NoError(t, err)
defer f.Close()
root := f.Root()
require.NotNil(t, root)
// Note: link exists in symbol table, but target does not (dangling link)
}
// TestCreateSoftLink_RelativePath tests that relative paths are rejected.
func TestCreateSoftLink_RelativePath(t *testing.T) {
tempDir := t.TempDir()
filename := filepath.Join(tempDir, "test_softlink_relative.h5")
fw, err := CreateForWrite(filename, CreateTruncate)
require.NoError(t, err)
defer fw.Close()
// Try to create soft link with relative target path
err = fw.CreateSoftLink("/link", "relative/path")
assert.Error(t, err, "Should reject relative target path")
assert.Contains(t, err.Error(), "must be absolute", "Error should mention absolute path requirement")
// Try with relative link path
err = fw.CreateSoftLink("relative/link", "/data/target")
assert.Error(t, err, "Should reject relative link path")
assert.Contains(t, err.Error(), "must start with '/'", "Error should mention leading slash")
}
// TestCreateSoftLink_InvalidLinkPath tests various invalid link path formats.
func TestCreateSoftLink_InvalidLinkPath(t *testing.T) {
tempDir := t.TempDir()
filename := filepath.Join(tempDir, "test_softlink_invalid.h5")
fw, err := CreateForWrite(filename, CreateTruncate)
require.NoError(t, err)
defer fw.Close()
tests := []struct {
name string
linkPath string
targetPath string
errMsg string
}{
{
name: "empty link path",
linkPath: "",
targetPath: "/data/target",
errMsg: "cannot be empty",
},
{
name: "no leading slash in link",
linkPath: "link",
targetPath: "/data/target",
errMsg: "must start with '/'",
},
{
name: "root link path",
linkPath: "/",
targetPath: "/data/target",
errMsg: "cannot create link to root",
},
{
name: "consecutive slashes in link",
linkPath: "/group//link",
targetPath: "/data/target",
errMsg: "consecutive slashes",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := fw.CreateSoftLink(tt.linkPath, tt.targetPath)
assert.Error(t, err, "Should fail for invalid link path")
assert.Contains(t, err.Error(), tt.errMsg, "Error message should be descriptive")
})
}
}
// TestCreateSoftLink_TargetPathFormat tests various target path formats.
func TestCreateSoftLink_TargetPathFormat(t *testing.T) {
tempDir := t.TempDir()
filename := filepath.Join(tempDir, "test_softlink_target_fmt.h5")
fw, err := CreateForWrite(filename, CreateTruncate)
require.NoError(t, err)
defer fw.Close()
tests := []struct {
name string
targetPath string
wantErr bool
errMsg string
}{
{
name: "valid absolute path",
targetPath: "/data/dataset1",
wantErr: false,
},
{
name: "valid nested path",
targetPath: "/group1/group2/group3/dataset",
wantErr: false,
},
{
name: "empty target path",
targetPath: "",
wantErr: true,
errMsg: "cannot be empty",
},
{
name: "relative target path",
targetPath: "relative/path",
wantErr: true,
errMsg: "must be absolute",
},
{
name: "consecutive slashes in target",
targetPath: "/data//dataset",
wantErr: true,
errMsg: "consecutive slashes",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
linkPath := "/link_" + tt.name
err := fw.CreateSoftLink(linkPath, tt.targetPath)
if tt.wantErr {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.errMsg)
} else {
assert.NoError(t, err)
}
})
}
}
// TestValidateSoftLinkTargetPath tests target path validation function.
func TestValidateSoftLinkTargetPath(t *testing.T) {
tests := []struct {
name string
path string
wantErr bool
errMsg string
}{
{
name: "valid absolute path",
path: "/data/dataset",
wantErr: false,
},
{
name: "valid nested path",
path: "/a/b/c/d",
wantErr: false,
},
{
name: "empty path",
path: "",
wantErr: true,
errMsg: "cannot be empty",
},
{
name: "relative path",
path: "relative/path",
wantErr: true,
errMsg: "must be absolute",
},
{
name: "consecutive slashes",
path: "/data//dataset",
wantErr: true,
errMsg: "consecutive slashes",
},
{
name: "valid single level",
path: "/dataset",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateSoftLinkTargetPath(tt.path)
if tt.wantErr {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.errMsg)
} else {
assert.NoError(t, err)
}
})
}
}
// TestResolveSoftLink_NotImplemented tests that resolution is not yet implemented.
// MVP v0.11.5-beta: Soft link resolution will be added in v0.12.0.
func TestResolveSoftLink_NotImplemented(t *testing.T) {
// Soft link resolution is not implemented in MVP v0.11.5-beta
// This test documents the planned API for v0.12.0
t.Skip("Soft link resolution not implemented in MVP v0.11.5-beta (planned for v0.12.0)")
}
// Future tests (skipped in MVP v0.11.5-beta):
// - TestResolveSoftLink_Simple
// - TestResolveSoftLink_Chain
// - TestResolveSoftLink_CircularReference
// - TestResolveSoftLink_MaxDepth
// - TestResolveSoftLink_DanglingLink
// - TestSoftLink_ReadAfterCreate
// - TestCreateSoftLink_ParentNotExists
// - TestCreateSoftLink_MultipleLinks