Skip to content

Commit

Permalink
Merge "[FAB-3924] fabric-ca-client test coverage >85%"
Browse files Browse the repository at this point in the history
  • Loading branch information
christo4ferris authored and Gerrit Code Review committed May 18, 2017
2 parents 55b135d + c200c02 commit 6eec8f5
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 20 deletions.
3 changes: 1 addition & 2 deletions cmd/fabric-ca-client/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ var enrollCmd = &cobra.Command{
Long: "Enroll identity with fabric-ca server",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
cmd.Help()
return nil
return fmt.Errorf(extraArgsError, args, cmd.UsageString())
}

err := runEnroll(cmd)
Expand Down
3 changes: 1 addition & 2 deletions cmd/fabric-ca-client/getcacert.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ var getCACertCmd = &cobra.Command{
Short: "Get CA certificate chain",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
cmd.Help()
return nil
return fmt.Errorf(extraArgsError, args, cmd.UsageString())
}
err := runGetCACert()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/fabric-ca-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var rootCmd = &cobra.Command{

const (
fabricCAClientProfileMode = "FABRIC_CA_CLIENT_PROFILE_MODE"
extraArgsError = "Unrecognized arguments found: %v\n\n%s"
)

var (
Expand Down
33 changes: 32 additions & 1 deletion cmd/fabric-ca-client/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/hyperledger/fabric-ca/lib"
"github.com/hyperledger/fabric-ca/lib/dbutil"
"github.com/hyperledger/fabric-ca/util"
"github.com/stretchr/testify/assert"
)

const (
Expand Down Expand Up @@ -109,6 +110,24 @@ var (
srv *lib.Server
)

type TestData struct {
input []string // input
}

func TestExtraArguments(t *testing.T) {
errCases := []TestData{
{[]string{cmdName, "enroll", "extraArg", "extraArg2"}},
{[]string{cmdName, "reenroll", "extraArg", "extraArg2"}},
{[]string{cmdName, "register", "extraArg", "extraArg2"}},
{[]string{cmdName, "revoke", "extraArg", "extraArg2"}},
{[]string{cmdName, "getcacert", "extraArg", "extraArg2"}},
}

for _, e := range errCases {
extraArgErrorTest(&e, t)
}
}

// TestCreateDefaultConfigFile test to make sure default config file gets generated correctly
func TestCreateDefaultConfigFile(t *testing.T) {
defYaml = util.GetDefaultConfigFile("fabric-ca-client")
Expand All @@ -118,7 +137,7 @@ func TestCreateDefaultConfigFile(t *testing.T) {

err := RunMain([]string{cmdName, "enroll", "-u", enrollURL, "-m", myhost})
if err == nil {
t.Errorf("No username/password provided, should have errored")
t.Errorf("No server running, should have failed")
}

fileBytes, err := ioutil.ReadFile(defYaml)
Expand Down Expand Up @@ -780,3 +799,15 @@ func getSerialAKIByID(id string) (serial, aki string, err error) {

return
}

func extraArgErrorTest(in *TestData, t *testing.T) {
err := RunMain(in.input)
if err == nil {
assert.Error(t, errors.New("Should have resulted in an error as extra agruments provided"))
}
if err != nil {
if !strings.Contains(err.Error(), "Extra arguments") {
assert.Error(t, fmt.Errorf("Failed for other reason besides extra argument: %s", err))
}
}
}
11 changes: 6 additions & 5 deletions cmd/fabric-ca-client/reenroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ var reenrollCmd = &cobra.Command{
Use: "reenroll",
Short: "Reenroll an identity",
Long: "Reenroll an identity with fabric-ca server",
// PreRunE block for this command will check to make sure enrollment
// information exists before running the command
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return fmt.Errorf(extraArgsError, args, cmd.UsageString())
}

err := configInit(cmd.Name())
if err != nil {
return err
Expand All @@ -42,11 +48,6 @@ var reenrollCmd = &cobra.Command{
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
cmd.Help()
return nil
}

err := runReenroll()
if err != nil {
return err
Expand Down
11 changes: 6 additions & 5 deletions cmd/fabric-ca-client/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ var registerCmd = &cobra.Command{
Use: "register",
Short: "Register an identity",
Long: "Register an identity with fabric-ca server",
// PreRunE block for this command will check to make sure enrollment
// information exists before running the command
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return fmt.Errorf(extraArgsError, args, cmd.UsageString())
}

err := configInit(cmd.Name())
if err != nil {
return err
Expand All @@ -41,11 +47,6 @@ var registerCmd = &cobra.Command{
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
cmd.Help()
return nil
}

err := runRegister()
if err != nil {
return err
Expand Down
12 changes: 7 additions & 5 deletions cmd/fabric-ca-client/revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"errors"
"fmt"
"path/filepath"

"github.com/cloudflare/cfssl/log"
Expand All @@ -33,7 +34,13 @@ var revokeCmd = &cobra.Command{
Use: "revoke",
Short: "Revoke an identity",
Long: "Revoke an identity with fabric-ca server",
// PreRunE block for this command will check to make sure enrollment
// information exists before running the command
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return fmt.Errorf(extraArgsError, args, cmd.UsageString())
}

err := configInit(cmd.Name())
if err != nil {
return err
Expand All @@ -44,11 +51,6 @@ var revokeCmd = &cobra.Command{
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
cmd.Help()
return nil
}

err := runRevoke(cmd)
if err != nil {
return err
Expand Down

0 comments on commit 6eec8f5

Please sign in to comment.