Skip to content

Commit f042c96

Browse files
author
Anil Ambati
committed
[FAB-3638] Increase test coverage for util pkg
Added additional tests for function in core/chaincode/platforms/util/utils.go Change-Id: I5b74c0fa1a62b8cdc5af2e86947c2f26f652c0e5 Signed-off-by: Anil Ambati <[email protected]>
1 parent b7841b5 commit f042c96

File tree

3 files changed

+136
-20
lines changed

3 files changed

+136
-20
lines changed

core/chaincode/platforms/golang/platform_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ import (
3434

3535
func testerr(err error, succ bool) error {
3636
if succ && err != nil {
37-
return fmt.Errorf("Expected success but got %s", err)
37+
return fmt.Errorf("Expected success but got error %s", err)
3838
} else if !succ && err == nil {
39-
return fmt.Errorf("Expected failer but succeeded")
39+
return fmt.Errorf("Expected failure but succeeded")
4040
}
4141
return nil
4242
}
@@ -210,7 +210,7 @@ func TestGetDeploymentPayload(t *testing.T) {
210210
for _, tst := range tests {
211211
_, err := platform.GetDeploymentPayload(tst.spec)
212212
if err = testerr(err, tst.succ); err != nil {
213-
t.Errorf("Error to validating chaincode spec: %s, %s", tst.spec.ChaincodeId.Path, err)
213+
t.Errorf("Error validating chaincode spec: %s, %s", tst.spec.ChaincodeId.Path, err)
214214
}
215215
}
216216
}
@@ -255,7 +255,7 @@ func TestGenerateDockerBuild(t *testing.T) {
255255
}
256256
err = platform.GenerateDockerBuild(cds, tw)
257257
if err = testerr(err, tst.SuccessExpected); err != nil {
258-
t.Errorf("Error to validating chaincode spec: %s, %s", cds.ChaincodeSpec.ChaincodeId.Path, err)
258+
t.Errorf("Error validating chaincode spec: %s, %s", cds.ChaincodeSpec.ChaincodeId.Path, err)
259259
}
260260
}
261261
}

