github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/decomposedfs/decomposedfs_test.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package decomposedfs_test
    20  
    21  import (
    22  	"github.com/cs3org/reva/v2/pkg/errtypes"
    23  	"github.com/stretchr/testify/mock"
    24  
    25  	provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
    26  	"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs"
    27  	helpers "github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/testhelpers"
    28  	treemocks "github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/tree/mocks"
    29  
    30  	. "github.com/onsi/ginkgo/v2"
    31  	. "github.com/onsi/gomega"
    32  )
    33  
    34  var _ = Describe("Decomposed", func() {
    35  	var (
    36  		env *helpers.TestEnv
    37  
    38  		ref *provider.Reference
    39  	)
    40  
    41  	JustBeforeEach(func() {
    42  		var err error
    43  		env, err = helpers.NewTestEnv(nil)
    44  		Expect(err).ToNot(HaveOccurred())
    45  
    46  		ref = &provider.Reference{
    47  			ResourceId: env.SpaceRootRes,
    48  			Path:       "/dir1",
    49  		}
    50  	})
    51  
    52  	AfterEach(func() {
    53  		if env != nil {
    54  			env.Cleanup()
    55  		}
    56  	})
    57  
    58  	Describe("NewDefault", func() {
    59  		It("works", func() {
    60  			bs := &treemocks.Blobstore{}
    61  			_, err := decomposedfs.NewDefault(map[string]interface{}{
    62  				"root":           env.Root,
    63  				"permissionssvc": "any",
    64  			}, bs, nil, nil)
    65  			Expect(err).ToNot(HaveOccurred())
    66  		})
    67  	})
    68  
    69  	Describe("CreateDir", func() {
    70  		Context("Existing and non existing parent folders", func() {
    71  			It("CreateDir succeeds", func() {
    72  				dir2 := &provider.Reference{
    73  					ResourceId: env.SpaceRootRes,
    74  					Path:       "/dir2",
    75  				}
    76  				env.Permissions.On("AssemblePermissions", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ResourcePermissions{CreateContainer: true, Stat: true}, nil)
    77  				err := env.Fs.CreateDir(env.Ctx, dir2)
    78  				Expect(err).ToNot(HaveOccurred())
    79  				ri, err := env.Fs.GetMD(env.Ctx, dir2, []string{}, []string{})
    80  				Expect(err).ToNot(HaveOccurred())
    81  				Expect(ri.Path).To(Equal(dir2.Path))
    82  			})
    83  			It("CreateDir succeeds in subdir", func() {
    84  				dir2 := &provider.Reference{
    85  					ResourceId: env.SpaceRootRes,
    86  					Path:       "/dir1/dir2",
    87  				}
    88  				env.Permissions.On("AssemblePermissions", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ResourcePermissions{CreateContainer: true, Stat: true}, nil)
    89  				err := env.Fs.CreateDir(env.Ctx, dir2)
    90  				Expect(err).ToNot(HaveOccurred())
    91  				ri, err := env.Fs.GetMD(env.Ctx, dir2, []string{}, []string{})
    92  				Expect(err).ToNot(HaveOccurred())
    93  				Expect(ri.Path).To(Equal(dir2.Path))
    94  			})
    95  			It("dir already exists", func() {
    96  				env.Permissions.On("AssemblePermissions", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ResourcePermissions{CreateContainer: true}, nil)
    97  				err := env.Fs.CreateDir(env.Ctx, ref)
    98  				Expect(err).To(HaveOccurred())
    99  				Expect(err).Should(MatchError(errtypes.AlreadyExists("/dir1")))
   100  			})
   101  			It("dir already exists in subdir", func() {
   102  				dir3 := &provider.Reference{
   103  					ResourceId: env.SpaceRootRes,
   104  					Path:       "/dir1/dir3",
   105  				}
   106  				env.Permissions.On("AssemblePermissions", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ResourcePermissions{CreateContainer: true}, nil)
   107  				err := env.Fs.CreateDir(env.Ctx, dir3)
   108  				Expect(err).ToNot(HaveOccurred())
   109  				err = env.Fs.CreateDir(env.Ctx, dir3)
   110  				Expect(err).To(HaveOccurred())
   111  				Expect(err).Should(MatchError(errtypes.AlreadyExists("/dir1/dir3")))
   112  			})
   113  			It("CreateDir fails in subdir", func() {
   114  				dir2 := &provider.Reference{
   115  					ResourceId: env.SpaceRootRes,
   116  					Path:       "/dir1/dir2/dir3",
   117  				}
   118  				env.Permissions.On("AssemblePermissions", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ResourcePermissions{CreateContainer: true}, nil)
   119  				err := env.Fs.CreateDir(env.Ctx, dir2)
   120  				Expect(err).To(HaveOccurred())
   121  				Expect(err).Should(MatchError(errtypes.PreconditionFailed("/dir1/dir2")))
   122  			})
   123  			It("CreateDir fails in non existing sub subbdir", func() {
   124  				dir2 := &provider.Reference{
   125  					ResourceId: env.SpaceRootRes,
   126  					Path:       "/dir1/dir2/dir3/dir4",
   127  				}
   128  				env.Permissions.On("AssemblePermissions", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ResourcePermissions{CreateContainer: true}, nil)
   129  				err := env.Fs.CreateDir(env.Ctx, dir2)
   130  				Expect(err).To(HaveOccurred())
   131  				Expect(err).Should(MatchError(errtypes.PreconditionFailed("error: not found: dir2")))
   132  			})
   133  		})
   134  	})
   135  
   136  	Describe("Delete", func() {
   137  		Context("with no permissions", func() {
   138  			It("returns an error", func() {
   139  				env.Permissions.On("AssemblePermissions", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ResourcePermissions{}, nil)
   140  
   141  				err := env.Fs.Delete(env.Ctx, ref)
   142  
   143  				Expect(err).To(MatchError(ContainSubstring("not found")))
   144  			})
   145  		})
   146  
   147  		Context("with insufficient permissions", func() {
   148  			It("returns an error", func() {
   149  				env.Permissions.On("AssemblePermissions", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ResourcePermissions{
   150  					Stat:   true,
   151  					Delete: false,
   152  				}, nil)
   153  
   154  				err := env.Fs.Delete(env.Ctx, ref)
   155  
   156  				Expect(err).To(MatchError(ContainSubstring("permission denied")))
   157  			})
   158  		})
   159  
   160  		Context("with sufficient permissions", func() {
   161  			JustBeforeEach(func() {
   162  				env.Permissions.On("AssemblePermissions", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ResourcePermissions{
   163  					Stat:   true,
   164  					Delete: true,
   165  				}, nil)
   166  			})
   167  
   168  			It("does not (yet) delete the blob from the blobstore", func() {
   169  				err := env.Fs.Delete(env.Ctx, ref)
   170  
   171  				Expect(err).ToNot(HaveOccurred())
   172  				env.Blobstore.AssertNotCalled(GinkgoT(), "Delete", mock.AnythingOfType("string"))
   173  			})
   174  		})
   175  	})
   176  })