File: copy_test.go

package info (click to toggle)
singularity-container 4.0.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,672 kB
  • sloc: asm: 3,857; sh: 2,125; ansic: 1,677; awk: 414; makefile: 110; python: 99
file content (872 lines) | stat: -rw-r--r-- 22,603 bytes parent folder | download | duplicates (2)
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
// Copyright (c) 2019-2023, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.

package files

import (
	"os"
	"path/filepath"
	"testing"

	"github.com/sylabs/singularity/v4/internal/pkg/util/fs"
)

var sourceFileContent = "Source File Content\n"

func TestMakeParentDir(t *testing.T) {
	tests := []struct {
		name   string
		srcNum int
		path   string
		parent bool // this specifies if the correct path should have the full path created or just the parent
	}{
		{
			name:   "basic",
			path:   "basic/path",
			parent: true,
		},
		{
			name:   "trailing slash",
			path:   "trailing/slash/",
			parent: false,
		},
		{
			name:   "exists",
			path:   "", // this will create a path of just the testdir, which will always exist
			parent: false,
		},
	}

	// while running tests, make sure to remove everything past the tmp dir created so tests to accidentally collide
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// create tmpdir for each test
			dir := t.TempDir()

			// concatenate test path with directory, do not use a join function so that we do not remove a trailing slash
			path := dir + "/" + tt.path
			if err := makeParentDir(path); err != nil {
				t.Errorf("")
			}

			clean := filepath.Clean(path)
			if tt.parent {
				// full path should not exist
				_, err := os.Stat(clean)
				if !os.IsNotExist(err) {
					t.Errorf("full path created when only parent should have been made")
				}

				// parent should exist
				_, err = os.Stat(filepath.Dir(clean))
				if os.IsNotExist(err) {
					t.Errorf("parent not created when it should have been made")
				}
			} else {
				// full path should exist
				_, err := os.Stat(clean)
				if os.IsNotExist(err) {
					t.Errorf("full path not created when it should have been made")
				}
			}
		})
	}
}

