github.com/replicatedhq/ship@v0.55.0/pkg/specs/localgetter/local_getter_test.go (about) 1 package localgetter 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/go-kit/kit/log" 10 "github.com/spf13/afero" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestLocalGetter_copyDir(t *testing.T) { 15 type file struct { 16 contents []byte 17 path string 18 } 19 tests := []struct { 20 name string 21 upstream string 22 savePath string 23 inFiles []file 24 outFiles []file 25 wantErr bool 26 }{ 27 { 28 name: "single file", 29 upstream: "/upstream/file", 30 savePath: "/save/file", 31 inFiles: []file{ 32 { 33 contents: []byte("hello world"), 34 path: "/upstream/file", 35 }, 36 }, 37 outFiles: []file{ 38 { 39 contents: []byte("hello world"), 40 path: "/upstream/file", 41 }, 42 { 43 contents: []byte("hello world"), 44 path: "/save/file", 45 }, 46 }, 47 }, 48 { 49 name: "single file in dir", 50 upstream: "/upstream/dir", 51 savePath: "/save/dir", 52 inFiles: []file{ 53 { 54 contents: []byte("hello world"), 55 path: "/upstream/dir/file", 56 }, 57 }, 58 outFiles: []file{ 59 { 60 contents: []byte("hello world"), 61 path: "/upstream/dir/file", 62 }, 63 { 64 contents: []byte("hello world"), 65 path: "/save/dir/file", 66 }, 67 }, 68 }, 69 { 70 name: "file plus subdirs", 71 upstream: "/upstream/", 72 savePath: "/save/", 73 inFiles: []file{ 74 { 75 contents: []byte("hello world"), 76 path: "/upstream/dir/file", 77 }, 78 { 79 contents: []byte("abc xyz"), 80 path: "/upstream/dir2/file", 81 }, 82 { 83 contents: []byte("123456789"), 84 path: "/upstream/file", 85 }, 86 }, 87 outFiles: []file{ 88 { 89 contents: []byte("hello world"), 90 path: "/upstream/dir/file", 91 }, 92 { 93 contents: []byte("abc xyz"), 94 path: "/upstream/dir2/file", 95 }, 96 { 97 contents: []byte("123456789"), 98 path: "/upstream/file", 99 }, 100 { 101 contents: []byte("hello world"), 102 path: "/save/dir/file", 103 }, 104 { 105 contents: []byte("abc xyz"), 106 path: "/save/dir2/file", 107 }, 108 { 109 contents: []byte("123456789"), 110 path: "/save/file", 111 }, 112 }, 113 }, 114 } 115 for _, tt := range tests { 116 t.Run(tt.name, func(t *testing.T) { 117 req := require.New(t) 118 119 mmFs := afero.Afero{Fs: afero.NewMemMapFs()} 120 121 g := &LocalGetter{ 122 Logger: log.NewNopLogger(), 123 FS: mmFs, 124 } 125 126 for _, file := range tt.inFiles { 127 req.NoError(mmFs.MkdirAll(filepath.Dir(file.path), os.ModePerm)) 128 req.NoError(mmFs.WriteFile(file.path, file.contents, os.ModePerm)) 129 } 130 131 err := g.copyDir(context.Background(), tt.upstream, tt.savePath) 132 if tt.wantErr { 133 req.Error(err) 134 } else { 135 req.NoError(err) 136 } 137 138 for _, file := range tt.outFiles { 139 contents, err := mmFs.ReadFile(file.path) 140 req.NoError(err) 141 req.Equal(file.contents, contents, "expected equal contents: expected %q, got %q", string(file.contents), string(contents)) 142 } 143 }) 144 } 145 }