-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unbag bagged SIPs after validation to avoid issues with identification and double bagging. [skip codecov]
- Loading branch information
Showing
9 changed files
with
392 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package activities | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"go.artefactual.dev/tools/fsutil" | ||
) | ||
|
||
const UnbagName = "unbag" | ||
|
||
type ( | ||
Unbag struct{} | ||
|
||
UnbagParams struct { | ||
Path string | ||
} | ||
|
||
UnbagResult struct { | ||
Path string | ||
} | ||
) | ||
|
||
func NewUnbag() *Unbag { | ||
return &Unbag{} | ||
} | ||
|
||
func (a *Unbag) Execute(ctx context.Context, params *UnbagParams) (*UnbagResult, error) { | ||
if _, err := os.Stat(filepath.Join(params.Path, "bagit.txt")); err != nil { | ||
// Do nothing if not a bag (bagit.txt doesn't exist). | ||
return &UnbagResult{Path: params.Path}, nil | ||
} | ||
if _, err := os.Stat(filepath.Join(params.Path, "data")); err != nil { | ||
return nil, errors.New("missing data directory") | ||
} | ||
|
||
entries, err := os.ReadDir(params.Path) | ||
if err != nil { | ||
return nil, fmt.Errorf("read dir: %v", err) | ||
} | ||
|
||
// Delete everything except the data directory. | ||
for _, e := range entries { | ||
if e.Name() != "data" { | ||
if err := os.RemoveAll(filepath.Join(params.Path, e.Name())); err != nil { | ||
return nil, fmt.Errorf("delete: %v", err) | ||
} | ||
} | ||
} | ||
|
||
// Move the data directory contents to the SIP root. | ||
entries, err = os.ReadDir(filepath.Join(params.Path, "data")) | ||
if err != nil { | ||
return nil, fmt.Errorf("read data dir: %v", err) | ||
} | ||
|
||
for _, e := range entries { | ||
if err = fsutil.Move( | ||
filepath.Join(params.Path, "data", e.Name()), | ||
filepath.Join(params.Path, e.Name()), | ||
); err != nil { | ||
return nil, fmt.Errorf("move: %v", err) | ||
} | ||
} | ||
|
||
// Delete the empty data directory. | ||
if err := os.Remove(filepath.Join(params.Path, "data")); err != nil { | ||
return nil, fmt.Errorf("remove data dir: %v", err) | ||
} | ||
|
||
return &UnbagResult{Path: params.Path}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package activities_test | ||
|
||
import ( | ||
"testing" | ||
|
||
temporalsdk_activity "go.temporal.io/sdk/activity" | ||
temporalsdk_testsuite "go.temporal.io/sdk/testsuite" | ||
"gotest.tools/v3/assert" | ||
"gotest.tools/v3/fs" | ||
|
||
"github.com/artefactual-sdps/preprocessing-sfa/internal/activities" | ||
) | ||
|
||
func TestUnbag(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
path string | ||
params func(string) activities.UnbagParams | ||
result func(string) activities.UnbagResult | ||
wantFS fs.Manifest | ||
wantErr string | ||
}{ | ||
{ | ||
name: "Unbags a bag", | ||
path: fs.NewDir(t, "enduro-test", | ||
fs.WithDir("data", | ||
fs.WithDir("d_0000001", | ||
fs.WithFile("Prozess_Digitalisierung_PREMIS.xml", ""), | ||
), | ||
fs.WithDir("additional"), | ||
), | ||
fs.WithFile("bagit.txt", ""), | ||
fs.WithFile("manifest-md5.txt", ""), | ||
).Path(), | ||
params: func(path string) activities.UnbagParams { | ||
return activities.UnbagParams{Path: path} | ||
}, | ||
result: func(path string) activities.UnbagResult { | ||
return activities.UnbagResult{Path: path} | ||
}, | ||
wantFS: fs.Expected(t, | ||
fs.WithDir("d_0000001", | ||
fs.WithFile("Prozess_Digitalisierung_PREMIS.xml", ""), | ||
), | ||
fs.WithDir("additional"), | ||
), | ||
}, | ||
{ | ||
name: "Does nothing when path is not a bag", | ||
path: fs.NewDir(t, "enduro-test", | ||
fs.WithDir("d_0000001", | ||
fs.WithFile("Prozess_Digitalisierung_PREMIS.xml", ""), | ||
), | ||
fs.WithDir("additional"), | ||
).Path(), | ||
params: func(path string) activities.UnbagParams { | ||
return activities.UnbagParams{Path: path} | ||
}, | ||
result: func(path string) activities.UnbagResult { | ||
return activities.UnbagResult{Path: path} | ||
}, | ||
wantFS: fs.Expected(t, | ||
fs.WithDir("d_0000001", | ||
fs.WithFile("Prozess_Digitalisierung_PREMIS.xml", ""), | ||
), | ||
fs.WithDir("additional"), | ||
), | ||
}, | ||
{ | ||
name: "Errors when bag is missing data dir", | ||
path: fs.NewDir(t, "enduro-test", | ||
fs.WithDir("content", | ||
fs.WithDir("d_0000001", | ||
fs.WithFile("Prozess_Digitalisierung_PREMIS.xml", ""), | ||
), | ||
fs.WithDir("additional"), | ||
), | ||
fs.WithFile("bagit.txt", ""), | ||
).Path(), | ||
params: func(path string) activities.UnbagParams { | ||
return activities.UnbagParams{Path: path} | ||
}, | ||
wantErr: "activity error (type: unbag, scheduledEventID: 0, startedEventID: 0, identity: ): missing data directory", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ts := &temporalsdk_testsuite.WorkflowTestSuite{} | ||
env := ts.NewTestActivityEnvironment() | ||
env.RegisterActivityWithOptions( | ||
activities.NewUnbag().Execute, | ||
temporalsdk_activity.RegisterOptions{Name: activities.UnbagName}, | ||
) | ||
|
||
var res activities.UnbagResult | ||
future, err := env.ExecuteActivity(activities.UnbagName, tt.params(tt.path)) | ||
|
||
if tt.wantErr != "" { | ||
if err == nil { | ||
t.Errorf("error is nil, expecting: %q", tt.wantErr) | ||
} else { | ||
assert.ErrorContains(t, err, tt.wantErr) | ||
} | ||
|
||
return | ||
} | ||
assert.NilError(t, err) | ||
|
||
future.Get(&res) | ||
assert.DeepEqual(t, res, tt.result(tt.path)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package localact | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
|
||
"github.com/artefactual-sdps/preprocessing-sfa/internal/fsutil" | ||
) | ||
|
||
type ( | ||
IsBagParams struct { | ||
Path string | ||
} | ||
|
||
IsBagResult struct { | ||
IsBag bool | ||
} | ||
) | ||
|
||
func IsBag(ctx context.Context, params *IsBagParams) (*IsBagResult, error) { | ||
return &IsBagResult{ | ||
IsBag: fsutil.FileExists(filepath.Join(params.Path, "bagit.txt")), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package localact_test | ||
|
||
import ( | ||
"testing" | ||
|
||
temporalsdk_testsuite "go.temporal.io/sdk/testsuite" | ||
"gotest.tools/v3/assert" | ||
"gotest.tools/v3/fs" | ||
|
||
"github.com/artefactual-sdps/preprocessing-sfa/internal/localact" | ||
) | ||
|
||
func TestIsBag(t *testing.T) { | ||
t.Parallel() | ||
|
||
bagPath := fs.NewDir(t, "ppsfa-test", | ||
fs.WithFile("bagit.txt", ""), | ||
).Path() | ||
emptyPath := fs.NewDir(t, "ppsfa-test").Path() | ||
|
||
type test struct { | ||
name string | ||
params localact.IsBagParams | ||
want localact.IsBagResult | ||
} | ||
for _, tt := range []test{ | ||
{ | ||
name: "Is a bag", | ||
params: localact.IsBagParams{Path: bagPath}, | ||
want: localact.IsBagResult{IsBag: true}, | ||
}, | ||
{ | ||
name: "Is not a bag", | ||
params: localact.IsBagParams{Path: emptyPath}, | ||
want: localact.IsBagResult{IsBag: false}, | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ts := &temporalsdk_testsuite.WorkflowTestSuite{} | ||
env := ts.NewTestActivityEnvironment() | ||
|
||
var res localact.IsBagResult | ||
enc, err := env.ExecuteLocalActivity(localact.IsBag, &tt.params) | ||
assert.NilError(t, err) | ||
|
||
enc.Get(&res) | ||
assert.DeepEqual(t, res, tt.want) | ||
}) | ||
} | ||
} |
Oops, something went wrong.