// TestCopyFromHost tests that copying non-nested source dirs, files, links to various
// destinations works. CopyFromHost should always resolve symlinks.
//
//nolint:maintidx
func TestCopyFromHost(t *testing.T) {
	// create tmpdir
	dir := t.TempDir()

	// Source Files
	srcFile := filepath.Join(dir, "srcFile")
	if err := os.WriteFile(srcFile, []byte(sourceFileContent), 0o644); err != nil {
		t.Fatal(err)
	}
	srcFileGlob := filepath.Join(dir, "srcFi?*")
	srcSpaceFile := filepath.Join(dir, "src File")
	if err := os.WriteFile(srcSpaceFile, []byte(sourceFileContent), 0o644); err != nil {
		t.Fatal(err)
	}
	// Source Dirs
	srcDir := filepath.Join(dir, "srcDir")
	if err := os.Mkdir(srcDir, 0o755); err != nil {
		t.Fatal(err)
	}
	srcDirGlob := filepath.Join(dir, "srcD?*")
	srcSpaceDir := filepath.Join(dir, "src Dir")
	if err := os.Mkdir(srcSpaceDir, 0o755); err != nil {
		t.Fatal(err)
	}
	srcGlob := filepath.Join(dir, "src*")
	// Nested File (to test multi level glob)
	srcFileNested := filepath.Join(dir, "srcDir/srcFileNested")
	if err := os.WriteFile(srcFileNested, []byte(sourceFileContent), 0o644); err != nil {
		t.Fatal(err)
	}
	srcFileNestedGlob := filepath.Join(dir, "srcDi?/srcFil?Nested")
	// Source Symlinks
	srcFileLinkAbs := filepath.Join(dir, "srcFileLinkAbs")
	if err := os.Symlink(srcFile, srcFileLinkAbs); err != nil {
		t.Fatal(err)
	}
	srcFileLinkRel := filepath.Join(dir, "srcFileLinkRel")
	if err := os.Symlink("./srcFile", srcFileLinkRel); err != nil {
		t.Fatal(err)
	}
	srcDirLinkAbs := filepath.Join(dir, "srcDirLinkAbs")
	if err := os.Symlink(srcDir, srcDirLinkAbs); err != nil {
		t.Fatal(err)
	}
	srcDirLinkRel := filepath.Join(dir, "srcDirLinkRel")
	if err := os.Symlink("./srcDir", srcDirLinkRel); err != nil {
		t.Fatal(err)
	}

	tests := []struct {
		name       string
		src        string
		dst        string
		expectPath string
		expectFile bool
		expectDir  bool
	}{
		// Source is a file
		{
			name:       "SrcFileNoDest",
			src:        srcFile,
			dst:        "",
			expectPath: srcFile,
			expectFile: true,
		},
		{
			name:       "SrcFileToDir",
			src:        srcFile,
			dst:        "dstDir/",
			expectPath: "dstDir/srcFile",
			expectFile: true,
		},
		{
			name:       "srcFileToFile",
			src:        srcFile,
			dst:        "dstDir/dstFile",
			expectPath: "dstDir/dstFile",
			expectFile: true,
		},
		{
			name:       "srcFileToFileLongPath",
			src:        srcFile,
			dst:        "dstDir/long/path/to/dstFile",
			expectPath: "dstDir/long/path/to/dstFile",
			expectFile: true,
		},
		{
			name:       "srcFileSpace",
			src:        srcSpaceFile,
			dst:        "src File",
			expectPath: "src File",
			expectFile: true,
		},
		{
			name:       "dstFileSpace",
			src:        srcFile,
			dst:        "dst File",
			expectPath: "dst File",
			expectFile: true,
		},
		{
			name:       "srcFileGlob",
			src:        srcFileGlob,
			dst:        "dstDir/",
			expectPath: "dstDir/srcFile",
			expectFile: true,
		},
		{
			name:       "srcFileGlobNoDest",
			src:        srcFileGlob,
			dst:        "",
			expectPath: srcFile,
			expectFile: true,
		},
		{
			name:       "srcFileNestedGlob",
			src:        srcFileNestedGlob,
			dst:        "dstDir/",
			expectPath: "dstDir/srcFileNested",
			expectFile: true,
		},
		{
			name:       "srcFileNestedGlobNoDest",
			src:        srcFileNestedGlob,
			dst:        "",
			expectPath: srcFileNested,
			expectFile: true,
		},
		{
			name: "dstRestricted",
			src:  srcFile,
			// Will be restricted to `/` in the rootfs and should copy to there OK
			dst:        "../../../../",
			expectPath: "srcFile",
			expectFile: true,
		},
		// Source is a Directory
		{
			name:       "SrcDirNoDest",
			src:        srcDir,
			dst:        "",
			expectPath: srcDir,
			expectDir:  true,
		},
		{
			name:       "SrcDirDest",
			src:        srcDir,
			dst:        "dstDir",
			expectPath: "dstDir",
			expectDir:  true,
		},
		{
			name:       "SrcDirToDir",
			src:        srcDir,
			dst:        "dstDir/",
			expectPath: "dstDir/srcDir",
			expectDir:  true,
		},
		{
			name:       "srcDirToDirLongPath",
			src:        srcDir,
			dst:        "dstDir/long/path/to/srcDir",
			expectPath: "dstDir/long/path/to/srcDir",
			expectDir:  true,
		},
		{
			name:       "srcDirSpace",
			src:        srcSpaceDir,
			dst:        "src Dir",
			expectPath: "src Dir",
			expectDir:  true,
		},
		{
			name:       "dstDirSpace",
			src:        srcDir,
			dst:        "dst Dir",
			expectPath: "dst Dir",
			expectDir:  true,
		},
		{
			name:       "srcDirGlob",
			src:        srcDirGlob,
			dst:        "dstDir/",
			expectPath: "dstDir/srcDir",
			expectDir:  true,
		},
		{
			name:       "srcDirGlobNoDest",
			src:        srcDirGlob,
			dst:        "",
			expectPath: srcDir,
			expectDir:  true,
		},
		// Source is a Symlink
		{
			name:       "srcFileLinkRel",
			src:        srcFileLinkRel,
			dst:        "srcFileLinkRel",
			expectPath: "srcFileLinkRel",
			// Copied the file, not the link itself
			expectFile: true,
		},
		{
			name:       "srcFileLinkAbs",
			src:        srcFileLinkAbs,
			dst:        "srcFileLinkAbs",
			expectPath: "srcFileLinkAbs",
			// Copied the file, not the link itself
			expectFile: true,
		},
		{
			name:       "srcDirLinkRel",
			src:        srcDirLinkRel,
			dst:        "srcDirLinkRel",
			expectPath: "srcDirLinkRel",
			// Copied the dir, not the link itself
			expectDir: true,
		},
		{
			name:       "srcDirLinkAbs",
			src:        srcDirLinkAbs,
			dst:        "srcDirLinkAbs",
			expectPath: "srcDirLinkAbs",
			// Copied the dir, not the link itself
			expectDir: true,
		},
		// issue 261 - multiple globbed sources, with no dest
		// both srcfile and srcdir should be copied for glob of "src*"
		{
			name:       "srcDirGlobNoDestMulti1",
			src:        srcGlob,
			dst:        "",
			expectPath: srcDir,
			expectDir:  true,
		},
		{
			name:       "srcDirGlobNoDestMulti2",
			src:        srcGlob,
			dst:        "",
			expectPath: srcFile,
			expectFile: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Create outer destination dir
			dstRoot := t.TempDir()

			if err := CopyFromHost(tt.src, tt.dst, dstRoot); err != nil {
				t.Errorf("unexpected failure running %s test: %s", t.Name(), err)
			}

			dstFinal := filepath.Join(dstRoot, tt.expectPath)
			// verify file was copied
			_, err := os.Stat(dstFinal)
			if err != nil && !os.IsNotExist(err) {
				t.Fatalf("while checking for destination file: %s", err)
			}
			if os.IsNotExist(err) {
				t.Errorf("expected destination %s does not exist", dstFinal)
			}

			// File when expected?
			if tt.expectFile && !fs.IsFile(dstFinal) {
				t.Errorf("destination should be a file, but isn't")
			}
			// Dir when expected?
			if tt.expectDir && !fs.IsDir(dstFinal) {
				t.Errorf("destination should be a directory, but isn't")
			}
			// None of these test cases should result in dst being a symlink
			if fs.IsLink(dstFinal) {
				t.Errorf("destination should not be a symlink, but is")
			}
		})
	}
}

