mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-10 16:42:51 +00:00
* test: use `T.TempDir` to create temporary test directory
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.
Prior to this commit, temporary directory created using `ioutil.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
is also tedious, but `t.TempDir` handles this for us nicely.
Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix `TestInit_noargs` on Windows
--- FAIL: TestInit_noargs (0.01s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestInit_noargs3136084632\001: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix `TestTreeCommandDefaultCurDir_files` on Windows
--- FAIL: TestTreeCommandDefaultCurDir_files (0.01s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestTreeCommandDefaultCurDir_files716679291\001: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix `TestCreateSetterCommand` on Windows
--- FAIL: TestCreateSetterCommand (13.27s)
--- FAIL: TestCreateSetterCommand/add_replicas (1.45s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestCreateSetterCommandadd_replicas176222064\001\k8s-cli-487197005.yaml: The process cannot access the file because it is being used by another process.
...
and all subtests
...
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix `TestCreateSubstitutionCommand` on Windows
--- FAIL: TestCreateSubstitutionCommand (4.16s)
--- FAIL: TestCreateSubstitutionCommand/substitution_replicas (1.30s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestCreateSubstitutionCommandsubstitution_replicas1352417113\001\k8s-cli-3183612276.yaml: The process cannot access the file because it is being used by another process.
...
and all subtests
...
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix `TestDeleteSetterCommand` on Windows
--- FAIL: TestDeleteSetterCommand (4.36s)
--- FAIL: TestDeleteSetterCommand/delete_replicas (1.31s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestDeleteSetterCommanddelete_replicas3949811929\001\k8s-cli-957077271.yaml: The process cannot access the file because it is being used by another process.
...
and all subtests
...
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix `TestDeleteSubstitutionCommand` on Windows
--- FAIL: TestDeleteSubstitutionCommand (2.35s)
--- FAIL: TestDeleteSubstitutionCommand/delete_only_subst_if_setter_has_same_name_-_long_ref (1.15s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestDeleteSubstitutionCommanddelete_only_subst_if_setter_has_same_name_-_long_ref2039728641\001\k8s-cli-1602861478.yaml: The process cannot access the file because it is being used by another process.
...
and all subtests
...
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix `TestSetCommand` on Windows
--- FAIL: TestSetCommand (13.76s)
--- FAIL: TestSetCommand/set_replicas (1.13s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestSetCommandset_replicas3781384539\001\k8s-cli-1030344459.yaml: The process cannot access the file because it is being used by another process.
...
and all subtests
...
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
368 lines
9.9 KiB
Go
368 lines
9.9 KiB
Go
// Copyright 2019 The Kubernetes Authors.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package copyutil_test
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
. "sigs.k8s.io/kustomize/kyaml/copyutil"
|
|
)
|
|
|
|
// TestDiff_identical verifies identical directories return an empty set
|
|
func TestDiff_identical(t *testing.T) {
|
|
s := t.TempDir()
|
|
d := t.TempDir()
|
|
|
|
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(s, "a1", "f.yaml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Mkdir(filepath.Join(d, "a1"), 0700)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(d, "a1", "f.yaml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
diff, err := Diff(s, d)
|
|
assert.NoError(t, err)
|
|
assert.Empty(t, diff.List())
|
|
}
|
|
|
|
// TestDiff_additionalSourceFiles verifies if there are additional files
|
|
// in the source, the diff will contain them
|
|
func TestDiff_additionalSourceFiles(t *testing.T) {
|
|
s := t.TempDir()
|
|
d := t.TempDir()
|
|
|
|
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Mkdir(filepath.Join(s, "a2"), 0700)
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Mkdir(filepath.Join(d, "a1"), 0700)
|
|
assert.NoError(t, err)
|
|
|
|
diff, err := Diff(s, d)
|
|
assert.NoError(t, err)
|
|
assert.ElementsMatch(t, diff.List(), []string{"a2"})
|
|
}
|
|
|
|
// TestDiff_additionalDestFiles verifies if there are additional files
|
|
// in the dest, the diff will contain them
|
|
func TestDiff_additionalDestFiles(t *testing.T) {
|
|
s := t.TempDir()
|
|
d := t.TempDir()
|
|
|
|
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Mkdir(filepath.Join(d, "a1"), 0700)
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Mkdir(filepath.Join(d, "a2"), 0700)
|
|
assert.NoError(t, err)
|
|
|
|
diff, err := Diff(s, d)
|
|
assert.NoError(t, err)
|
|
assert.ElementsMatch(t, diff.List(), []string{"a2"})
|
|
}
|
|
|
|
// TestDiff_srcDestContentsDiffer verifies if the file contents
|
|
// differ between the source and destination, the diff
|
|
// contains the differing files
|
|
func TestDiff_srcDestContentsDiffer(t *testing.T) {
|
|
s := t.TempDir()
|
|
d := t.TempDir()
|
|
|
|
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(s, "a1", "f.yaml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Mkdir(filepath.Join(d, "a1"), 0700)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(d, "a1", "f.yaml"), []byte(`b`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
diff, err := Diff(s, d)
|
|
assert.NoError(t, err)
|
|
assert.ElementsMatch(t, diff.List(), []string{
|
|
fmt.Sprintf("a1%sf.yaml", string(filepath.Separator)),
|
|
})
|
|
}
|
|
|
|
// TestDiff_srcDestContentsDifferInDirs verifies if identical files
|
|
// exist in different directories, they are included in the diff
|
|
func TestDiff_srcDestContentsDifferInDirs(t *testing.T) {
|
|
s := t.TempDir()
|
|
d := t.TempDir()
|
|
|
|
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(s, "a1", "f.yaml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Mkdir(filepath.Join(d, "b1"), 0700)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(d, "b1", "f.yaml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
diff, err := Diff(s, d)
|
|
assert.NoError(t, err)
|
|
assert.ElementsMatch(t, diff.List(), []string{
|
|
"a1",
|
|
fmt.Sprintf("a1%sf.yaml", string(filepath.Separator)),
|
|
fmt.Sprintf("b1%sf.yaml", string(filepath.Separator)),
|
|
"b1",
|
|
})
|
|
}
|
|
|
|
// TestDiff_skipGitSrc verifies that .git directories in the source
|
|
// are not looked at
|
|
func TestDiff_skipGitSrc(t *testing.T) {
|
|
s := t.TempDir()
|
|
d := t.TempDir()
|
|
|
|
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(s, "a1", "f.yaml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
// files that just happen to start with .git should not be ignored.
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(s, ".gitlab-ci.yml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
// git should be ignored
|
|
err = os.Mkdir(filepath.Join(s, ".git"), 0700)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(s, ".git", "f.yaml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Mkdir(filepath.Join(d, "a1"), 0700)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(d, "a1", "f.yaml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(d, ".gitlab-ci.yml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
diff, err := Diff(s, d)
|
|
assert.NoError(t, err)
|
|
assert.Empty(t, diff.List())
|
|
}
|
|
|
|
// TestDiff_skipGitDest verifies that .git directories in the destination
|
|
// are not looked at
|
|
func TestDiff_skipGitDest(t *testing.T) {
|
|
s := t.TempDir()
|
|
d := t.TempDir()
|
|
|
|
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(s, "a1", "f.yaml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Mkdir(filepath.Join(d, "a1"), 0700)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(d, "a1", "f.yaml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
// git should be ignored
|
|
err = os.Mkdir(filepath.Join(d, ".git"), 0700)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(d, ".git", "f.yaml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
diff, err := Diff(s, d)
|
|
assert.NoError(t, err)
|
|
assert.Empty(t, diff.List())
|
|
}
|
|
|
|
// TestSyncFile tests if destination file is replaced by source file content
|
|
func TestSyncFile(t *testing.T) {
|
|
d1 := t.TempDir()
|
|
d2 := t.TempDir()
|
|
f1Name := d1 + "/temp.txt"
|
|
f2Name := d2 + "/temp.txt"
|
|
err := ioutil.WriteFile(f1Name, []byte("abc"), 0600)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(f2Name, []byte("def"), 0644)
|
|
expectedFileInfo, _ := os.Stat(f2Name)
|
|
assert.NoError(t, err)
|
|
err = SyncFile(f1Name, f2Name)
|
|
assert.NoError(t, err)
|
|
actual, err := ioutil.ReadFile(f2Name)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "abc", string(actual))
|
|
dstFileInfo, _ := os.Stat(f2Name)
|
|
assert.Equal(t, expectedFileInfo.Mode().String(), dstFileInfo.Mode().String())
|
|
}
|
|
|
|
// TestSyncFileNoDestFile tests if new file is created at destination with source file content
|
|
func TestSyncFileNoDestFile(t *testing.T) {
|
|
d1 := t.TempDir()
|
|
d2 := t.TempDir()
|
|
f1Name := d1 + "/temp.txt"
|
|
f2Name := d2 + "/temp.txt"
|
|
err := ioutil.WriteFile(f1Name, []byte("abc"), 0644)
|
|
assert.NoError(t, err)
|
|
err = SyncFile(f1Name, f2Name)
|
|
assert.NoError(t, err)
|
|
actual, err := ioutil.ReadFile(f2Name)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "abc", string(actual))
|
|
dstFileInfo, _ := os.Stat(f2Name)
|
|
srcFileInfo, _ := os.Stat(f1Name)
|
|
assert.Equal(t, srcFileInfo.Mode().String(), dstFileInfo.Mode().String())
|
|
}
|
|
|
|
// TestSyncFileNoSrcFile tests if destination file is deleted if source file doesn't exist
|
|
func TestSyncFileNoSrcFile(t *testing.T) {
|
|
d1 := t.TempDir()
|
|
d2 := t.TempDir()
|
|
f1Name := d1 + "/temp.txt"
|
|
f2Name := d2 + "/temp.txt"
|
|
err := ioutil.WriteFile(f2Name, []byte("abc"), 0644)
|
|
assert.NoError(t, err)
|
|
err = SyncFile(f1Name, f2Name)
|
|
assert.NoError(t, err)
|
|
_, err = ioutil.ReadFile(f2Name)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestPrettyFileDiff(t *testing.T) {
|
|
s1 := `apiVersion: someversion/v1alpha2
|
|
kind: ContainerCluster
|
|
metadata:
|
|
clusterName: "some_cluster"
|
|
name: asm-cluster
|
|
namespace: "PROJECT_ID" # {"$ref":"#/definitions/io.k8s.cli.setters.gcloud.core.project"}`
|
|
|
|
s2 := `apiVersion: someversion/v1alpha2
|
|
kind: ContainerCluster
|
|
metadata:
|
|
clusterName: "some_cluster"
|
|
name: asm-cluster
|
|
namespace: "some_project" # {"$ref":"#/definitions/io.k8s.cli.setters.gcloud.core.project"}`
|
|
|
|
expectedLine1 := `[31m namespace: "PROJECT_ID" # {"$ref":"#/definitions/io.k8s.cli.setters.gcloud.core.project"}`
|
|
expectedLine2 := `[32m namespace: "some_project" # {"$ref":"#/definitions/io.k8s.cli.setters.gcloud.core.project"}`
|
|
|
|
assert.Contains(t, PrettyFileDiff(s1, s2), expectedLine1)
|
|
assert.Contains(t, PrettyFileDiff(s1, s2), expectedLine2)
|
|
}
|
|
|
|
func TestCopyDir(t *testing.T) {
|
|
s := t.TempDir()
|
|
v := t.TempDir()
|
|
|
|
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(s, "a1", "f.yaml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
// files that just happen to start with .git should not be ignored.
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(s, ".gitlab-ci.yml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
// git should be ignored
|
|
err = os.Mkdir(filepath.Join(s, ".git"), 0700)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(s, ".git", "f.yaml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Mkdir(filepath.Join(v, "a1"), 0700)
|
|
assert.NoError(t, err)
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(v, "a1", "f.yaml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
err = ioutil.WriteFile(
|
|
filepath.Join(v, ".gitlab-ci.yml"), []byte(`a`), 0600)
|
|
assert.NoError(t, err)
|
|
|
|
d := t.TempDir()
|
|
|
|
err = CopyDir(s, d)
|
|
assert.NoError(t, err)
|
|
|
|
diff, err := Diff(d, v)
|
|
assert.NoError(t, err)
|
|
assert.Empty(t, diff.List())
|
|
}
|
|
|
|
func TestIsDotGitFolder(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
path string
|
|
isDotGitFolder bool
|
|
}{
|
|
{
|
|
name: ".git folder",
|
|
path: "/foo/bar/.git",
|
|
isDotGitFolder: true,
|
|
},
|
|
{
|
|
name: "subfolder of .git folder",
|
|
path: "/foo/.git/bar/zoo",
|
|
isDotGitFolder: true,
|
|
},
|
|
{
|
|
name: "subfolder of .gitignore folder",
|
|
path: "/foo/.gitignore/bar",
|
|
isDotGitFolder: false,
|
|
},
|
|
{
|
|
name: ".gitignore file",
|
|
path: "foo/bar/.gitignore",
|
|
isDotGitFolder: false,
|
|
},
|
|
{
|
|
name: ".gitlab-ci.yml under .git folder",
|
|
path: "/foo/.git/bar/.gitignore",
|
|
isDotGitFolder: true,
|
|
},
|
|
{
|
|
name: "windows path with .git folder",
|
|
path: "c:/foo/.git/bar",
|
|
isDotGitFolder: true,
|
|
},
|
|
{
|
|
name: "windows path with .gitignore file",
|
|
path: "d:/foo/bar/.gitignore",
|
|
isDotGitFolder: false,
|
|
},
|
|
}
|
|
|
|
for i := range testCases {
|
|
test := testCases[i]
|
|
t.Run(test.name, func(t *testing.T) {
|
|
assert.Equal(t, test.isDotGitFolder, IsDotGitFolder(test.path))
|
|
})
|
|
}
|
|
}
|