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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
package dir
import (
"os"
"path/filepath"
"testing"
resource "codeberg.org/snonux/gonf/internal/resource"
"codeberg.org/snonux/gonf/internal/resource/file"
. "codeberg.org/snonux/gonf/api/option"
)
func TestHaveDirectoryCreate(t *testing.T) {
resource.ResetRepository()
tmp := t.TempDir()
path := filepath.Join(tmp, "sub", "nested")
Have(path)
if err := resource.Apply(); err != nil {
t.Fatalf("Apply failed: %v", err)
}
info, err := os.Stat(path)
if err != nil {
t.Fatalf("stat: %v", err)
}
if !info.IsDir() {
t.Errorf("expected a directory at %s", path)
}
if info.Mode().Perm() != 0o750 {
t.Errorf("expected default dir mode 0750, got %v", info.Mode().Perm())
}
}
func TestHaveDirectoryIdempotentWithMode(t *testing.T) {
resource.ResetRepository()
tmp := t.TempDir()
path := filepath.Join(tmp, "d")
// Call ensureDirectorySelf directly to exercise idempotency without the
// one-per-process resource registry rejecting a duplicate registration.
d1 := &Dir{path: path, mode: 0o755}
if err := ensureDirectorySelf(d1); err != nil {
t.Fatalf("first apply: %v", err)
}
d2 := &Dir{path: path, mode: 0o700}
if err := ensureDirectorySelf(d2); err != nil {
t.Fatalf("second apply: %v", err)
}
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if info.Mode().Perm() != 0o700 {
t.Errorf("expected mode 0700 enforced, got %v", info.Mode().Perm())
}
}
func TestHaveDirectoryFailsWhenFileExists(t *testing.T) {
resource.ResetRepository()
tmp := t.TempDir()
path := filepath.Join(tmp, "afile")
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
if err := Ensure(path); err == nil {
t.Error("expected error when a regular file is in the way of a directory")
}
}
func TestHaveAbsentNonEmptyDirWithoutPruneFails(t *testing.T) {
resource.ResetRepository()
tmp := t.TempDir()
target := filepath.Join(tmp, "d")
if err := os.MkdirAll(filepath.Join(target, "sub"), 0o755); err != nil {
t.Fatal(err)
}
if err := Ensure(target, IsAbsent()); err == nil {
t.Error("expected error removing a non-empty directory without WithPrune()")
}
if _, err := os.Stat(target); err != nil {
t.Errorf("expected %s to still exist, got %v", target, err)
}
}
func TestHaveAbsentPruneDirectoryRecursive(t *testing.T) {
resource.ResetRepository()
tmp := t.TempDir()
target := filepath.Join(tmp, "d")
if err := os.MkdirAll(filepath.Join(target, "sub", "deep"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(target, "sub", "f.txt"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
Have(target, IsAbsent(), WithPrune())
if err := resource.Apply(); err != nil {
t.Fatalf("Apply failed: %v", err)
}
if _, err := os.Stat(target); !os.IsNotExist(err) {
t.Errorf("expected %s to be removed recursively", target)
}
// Idempotent: removing a missing tree is not an error.
d := &Dir{path: target, absent: true, prune: true}
if err := ensureAbsent(d); err != nil {
t.Fatalf("prune on missing tree: %v", err)
}
}
func TestAbsentPruneDirectoryRecursive(t *testing.T) {
resource.ResetRepository()
tmp := t.TempDir()
target := filepath.Join(tmp, "d")
if err := os.MkdirAll(filepath.Join(target, "sub", "deep"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(target, "sub", "f.txt"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
Absent(target, WithPrune())
if err := resource.Apply(); err != nil {
t.Fatalf("Apply failed: %v", err)
}
if _, err := os.Stat(target); !os.IsNotExist(err) {
t.Errorf("expected %s to be removed recursively", target)
}
// Idempotent: removing a missing tree is not an error.
d := &Dir{path: target, absent: true, prune: true}
if err := ensureAbsent(d); err != nil {
t.Fatalf("prune on missing tree: %v", err)
}
}
func TestHaveDirectoryWithSource(t *testing.T) {
t.Run("recursive copy", func(t *testing.T) {
resource.ResetRepository()
tmp := t.TempDir()
src := t.TempDir()
dst := filepath.Join(tmp, "dst")
srcFile := filepath.Join(src, "file.txt")
if err := os.WriteFile(srcFile, []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}
srcSub := filepath.Join(src, "sub")
if err := os.MkdirAll(srcSub, 0o755); err != nil {
t.Fatal(err)
}
srcSubFile := filepath.Join(srcSub, "subfile.txt")
if err := os.WriteFile(srcSubFile, []byte("sub hello"), 0o644); err != nil {
t.Fatal(err)
}
Have(dst, WithSource(src))
if err := resource.Apply(); err != nil {
t.Fatalf("Apply failed: %v", err)
}
if data, err := os.ReadFile(filepath.Join(dst, "file.txt")); err != nil || string(data) != "hello" {
t.Errorf("expected 'hello' at %s, got %q err %v", filepath.Join(dst, "file.txt"), string(data), err)
}
if data, err := os.ReadFile(filepath.Join(dst, "sub", "subfile.txt")); err != nil || string(data) != "sub hello" {
t.Errorf("expected 'sub hello' at %s, got %q err %v", filepath.Join(dst, "sub", "subfile.txt"), string(data), err)
}
})
t.Run("pruning", func(t *testing.T) {
resource.ResetRepository()
tmp := t.TempDir()
src := t.TempDir()
dst := filepath.Join(tmp, "dst")
srcFile := filepath.Join(src, "file.txt")
if err := os.WriteFile(srcFile, []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(dst, 0o755); err != nil {
t.Fatal(err)
}
extra := filepath.Join(dst, "extra.txt")
if err := os.WriteFile(extra, []byte("extra"), 0o644); err != nil {
t.Fatal(err)
}
extraSub := filepath.Join(dst, "extra-sub")
if err := os.MkdirAll(extraSub, 0o755); err != nil {
t.Fatal(err)
}
Have(dst, WithSource(src), WithPrune())
if err := resource.Apply(); err != nil {
t.Fatalf("Apply failed: %v", err)
}
if _, err := os.Stat(extra); !os.IsNotExist(err) {
t.Errorf("expected %s to be pruned", extra)
}
if _, err := os.Stat(extraSub); !os.IsNotExist(err) {
t.Errorf("expected %s to be pruned", extraSub)
}
if data, err := os.ReadFile(filepath.Join(dst, "file.txt")); err != nil || string(data) != "hello" {
t.Errorf("expected 'hello' at %s, got %q err %v", filepath.Join(dst, "file.txt"), string(data), err)
}
})
}
func TestSourceCopyUsesFileModeDefaultNotDirMode(t *testing.T) {
resource.ResetRepository()
tmp := t.TempDir()
src := t.TempDir()
dst := filepath.Join(tmp, "dst")
if err := os.WriteFile(filepath.Join(src, "file.txt"), []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}
Have(dst, WithSource(src))
if err := resource.Apply(); err != nil {
t.Fatalf("Apply failed: %v", err)
}
dirInfo, err := os.Stat(dst)
if err != nil {
t.Fatal(err)
}
if dirInfo.Mode().Perm() != 0o750 {
t.Errorf("expected dir mode 0750, got %v", dirInfo.Mode().Perm())
}
fileInfo, err := os.Stat(filepath.Join(dst, "file.txt"))
if err != nil {
t.Fatal(err)
}
if fileInfo.Mode().Perm() != 0o640 {
t.Errorf("expected copied file mode 0640 (not the directory's 0750), got %v", fileInfo.Mode().Perm())
}
}
func TestSourceCopyRespectsExplicitWithFileMode(t *testing.T) {
resource.ResetRepository()
tmp := t.TempDir()
src := t.TempDir()
dst := filepath.Join(tmp, "dst")
if err := os.WriteFile(filepath.Join(src, "file.txt"), []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}
Have(dst, WithSource(src), WithMode(0o755), WithFileMode(0o600))
if err := resource.Apply(); err != nil {
t.Fatalf("Apply failed: %v", err)
}
dirInfo, err := os.Stat(dst)
if err != nil {
t.Fatal(err)
}
if dirInfo.Mode().Perm() != 0o755 {
t.Errorf("expected dir mode 0755, got %v", dirInfo.Mode().Perm())
}
fileInfo, err := os.Stat(filepath.Join(dst, "file.txt"))
if err != nil {
t.Fatal(err)
}
if fileInfo.Mode().Perm() != 0o600 {
t.Errorf("expected copied file mode 0600, got %v", fileInfo.Mode().Perm())
}
}
func TestSourceCopyStripsTmplSuffixOnCopiedFile(t *testing.T) {
resource.ResetRepository()
tmp := t.TempDir()
src := t.TempDir()
dst := filepath.Join(tmp, "dst")
if err := os.WriteFile(filepath.Join(src, "foo.conf.tmpl"), []byte("hello {{.Param}}"), 0o644); err != nil {
t.Fatal(err)
}
Have(dst, WithSource(src))
if err := resource.Apply(); err != nil {
t.Fatalf("Apply failed: %v", err)
}
if _, err := os.Stat(filepath.Join(dst, "foo.conf")); err != nil {
t.Errorf("expected de-suffixed foo.conf to exist: %v", err)
}
if _, err := os.Stat(filepath.Join(dst, "foo.conf.tmpl")); !os.IsNotExist(err) {
t.Errorf("expected foo.conf.tmpl to NOT exist on disk")
}
}
func TestSourceCopyWithPruneKeepsTemplatedFile(t *testing.T) {
resource.ResetRepository()
tmp := t.TempDir()
src := t.TempDir()
dst := filepath.Join(tmp, "dst")
if err := os.WriteFile(filepath.Join(src, "foo.conf.tmpl"), []byte("hello {{.Param}}"), 0o644); err != nil {
t.Fatal(err)
}
// WithPrune reconciles the destination against the source on every
// apply; a de-suffixed templated file must not be pruned just because
// its own name has no direct match in the source tree.
Have(dst, WithSource(src), WithPrune())
if err := resource.Apply(); err != nil {
t.Fatalf("Apply failed: %v", err)
}
if _, err := os.Stat(filepath.Join(dst, "foo.conf")); err != nil {
t.Errorf("expected foo.conf to survive pruning, got %v", err)
}
}
func TestSourceCopyParamMatchesSingleFilePath(t *testing.T) {
resource.ResetRepository()
tmp := t.TempDir()
src := t.TempDir()
dst := filepath.Join(tmp, "dst")
sourcePath := filepath.Join(src, "foo.conf.tmpl")
if err := os.WriteFile(sourcePath, []byte("{{.Param}}"), 0o644); err != nil {
t.Fatal(err)
}
Have(dst, WithSource(src))
if err := resource.Apply(); err != nil {
t.Fatalf("Apply failed: %v", err)
}
viaDir, err := os.ReadFile(filepath.Join(dst, "foo.conf"))
if err != nil {
t.Fatal(err)
}
singleTarget := filepath.Join(tmp, "single.conf")
if err := file.Ensure(singleTarget, WithSource(sourcePath)); err != nil {
t.Fatal(err)
}
viaFile, err := os.ReadFile(singleTarget)
if err != nil {
t.Fatal(err)
}
if string(viaDir) != string(viaFile) {
t.Errorf("expected identical .Param rendering via both paths, dir-copy=%q file-direct=%q", viaDir, viaFile)
}
}
func TestSourceCopyRecreatesSymlinkNotContent(t *testing.T) {
resource.ResetRepository()
tmp := t.TempDir()
src := t.TempDir()
dst := filepath.Join(tmp, "dst")
if err := os.WriteFile(filepath.Join(src, "target.txt"), []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Symlink("target.txt", filepath.Join(src, "link.txt")); err != nil {
t.Fatal(err)
}
Have(dst, WithSource(src))
if err := resource.Apply(); err != nil {
t.Fatalf("Apply failed: %v", err)
}
linkPath := filepath.Join(dst, "link.txt")
info, err := os.Lstat(linkPath)
if err != nil {
t.Fatalf("lstat: %v", err)
}
if info.Mode()&os.ModeSymlink == 0 {
t.Fatalf("expected %s to be a symlink, got mode %v", linkPath, info.Mode())
}
got, err := os.Readlink(linkPath)
if err != nil {
t.Fatalf("readlink: %v", err)
}
if got != "target.txt" {
t.Errorf("expected symlink target %q, got %q", "target.txt", got)
}
}
|