// TestCopyFromHostNested tests that copying a single directory containing nested dirs, files, links
// works. CopyFromHost should always resolve symlinks, even those nested inside a source dir.
func TestCopyFromHostNested(t *testing.T) {
	// create tmpdir
	dir := t.TempDir()
	t.Logf("src dir location: %s\n", dir)

	// All our test files/dirs/links will be nested inside innerDir
	innerDir := filepath.Join(dir, "innerDir")
	if err := os.Mkdir(innerDir, 0o755); err != nil {
		t.Fatal(err)
	}
	// Source Files
	srcFile := filepath.Join(innerDir, "srcFile")
	if err := os.WriteFile(srcFile, []byte(sourceFileContent), 0o644); err != nil {
		t.Fatal(err)
	}
	// Source Dirs
	srcDir := filepath.Join(innerDir, "srcDir")
	if err := os.Mkdir(srcDir, 0o755); err != nil {
		t.Fatal(err)
	}
	// Source Symlinks
	srcFileLinkAbs := filepath.Join(innerDir, "srcFileLinkAbs")
	if err := os.Symlink(srcFile, srcFileLinkAbs); err != nil {
		t.Fatal(err)
	}
	srcFileLinkRel := filepath.Join(innerDir, "srcFileLinkRel")
	if err := os.Symlink("./srcFile", srcFileLinkRel); err != nil {
		t.Fatal(err)
	}
	srcDirLinkAbs := filepath.Join(innerDir, "srcDirLinkAbs")
	if err := os.Symlink(srcDir, srcDirLinkAbs); err != nil {
		t.Fatal(err)
	}
	srcDirLinkRel := filepath.Join(innerDir, "srcDirLinkRel")
	if err := os.Symlink("./srcDir", srcDirLinkRel); err != nil {
		t.Fatal(err)
	}

	// Create outer destination dir
	dstDir := t.TempDir()
	t.Logf("dstDir location: %s\n", dstDir)

	// Copy our source innerDir over into the destination dir
	if err := CopyFromHost(innerDir, "innerDir", dstDir); err != nil {
		t.Errorf("unexpected failure copying directory: %s", err)
	}

	// Now verify all the nested copied files are as expected
	tests := []struct {
		expectPath string
		expectFile bool
		expectDir  bool
	}{
		{
			expectPath: "innerDir/srcFile",
			expectFile: true,
		},
		{
			expectPath: "innerDir/srcDir",
			expectDir:  true,
		},
		// Source is a Symlink
		// Should always have copied the target, not the link.
		{
			expectPath: "innerDir/srcFileLinkRel",
			expectFile: true,
		},
		{
			expectPath: "innerDir/srcFileLinkAbs",
			expectFile: true,
		},
		{
			expectPath: "innerDir/srcDirLinkRel",
			expectDir:  true,
		},
		{
			expectPath: "innerDir/srcDirLinkAbs",
			expectDir:  true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.expectPath, func(t *testing.T) {
			dstFinal := filepath.Join(dstDir, tt.expectPath)
			// File when expected?
			if tt.expectFile && !fs.IsFile(dstFinal) {
				t.Errorf("destination should be a file, but isn't")
			}
			// Dir when expected?
			if tt.expectDir && !fs.IsDir(dstFinal) {
				t.Errorf("destination should be a directory, but isn't")
			}
			// None of these test cases should result in dst being a symlink
			if fs.IsLink(dstFinal) {
				t.Errorf("destination should not be a symlink, but is")
			}
		})
	}
}

