File: machine_windows_test.go

package info (click to toggle)
podman 5.4.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 23,124 kB
  • sloc: sh: 6,119; perl: 2,710; python: 2,258; ansic: 1,556; makefile: 1,022; xml: 121; ruby: 42; awk: 12; csh: 8
file content (116 lines) | stat: -rw-r--r-- 3,515 bytes parent folder | download
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
//go:build windows

package machine

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

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// CreateNewItemWithPowerShell creates a new item using PowerShell.
// It's an helper to easily create junctions on Windows (as well as other file types).
// It constructs a PowerShell command to create a new item at the specified path with the given item type.
// If a target is provided, it includes it in the command.
//
// Parameters:
//   - t: The testing.T instance.
//   - path: The path where the new item will be created.
//   - itemType: The type of the item to be created (e.g., "File", "SymbolicLink", "Junction").
//   - target: The target for the new item, if applicable.
func CreateNewItemWithPowerShell(t *testing.T, path string, itemType string, target string) {
	var pwshCmd string
	pwshCmd = "New-Item -Path " + path + " -ItemType " + itemType
	if target != "" {
		pwshCmd += " -Target " + target
	}
	cmd := exec.Command("pwsh", "-Command", pwshCmd)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err := cmd.Run()
	require.NoError(t, err)
}

// TestEvalSymlinksOrClean tests the EvalSymlinksOrClean function.
// In particular it verifies that EvalSymlinksOrClean behaves as
// filepath.EvalSymlink before Go 1.23 - with the exception of
// files under a mount point (juntion) that aren't resolved
// anymore.
// The old behavior of filepath.EvalSymlinks can be tested with
// the directive "//go:debug winsymlink=0" and replacing EvalSymlinksOrClean()
// with filepath.EvalSymlink().
func TestEvalSymlinksOrClean(t *testing.T) {
	// Create a temporary directory to store the normal file
	normalFileDir := t.TempDir()

	// Create a temporary directory to store the (hard/sym)link files
	linkFilesDir := t.TempDir()

	// Create a temporary directory where the mount point will be created
	mountPointDir := t.TempDir()

	// Create a normal file
	normalFile := filepath.Join(normalFileDir, "testFile")
	CreateNewItemWithPowerShell(t, normalFile, "File", "")

	// Create a symlink file
	symlinkFile := filepath.Join(linkFilesDir, "testSymbolicLink")
	CreateNewItemWithPowerShell(t, symlinkFile, "SymbolicLink", normalFile)

	// Create a hardlink file
	hardlinkFile := filepath.Join(linkFilesDir, "testHardLink")
	CreateNewItemWithPowerShell(t, hardlinkFile, "HardLink", normalFile)

	// Create a mount point file
	mountPoint := filepath.Join(mountPointDir, "testJunction")
	mountPointFile := filepath.Join(mountPoint, "testFile")
	CreateNewItemWithPowerShell(t, mountPoint, "Junction", normalFileDir)

	// Replaces the backslashes with forward slashes in the normal file path
	normalFileWithBadSeparators := filepath.ToSlash(normalFile)

	tests := []struct {
		name     string
		filePath string
		want     string
	}{
		{
			name:     "Normal file",
			filePath: normalFile,
			want:     normalFile,
		},
		{
			name:     "File under a mount point (juntion)",
			filePath: mountPointFile,
			want:     mountPointFile,
		},
		{
			name:     "Symbolic link",
			filePath: symlinkFile,
			want:     normalFile,
		},
		{
			name:     "Hard link",
			filePath: hardlinkFile,
			want:     hardlinkFile,
		},
		{
			name:     "Bad separators in path",
			filePath: normalFileWithBadSeparators,
			want:     normalFile,
		},
	}

	for _, tt := range tests {
		assert := assert.New(t)
		t.Run(tt.name, func(t *testing.T) {
			got, err := EvalSymlinksOrClean(tt.filePath)
			require.NoError(t, err)
			assert.Equal(tt.want, got)
		})
	}
}