Merge pull request #4307 from reasonerjt/vol-empty-path

Return zero instead of error if the volume directory is not found at …
This commit is contained in:
Daniel Jiang 2018-02-28 14:48:43 +08:00 committed by GitHub
commit 78492a9d64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -15,9 +15,11 @@
package filesystem
import (
"os"
"syscall"
storage "github.com/vmware/harbor/src/adminserver/systeminfo/imagestorage"
"github.com/vmware/harbor/src/common/utils/log"
)
const (
@ -43,6 +45,12 @@ func (d *driver) Name() string {
// Cap returns the capacity of the filesystem storage
func (d *driver) Cap() (*storage.Capacity, error) {
var stat syscall.Statfs_t
if _, err := os.Stat(d.path); os.IsNotExist(err) {
// Return zero value if the path does not exist.
log.Warningf("The path %s is not found, will return zero value of capacity", d.path)
return &storage.Capacity{Total: 0, Free: 0}, nil
}
err := syscall.Statfs(d.path, &stat)
if err != nil {
return nil, err

View File

@ -18,6 +18,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
storage "github.com/vmware/harbor/src/adminserver/systeminfo/imagestorage"
)
func TestName(t *testing.T) {
@ -32,3 +33,11 @@ func TestCap(t *testing.T) {
_, err := driver.Cap()
assert.Nil(t, err, "unexpected error")
}
func TestCapNonExistPath(t *testing.T) {
path := "/not/exist"
driver := NewDriver(path)
c, err := driver.Cap()
assert.Nil(t, err, "unexpected error")
assert.Equal(t, storage.Capacity{0, 0}, *c)
}