@@ -11,6 +11,7 @@ import (
11
11
"bytes"
12
12
"compress/gzip"
13
13
"fmt"
14
+ "io"
14
15
"io/ioutil"
15
16
"os"
16
17
"path/filepath"
@@ -42,6 +43,7 @@ func Test_WriteFileToPackage(t *testing.T) {
42
43
// Read the file from the archive and check the name and file content
43
44
r := bytes .NewReader (buf .Bytes ())
44
45
gr , err1 := gzip .NewReader (r )
46
+ defer gr .Close ()
45
47
assert .NoError (t , err1 , "Error creating a gzip reader" )
46
48
tr := tar .NewReader (gr )
47
49
header , err2 := tr .Next ()
@@ -110,6 +112,7 @@ func Test_WriteStreamToPackage(t *testing.T) {
110
112
// Read the file from the archive and check the name and file content
111
113
br := bytes .NewReader (buf .Bytes ())
112
114
gr , err1 := gzip .NewReader (br )
115
+ defer gr .Close ()
113
116
assert .NoError (t , err1 , "Error creating a gzip reader" )
114
117
tr := tar .NewReader (gr )
115
118
header , err2 := tr .Next ()
@@ -124,14 +127,12 @@ func Test_WriteStreamToPackage(t *testing.T) {
124
127
"file content from the archive is not same as original file content" )
125
128
}
126
129
127
- func Test_WriteFolderToTarPackage (t * testing.T ) {
128
- buf := bytes .NewBuffer (nil )
129
- gw := gzip .NewWriter (buf )
130
- tw := tar .NewWriter (gw )
130
+ // Success case 1: with include and exclude file types and without exclude dir
131
+ func Test_WriteFolderToTarPackage1 (t * testing.T ) {
131
132
132
- // Success case 1: with include and exclude file types and without exclude dir
133
133
gopath := os .Getenv ("GOPATH" )
134
134
gopath = filepath .SplitList (gopath )[0 ]
135
+
135
136
srcPath := filepath .Join (gopath , "src" ,
136
137
"github.com/hyperledger/fabric/examples/chaincode/java/SimpleSample" )
137
138
filePath := "src/src/main/java/example/SimpleSample.java"
@@ -142,41 +143,137 @@ func Test_WriteFolderToTarPackage(t *testing.T) {
142
143
".xml" : true ,
143
144
}
144
145
145
- err := WriteFolderToTarPackage (tw , srcPath , "" ,
146
- includeFileTypes , excludeFileTypes )
147
- assert .NoError (t , err , "Error writing folder to package" )
148
-
149
- tw .Close ()
150
- gw .Close ()
146
+ tarBytes := createTestTar (t , srcPath , "" , includeFileTypes , excludeFileTypes )
151
147
152
148
// Read the file from the archive and check the name
153
- br := bytes .NewReader (buf . Bytes () )
149
+ br := bytes .NewReader (tarBytes )
154
150
gr , err1 := gzip .NewReader (br )
151
+ defer gr .Close ()
155
152
assert .NoError (t , err1 , "Error creating a gzip reader" )
156
153
tr := tar .NewReader (gr )
157
154
header , err2 := tr .Next ()
158
155
assert .NoError (t , err2 , "Error getting the file from the tar" )
159
156
assert .Equal (t , filePath , header .Name ,
160
157
"Name of the file read from the archive is not same as the file added to the archive" )
158
+ }
161
159
162
- // Success case 2: with exclude dir and no include file types
163
- srcPath = filepath .Join (gopath , "src" ,
160
+ // Success case 2: with exclude dir and no include file types
161
+ func Test_WriteFolderToTarPackage2 (t * testing.T ) {
162
+
163
+ gopath := os .Getenv ("GOPATH" )
164
+ gopath = filepath .SplitList (gopath )[0 ]
165
+
166
+ srcPath := filepath .Join (gopath , "src" ,
164
167
"github.com/hyperledger/fabric/examples/chaincode/java" )
165
- tarw := tar .NewWriter (bytes .NewBuffer (nil ))
166
- defer tarw .Close ()
167
- err = WriteFolderToTarPackage (tarw , srcPath , "SimpleSample" ,
168
- nil , excludeFileTypes )
168
+ excludeFileTypes := map [string ]bool {
169
+ ".xml" : true ,
170
+ }
171
+
172
+ createTestTar (t , srcPath , "SimpleSample" , nil , excludeFileTypes )
173
+ }
174
+
175
+ // Success case 3: with chaincode metadata in META-INF directory
176
+ func Test_WriteFolderToTarPackage3 (t * testing.T ) {
177
+
178
+ gopath := os .Getenv ("GOPATH" )
179
+ gopath = filepath .SplitList (gopath )[0 ]
180
+
181
+ // Note - go chaincode does not use WriteFolderToTarPackage(),
182
+ // but we can still use the go example for unit test,
183
+ // since there are no node chaincode examples in fabric repos
184
+ srcPath := filepath .Join (gopath , "src" ,
185
+ "github.com/hyperledger/fabric/examples/chaincode/go/marbles02" )
186
+ filePath := "META-INF/statedb/couchdb/indexes/indexOwner.json"
187
+
188
+ tarBytes := createTestTar (t , srcPath , "" , nil , nil )
189
+
190
+ // Read the files from the archive and check for the metadata index file
191
+ br := bytes .NewReader (tarBytes )
192
+ gr , err := gzip .NewReader (br )
193
+ defer gr .Close ()
194
+ assert .NoError (t , err , "Error creating a gzip reader" )
195
+ tr := tar .NewReader (gr )
196
+ var foundIndexArtifact bool
197
+ for {
198
+ header , err := tr .Next ()
199
+ if err == io .EOF { // No more entries
200
+ break
201
+ }
202
+ assert .NoError (t , err , "Error getting Next() file in tar" )
203
+ t .Logf ("Found file in tar: %s" , header .Name )
204
+ if header .Name == filePath {
205
+ foundIndexArtifact = true
206
+ break
207
+ }
208
+ }
209
+ assert .True (t , foundIndexArtifact , "should have found statedb index artifact in marbles02 META-INF directory" )
210
+ }
211
+
212
+ // Success case 4: with chaincode metadata in META-INF directory, pass trailing slash in srcPath
213
+ func Test_WriteFolderToTarPackage4 (t * testing.T ) {
214
+
215
+ gopath := os .Getenv ("GOPATH" )
216
+ gopath = filepath .SplitList (gopath )[0 ]
217
+
218
+ // Note - go chaincode does not use WriteFolderToTarPackage(),
219
+ // but we can still use the go example for unit test,
220
+ // since there are no node chaincode examples in fabric repos
221
+ srcPath := filepath .Join (gopath , "src" ,
222
+ "github.com/hyperledger/fabric/examples/chaincode/go/marbles02" )
223
+ srcPath = srcPath + "/"
224
+ filePath := "META-INF/statedb/couchdb/indexes/indexOwner.json"
225
+
226
+ tarBytes := createTestTar (t , srcPath , "" , nil , nil )
227
+
228
+ // Read the files from the archive and check for the metadata index file
229
+ br := bytes .NewReader (tarBytes )
230
+ gr , err := gzip .NewReader (br )
231
+ defer gr .Close ()
232
+ assert .NoError (t , err , "Error creating a gzip reader" )
233
+ tr := tar .NewReader (gr )
234
+ var foundIndexArtifact bool
235
+ for {
236
+ header , err := tr .Next ()
237
+ if err == io .EOF { // No more entries
238
+ break
239
+ }
240
+ assert .NoError (t , err , "Error getting Next() file in tar" )
241
+ t .Logf ("Found file in tar: %s" , header .Name )
242
+ if header .Name == filePath {
243
+ foundIndexArtifact = true
244
+ break
245
+ }
246
+ }
247
+ assert .True (t , foundIndexArtifact , "should have found statedb index artifact in marbles02 META-INF directory" )
248
+ }
249
+
250
+ func createTestTar (t * testing.T , srcPath string , excludeDir string , includeFileTypeMap map [string ]bool , excludeFileTypeMap map [string ]bool ) []byte {
251
+ buf := bytes .NewBuffer (nil )
252
+ gw := gzip .NewWriter (buf )
253
+ tw := tar .NewWriter (gw )
254
+
255
+ err := WriteFolderToTarPackage (tw , srcPath , "" , includeFileTypeMap , excludeFileTypeMap )
169
256
assert .NoError (t , err , "Error writing folder to package" )
170
257
171
- // Failure case 1: no files in directory
172
- srcPath = filepath .Join (gopath , "src" ,
258
+ tw .Close ()
259
+ gw .Close ()
260
+ return buf .Bytes ()
261
+ }
262
+
263
+ // Failure case 1: no files in directory
264
+ func Test_WriteFolderToTarPackageFailure1 (t * testing.T ) {
265
+ gopath := os .Getenv ("GOPATH" )
266
+ gopath = filepath .SplitList (gopath )[0 ]
267
+
268
+ srcPath := filepath .Join (gopath , "src" ,
173
269
"github.com/hyperledger/fabric/core/container/util" ,
174
270
fmt .Sprintf ("%d" , os .Getpid ()))
175
271
os .Mkdir (srcPath , os .ModePerm )
176
272
defer os .Remove (srcPath )
177
- tarw = tar .NewWriter (bytes .NewBuffer (nil ))
178
- defer tarw .Close ()
179
- err = WriteFolderToTarPackage (tw , srcPath , "" , nil , nil )
273
+
274
+ tw := tar .NewWriter (bytes .NewBuffer (nil ))
275
+ defer tw .Close ()
276
+ err := WriteFolderToTarPackage (tw , srcPath , "" , nil , nil )
180
277
assert .Contains (t , err .Error (), "no source files found" )
181
278
}
182
279
0 commit comments