// TestCopyFromStage tests that copying non-nested source dirs, files, links to various
// destinations works. CopyFromStage should resolve top-level symlinks for sources it is
// called against.
//
//nolint:maintidx
func TestCopyFromStage(t *testing.T) {
	// create tmpdir
	srcRoot := t.TempDir()
	t.Logf("srcRoot location: %s\n", srcRoot)

	// Source Files
	srcFile := filepath.Join(srcRoot, "srcFile")
	if err := os.WriteFile(srcFile, []byte(sourceFileContent), 0o644); err != nil {
		t.Fatal(err)
	}
	srcSpaceFile := filepath.Join(srcRoot, "src File")
	if err := os.WriteFile(srcSpaceFile, []byte(sourceFileContent), 0o644); err != nil {
		t.Fatal(err)
	}
	// Source Dirs
	srcDir := filepath.Join(srcRoot, "srcDir")
	if err := os.Mkdir(srcDir, 0o755); err != nil {
		t.Fatal(err)
	}
	srcSpaceDir := filepath.Join(srcRoot, "src Dir")
	if err := os.Mkdir(srcSpaceDir, 0o755); err != nil {
		t.Fatal(err)
	}
	// Nested File (to test multi level glob)
	srcFileNested := filepath.Join(srcRoot, "srcDir/srcFileNested")
	if err := os.WriteFile(srcFileNested, []byte(sourceFileContent), 0o644); err != nil {
		t.Fatal(err)
	}
	// Source Symlinks
	// Note the absolute links are absolute paths inside the srcRoot
	srcFileLinkAbs := filepath.Join(srcRoot, "srcFileLinkAbs")
	if err := os.Symlink("/srcFile", srcFileLinkAbs); err != nil {
		t.Fatal(err)
	}
	srcFileLinkRel := filepath.Join(srcRoot, "srcFileLinkRel")
	if err := os.Symlink("./srcFile", srcFileLinkRel); err != nil {
		t.Fatal(err)
	}
	srcDirLinkAbs := filepath.Join(srcRoot, "srcDirLinkAbs")
	if err := os.Symlink("/srcDir", srcDirLinkAbs); err != nil {
		t.Fatal(err)
	}
	srcDirLinkRel := filepath.Join(srcRoot, "srcDirLinkRel")
	if err := os.Symlink("./srcDir", srcDirLinkRel); err != nil {
		t.Fatal(err)
	}

	tests := []struct {
		name       string
		srcRel     string
		dstRel     string
		expectPath string
		expectFile bool
		expectDir  bool
	}{
		// Source is a file
		{
			name:       "SrcFileNoDest",
			srcRel:     "srcFile",
			dstRel:     "",
			expectPath: "srcFile",
			expectFile: true,
		},
		{
			name:       "SrcFileAbs",
			srcRel:     "/srcFile",
			dstRel:     "",
			expectPath: "srcFile",
			expectFile: true,
		},
		{
			name:       "SrcFileToDir",
			srcRel:     "srcFile",
			dstRel:     "dstDir/",
			expectPath: "dstDir/srcFile",
			expectFile: true,
		},
		{
			name:       "srcFileToFile",
			srcRel:     "srcFile",
			dstRel:     "dstDir/dstFile",
			expectPath: "dstDir/dstFile",
			expectFile: true,
		},
		{
			name:       "srcFileToFileLongPath",
			srcRel:     "srcFile",
			dstRel:     "dstDir/long/path/to/dstFile",
			expectPath: "dstDir/long/path/to/dstFile",
			expectFile: true,
		},
		{
			name:       "srcFileSpace",
			srcRel:     "src File",
			dstRel:     "",
			expectPath: "src File",
			expectFile: true,
		},
		{
			name:       "dstFileSpace",
			srcRel:     "srcFile",
			dstRel:     "dst File",
			expectPath: "dst File",
			expectFile: true,
		},
		{
			name:       "srcFileGlob",
			srcRel:     "srcF?*",
			dstRel:     "dstDir/",
			expectPath: "dstDir/srcFile",
			expectFile: true,
		},
		{
			name:       "srcFileGlobNoDest",
			srcRel:     "srcF?*",
			dstRel:     "",
			expectPath: "srcFile",
			expectFile: true,
		},
		{
			name:       "srcFileNestedGlob",
			srcRel:     "srcDi?/srcFil?Nested",
			dstRel:     "dstDir/",
			expectPath: "dstDir/srcFileNested",
			expectFile: true,
		},
		{
			name:       "srcFileNestedGlobNoDest",
			srcRel:     "srcDi?/srcFil?Nested",
			dstRel:     "",
			expectPath: "srcDir/srcFileNested",
			expectFile: true,
		},
		{
			name:   "dstRestricted",
			srcRel: "srcFile",
			// Will be cleaned to `/` in the dst rootfs and should copy to there OK
			dstRel:     "../../../../",
			expectPath: "srcFile",
			expectFile: true,
		},
		{
			name: "srcRestricted",
			// Will be cleaned to `/srcFile` in the src rootfs and should copy from there OK
			srcRel:     "../../../../srcFile",
			dstRel:     "",
			expectPath: "srcFile",
			expectFile: true,
		},
		// Source is a Directory
		{
			name:       "SrcDirNoDest",
			srcRel:     "srcDir",
			dstRel:     "",
			expectPath: "srcDir",
			expectDir:  true,
		},
		{
			name:       "SrcDirDest",
			srcRel:     "srcDir",
			dstRel:     "dstDir",
			expectPath: "dstDir",
			expectDir:  true,
		},
		{
			name:       "SrcDirToDir",
			srcRel:     "srcDir",
			dstRel:     "dstDir/",
			expectPath: "dstDir/srcDir",
			expectDir:  true,
		},
		{
			name:       "srcDirToDirLongPath",
			srcRel:     "srcDir",
			dstRel:     "dstDir/long/path/to/srcDir",
			expectPath: "dstDir/long/path/to/srcDir",
			expectDir:  true,
		},
		{
			name:       "srcDirSpace",
			srcRel:     "src Dir",
			dstRel:     "",
			expectPath: "src Dir",
			expectDir:  true,
		},
		{
			name:       "dstDirSpace",
			srcRel:     "srcDir",
			dstRel:     "dst Dir",
			expectPath: "dst Dir",
			expectDir:  true,
		},
		{
			name:       "srcDirGlob",
			srcRel:     "srcD?*",
			dstRel:     "dstDir/",
			expectPath: "dstDir/srcDir",
			expectDir:  true,
		},
		{
			name:       "srcDirGlobNoDest",
			srcRel:     "srcD?*",
			dstRel:     "",
			expectPath: "srcDir",
			expectDir:  true,
		},
		// Source is a Symlink
		{
			name:       "srcFileLinkRel",
			srcRel:     "srcFileLinkRel",
			dstRel:     "",
			expectPath: "srcFileLinkRel",
			// Copied the file, not the link itself
			expectFile: true,
		},
		{
			name:       "srcFileLinkAbs",
			srcRel:     "srcFileLinkAbs",
			dstRel:     "",
			expectPath: "srcFileLinkAbs",
			// Copied the file, not the link itself
			expectFile: true,
		},
		{
			name:       "srcDirLinkRel",
			srcRel:     "srcDirLinkRel",
			dstRel:     "",
			expectPath: "srcDirLinkRel",
			// Copied the dir, not the link itself
			expectDir: true,
		},
		{
			name:       "srcDirLinkAbs",
			srcRel:     "srcDirLinkAbs",
			dstRel:     "",
			expectPath: "srcDirLinkAbs",
			// Copied the dir, not the link itself
			expectDir: true,
		},
		// issue 261 - multiple globbed sources, with no dest
		// both srcfile and srcdir should be copied for glob of "src*"
		{
			name:       "srcDirGlobNoDestMulti1",
			srcRel:     "src*",
			dstRel:     "",
			expectPath: "srcDir",
			expectDir:  true,
		},
		{
			name:       "srcDirGlobNoDestMulti2",
			srcRel:     "src*",
			dstRel:     "",
			expectPath: "srcFile",
			expectFile: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Create outer destination dir
			dstRoot := t.TempDir()
			t.Logf("dstRoot location: %s\n", dstRoot)

			// Manually concatenating because we need to preserve any trailing slash that is
			// stripped by Join.
			if err := CopyFromStage(tt.srcRel, tt.dstRel, srcRoot, dstRoot, false); err != nil {
				t.Errorf("unexpected failure running %s test: %s", t.Name(), err)
			}

			dstFinal := filepath.Join(dstRoot, tt.expectPath)
			// verify file was copied
			_, err := os.Stat(dstFinal)
			if err != nil && !os.IsNotExist(err) {
				t.Fatalf("while checking for destination file: %s", err)
			}
			if os.IsNotExist(err) {
				t.Errorf("expected destination %s does not exist", dstFinal)
			}

			// File when expected?
			if tt.expectFile && !fs.IsFile(dstFinal) {
				t.Errorf("destination %s should be a file, but isn't", dstFinal)
			}
			// Dir when expected?
			if tt.expectDir && !fs.IsDir(dstFinal) {
				t.Errorf("destination %s should be a directory, but isn't", dstFinal)
			}
			// None of these test cases should result in dst being a symlink
			if fs.IsLink(dstFinal) {
				t.Errorf("destination %s should not be a symlink, but is", dstFinal)
			}
		})
	}
}