core/chaincode/platforms/util/utils.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ import (
2020
"archive/tar"
2121
"bytes"
2222
"fmt"
23+
"io"
2324
"io/ioutil"
2425
"os"
2526
"path/filepath"
2627

27-
"io"
28-
29-
"github.com/fsouza/go-dockerclient"
28+
docker "github.com/fsouza/go-dockerclient"
3029
"github.com/hyperledger/fabric/common/flogging"
3130
"github.com/hyperledger/fabric/common/util"
3231
cutil "github.com/hyperledger/fabric/core/container/util"
@@ -147,7 +146,6 @@ func DockerBuild(opts DockerBuildOptions) error {
147146
if err != nil {
148147
return fmt.Errorf("Error creating docker client: %s", err)
149148
}
150-
151149
if opts.Image == "" {
152150
opts.Image = cutil.GetDockerfileFromConfig("chaincode.builder")
153151
if opts.Image == "" {

core/chaincode/platforms/util/utils_test.go

+130-12
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,24 @@ limitations under the License.
1717
package util
1818

1919
import (
20+
"archive/tar"
2021
"bytes"
22+
"compress/gzip"
2123
"encoding/hex"
22-
"math/rand"
23-
"testing"
24-
"time"
25-
26-
"archive/tar"
2724
"fmt"
2825
"io"
26+
"io/ioutil"
27+
"math/rand"
2928
"os"
3029
"strings"
30+
"testing"
31+
"time"
3132

3233
"github.com/hyperledger/fabric/common/util"
3334
"github.com/hyperledger/fabric/core/config"
3435
cutil "github.com/hyperledger/fabric/core/container/util"
3536
"github.com/spf13/viper"
37+
"github.com/stretchr/testify/assert"
3638
)
3739

3840
// TestHashContentChange changes a random byte in a content and checks for hash change
@@ -178,23 +180,139 @@ func TestHashDiffDir(t *testing.T) {
178180
if bytes.Compare(hash1, hash2) == 0 {
179181
t.Error("Hash should be different for 2 different remote repos")
180182
}
181-
182183
}
184+
183185
func TestHashSameDir(t *testing.T) {
186+
assert := assert.New(t)
187+
184188
b := []byte("firstcontent")
185189
hash := util.ComputeSHA256(b)
186-
187190
hash1, err := HashFilesInDir(".", "hashtestfiles1", hash, nil)
191+
assert.NoError(err, "Error getting code")
192+
193+
fname := os.TempDir() + "/hash.tar"
194+
w, err := os.Create(fname)
188195
if err != nil {
189-
t.Errorf("Error getting code %s", err)
196+
t.Fatal(err)
190197
}
191-
hash2, err := HashFilesInDir(".", "hashtestfiles1", hash, nil)
198+
defer os.Remove(fname)
199+
tw := tar.NewWriter(w)
200+
defer w.Close()
201+
defer tw.Close()
202+
hash2, err := HashFilesInDir(".", "hashtestfiles1", hash, tw)
203+
assert.NoError(err, "Error getting code")
204+
205+
assert.Equal(bytes.Compare(hash1, hash2), 0,
206+
"Hash should be same across multiple downloads")
207+
}
208+
209+
func TestHashBadWriter(t *testing.T) {
210+
b := []byte("firstcontent")
211+
hash := util.ComputeSHA256(b)
212+
213+
fname := os.TempDir() + "/hash.tar"
214+
w, err := os.Create(fname)
192215
if err != nil {
193-
t.Errorf("Error getting code %s", err)
216+
t.Fatal(err)
217+
}
218+
defer os.Remove(fname)
219+
tw := tar.NewWriter(w)
220+
defer w.Close()
221+
tw.Close()
222+
223+
_, err = HashFilesInDir(".", "hashtestfiles1", hash, tw)
224+
assert.Error(t, err,
225+
"HashFilesInDir invoked with closed writer, should have failed")
226+
}
227+
228+
// TestHashNonExistentDir tests HashFilesInDir with non existant directory
229+
func TestHashNonExistentDir(t *testing.T) {
230+
b := []byte("firstcontent")
231+
hash := util.ComputeSHA256(b)
232+
_, err := HashFilesInDir(".", "idontexist", hash, nil)
233+
assert.Error(t, err, "Expected an error for non existent directory %s", "idontexist")
234+
}
235+
236+
// TestIsCodeExist tests isCodeExist function
237+
func TestIsCodeExist(t *testing.T) {
238+
assert := assert.New(t)
239+
path := os.TempDir()
240+
err := IsCodeExist(path)
241+
assert.NoError(err,
242+
"%s directory exists, IsCodeExist should not have returned error: %v",
243+
path, err)
244+
245+
dir, err := ioutil.TempDir(os.TempDir(), "iscodeexist")
246+
assert.NoError(err)
247+
defer os.RemoveAll(dir)
248+
path = dir + "/blah"
249+
err = IsCodeExist(path)
250+
assert.Error(err,
251+
"%s directory does not exist, IsCodeExist should have returned error", path)
252+
253+
f := createTempFile(t)
254+
defer os.Remove(f)
255+
err = IsCodeExist(f)
256+
assert.Error(err, "%s is a file, IsCodeExist should have returned error", f)
257+
}
258+
259+
// TestDockerBuild tests DockerBuild function
260+
func TestDockerBuild(t *testing.T) {
261+
assert := assert.New(t)
262+
var err error
263+
264+
ldflags := "-linkmode external -extldflags '-static'"
265+
codepackage := bytes.NewReader(getDeploymentPayload())
266+
binpackage := bytes.NewBuffer(nil)
267+
if err != nil {
268+
t.Fatal(err)
269+
}
270+
err = DockerBuild(DockerBuildOptions{
271+
Cmd: fmt.Sprintf("GOPATH=/chaincode/input:$GOPATH go build -ldflags \"%s\" -o /chaincode/output/chaincode helloworld",
272+
ldflags),
273+
InputStream: codepackage,
274+
OutputStream: binpackage,
275+
})
276+
assert.NoError(err, "DockerBuild failed")
277+
}
278+
279+
func getDeploymentPayload() []byte {
280+
var goprog = `
281+
package main
282+
import "fmt"
283+
func main() {
284+
fmt.Println("Hello World")
285+
}
286+
`
287+
var zeroTime time.Time
288+
payload := bytes.NewBufferString(goprog).Bytes()
289+
inputbuf := bytes.NewBuffer(nil)
290+
gw := gzip.NewWriter(inputbuf)
291+
tw := tar.NewWriter(gw)
292+
tw.WriteHeader(&tar.Header{
293+
Name: "src/helloworld/helloworld.go",
294+
Size: int64(len(payload)),
295+
Mode: 0600,
296+
ModTime: zeroTime,
297+
AccessTime: zeroTime,
298+
ChangeTime: zeroTime,
299+
})
300+
tw.Write(payload)
301+
tw.Close()
302+
gw.Close()
303+
return inputbuf.Bytes()
304+
}
305+
306+
func createTempFile(t *testing.T) string {
307+
tmpfile, err := ioutil.TempFile("", "test")
308+
if err != nil {
309+
t.Fatal(err)
310+
return ""
194311
}
195-
if bytes.Compare(hash1, hash2) != 0 {
196-
t.Error("Hash should be same across multiple downloads")
312+
if err := tmpfile.Close(); err != nil {
313+
t.Fatal(err)
197314
}
315+
return tmpfile.Name()
198316
}
199317

200318
func TestDockerPull(t *testing.T) {

0 commit comments

Comments
 (0)