mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-01-07 13:55:20 +00:00
fix: delete empty backups
Signed-off-by: Yuval Manor <yuvalman958@gmail.com>
This commit is contained in:
@@ -29,6 +29,8 @@ import (
|
||||
"github.com/vmware-tanzu/velero/pkg/util/filesystem"
|
||||
)
|
||||
|
||||
var ErrNotExist = errors.New("does not exist")
|
||||
|
||||
// Parser traverses an extracted archive on disk to validate
|
||||
// it and provide a helpful representation of it to consumers.
|
||||
type Parser struct {
|
||||
@@ -66,17 +68,9 @@ func (p *Parser) Parse(dir string) (map[string]*ResourceItems, error) {
|
||||
// ensure top-level "resources" directory exists, and read subdirectories
|
||||
// of it, where each one is expected to correspond to a resource.
|
||||
resourcesDir := filepath.Join(dir, velerov1api.ResourcesDir)
|
||||
exists, err := p.fs.DirExists(resourcesDir)
|
||||
resourceDirs, err := p.checkAndReadDir(resourcesDir)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error checking for existence of directory %q", strings.TrimPrefix(resourcesDir, dir+"/"))
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.Errorf("directory %q does not exist", strings.TrimPrefix(resourcesDir, dir+"/"))
|
||||
}
|
||||
|
||||
resourceDirs, err := p.fs.ReadDir(resourcesDir)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error reading contents of directory %q", strings.TrimPrefix(resourcesDir, dir+"/"))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// loop through each subdirectory (one per resource) and assemble
|
||||
@@ -173,15 +167,15 @@ func (p *Parser) getResourceItemsForScope(dir, archiveRootDir string) ([]string,
|
||||
func (p *Parser) checkAndReadDir(dir string) ([]os.FileInfo, error) {
|
||||
exists, err := p.fs.DirExists(dir)
|
||||
if err != nil {
|
||||
return []os.FileInfo{}, errors.Wrapf(err, "finding %q", dir)
|
||||
return nil, errors.Wrapf(err, "error checking for existence of directory %q", filepath.ToSlash(dir))
|
||||
}
|
||||
if !exists {
|
||||
return []os.FileInfo{}, errors.Errorf("%q not found", dir)
|
||||
return nil, errors.Wrapf(ErrNotExist, "directory %q", filepath.ToSlash(dir))
|
||||
}
|
||||
|
||||
contents, err := p.fs.ReadDir(dir)
|
||||
if err != nil {
|
||||
return []os.FileInfo{}, errors.Wrapf(err, "reading contents of %q", dir)
|
||||
return nil, errors.Wrapf(err, "reading contents of %q", filepath.ToSlash(dir))
|
||||
}
|
||||
|
||||
return contents, nil
|
||||
|
||||
@@ -32,13 +32,13 @@ func TestParse(t *testing.T) {
|
||||
name string
|
||||
files []string
|
||||
dir string
|
||||
wantErrMsg string
|
||||
wantErrMsg error
|
||||
want map[string]*ResourceItems
|
||||
}{
|
||||
{
|
||||
name: "when there is no top-level resources directory, an error is returned",
|
||||
dir: "root-dir",
|
||||
wantErrMsg: "directory \"resources\" does not exist",
|
||||
wantErrMsg: ErrNotExist,
|
||||
},
|
||||
{
|
||||
name: "when there are no directories under the resources directory, an empty map is returned",
|
||||
@@ -109,8 +109,8 @@ func TestParse(t *testing.T) {
|
||||
}
|
||||
|
||||
res, err := p.Parse(tc.dir)
|
||||
if tc.wantErrMsg != "" {
|
||||
assert.EqualError(t, err, tc.wantErrMsg)
|
||||
if tc.wantErrMsg != nil {
|
||||
assert.ErrorIs(t, err, tc.wantErrMsg, "Error should be: %v, got: %v", tc.wantErrMsg, err)
|
||||
} else {
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, tc.want, res)
|
||||
@@ -124,13 +124,13 @@ func TestParseGroupVersions(t *testing.T) {
|
||||
name string
|
||||
files []string
|
||||
backupDir string
|
||||
wantErrMsg string
|
||||
wantErrMsg error
|
||||
want map[string]metav1.APIGroup
|
||||
}{
|
||||
{
|
||||
name: "when there is no top-level resources directory, an error is returned",
|
||||
backupDir: "/var/folders",
|
||||
wantErrMsg: "\"/var/folders/resources\" not found",
|
||||
wantErrMsg: ErrNotExist,
|
||||
},
|
||||
{
|
||||
name: "when there are no directories under the resources directory, an empty map is returned",
|
||||
@@ -223,8 +223,8 @@ func TestParseGroupVersions(t *testing.T) {
|
||||
}
|
||||
|
||||
res, err := p.ParseGroupVersions(tc.backupDir)
|
||||
if tc.wantErrMsg != "" {
|
||||
assert.EqualError(t, err, tc.wantErrMsg)
|
||||
if tc.wantErrMsg != nil {
|
||||
assert.ErrorIs(t, err, tc.wantErrMsg, "Error should be: %v, got: %v", tc.wantErrMsg, err)
|
||||
} else {
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, tc.want, res)
|
||||
|
||||
Reference in New Issue
Block a user