github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/decomposedfs/lookup/lookup_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 lookup_test
    20  
    21  import (
    22  	provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
    23  	"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/node"
    24  	helpers "github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/testhelpers"
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  )
    28  
    29  var _ = Describe("Lookup", func() {
    30  	var (
    31  		env *helpers.TestEnv
    32  	)
    33  
    34  	JustBeforeEach(func() {
    35  		var err error
    36  		env, err = helpers.NewTestEnv(nil)
    37  		Expect(err).ToNot(HaveOccurred())
    38  	})
    39  
    40  	AfterEach(func() {
    41  		if env != nil {
    42  			env.Cleanup()
    43  		}
    44  	})
    45  
    46  	Describe("Node from path", func() {
    47  		It("returns the path including a leading slash", func() {
    48  			n, err := env.Lookup.NodeFromResource(env.Ctx, &provider.Reference{
    49  				ResourceId: env.SpaceRootRes,
    50  				Path:       "/dir1/file1",
    51  			})
    52  			Expect(err).ToNot(HaveOccurred())
    53  
    54  			path, err := env.Lookup.Path(env.Ctx, n, func(n *node.Node) bool { return true })
    55  			Expect(err).ToNot(HaveOccurred())
    56  			Expect(path).To(Equal("/dir1/file1"))
    57  		})
    58  	})
    59  
    60  	Describe("Node From Resource only by path", func() {
    61  		It("returns the path including a leading slash and the space root is set", func() {
    62  			n, err := env.Lookup.NodeFromResource(env.Ctx, &provider.Reference{
    63  				ResourceId: env.SpaceRootRes,
    64  				Path:       "/dir1/subdir1/file2",
    65  			})
    66  			Expect(err).ToNot(HaveOccurred())
    67  
    68  			path, err := env.Lookup.Path(env.Ctx, n, func(n *node.Node) bool { return true })
    69  			Expect(err).ToNot(HaveOccurred())
    70  			Expect(path).To(Equal("/dir1/subdir1/file2"))
    71  			Expect(n.SpaceRoot.Name).To(Equal("userid"))
    72  			Expect(n.SpaceRoot.ParentID).To(Equal("root"))
    73  		})
    74  	})
    75  
    76  	Describe("Node From Resource only by id", func() {
    77  		It("returns the path including a leading slash and the space root is set", func() {
    78  			// do a node lookup by path
    79  			nRef, err := env.Lookup.NodeFromResource(env.Ctx, &provider.Reference{
    80  				ResourceId: env.SpaceRootRes,
    81  				Path:       "/dir1/file1",
    82  			})
    83  			Expect(err).ToNot(HaveOccurred())
    84  
    85  			// try to find the same node by id
    86  			n, err := env.Lookup.NodeFromResource(env.Ctx, &provider.Reference{ResourceId: &provider.ResourceId{OpaqueId: nRef.ID}})
    87  			Expect(err).ToNot(HaveOccurred())
    88  			Expect(n.SpaceRoot).ToNot(BeNil())
    89  
    90  			// Check if we got the right node and spaceRoot
    91  			path, err := env.Lookup.Path(env.Ctx, n, func(n *node.Node) bool { return true })
    92  			Expect(err).ToNot(HaveOccurred())
    93  			Expect(path).To(Equal("/dir1/file1"))
    94  			Expect(n.SpaceRoot.Name).To(Equal("userid"))
    95  			Expect(n.SpaceRoot.ParentID).To(Equal("root"))
    96  		})
    97  	})
    98  
    99  	Describe("Node From Resource by id and relative path", func() {
   100  		It("returns the path including a leading slash and the space root is set", func() {
   101  			// do a node lookup by path for the parent
   102  			nRef, err := env.Lookup.NodeFromResource(env.Ctx, &provider.Reference{
   103  				ResourceId: env.SpaceRootRes,
   104  				Path:       "/dir1",
   105  			})
   106  			Expect(err).ToNot(HaveOccurred())
   107  
   108  			// try to find the child node by parent id and relative path
   109  			n, err := env.Lookup.NodeFromResource(env.Ctx, &provider.Reference{ResourceId: &provider.ResourceId{OpaqueId: nRef.ID}, Path: "./file1"})
   110  			Expect(err).ToNot(HaveOccurred())
   111  			Expect(n.SpaceRoot).ToNot(BeNil())
   112  
   113  			// Check if we got the right node and spaceRoot
   114  			path, err := env.Lookup.Path(env.Ctx, n, func(n *node.Node) bool { return true })
   115  			Expect(err).ToNot(HaveOccurred())
   116  			Expect(path).To(Equal("/dir1/file1"))
   117  			Expect(n.SpaceRoot.Name).To(Equal("userid"))
   118  			Expect(n.SpaceRoot.ParentID).To(Equal("root"))
   119  		})
   120  	})
   121  })