Return zero instead of error if the volume directory is not found at admin server

This commit is contained in:
Tan Jiang 2018-02-27 19:13:19 +08:00
parent ae2f2dfc6d
commit fc4e427c41
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)
}