// TestCopyFromStageNested tests that copying a single directory containing nested dirs, files, links
// works. CopyFromStage should *not* resolve the symlinks that are nested in the dir.
func TestCopyFromStageNested(t *testing.T) {
	// create tmpdir
	srcRoot := t.TempDir()
	t.Logf("srcRoot location: %s\n", srcRoot)

	// All our test files/dirs/links will be nested inside innerDir
	innerDir := filepath.Join(srcRoot, "innerDir")
	if err := os.Mkdir(innerDir, 0o755); err != nil {
		t.Fatal(err)
	}
	// Source Files
	srcFile := filepath.Join(innerDir, "srcFile")
	if err := os.WriteFile(srcFile, []byte(sourceFileContent), 0o644); err != nil {
		t.Fatal(err)
	}
	// Source Dirs
	srcDir := filepath.Join(innerDir, "srcDir")
	if err := os.Mkdir(srcDir, 0o755); err != nil {
		t.Fatal(err)
	}
	// Source Symlinks
	srcFileLinkAbs := filepath.Join(innerDir, "srcFileLinkAbs")
	if err := os.Symlink(srcFile, srcFileLinkAbs); err != nil {
		t.Fatal(err)
	}
	srcFileLinkRel := filepath.Join(innerDir, "srcFileLinkRel")
	if err := os.Symlink("./srcFile", srcFileLinkRel); err != nil {
		t.Fatal(err)
	}
	srcDirLinkAbs := filepath.Join(innerDir, "srcDirLinkAbs")
	if err := os.Symlink(srcDir, srcDirLinkAbs); err != nil {
		t.Fatal(err)
	}
	srcDirLinkRel := filepath.Join(innerDir, "srcDirLinkRel")
	if err := os.Symlink("./srcDir", srcDirLinkRel); err != nil {
		t.Fatal(err)
	}

	// Create outer destination dir
	dstRoot := t.TempDir()
	t.Logf("dstRoot location: %s\n", dstRoot)

	// Copy our source innerDir over into the destination dir
	if err := CopyFromStage("innerDir", "", srcRoot, dstRoot, false); err != nil {
		t.Errorf("unexpected failure copying directory: %s", err)
	}

	// Now verify all the nested copied files are as expected
	tests := []struct {
		expectPath string
		expectFile bool
		expectDir  bool
		expectLink bool
	}{
		{
			expectPath: "innerDir/srcFile",
			expectFile: true,
		},
		{
			expectPath: "innerDir/srcDir",
			expectDir:  true,
		},
		// Nested symlink, inside the src directory.
		// Should always have copied the link itself.
		{
			expectPath: "innerDir/srcFileLinkRel",
			expectFile: true,
			expectLink: true,
		},
		{
			expectPath: "innerDir/srcFileLinkAbs",
			expectFile: true,
			expectLink: true,
		},
		{
			expectPath: "innerDir/srcDirLinkRel",
			expectDir:  true,
			expectLink: true,
		},
		{
			expectPath: "innerDir/srcDirLinkAbs",
			expectDir:  true,
			expectLink: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.expectPath, func(t *testing.T) {
			dstFinal := filepath.Join(dstRoot, tt.expectPath)
			// File when expected?
			if tt.expectFile && !fs.IsFile(dstFinal) {
				t.Errorf("destination should be a file, but isn't")
			}
			// Dir when expected?
			if tt.expectDir && !fs.IsDir(dstFinal) {
				t.Errorf("destination should be a directory, but isn't")
			}
			// Link when expected?
			if tt.expectLink && !fs.IsLink(dstFinal) {
				t.Errorf("destination should be a symlink, but isn't")
			}
			// Not a link when not expected
			if !tt.expectLink && fs.IsLink(dstFinal) {
				t.Errorf("destination should not be a symlink, but is")
			}
		})
	}
}