File: util_test.go

package info (click to toggle)
golang-k8s-sigs-kustomize-api 0.20.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,768 kB
  • sloc: makefile: 206; sh: 67
file content (326 lines) | stat: -rw-r--r-- 10,083 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
// Copyright 2022 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package localizer //nolint:testpackage

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

	"github.com/stretchr/testify/require"
	"sigs.k8s.io/kustomize/api/ifc"
	"sigs.k8s.io/kustomize/api/internal/git"
	"sigs.k8s.io/kustomize/kyaml/filesys"
)

func TestDefaultNewDirRepo(t *testing.T) {
	for name, test := range map[string]struct {
		url, dst string
	}{
		"simple": {
			url: "https://github.com/org/repo?ref=value",
			dst: "localized-repo-value",
		},
		"slashed_ref": {
			url: "https://github.com/org/repo?ref=group/version",
			dst: "localized-repo-group-version",
		},
	} {
		t.Run(name, func(t *testing.T) {
			repoSpec, err := git.NewRepoSpecFromURL(test.url)
			require.NoError(t, err)
			require.Equal(t, test.dst, defaultNewDir(&fakeLoader{t.TempDir()}, repoSpec))
		})
	}
}

type fakeLoader struct {
	root string
}

func (fl *fakeLoader) Root() string {
	return fl.root
}
func (fl *fakeLoader) Repo() string {
	return fl.root
}
func (fl *fakeLoader) Load(_ string) ([]byte, error) {
	return []byte{}, nil
}
func (fl *fakeLoader) New(path string) (ifc.Loader, error) {
	return &fakeLoader{path}, nil
}
func (fl *fakeLoader) Cleanup() error {
	return nil
}

func TestUrlBase(t *testing.T) {
	require.Equal(t, "repo", urlBase("https://github.com/org/repo"))
}

func TestUrlBaseTrailingSlash(t *testing.T) {
	require.Equal(t, "repo", urlBase("github.com/org/repo//"))
}

// simpleJoin is filepath.Join() without the side effects of filepath.Clean()
func simpleJoin(t *testing.T, elems ...string) string {
	t.Helper()

	return strings.Join(elems, string(filepath.Separator))
}

func TestLocFilePath(t *testing.T) {
	for name, tUnit := range map[string]struct {
		url, path string
	}{
		"official": {
			url:  "https://raw.githubusercontent.com/org/repo/ref/path/to/file.yaml",
			path: simpleJoin(t, "raw.githubusercontent.com", "org", "repo", "ref", "path", "to", "file.yaml"),
		},
		"http-scheme": {
			url:  "http://host/path",
			path: simpleJoin(t, "host", "path"),
		},
		"extraneous_components": {
			url:  "http://userinfo@host:1234/path/file?query",
			path: simpleJoin(t, "host", "path", "file"),
		},
		"empty_path": {
			url:  "https://host",
			path: "host",
		},
		"empty_path_segment": {
			url:  "https://host//",
			path: "host",
		},
		"percent-encoded_path": {
			url:  "https://host/file%2Eyaml",
			path: simpleJoin(t, "host", "file%2Eyaml"),
		},
		"dot-segments": {
			url:  "https://host/path/blah/../to/foo/bar/../../file/./",
			path: simpleJoin(t, "host", "path", "to", "file"),
		},
		"extraneous_dot-segments": {
			url:  "https://host/foo/bar/baz/../../../../file",
			path: simpleJoin(t, "host", "file"),
		},
	} {
		t.Run(name, func(t *testing.T) {
			require.Equal(t, simpleJoin(t, LocalizeDir, tUnit.path), locFilePath(tUnit.url))
		})
	}
}

func TestLocFilePathColon(t *testing.T) {
	req := require.New(t)

	// The colon is special because it was once used as the unix file separator.
	const url = "https://[2001:4860:4860::8888]/file.yaml"
	const host = "2001:4860:4860::8888"
	const file = "file.yaml"
	req.Equal(simpleJoin(t, LocalizeDir, host, file), locFilePath(url))

	fSys := filesys.MakeFsOnDisk()
	targetDir := simpleJoin(t, t.TempDir(), host)

	// We check that we can create single directory, meaning ':' not used as file separator.
	req.NoError(fSys.Mkdir(targetDir))
	_, err := fSys.Create(simpleJoin(t, targetDir, file))
	req.NoError(err)

	// We check that the directory with such name is readable.
	files, err := fSys.ReadDir(targetDir)
	req.NoError(err)
	req.Equal([]string{file}, files)
}

