diff --git a/.gitignore b/.gitignore index 23f5b09ef0..b0664b8a45 100644 --- a/.gitignore +++ b/.gitignore @@ -46,3 +46,5 @@ composer.lock vendor-bin/**/vendor vendor-bin/**/composer.lock tests/acceptance/output +tmp/ + diff --git a/pkg/storage/fs/ocis/blobstore/blobstore_test.go b/pkg/storage/fs/ocis/blobstore/blobstore_test.go index 7e213ba5af..450226a7fa 100644 --- a/pkg/storage/fs/ocis/blobstore/blobstore_test.go +++ b/pkg/storage/fs/ocis/blobstore/blobstore_test.go @@ -23,9 +23,9 @@ import ( "io/ioutil" "os" "path" - "strings" "github.com/cs3org/reva/pkg/storage/fs/ocis/blobstore" + "github.com/cs3org/reva/tests/helpers" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -43,7 +43,7 @@ var _ = Describe("Blobstore", func() { BeforeEach(func() { var err error - tmpRoot, err = ioutil.TempDir("", "reva-unit-tests-*-root") + tmpRoot, err = helpers.TempDir("reva-unit-tests-*-root") Expect(err).ToNot(HaveOccurred()) data = []byte("1234567890") @@ -55,7 +55,7 @@ var _ = Describe("Blobstore", func() { }) AfterEach(func() { - if strings.HasPrefix(tmpRoot, os.TempDir()) { + if tmpRoot != "" { os.RemoveAll(tmpRoot) } }) diff --git a/pkg/storage/fs/ocis/ocis_test.go b/pkg/storage/fs/ocis/ocis_test.go index 6bb4b49971..e2f4e32efc 100644 --- a/pkg/storage/fs/ocis/ocis_test.go +++ b/pkg/storage/fs/ocis/ocis_test.go @@ -19,11 +19,10 @@ package ocis_test import ( - "io/ioutil" "os" - "strings" "github.com/cs3org/reva/pkg/storage/fs/ocis" + "github.com/cs3org/reva/tests/helpers" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -36,7 +35,7 @@ var _ = Describe("Ocis", func() { ) BeforeEach(func() { - tmpRoot, err := ioutil.TempDir("", "reva-unit-tests-*-root") + tmpRoot, err := helpers.TempDir("reva-unit-tests-*-root") Expect(err).ToNot(HaveOccurred()) options = map[string]interface{}{ @@ -47,7 +46,7 @@ var _ = Describe("Ocis", func() { }) AfterEach(func() { - if strings.HasPrefix(tmpRoot, os.TempDir()) { + if tmpRoot != "" { os.RemoveAll(tmpRoot) } }) diff --git a/pkg/storage/fs/s3ng/s3ng_test.go b/pkg/storage/fs/s3ng/s3ng_test.go index e4fc6984b5..697006f8bb 100644 --- a/pkg/storage/fs/s3ng/s3ng_test.go +++ b/pkg/storage/fs/s3ng/s3ng_test.go @@ -19,11 +19,10 @@ package s3ng_test import ( - "io/ioutil" "os" - "strings" "github.com/cs3org/reva/pkg/storage/fs/s3ng" + "github.com/cs3org/reva/tests/helpers" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -36,7 +35,7 @@ var _ = Describe("S3ng", func() { ) BeforeEach(func() { - tmpRoot, err := ioutil.TempDir("", "reva-unit-tests-*-root") + tmpRoot, err := helpers.TempDir("reva-unit-tests-*-root") Expect(err).ToNot(HaveOccurred()) options = map[string]interface{}{ @@ -52,7 +51,7 @@ var _ = Describe("S3ng", func() { }) AfterEach(func() { - if strings.HasPrefix(tmpRoot, os.TempDir()) { + if tmpRoot != "" { os.RemoveAll(tmpRoot) } }) diff --git a/pkg/storage/utils/decomposedfs/decomposedfs_concurrency_test.go b/pkg/storage/utils/decomposedfs/decomposedfs_concurrency_test.go index 02bcf02175..2db853e810 100644 --- a/pkg/storage/utils/decomposedfs/decomposedfs_concurrency_test.go +++ b/pkg/storage/utils/decomposedfs/decomposedfs_concurrency_test.go @@ -24,7 +24,6 @@ import ( "io/ioutil" "os" "path" - "strings" "sync" userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" @@ -33,6 +32,7 @@ import ( "github.com/cs3org/reva/pkg/storage/utils/decomposedfs" treemocks "github.com/cs3org/reva/pkg/storage/utils/decomposedfs/tree/mocks" "github.com/cs3org/reva/pkg/user" + "github.com/cs3org/reva/tests/helpers" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) @@ -46,7 +46,7 @@ var _ = Describe("Decomposed", func() { ) BeforeEach(func() { - tmpRoot, err := ioutil.TempDir("", "reva-unit-tests-*-root") + tmpRoot, err := helpers.TempDir("reva-unit-tests-*-root") Expect(err).ToNot(HaveOccurred()) options = map[string]interface{}{ @@ -77,7 +77,7 @@ var _ = Describe("Decomposed", func() { }) AfterEach(func() { - if strings.HasPrefix(tmpRoot, os.TempDir()) { + if tmpRoot != "" { os.RemoveAll(tmpRoot) } }) diff --git a/pkg/storage/utils/decomposedfs/testhelpers/helpers.go b/pkg/storage/utils/decomposedfs/testhelpers/helpers.go index 808ef0215d..73015d9251 100644 --- a/pkg/storage/utils/decomposedfs/testhelpers/helpers.go +++ b/pkg/storage/utils/decomposedfs/testhelpers/helpers.go @@ -20,7 +20,6 @@ package helpers import ( "context" - "io/ioutil" "os" "path/filepath" @@ -36,6 +35,7 @@ import ( "github.com/cs3org/reva/pkg/storage/utils/decomposedfs/tree" treemocks "github.com/cs3org/reva/pkg/storage/utils/decomposedfs/tree/mocks" ruser "github.com/cs3org/reva/pkg/user" + "github.com/cs3org/reva/tests/helpers" ) // TestEnv represents a test environment for unit tests @@ -57,7 +57,7 @@ type TestEnv struct { // /dir1/file1 // /dir1/subdir1/ func NewTestEnv() (*TestEnv, error) { - tmpRoot, err := ioutil.TempDir("", "reva-unit-tests-*-root") + tmpRoot, err := helpers.TempDir("reva-unit-tests-*-root") if err != nil { return nil, err } diff --git a/pkg/storage/utils/decomposedfs/upload_test.go b/pkg/storage/utils/decomposedfs/upload_test.go index 89b1040c12..ebe2791428 100644 --- a/pkg/storage/utils/decomposedfs/upload_test.go +++ b/pkg/storage/utils/decomposedfs/upload_test.go @@ -24,7 +24,6 @@ import ( "io" "io/ioutil" "os" - "strings" userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" @@ -37,6 +36,7 @@ import ( "github.com/cs3org/reva/pkg/storage/utils/decomposedfs/tree" treemocks "github.com/cs3org/reva/pkg/storage/utils/decomposedfs/tree/mocks" ruser "github.com/cs3org/reva/pkg/user" + "github.com/cs3org/reva/tests/helpers" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -70,7 +70,7 @@ var _ = Describe("File uploads", func() { } ctx = ruser.ContextSetUser(context.Background(), user) - tmpRoot, err := ioutil.TempDir("", "reva-unit-tests-*-root") + tmpRoot, err := helpers.TempDir("reva-unit-tests-*-root") Expect(err).ToNot(HaveOccurred()) o, err = options.New(map[string]interface{}{ @@ -84,7 +84,7 @@ var _ = Describe("File uploads", func() { AfterEach(func() { root := o.Root - if strings.HasPrefix(root, os.TempDir()) { + if root != "" { os.RemoveAll(root) } }) diff --git a/tests/helpers/helpers.go b/tests/helpers/helpers.go new file mode 100644 index 0000000000..6b6ba37248 --- /dev/null +++ b/tests/helpers/helpers.go @@ -0,0 +1,46 @@ +// Copyright 2018-2021 CERN +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// In applying this license, CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +package helpers + +import ( + "io/ioutil" + "os" + "path/filepath" + "runtime" +) + +// TempDir creates a temporary directory in tmp/ and returns its path +// +// Temporary test directories are created in reva/tmp because system +// /tmp directories are often tmpfs mounts which do not support user +// extended attributes. +func TempDir(name string) (string, error) { + _, currentFileName, _, _ := runtime.Caller(0) + tmpDir := filepath.Join(filepath.Dir(currentFileName), "../../tmp") + err := os.MkdirAll(tmpDir, 0755) + if err != nil { + return "nil", err + } + tmpRoot, err := ioutil.TempDir(tmpDir, "reva-unit-tests-*-root") + if err != nil { + return "nil", err + } + + return tmpRoot, nil +}