From 68f18da5e3bf16163dd071bb313e36b5215e792c Mon Sep 17 00:00:00 2001 From: David Juhasz Date: Tue, 16 Apr 2024 16:59:11 -0700 Subject: [PATCH] Add a preprocessing workflow unit test --- go.mod | 6 +- go.sum | 10 ++ internal/workflow/preprocessing_test.go | 105 ++++++++++++++++++ .../testdata/transfer_with_ds_store/.DS_Store | 0 .../testdata/transfer_with_ds_store/small.txt | 1 + 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 internal/workflow/preprocessing_test.go create mode 100644 internal/workflow/testdata/transfer_with_ds_store/.DS_Store create mode 100644 internal/workflow/testdata/transfer_with_ds_store/small.txt diff --git a/go.mod b/go.mod index d8e1c28..1a50e6c 100644 --- a/go.mod +++ b/go.mod @@ -3,16 +3,20 @@ module github.com/artefactual-sdps/preprocessing-moma go 1.22.2 require ( + github.com/artefactual-sdps/remove-files-activity v0.0.0-20240301164716-575fb8b7b834 github.com/artefactual-sdps/temporal-activities v0.0.0-20240418211119-e97b26fed41b github.com/go-logr/logr v1.4.1 + github.com/otiai10/copy v1.14.0 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.18.2 + github.com/stretchr/testify v1.9.0 go.artefactual.dev/tools v0.8.0 go.temporal.io/sdk v1.26.1 gotest.tools/v3 v3.5.1 ) require ( + github.com/Diogenesoftoronto/go-gitignore v1.1.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect @@ -36,13 +40,13 @@ require ( github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect github.com/stretchr/objx v0.5.2 // indirect - github.com/stretchr/testify v1.9.0 // indirect github.com/subosito/gotenv v1.6.0 // indirect go.temporal.io/api v1.32.0 // indirect go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.27.0 // indirect golang.org/x/exp v0.0.0-20231127185646-65229373498e // indirect golang.org/x/net v0.24.0 // indirect + golang.org/x/sync v0.6.0 // indirect golang.org/x/sys v0.19.0 // indirect golang.org/x/text v0.14.0 // indirect golang.org/x/time v0.5.0 // indirect diff --git a/go.sum b/go.sum index 41f1f6f..e8b9ed6 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,9 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/Diogenesoftoronto/go-gitignore v1.1.0 h1:G8jh5GxjmCidSTvotSiNeBMxMLma9zOtjb2wZ/QvLk8= +github.com/Diogenesoftoronto/go-gitignore v1.1.0/go.mod h1:mmVD39ljrmsXQECsd829osXfYy+nzbxqYEH2rCLE+XA= +github.com/artefactual-sdps/remove-files-activity v0.0.0-20240301164716-575fb8b7b834 h1:EJzGpWDLTrx/FbXfwewHqXsGeIihz1CyKTjCdAO67mU= +github.com/artefactual-sdps/remove-files-activity v0.0.0-20240301164716-575fb8b7b834/go.mod h1:5U5uQ147WRiusG3LMDopp9MOZEcOJ649TUnccLefhKU= github.com/artefactual-sdps/temporal-activities v0.0.0-20240418211119-e97b26fed41b h1:gfScS2NGX6STL9BkTuuDkCp9/f4PUqG4QITbsTKyuJA= github.com/artefactual-sdps/temporal-activities v0.0.0-20240418211119-e97b26fed41b/go.mod h1:oviveqOfixKKhUlNYb/gRq/NceVLdseOB/GUK8Rinfk= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= @@ -67,6 +71,10 @@ github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3v github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU= +github.com/otiai10/copy v1.14.0/go.mod h1:ECfuL02W+/FkTWZWgQqXPWZgW9oeKCSQ5qVfSc4qc4w= +github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks= +github.com/otiai10/mint v1.5.1/go.mod h1:MJm72SBthJjz8qhefc4z1PYEieWmy8Bku7CjcAqyUSM= github.com/pborman/uuid v1.2.1 h1:+ZZIw58t/ozdjRaXh/3awHfmWRbzYxJoAdNJxe/3pvw= github.com/pborman/uuid v1.2.1/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= @@ -166,6 +174,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= +golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/internal/workflow/preprocessing_test.go b/internal/workflow/preprocessing_test.go new file mode 100644 index 0000000..5b33439 --- /dev/null +++ b/internal/workflow/preprocessing_test.go @@ -0,0 +1,105 @@ +package workflow_test + +import ( + "path/filepath" + "testing" + + "github.com/artefactual-sdps/preprocessing-moma/internal/config" + "github.com/artefactual-sdps/preprocessing-moma/internal/workflow" + remove "github.com/artefactual-sdps/remove-files-activity" + + "github.com/artefactual-sdps/temporal-activities/removefiles" + cp "github.com/otiai10/copy" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/suite" + temporalsdk_activity "go.temporal.io/sdk/activity" + "go.temporal.io/sdk/testsuite" + temporalsdk_worker "go.temporal.io/sdk/worker" +) + +type PreprocessingTestSuite struct { + suite.Suite + testsuite.WorkflowTestSuite + + env *testsuite.TestWorkflowEnvironment + + // Each test creates its own temporary transfer directory. + testDir string + + // Each test registers the workflow with a different name to avoid + // duplicates. + workflow *workflow.PreprocessingWorkflow +} + +func (s *PreprocessingTestSuite) copyTestTransfer(src, relPath string) { + err := cp.Copy(src, filepath.Join(s.testDir, relPath)) + if err != nil { + s.T().Fatalf("copyTestTransfer: %v", err) + } +} + +func (s *PreprocessingTestSuite) SetupTest( + cfg config.Configuration, + transferPath, relPath string, +) { + s.env = s.NewTestWorkflowEnvironment() + s.env.SetWorkerOptions(temporalsdk_worker.Options{EnableSessionWorker: true}) + s.testDir = s.T().TempDir() + + // Register activities. + s.env.RegisterActivityWithOptions( + removefiles.NewActivity(cfg.RemoveFiles).Execute, + temporalsdk_activity.RegisterOptions{Name: remove.RemoveFilesName}, + ) + + s.workflow = workflow.NewPreprocessingWorkflow(s.testDir) + + // Copy test files to testDir. + s.copyTestTransfer(transferPath, relPath) +} + +func (s *PreprocessingTestSuite) AfterTest(suiteName, testName string) { + s.env.AssertExpectations(s.T()) +} + +func TestPreprocessingWorkflow(t *testing.T) { + suite.Run(t, new(PreprocessingTestSuite)) +} + +func (s *PreprocessingTestSuite) TestExecute() { + relPath := "transfer" + cfg := config.Configuration{ + RemoveFiles: removefiles.Config{RemoveNames: ".DS_Store"}, + } + + s.SetupTest( + cfg, + filepath.Join("testdata", "transfer_with_ds_store"), + relPath, + ) + + // Mock activities. + sessionCtx := mock.AnythingOfType("*context.timerCtx") + s.env.OnActivity( + removefiles.ActivityName, + sessionCtx, + &removefiles.ActivityParams{Path: filepath.Join(s.testDir, relPath)}, + ).Return( + &removefiles.ActivityResult{Count: 1}, nil, + ) + + s.env.ExecuteWorkflow( + s.workflow.Execute, + &workflow.PreprocessingWorkflowParams{RelativePath: relPath}, + ) + + s.True(s.env.IsWorkflowCompleted()) + + var result workflow.PreprocessingWorkflowResult + err := s.env.GetWorkflowResult(&result) + s.NoError(err) + s.Equal( + &result, + &workflow.PreprocessingWorkflowResult{RelativePath: relPath}, + ) +} diff --git a/internal/workflow/testdata/transfer_with_ds_store/.DS_Store b/internal/workflow/testdata/transfer_with_ds_store/.DS_Store new file mode 100644 index 0000000..e69de29 diff --git a/internal/workflow/testdata/transfer_with_ds_store/small.txt b/internal/workflow/testdata/transfer_with_ds_store/small.txt new file mode 100644 index 0000000..9524014 --- /dev/null +++ b/internal/workflow/testdata/transfer_with_ds_store/small.txt @@ -0,0 +1 @@ +I am a small file.