func TestLocFilePath_SpecialChar(t *testing.T) {
	req := require.New(t)

	// The wild card character is one of the legal uri characters with more meaning
	// to the system, so we test it here.
	const wildcard = "*"
	req.Equal(simpleJoin(t, LocalizeDir, "host", wildcard), locFilePath("https://host/*"))

	fSys := filesys.MakeFsOnDisk()
	testDir := t.TempDir()
	req.NoError(fSys.Mkdir(simpleJoin(t, testDir, "a")))
	req.NoError(fSys.WriteFile(simpleJoin(t, testDir, "b"), []byte{}))

	// We check that we can create and read from wild card-named file.
	// We check that the file system is not matching it to existing file names.
	req.NoError(fSys.WriteFile(simpleJoin(t, testDir, wildcard), []byte("test")))
	content, err := fSys.ReadFile(simpleJoin(t, testDir, wildcard))
	req.NoError(err)
	req.Equal("test", string(content))
}

func TestLocFilePath_SpecialFiles(t *testing.T) {
	for name, tFSys := range map[string]struct {
		urlPath           string
		pathDir, pathFile string
	}{
		"windows_reserved_name": {
			urlPath:  "/aux/file",
			pathDir:  "aux",
			pathFile: "file",
		},
		"hidden_files": {
			urlPath:  "/.../.file",
			pathDir:  "...",
			pathFile: ".file",
		},
	} {
		t.Run(name, func(t *testing.T) {
			req := require.New(t)

			expectedPath := simpleJoin(t, LocalizeDir, "host", tFSys.pathDir, tFSys.pathFile)
			req.Equal(expectedPath, locFilePath("https://host"+tFSys.urlPath))

			fSys := filesys.MakeFsOnDisk()
			targetDir := simpleJoin(t, t.TempDir(), tFSys.pathDir)
			req.NoError(fSys.Mkdir(targetDir))
			req.NoError(fSys.WriteFile(simpleJoin(t, targetDir, tFSys.pathFile), []byte("test")))

			content, err := fSys.ReadFile(simpleJoin(t, targetDir, tFSys.pathFile))
			req.NoError(err)
			req.Equal([]byte("test"), content)
		})
	}
}

func makeConfirmedDir(t *testing.T) (filesys.FileSystem, filesys.ConfirmedDir) {
	t.Helper()

	fSys := filesys.MakeFsOnDisk()
	testDir, err := filesys.NewTmpConfirmedDir()
	require.NoError(t, err)
	t.Cleanup(func() {
		_ = fSys.RemoveAll(testDir.String())
	})

	return fSys, testDir
}

