github.com/mgoltzsche/ctnr@v0.7.1-alpha/pkg/fs/testutils/sourcemocks.go (about) 1 package testutils 2 3 import ( 4 "path/filepath" 5 6 "github.com/mgoltzsche/ctnr/pkg/fs" 7 ) 8 9 type SourceMock struct { 10 fs.NodeAttrs 11 fs.Readable 12 Err error 13 } 14 15 func NewSourceMock(t fs.NodeType, a fs.FileAttrs, hash string) *SourceMock { 16 r := SourceMock{fs.NodeAttrs{fs.NodeInfo{t, a}, fs.DerivedAttrs{Hash: hash}}, fs.NewReadableBytes([]byte("mockcontent")), nil} 17 return &r 18 } 19 20 func (s *SourceMock) Attrs() fs.NodeInfo { return s.NodeInfo } 21 func (s *SourceMock) DeriveAttrs() (fs.DerivedAttrs, error) { 22 return s.DerivedAttrs, s.Err 23 } 24 func (s *SourceMock) Write(dest, name string, w fs.Writer, written map[fs.Source]string) (err error) { 25 a := s.NodeAttrs 26 t := a.NodeType 27 if t == fs.TypeDir { 28 err = w.Dir(dest, name, a.FileAttrs) 29 } else if linkDest, ok := written[s]; ok { 30 err = w.Link(dest, linkDest) 31 } else if t == fs.TypeSymlink { 32 written[s] = dest 33 err = w.Symlink(dest, a.FileAttrs) 34 } else { 35 written[s] = dest 36 _, err = w.File(dest, s) 37 } 38 if s.Err != nil { 39 err = s.Err 40 } 41 return err 42 } 43 func (s *SourceMock) HashIfAvailable() string { 44 return s.Hash 45 } 46 func (s *SourceMock) String() string { 47 return "sourceMock{" + s.NodeAttrs.String() + "}" 48 } 49 50 type SourceOverlayMock struct { 51 *SourceMock 52 } 53 54 func (s *SourceOverlayMock) Write(dest, name string, w fs.Writer, written map[fs.Source]string) error { 55 return w.Lazy(dest, name, s, written) 56 } 57 58 func (s *SourceOverlayMock) Expand(dest string, w fs.Writer, _ map[fs.Source]string) (err error) { 59 a := s.Attrs().FileAttrs 60 a.Mode = 0644 61 src := NewSourceMock(fs.TypeFile, a, "") 62 src.Readable = fs.NewReadableBytes([]byte("content")) 63 _, err = w.File(filepath.Join(dest, "xtracted"), src) 64 return 65 } 66 67 func (s *SourceOverlayMock) String() string { 68 return "sourceOverlayMock{" + s.SourceMock.String() + "}" 69 }