Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage for errors #1068

Merged
merged 1 commit into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cmd/configtxgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func doOutputBlock(config *genesisconfig.Profile, channelID string, outputBlock
logger.Info("Writing genesis block")
err = writeFile(outputBlock, protoutil.MarshalOrPanic(genesisBlock), 0640)
if err != nil {
return fmt.Errorf("Error writing genesis block: %s", err)
return fmt.Errorf("error writing genesis block: %s", err)
}
return nil
}
Expand All @@ -69,19 +69,19 @@ func doOutputChannelCreateTx(conf, baseProfile *genesisconfig.Profile, channelID
logger.Info("Writing new channel tx")
err = writeFile(outputChannelCreateTx, protoutil.MarshalOrPanic(configtx), 0640)
if err != nil {
return fmt.Errorf("Error writing channel create tx: %s", err)
return fmt.Errorf("error writing channel create tx: %s", err)
}
return nil
}

func doOutputAnchorPeersUpdate(conf *genesisconfig.Profile, channelID string, outputAnchorPeersUpdate string, asOrg string) error {
logger.Info("Generating anchor peer update")
if asOrg == "" {
return fmt.Errorf("Must specify an organization to update the anchor peer for")
return fmt.Errorf("must specify an organization to update the anchor peer for")
}

if conf.Application == nil {
return fmt.Errorf("Cannot update anchor peers without an application section")
return fmt.Errorf("cannot update anchor peers without an application section")
}

original, err := encoder.NewChannelGroup(conf)
Expand Down Expand Up @@ -127,7 +127,7 @@ func doInspectBlock(inspectBlock string) error {
logger.Info("Inspecting block")
data, err := ioutil.ReadFile(inspectBlock)
if err != nil {
return fmt.Errorf("Could not read block %s", inspectBlock)
return fmt.Errorf("could not read block %s", inspectBlock)
}

logger.Info("Parsing genesis block")
Expand Down
31 changes: 21 additions & 10 deletions cmd/configtxgen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main

import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -34,7 +35,7 @@ func TestMain(m *testing.M) {
}

func TestInspectMissing(t *testing.T) {
assert.Error(t, doInspectBlock("NonSenseBlockFileThatDoesn'tActuallyExist"), "Missing block")
assert.EqualError(t, doInspectBlock("NonSenseBlockFileThatDoesn'tActuallyExist"), "could not read block NonSenseBlockFileThatDoesn'tActuallyExist")
}

func TestInspectBlock(t *testing.T) {
Expand All @@ -49,8 +50,8 @@ func TestInspectBlock(t *testing.T) {
func TestInspectBlockErr(t *testing.T) {
config := genesisconfig.Load(genesisconfig.SampleInsecureSoloProfile, configtest.GetDevConfigDir())

assert.Error(t, doOutputBlock(config, "foo", ""), "Error writing genesis block:")
assert.Error(t, doInspectBlock(""), "Could not read block")
assert.EqualError(t, doOutputBlock(config, "foo", ""), "error writing genesis block: open : no such file or directory")
assert.EqualError(t, doInspectBlock(""), "could not read block ")
}

func TestMissingOrdererSection(t *testing.T) {
Expand All @@ -59,7 +60,7 @@ func TestMissingOrdererSection(t *testing.T) {
config := genesisconfig.Load(genesisconfig.SampleInsecureSoloProfile, configtest.GetDevConfigDir())
config.Orderer = nil

assert.Error(t, doOutputBlock(config, "foo", blockDest), "Missing orderer section")
assert.EqualError(t, doOutputBlock(config, "foo", blockDest), "refusing to generate block which is missing orderer section")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly enough we follow the convention here, further demonstrating how inconsistent error handling is in Fabric

}

func TestMissingConsortiumSection(t *testing.T) {
Expand All @@ -77,7 +78,17 @@ func TestMissingConsortiumValue(t *testing.T) {
config := genesisconfig.Load(genesisconfig.SampleSingleMSPChannelProfile, configtest.GetDevConfigDir())
config.Consortium = ""

assert.Error(t, doOutputChannelCreateTx(config, nil, "foo", configTxDest), "Missing Consortium value in Application Profile definition")
assert.EqualError(t, doOutputChannelCreateTx(config, nil, "foo", configTxDest), "config update generation failure: cannot define a new channel with no Consortium value")
}

func TestUnsuccessfulChannelTxFileCreation(t *testing.T) {
configTxDest := filepath.Join(tmpDir, "configtx")

config := genesisconfig.Load(genesisconfig.SampleSingleMSPChannelProfile, configtest.GetDevConfigDir())
assert.NoError(t, ioutil.WriteFile(configTxDest, []byte{}, 0440))
defer os.Remove(configTxDest)

assert.EqualError(t, doOutputChannelCreateTx(config, nil, "foo", configTxDest), fmt.Sprintf("error writing channel create tx: open %s: permission denied", configTxDest))
}

func TestMissingApplicationValue(t *testing.T) {
Expand All @@ -86,11 +97,11 @@ func TestMissingApplicationValue(t *testing.T) {
config := genesisconfig.Load(genesisconfig.SampleSingleMSPChannelProfile, configtest.GetDevConfigDir())
config.Application = nil

assert.Error(t, doOutputChannelCreateTx(config, nil, "foo", configTxDest), "Missing Application value in Application Profile definition")
assert.EqualError(t, doOutputChannelCreateTx(config, nil, "foo", configTxDest), "could not generate default config template: channel template configs must contain an application section")
}

func TestInspectMissingConfigTx(t *testing.T) {
assert.Error(t, doInspectChannelCreateTx("ChannelCreateTxFileWhichDoesn'tReallyExist"), "Missing channel create tx file")
assert.EqualError(t, doInspectChannelCreateTx("ChannelCreateTxFileWhichDoesn'tReallyExist"), "could not read channel create tx: open ChannelCreateTxFileWhichDoesn'tReallyExist: no such file or directory")
}

func TestInspectConfigTx(t *testing.T) {
Expand All @@ -115,15 +126,15 @@ func TestBadAnchorPeersUpdates(t *testing.T) {

config := genesisconfig.Load(genesisconfig.SampleSingleMSPChannelProfile, configtest.GetDevConfigDir())

assert.Error(t, doOutputAnchorPeersUpdate(config, "foo", configTxDest, ""), "Bad anchorPeerUpdate request - asOrg empty")
assert.EqualError(t, doOutputAnchorPeersUpdate(config, "foo", configTxDest, ""), "must specify an organization to update the anchor peer for")

backupApplication := config.Application
config.Application = nil
assert.Error(t, doOutputAnchorPeersUpdate(config, "foo", configTxDest, genesisconfig.SampleOrgName), "Bad anchorPeerUpdate request")
assert.EqualError(t, doOutputAnchorPeersUpdate(config, "foo", configTxDest, genesisconfig.SampleOrgName), "cannot update anchor peers without an application section")
config.Application = backupApplication

config.Application.Organizations[0] = &genesisconfig.Organization{Name: "FakeOrg", ID: "FakeOrg"}
assert.Error(t, doOutputAnchorPeersUpdate(config, "foo", configTxDest, genesisconfig.SampleOrgName), "Bad anchorPeerUpdate request - fake org")
assert.EqualError(t, doOutputAnchorPeersUpdate(config, "foo", configTxDest, genesisconfig.SampleOrgName), "error parsing profile as channel group: could not create application group: failed to create application org: 1 - Error loading MSP configuration for org FakeOrg: unknown MSP type ''")
}

func TestConfigTxFlags(t *testing.T) {
Expand Down