func TestLocRootPath_URLComponents(t *testing.T) {
	for name, test := range map[string]struct {
		urlf, path string
	}{
		"ssh": {
			urlf: "ssh://git@github.com/org/repo//%s?ref=value",
			path: simpleJoin(t, "github.com", "org", "repo", "value"),
		},
		"rel_ssh": {
			urlf: "git@github.com:org/repo//%s?ref=value",
			path: simpleJoin(t, "github.com", "org", "repo", "value"),
		},
		"https": {
			urlf: "https://gitlab.com/org/repo//%s?ref=value",
			path: simpleJoin(t, "gitlab.com", "org", "repo", "value"),
		},
		"file": {
			urlf: "file:///var/run/repo//%s?ref=value",
			path: simpleJoin(t, FileSchemeDir, "var", "run", "repo", "value"),
		},
		"IPv6": {
			urlf: "https://[2001:4860:4860::8888]/org/repo//%s?ref=value",
			path: simpleJoin(t, "2001:4860:4860::8888", "org", "repo", "value"),
		},
		"port": {
			urlf: "https://localhost.com:8080/org/repo//%s?ref=value",
			path: simpleJoin(t, "localhost.com", "org", "repo", "value"),
		},
		"no_org": {
			urlf: "https://github.com/repo//%s?ref=value",
			path: simpleJoin(t, "github.com", "repo", "value"),
		},
		".git_suffix": {
			urlf: "https://github.com/org1/org2/repo.git//%s?ref=value",
			path: simpleJoin(t, "github.com", "org1", "org2", "repo", "value"),
		},
		"dot-segments": {
			urlf: "https://github.com/./../org/../org/repo.git//%s?ref=value",
			path: simpleJoin(t, "github.com", "org", "repo", "value"),
		},
		"no_path_delimiter": {
			urlf: "https://github.com/org/repo/%s?ref=value",
			path: simpleJoin(t, "github.com", "org", "repo", "value"),
		},
		"illegal_windows_dir": {
			urlf: "https://gitlab.com/org./repo..git//%s?ref=value",
			path: simpleJoin(t, "gitlab.com", "org.", "repo.", "value"),
		},
		"ref_has_slash": {
			urlf: "https://gitlab.com/org/repo//%s?ref=group/version/kind",
			path: simpleJoin(t, "gitlab.com", "org", "repo", "group", "version", "kind"),
		},
	} {
		t.Run(name, func(t *testing.T) {
			u := fmt.Sprintf(test.urlf, "path/to/root")
			path := simpleJoin(t, LocalizeDir, test.path, "path", "to", "root")

			fSys, testDir := makeConfirmedDir(t)
			repoDir := simpleJoin(t, testDir.String(), "repo_random-hash")
			require.NoError(t, fSys.Mkdir(repoDir))
			rootDir := simpleJoin(t, repoDir, "path", "to", "root")
			require.NoError(t, fSys.MkdirAll(rootDir))

			actual, err := locRootPath(u, repoDir, filesys.ConfirmedDir(rootDir), fSys)
			require.NoError(t, err)
			require.Equal(t, path, actual)

			require.NoError(t, fSys.MkdirAll(simpleJoin(t, testDir.String(), path)))
		})
	}
}

func TestLocRootPath_Repo(t *testing.T) {
	const url = "https://github.com/org/repo?ref=value"
	expected := simpleJoin(t, LocalizeDir, "github.com", "org", "repo", "value")

	fSys, testDir := makeConfirmedDir(t)
	actual, err := locRootPath(url, testDir.String(), testDir, fSys)
	require.NoError(t, err)
	require.Equal(t, expected, actual)
}

func TestLocRootPath_SymlinkPath(t *testing.T) {
	const url = "https://github.com/org/repo//symlink?ref=value"

	fSys, repoDir := makeConfirmedDir(t)
	rootDir := simpleJoin(t, repoDir.String(), "actual-root")
	require.NoError(t, fSys.Mkdir(rootDir))
	require.NoError(t, os.Symlink(rootDir, simpleJoin(t, repoDir.String(), "symlink")))

	expected := simpleJoin(t, LocalizeDir, "github.com", "org", "repo", "value", "actual-root")
	actual, err := locRootPath(url, repoDir.String(), filesys.ConfirmedDir(rootDir), fSys)
	require.NoError(t, err)
	require.Equal(t, expected, actual)
}

func TestCleanedRelativePath(t *testing.T) {
	fSys := filesys.MakeFsInMemory()
	require.NoError(t, fSys.MkdirAll("/root/test"))
	require.NoError(t, fSys.WriteFile("/root/test/file.yaml", []byte("")))
	require.NoError(t, fSys.WriteFile("/root/filetwo.yaml", []byte("")))

	// Absolute path is cleaned to relative path
	cleanedPath := cleanedRelativePath(fSys, "/root/", "/root/test/file.yaml")
	require.Equal(t, "test/file.yaml", cleanedPath)

	// Winding absolute path is cleaned to relative path
	cleanedPath = cleanedRelativePath(fSys, "/root/", "/root/test/../filetwo.yaml")
	require.Equal(t, "filetwo.yaml", cleanedPath)

	// Already clean relative path stays the same
	cleanedPath = cleanedRelativePath(fSys, "/root/", "test/file.yaml")
	require.Equal(t, "test/file.yaml", cleanedPath)

	// Winding relative path is cleaned
	cleanedPath = cleanedRelativePath(fSys, "/root/", "test/../filetwo.yaml")
	require.Equal(t, "filetwo.yaml", cleanedPath)
}