This is an automated email from the ASF dual-hosted git repository.

tsato pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 170ede6d5ab9c7adb7c82abb0cf1c2a1b131a1c4
Author: Tadayoshi Sato <[email protected]>
AuthorDate: Thu Dec 1 13:07:44 2022 +0900

    fix(e2e): isolate kustomize tests not to interfere with each other
    
    Fix #3772 #3861
---
 e2e/namespace/install/kustomize/common.go        |  4 +++
 e2e/namespace/install/kustomize/operator_test.go | 10 ++++--
 e2e/namespace/install/kustomize/setup_test.go    |  7 ++--
 e2e/support/util/temp_file.go                    | 42 ++++++++++++++++--------
 pkg/util/util.go                                 | 42 ++++++++++++++++++++++--
 pkg/util/util_test.go                            | 16 +++++++++
 6 files changed, 99 insertions(+), 22 deletions(-)

diff --git a/e2e/namespace/install/kustomize/common.go 
b/e2e/namespace/install/kustomize/common.go
index f697e43a6..15cc9bec4 100644
--- a/e2e/namespace/install/kustomize/common.go
+++ b/e2e/namespace/install/kustomize/common.go
@@ -59,6 +59,8 @@ const (
 )
 
 func ExecMake(t *testing.T, command *exec.Cmd) {
+       t.Helper()
+
        var cmdOut strings.Builder
        var cmdErr strings.Builder
 
@@ -80,6 +82,8 @@ func ExecMake(t *testing.T, command *exec.Cmd) {
 // Expect a make error with an exit code of 1
 //
 func ExecMakeError(t *testing.T, command *exec.Cmd) {
+       t.Helper()
+
        var cmdOut strings.Builder
        var cmdErr strings.Builder
 
diff --git a/e2e/namespace/install/kustomize/operator_test.go 
b/e2e/namespace/install/kustomize/operator_test.go
index 1992548a9..03654d05f 100644
--- a/e2e/namespace/install/kustomize/operator_test.go
+++ b/e2e/namespace/install/kustomize/operator_test.go
@@ -28,11 +28,13 @@ import (
        "testing"
 
        . "github.com/apache/camel-k/e2e/support"
+       testutil "github.com/apache/camel-k/e2e/support/util"
        . "github.com/onsi/gomega"
 )
 
 func TestBasicOperator(t *testing.T) {
-       os.Setenv("CAMEL_K_TEST_MAKE_DIR", "../../../../install")
+       makeDir := testutil.MakeTempCopyDir(t, "../../../../install")
+       os.Setenv("CAMEL_K_TEST_MAKE_DIR", makeDir)
 
        // Ensure no CRDs are already installed
        UninstallAll()
@@ -50,7 +52,8 @@ func TestBasicOperator(t *testing.T) {
 }
 
 func TestAlternativeImageOperator(t *testing.T) {
-       os.Setenv("CAMEL_K_TEST_MAKE_DIR", "../../../../install")
+       makeDir := testutil.MakeTempCopyDir(t, "../../../../install")
+       os.Setenv("CAMEL_K_TEST_MAKE_DIR", makeDir)
 
        // Ensure no CRDs are already installed
        UninstallAll()
@@ -72,7 +75,8 @@ func TestAlternativeImageOperator(t *testing.T) {
 }
 
 func TestGlobalOperator(t *testing.T) {
-       os.Setenv("CAMEL_K_TEST_MAKE_DIR", "../../../../install")
+       makeDir := testutil.MakeTempCopyDir(t, "../../../../install")
+       os.Setenv("CAMEL_K_TEST_MAKE_DIR", makeDir)
 
        // Ensure no CRDs are already installed
        UninstallAll()
diff --git a/e2e/namespace/install/kustomize/setup_test.go 
b/e2e/namespace/install/kustomize/setup_test.go
index 18cb75b5d..eef0b1a40 100644
--- a/e2e/namespace/install/kustomize/setup_test.go
+++ b/e2e/namespace/install/kustomize/setup_test.go
@@ -28,11 +28,13 @@ import (
        "testing"
 
        . "github.com/apache/camel-k/e2e/support"
+       testutil "github.com/apache/camel-k/e2e/support/util"
        . "github.com/onsi/gomega"
 )
 
 func TestBasicSetup(t *testing.T) {
-       os.Setenv("CAMEL_K_TEST_MAKE_DIR", "../../../../install")
+       makeDir := testutil.MakeTempCopyDir(t, "../../../../install")
+       os.Setenv("CAMEL_K_TEST_MAKE_DIR", makeDir)
 
        // Ensure no CRDs are already installed
        UninstallAll()
@@ -61,7 +63,8 @@ func TestBasicSetup(t *testing.T) {
 }
 
 func TestGlobalSetup(t *testing.T) {
-       os.Setenv("CAMEL_K_TEST_MAKE_DIR", "../../../../install")
+       makeDir := testutil.MakeTempCopyDir(t, "../../../../install")
+       os.Setenv("CAMEL_K_TEST_MAKE_DIR", makeDir)
 
        // Ensure no CRDs are already installed
        UninstallAll()
diff --git a/e2e/support/util/temp_file.go b/e2e/support/util/temp_file.go
index e10a3e326..b6c80f40b 100644
--- a/e2e/support/util/temp_file.go
+++ b/e2e/support/util/temp_file.go
@@ -21,48 +21,62 @@ limitations under the License.
 package util
 
 import (
-       "io/ioutil"
        "os"
-       "path"
+       "path/filepath"
        "strings"
        "testing"
+
+       "github.com/apache/camel-k/pkg/util"
 )
 
 func MakeTempCopy(t *testing.T, fileName string) string {
-       _, simpleName := path.Split(fileName)
-       var err error
+       t.Helper()
+
+       _, simpleName := filepath.Split(fileName)
        tmpDir := MakeTempDir(t)
-       tmpFileName := path.Join(tmpDir, simpleName)
-       var content []byte
-       if content, err = ioutil.ReadFile(fileName); err != nil {
+       tmpFileName := filepath.Join(tmpDir, simpleName)
+       if _, err := util.CopyFile(fileName, tmpFileName); err != nil {
                t.Error(err)
                t.FailNow()
        }
-       if err = ioutil.WriteFile(tmpFileName, content, os.FileMode(0777)); err 
!= nil {
+       return tmpFileName
+}
+
+func MakeTempCopyDir(t *testing.T, dirName string) string {
+       t.Helper()
+
+       dirName = strings.TrimSuffix(dirName, "/")
+       _, simpleName := filepath.Split(dirName)
+       tmpDir := MakeTempDir(t)
+       tmpDirName := filepath.Join(tmpDir, simpleName)
+       if err := util.CopyDir(dirName, tmpDirName); err != nil {
                t.Error(err)
                t.FailNow()
        }
-       return tmpFileName
+       return tmpDirName
 }
 
 // ReplaceInFile replace strings in a file with new values
 func ReplaceInFile(t *testing.T, fileName string, old, new string) {
-       content, err := ioutil.ReadFile(fileName)
+       t.Helper()
+
+       content, err := os.ReadFile(fileName)
        if err != nil {
                t.Error(err)
                t.FailNow()
        }
        res := strings.ReplaceAll(string(content), old, new)
-       if err = ioutil.WriteFile(fileName, []byte(res), os.FileMode(0777)); 
err != nil {
+       if err = os.WriteFile(fileName, []byte(res), os.FileMode(0777)); err != 
nil {
                t.Error(err)
                t.FailNow()
        }
 }
 
 func MakeTempDir(t *testing.T) string {
-       var tmpDir string
-       var err error
-       if tmpDir, err = ioutil.TempDir("", "camel-k-"); err != nil {
+       t.Helper()
+
+       tmpDir, err := os.MkdirTemp("", "camel-k-")
+       if err != nil {
                t.Error(err)
                t.FailNow()
                return ""
diff --git a/pkg/util/util.go b/pkg/util/util.go
index 25ca86517..80fb19910 100644
--- a/pkg/util/util.go
+++ b/pkg/util/util.go
@@ -23,7 +23,7 @@ import (
        "encoding/xml"
        "fmt"
        "io"
-       "io/ioutil"
+       "io/fs"
        "math/rand"
        "os"
        "path"
@@ -288,6 +288,42 @@ func CopyFile(src, dst string) (int64, error) {
        return nBytes, err
 }
 
+func CopyDir(src, dst string) error {
+       entries, err := os.ReadDir(src)
+       if err != nil {
+               return err
+       }
+
+       for _, entry := range entries {
+               srcEntry := filepath.Join(src, entry.Name())
+               dstEntry := filepath.Join(dst, entry.Name())
+               realEntry := entry
+               if entry.Type() == os.ModeSymlink {
+                       realPath, err := filepath.EvalSymlinks(srcEntry)
+                       if err != nil {
+                               return err
+                       }
+                       realInfo, err := os.Stat(realPath)
+                       if err != nil {
+                               return err
+                       }
+                       srcEntry = realPath
+                       realEntry = fs.FileInfoToDirEntry(realInfo)
+               }
+               if realEntry.IsDir() {
+                       if err := CopyDir(srcEntry, dstEntry); err != nil {
+                               return err
+                       }
+               } else {
+                       if _, err := CopyFile(srcEntry, dstEntry); err != nil {
+                               return err
+                       }
+               }
+       }
+
+       return nil
+}
+
 func WriteFileWithBytesMarshallerContent(basePath string, filePath string, 
content BytesMarshaller) error {
        data, err := content.MarshalBytes()
        if err != nil {
@@ -452,7 +488,7 @@ func MapToYAML(src map[string]interface{}) ([]byte, error) {
 }
 
 func WriteToFile(filePath string, fileContents string) error {
-       err := ioutil.WriteFile(filePath, []byte(fileContents), 0o400)
+       err := os.WriteFile(filePath, []byte(fileContents), 0o400)
        if err != nil {
                return errors.Errorf("error writing file: %v", filePath)
        }
@@ -598,7 +634,7 @@ func WriteFileWithContent(filePath string, content []byte) 
error {
 
 // WithTempDir a safe wrapper to deal with temporary directories.
 func WithTempDir(pattern string, consumer func(string) error) error {
-       tmpDir, err := ioutil.TempDir("", pattern)
+       tmpDir, err := os.MkdirTemp("", pattern)
        if err != nil {
                return err
        }
diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go
index 597308d25..6a428b234 100644
--- a/pkg/util/util_test.go
+++ b/pkg/util/util_test.go
@@ -18,9 +18,13 @@ limitations under the License.
 package util
 
 import (
+       "fmt"
+       "os"
+       "path/filepath"
        "testing"
 
        "github.com/stretchr/testify/assert"
+       "github.com/stretchr/testify/require"
 )
 
 func TestStringContainsPrefix(t *testing.T) {
@@ -44,3 +48,15 @@ func TestSubstringBefore(t *testing.T) {
        assert.Equal(t, "aaa/bbb", SubstringBefore("aaa/bbb?ccc=ddd", "?"))
        assert.Empty(t, SubstringBefore("aaa/bbb/ccc", "?"))
 }
+
+func TestCopyDir(t *testing.T) {
+       srcDir := "../../install"
+       tmpDir, err := os.MkdirTemp("", "TestCopyDir-*")
+       defer os.RemoveAll(tmpDir)
+       destDir := filepath.Join(tmpDir, "install")
+
+       require.NoError(t, err)
+       fmt.Println(destDir)
+       err = CopyDir(srcDir, destDir)
+       assert.NoError(t, err)
+}

Reply via email to