Skip to content

Commit

Permalink
Switch the ocis driver over to the decomposed base driver
Browse files Browse the repository at this point in the history
  • Loading branch information
aduffeck committed Feb 12, 2021
1 parent cfc7666 commit af4f3e5
Show file tree
Hide file tree
Showing 19 changed files with 403 additions and 3,858 deletions.
18 changes: 18 additions & 0 deletions pkg/storage/fs/decomposed/options/options_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
// 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 options_test

import (
Expand Down
85 changes: 85 additions & 0 deletions pkg/storage/fs/ocis/blobstore/blobstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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 blobstore

import (
"bufio"
"io"
"io/ioutil"
"os"
"path"

"github.com/pkg/errors"
)

// Blobstore provides an interface to an filesystem based blobstore
type Blobstore struct {
root string
}

// New returns a new Blobstore
func New(root string) (*Blobstore, error) {
err := os.MkdirAll(root, 0700)
if err != nil {
return nil, err
}

return &Blobstore{
root: root,
}, nil
}

// Upload stores some data in the blobstore under the given key
func (bs *Blobstore) Upload(key string, data io.Reader) error {
f, err := os.OpenFile(bs.path(key), os.O_CREATE|os.O_WRONLY, 0700)
if err != nil {
return errors.Wrapf(err, "could not open blob '%s' for writing", key)
}

w := bufio.NewWriter(f)
_, err = w.ReadFrom(data)
if err != nil {
return errors.Wrapf(err, "could not write blob '%s'", key)
}

w.Flush()
return nil
}

// Download retrieves a blob from the blobstore for reading
func (bs *Blobstore) Download(key string) (io.ReadCloser, error) {
file, err := os.Open(path.Join(bs.root, key))
if err != nil {
return nil, errors.Wrapf(err, "could not read blob '%s'", key)
}
return ioutil.NopCloser(file), nil
}

// Delete deletes a blob from the blobstore
func (bs *Blobstore) Delete(key string) error {
err := os.Remove(bs.path(key))
if err != nil {
return errors.Wrapf(err, "could not delete blod '%s'", key)
}
return nil
}

func (bs *Blobstore) path(key string) string {
return path.Join(bs.root, key)
}
31 changes: 31 additions & 0 deletions pkg/storage/fs/ocis/blobstore/blobstore_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 blobstore_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestBlobstore(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Blobstore Suite")
}
109 changes: 109 additions & 0 deletions pkg/storage/fs/ocis/blobstore/blobstore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// 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 blobstore_test

import (
"bytes"
"io/ioutil"
"os"
"path"
"strings"

"github.com/cs3org/reva/pkg/storage/fs/ocis/blobstore"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Blobstore", func() {
var (
tmpRoot string
key string
blobPath string
data []byte

bs *blobstore.Blobstore
)

BeforeEach(func() {
var err error
tmpRoot, err = ioutil.TempDir("", "reva-unit-tests-*-root")
Expect(err).ToNot(HaveOccurred())

data = []byte("1234567890")
key = "foo"
blobPath = path.Join(tmpRoot, "blobs", key)

bs, err = blobstore.New(path.Join(tmpRoot, "blobs"))
Expect(err).ToNot(HaveOccurred())
})

AfterEach(func() {
if strings.HasPrefix(tmpRoot, os.TempDir()) {
os.RemoveAll(tmpRoot)
}
})

It("creates the root directory if it doesn't exist", func() {
_, err := os.Stat(path.Join(tmpRoot, "blobs"))
Expect(err).ToNot(HaveOccurred())
})

Describe("Upload", func() {
It("writes the blob", func() {
err := bs.Upload(key, bytes.NewReader(data))
Expect(err).ToNot(HaveOccurred())

writtenBytes, err := ioutil.ReadFile(blobPath)
Expect(err).ToNot(HaveOccurred())
Expect(writtenBytes).To(Equal(data))
})
})

Context("with an existing blob", func() {
BeforeEach(func() {
Expect(ioutil.WriteFile(blobPath, data, 0700)).To(Succeed())
})

Describe("Download", func() {
It("returns a reader to the blob", func() {
reader, err := bs.Download(key)
Expect(err).ToNot(HaveOccurred())

readData, err := ioutil.ReadAll(reader)
Expect(err).ToNot(HaveOccurred())
Expect(readData).To(Equal(data))
})
})

Describe("Delete", func() {
It("deletes the blob", func() {
_, err := os.Stat(blobPath)
Expect(err).ToNot(HaveOccurred())

err = bs.Delete(key)
Expect(err).ToNot(HaveOccurred())

_, err = os.Stat(blobPath)
Expect(err).To(HaveOccurred())
})
})
})

})
Loading

0 comments on commit af4f3e5

Please sign in to comment.