From 3c71c85c73aababca8153b1cd7c3313ce6d2b939 Mon Sep 17 00:00:00 2001 From: Svetoslav Blyahov Date: Sun, 12 Apr 2020 22:53:01 +0300 Subject: [PATCH] Improve test coverege for errors Signed-off-by: Svetoslav Blyahov --- cmd/configtxgen/main.go | 10 +++++----- cmd/configtxgen/main_test.go | 31 +++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/cmd/configtxgen/main.go b/cmd/configtxgen/main.go index f8614de64bf..d112713beb9 100644 --- a/cmd/configtxgen/main.go +++ b/cmd/configtxgen/main.go @@ -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 } @@ -69,7 +69,7 @@ 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 } @@ -77,11 +77,11 @@ func doOutputChannelCreateTx(conf, baseProfile *genesisconfig.Profile, channelID 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) @@ -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") diff --git a/cmd/configtxgen/main_test.go b/cmd/configtxgen/main_test.go index 6cc0c56b16c..2549c529f1a 100644 --- a/cmd/configtxgen/main_test.go +++ b/cmd/configtxgen/main_test.go @@ -8,6 +8,7 @@ package main import ( "flag" + "fmt" "io/ioutil" "os" "path/filepath" @@ -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) { @@ -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) { @@ -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") } func TestMissingConsortiumSection(t *testing.T) { @@ -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) { @@ -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) { @@ -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) {