From 6d8f5e7479f7da05c4d688c640a4a23427724975 Mon Sep 17 00:00:00 2001 From: Dmitry Salakhov Date: Wed, 7 Apr 2021 12:12:19 +1200 Subject: [PATCH] go 1.13 compatibility --- api/archive/targz_test.go | 9 +++++---- api/backup/backup.go | 3 +-- api/backup/copy_test.go | 13 +++++++------ api/crypto/aes_test.go | 7 ++++--- api/http/handler/backup/backup_test.go | 5 +++-- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/api/archive/targz_test.go b/api/archive/targz_test.go index f91482b81..ed8a67543 100644 --- a/api/archive/targz_test.go +++ b/api/archive/targz_test.go @@ -9,6 +9,7 @@ import ( "path/filepath" "testing" + "github.com/docker/docker/pkg/ioutils" "github.com/stretchr/testify/assert" ) @@ -26,7 +27,7 @@ func listFiles(dir string) []string { } func Test_shouldCreateArhive(t *testing.T) { - tmpdir, _ := os.MkdirTemp("", "backup") + tmpdir, _ := ioutils.TempDir("", "backup") defer os.RemoveAll(tmpdir) content := []byte("content") @@ -39,7 +40,7 @@ func Test_shouldCreateArhive(t *testing.T) { assert.Nil(t, err) assert.Equal(t, filepath.Join(tmpdir, fmt.Sprintf("%s.tar.gz", filepath.Base(tmpdir))), gzPath) - extractionDir, _ := os.MkdirTemp("", "extract") + extractionDir, _ := ioutils.TempDir("", "extract") defer os.RemoveAll(extractionDir) cmd := exec.Command("tar", "-xzf", gzPath, "-C", extractionDir) @@ -62,7 +63,7 @@ func Test_shouldCreateArhive(t *testing.T) { } func Test_shouldCreateArhiveXXXXX(t *testing.T) { - tmpdir, _ := os.MkdirTemp("", "backup") + tmpdir, _ := ioutils.TempDir("", "backup") defer os.RemoveAll(tmpdir) content := []byte("content") @@ -75,7 +76,7 @@ func Test_shouldCreateArhiveXXXXX(t *testing.T) { assert.Nil(t, err) assert.Equal(t, filepath.Join(tmpdir, fmt.Sprintf("%s.tar.gz", filepath.Base(tmpdir))), gzPath) - extractionDir, _ := os.MkdirTemp("", "extract") + extractionDir, _ := ioutils.TempDir("", "extract") defer os.RemoveAll(extractionDir) r, _ := os.Open(gzPath) diff --git a/api/backup/backup.go b/api/backup/backup.go index 8c31a76f9..3da032c2c 100644 --- a/api/backup/backup.go +++ b/api/backup/backup.go @@ -2,7 +2,6 @@ package backup import ( "fmt" - "io/fs" "os" "path/filepath" "time" @@ -14,7 +13,7 @@ import ( "github.com/portainer/portainer/api/http/offlinegate" ) -const rwxr__r__ fs.FileMode = 0744 +const rwxr__r__ os.FileMode = 0744 var filesToBackup = []string{"compose", "config.json", "custom_templates", "edge_jobs", "edge_stacks", "extensions", "portainer.key", "portainer.pub", "tls"} diff --git a/api/backup/copy_test.go b/api/backup/copy_test.go index 171313181..b9ceaeaab 100644 --- a/api/backup/copy_test.go +++ b/api/backup/copy_test.go @@ -7,6 +7,7 @@ import ( "path/filepath" "testing" + "github.com/docker/docker/pkg/ioutils" "github.com/stretchr/testify/assert" ) @@ -30,7 +31,7 @@ func contains(t *testing.T, list []string, path string) { } func Test_copyFile_returnsError_whenSourceDoesNotExist(t *testing.T) { - tmpdir, _ := os.MkdirTemp("", "backup") + tmpdir, _ := ioutils.TempDir("", "backup") defer os.RemoveAll(tmpdir) err := copyFile("does-not-exist", tmpdir) @@ -38,7 +39,7 @@ func Test_copyFile_returnsError_whenSourceDoesNotExist(t *testing.T) { } func Test_copyFile_shouldMakeAbackup(t *testing.T) { - tmpdir, _ := os.MkdirTemp("", "backup") + tmpdir, _ := ioutils.TempDir("", "backup") defer os.RemoveAll(tmpdir) content := []byte("content") @@ -52,7 +53,7 @@ func Test_copyFile_shouldMakeAbackup(t *testing.T) { } func Test_copyDir_shouldCopyAllFilesAndDirectories(t *testing.T) { - destination, _ := os.MkdirTemp("", "destination") + destination, _ := ioutils.TempDir("", "destination") defer os.RemoveAll(destination) err := copyDir("./test_assets/copy_test", destination) assert.Nil(t, err) @@ -65,7 +66,7 @@ func Test_copyDir_shouldCopyAllFilesAndDirectories(t *testing.T) { } func Test_backupPath_shouldSkipWhenNotExist(t *testing.T) { - tmpdir, _ := os.MkdirTemp("", "backup") + tmpdir, _ := ioutils.TempDir("", "backup") defer os.RemoveAll(tmpdir) err := copyPath("does-not-exists", tmpdir) @@ -75,7 +76,7 @@ func Test_backupPath_shouldSkipWhenNotExist(t *testing.T) { } func Test_backupPath_shouldCopyFile(t *testing.T) { - tmpdir, _ := os.MkdirTemp("", "backup") + tmpdir, _ := ioutils.TempDir("", "backup") defer os.RemoveAll(tmpdir) content := []byte("content") @@ -91,7 +92,7 @@ func Test_backupPath_shouldCopyFile(t *testing.T) { } func Test_backupPath_shouldCopyDir(t *testing.T) { - destination, _ := os.MkdirTemp("", "destination") + destination, _ := ioutils.TempDir("", "destination") defer os.RemoveAll(destination) err := copyPath("./test_assets/copy_test", destination) assert.Nil(t, err) diff --git a/api/crypto/aes_test.go b/api/crypto/aes_test.go index d2c86f206..1a2e377ac 100644 --- a/api/crypto/aes_test.go +++ b/api/crypto/aes_test.go @@ -7,11 +7,12 @@ import ( "path/filepath" "testing" + "github.com/docker/docker/pkg/ioutils" "github.com/stretchr/testify/assert" ) func Test_encryptAndDecrypt_withTheSamePassword(t *testing.T) { - tmpdir, _ := os.MkdirTemp("", "encrypt") + tmpdir, _ := ioutils.TempDir("", "encrypt") defer os.RemoveAll(tmpdir) var ( @@ -51,7 +52,7 @@ func Test_encryptAndDecrypt_withTheSamePassword(t *testing.T) { } func Test_encryptAndDecrypt_withEmptyPassword(t *testing.T) { - tmpdir, _ := os.MkdirTemp("", "encrypt") + tmpdir, _ := ioutils.TempDir("", "encrypt") defer os.RemoveAll(tmpdir) var ( @@ -91,7 +92,7 @@ func Test_encryptAndDecrypt_withEmptyPassword(t *testing.T) { } func Test_decryptWithDifferentPassphrase_shouldProduceWrongResult(t *testing.T) { - tmpdir, _ := os.MkdirTemp("", "encrypt") + tmpdir, _ := ioutils.TempDir("", "encrypt") defer os.RemoveAll(tmpdir) var ( diff --git a/api/http/handler/backup/backup_test.go b/api/http/handler/backup/backup_test.go index fa9ccc3d8..40c7a01bc 100644 --- a/api/http/handler/backup/backup_test.go +++ b/api/http/handler/backup/backup_test.go @@ -15,6 +15,7 @@ import ( "testing" "time" + "github.com/docker/docker/pkg/ioutils" "github.com/portainer/portainer/api/adminmonitor" "github.com/portainer/portainer/api/crypto" "github.com/portainer/portainer/api/http/offlinegate" @@ -54,7 +55,7 @@ func Test_backupHandlerWithoutPassword_shouldCreateATarballArchive(t *testing.T) response := w.Result() body, _ := io.ReadAll(response.Body) - tmpdir, _ := os.MkdirTemp("", "backup") + tmpdir, _ := ioutils.TempDir("", "backup") defer os.RemoveAll(tmpdir) archivePath := filepath.Join(tmpdir, "archive.tar.gz") @@ -91,7 +92,7 @@ func Test_backupHandlerWithPassword_shouldCreateEncryptedATarballArchive(t *test response := w.Result() body, _ := io.ReadAll(response.Body) - tmpdir, _ := os.MkdirTemp("", "backup") + tmpdir, _ := ioutils.TempDir("", "backup") defer os.RemoveAll(tmpdir) dr, err := crypto.AesDecrypt(bytes.NewReader(body), []byte("secret"))