Skip to content

Commit

Permalink
[FAB-9051] Return InstallCC errors when applicable
Browse files Browse the repository at this point in the history
Change-Id: I9d8112a21883aa1c1efee94f2f88cd44810feb3a
Signed-off-by: Divyank Katira <Divyank.Katira@securekey.com>
  • Loading branch information
d1vyank committed Mar 22, 2018
1 parent a3b4280 commit a856787
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
19 changes: 10 additions & 9 deletions pkg/client/resmgmt/resmgmt.go
Expand Up @@ -309,7 +309,6 @@ func (rc *Client) isChaincodeInstalled(reqCtx reqContext.Context, req InstallCCR

// InstallCC installs chaincode with optional custom options (specific peers, filtered peers)
func (rc *Client) InstallCC(req InstallCCRequest, options ...RequestOption) ([]InstallCCResponse, error) {

// For each peer query if chaincode installed. If cc is installed treat as success with message 'already installed'.
// If cc is not installed try to install, and if that fails add to the list with error and peer name.

Expand Down Expand Up @@ -349,7 +348,7 @@ func (rc *Client) InstallCC(req InstallCCRequest, options ...RequestOption) ([]I
}

responses := make([]InstallCCResponse, 0)
var errs multi.Errors
errs := multi.Errors{}

// Targets will be adjusted if cc has already been installed
newTargets := make([]fab.Peer, 0)
Expand All @@ -360,7 +359,7 @@ func (rc *Client) InstallCC(req InstallCCRequest, options ...RequestOption) ([]I
installed, err := rc.isChaincodeInstalled(reqCtx, req, target)
if err != nil {
// Add to errors with unable to verify error message
errs = append(errs, errors.Errorf("unable to verify if cc is installed on %s", target.URL()))
errs = append(errs, errors.Errorf("unable to verify if cc is installed on %s. Got error: %s", target.URL(), err.Error()))
continue
}
if installed {
Expand All @@ -376,7 +375,7 @@ func (rc *Client) InstallCC(req InstallCCRequest, options ...RequestOption) ([]I
if len(newTargets) == 0 {
// CC is already installed on all targets and/or
// we are unable to verify if cc is installed on target(s)
return responses, nil
return responses, errs.ToError()
}

reqCtx, cancel := contextImpl.NewRequest(rc.ctx, contextImpl.WithTimeoutType(core.ResMgmt), contextImpl.WithParent(parentReqCtx))
Expand All @@ -392,13 +391,15 @@ func (rc *Client) InstallCC(req InstallCCRequest, options ...RequestOption) ([]I
}

if err != nil {
return responses, errors.WithMessage(err, "InstallChaincode failed")
}
if len(errs) > 0 {
return responses, errs
installErrs, ok := err.(multi.Errors)
if ok {
errs = append(errs, installErrs)
} else {
errs = append(errs, err)
}
}

return responses, nil
return responses, errs.ToError()
}

func checkRequiredInstallCCParams(req InstallCCRequest) error {
Expand Down
17 changes: 16 additions & 1 deletion pkg/client/resmgmt/resmgmt_test.go
Expand Up @@ -535,8 +535,23 @@ func TestInstallCCWithOpts(t *testing.T) {
}
}

func TestInstallCC(t *testing.T) {
func TestInstallError(t *testing.T) {
rc := setupDefaultResMgmtClient(t)

testErr := fmt.Errorf("Test error message")
peer1 := fcmocks.MockPeer{MockName: "Peer1", MockURL: "http://peer1.com",
Status: http.StatusInternalServerError, MockRoles: []string{}, MockCert: nil, MockMSP: "Org1MSP", Error: testErr}

peer2 := fcmocks.MockPeer{MockName: "Peer1", MockURL: "http://peer1.com",
Status: http.StatusOK, MockRoles: []string{}, MockCert: nil, MockMSP: "Org1MSP"}

req := InstallCCRequest{Name: "ID", Version: "v0", Path: "path", Package: &api.CCPackage{Type: 1, Code: []byte("code")}}
_, err := rc.InstallCC(req, WithTargets(&peer1, &peer2))
assert.NotNil(t, err)
assert.Contains(t, err.Error(), testErr.Error())
}

func TestInstallCC(t *testing.T) {
rc := setupDefaultResMgmtClient(t)

peer := fcmocks.MockPeer{MockName: "Peer1", MockURL: "http://peer1.com",
Expand Down

0 comments on commit a856787

Please sign in to comment.