@@ -17,6 +17,7 @@ import (
1717
1818 "github.com/google/uuid"
1919 "github.com/hashicorp/yamux"
20+ "github.com/spf13/afero"
2021 "go.uber.org/atomic"
2122 "golang.org/x/xerrors"
2223
@@ -45,7 +46,8 @@ type Provisioners map[string]sdkproto.DRPCProvisionerClient
4546
4647// Options provides customizations to the behavior of a provisioner daemon.
4748type Options struct {
48- Logger slog.Logger
49+ Filesystem afero.Fs
50+ Logger slog.Logger
4951
5052 ForceCancelInterval time.Duration
5153 UpdateInterval time.Duration
@@ -65,6 +67,9 @@ func New(clientDialer Dialer, opts *Options) *Server {
6567 if opts .ForceCancelInterval == 0 {
6668 opts .ForceCancelInterval = time .Minute
6769 }
70+ if opts .Filesystem == nil {
71+ opts .Filesystem = afero .NewOsFs ()
72+ }
6873 ctx , ctxCancel := context .WithCancel (context .Background ())
6974 daemon := & Server {
7075 clientDialer : clientDialer ,
@@ -303,7 +308,7 @@ func (p *Server) runJob(ctx context.Context, job *proto.AcquiredJob) {
303308 defer func () {
304309 // Cleanup the work directory after execution.
305310 for attempt := 0 ; attempt < 5 ; attempt ++ {
306- err := os .RemoveAll (p .opts .WorkDirectory )
311+ err := p . opts . Filesystem .RemoveAll (p .opts .WorkDirectory )
307312 if err != nil {
308313 // On Windows, open files cannot be removed.
309314 // When the provisioner daemon is shutting down,
@@ -326,7 +331,7 @@ func (p *Server) runJob(ctx context.Context, job *proto.AcquiredJob) {
326331 return
327332 }
328333
329- err := os .MkdirAll (p .opts .WorkDirectory , 0700 )
334+ err := p . opts . Filesystem .MkdirAll (p .opts .WorkDirectory , 0700 )
330335 if err != nil {
331336 p .failActiveJobf ("create work directory %q: %s" , p .opts .WorkDirectory , err )
332337 return
@@ -374,14 +379,14 @@ func (p *Server) runJob(ctx context.Context, job *proto.AcquiredJob) {
374379 }
375380 switch header .Typeflag {
376381 case tar .TypeDir :
377- err = os .MkdirAll (headerPath , mode )
382+ err = p . opts . Filesystem .MkdirAll (headerPath , mode )
378383 if err != nil {
379384 p .failActiveJobf ("mkdir %q: %s" , headerPath , err )
380385 return
381386 }
382387 p .opts .Logger .Debug (context .Background (), "extracted directory" , slog .F ("path" , headerPath ))
383388 case tar .TypeReg :
384- file , err := os .OpenFile (headerPath , os .O_CREATE | os .O_RDWR , mode )
389+ file , err := p . opts . Filesystem .OpenFile (headerPath , os .O_CREATE | os .O_RDWR , mode )
385390 if err != nil {
386391 p .failActiveJobf ("create file %q (mode %s): %s" , headerPath , mode , err )
387392 return
@@ -470,7 +475,7 @@ func (p *Server) runReadmeParse(ctx context.Context, job *proto.AcquiredJob) {
470475 return
471476 }
472477
473- fi , err := os .ReadFile (path .Join (p .opts .WorkDirectory , ReadmeFile ))
478+ fi , err := afero .ReadFile (p . opts . Filesystem , path .Join (p .opts .WorkDirectory , ReadmeFile ))
474479 if err != nil {
475480 _ , err := client .UpdateJob (ctx , & proto.UpdateJobRequest {
476481 JobId : job .GetJobId (),
0 commit comments