Skip to content

Commit

Permalink
[FAB-9392] Refactor client CLI code
Browse files Browse the repository at this point in the history
Refactor the fabric-ca-client CLI code to support integration tests.
Currently all the CLI related code lives in the "main" package.
However, it is desired that integration test be a self contained package.
To be able to do this requires moving the "RunMain" function outside of
the main package into a package of its own so that it can be referenced
and integration package can call to it.

Change-Id: I80d8f851e45ca558bd36acf944e8066ec4725244
Signed-off-by: Saad Karim <[email protected]>
  • Loading branch information
Saad Karim committed Apr 26, 2018
1 parent 6e186ea commit 403f2f7
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package command

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package command

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package command

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package command

import (
"io/ioutil"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main

package command

import (
"os"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package command

import (
"path/filepath"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package command

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package command

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package command

import (
"bufio"
Expand Down Expand Up @@ -51,9 +51,8 @@ import (
)

const (
testYaml = "../../testdata/test.yaml"
testdataDir = "homeDir"
mspDir = "../../testdata/msp"
mspDir = "../../../testdata/msp"
myhost = "hostname"
certfile = "ec.pem"
keyfile = "ec-key.pem"
Expand All @@ -63,7 +62,7 @@ const (
tlsClientCertFile = "tls_client-cert.pem"
tlsClientCertExpired = "expiredcert.pem"
tlsClientKeyFile = "tls_client-key.pem"
tdDir = "../../testdata"
tdDir = "../../../testdata"
db = "fabric-ca-server.db"
serverPort = 7090
rootCertEnvVar = "FABRIC_CA_CLIENT_TLS_CERTFILES"
Expand Down Expand Up @@ -132,6 +131,7 @@ var (
tlsServerURL = fmt.Sprintf("https://localhost:%d", serverPort)
tlsEnrollURL = fmt.Sprintf("https://admin:adminpw@localhost:%d", serverPort)
tlsEnrollURL1 = fmt.Sprintf("https://admin2:adminpw2@localhost:%d", serverPort)
testYaml = path.Join(tdDir, "test.yaml")
)

type TestData struct {
Expand Down Expand Up @@ -1457,13 +1457,13 @@ func testRegisterConfigFile(t *testing.T) {
t.Log("Testing Register command using config file")

err := RunMain([]string{cmdName, "enroll", "-d", "-c",
"../../testdata/fabric-ca-client-config.yaml", "-u", enrollURL1})
"../../../testdata/fabric-ca-client-config.yaml", "-u", enrollURL1})
if err != nil {
t.Errorf("client enroll -u failed: %s", err)
}

err = RunMain([]string{cmdName, "register", "-d", "-c",
"../../testdata/fabric-ca-client-config.yaml"})
"../../../testdata/fabric-ca-client-config.yaml"})
if err != nil {
t.Errorf("client register failed using config file: %s", err)
}
Expand All @@ -1473,7 +1473,7 @@ func testRegisterConfigFile(t *testing.T) {
func testRegisterEnvVar(t *testing.T) {
t.Log("Testing Register command using env variables")

os.Setenv("FABRIC_CA_CLIENT_HOME", "../../testdata/")
os.Setenv("FABRIC_CA_CLIENT_HOME", tdDir)
os.Setenv("FABRIC_CA_CLIENT_ID_NAME", "testRegister2")
os.Setenv("FABRIC_CA_CLIENT_ID_AFFILIATION", "hyperledger.org2")
os.Setenv("FABRIC_CA_CLIENT_ID_TYPE", "client")
Expand All @@ -1493,7 +1493,7 @@ func testRegisterEnvVar(t *testing.T) {
// testRegisterCommandLine tests fabric-ca-client register using command line input
func testRegisterCommandLine(t *testing.T, srv *lib.Server) {
t.Log("Testing Register using command line options")
os.Setenv("FABRIC_CA_CLIENT_HOME", "../../testdata/")
os.Setenv("FABRIC_CA_CLIENT_HOME", tdDir)
defer os.Unsetenv("FABRIC_CA_CLIENT_HOME")

fooName := "foo"
Expand Down Expand Up @@ -1579,7 +1579,7 @@ func testRegisterCommandLine(t *testing.T, srv *lib.Server) {
// TestRevoke tests fabric-ca-client revoke
func testRevoke(t *testing.T) {
t.Log("Testing Revoke command")
clientHome := "../../testdata/"
clientHome := tdDir
os.Setenv("FABRIC_CA_CLIENT_HOME", clientHome)
defer os.Unsetenv("FABRIC_CA_CLIENT_HOME")

Expand Down Expand Up @@ -1881,7 +1881,7 @@ func TestClientCommandsUsingConfigFile(t *testing.T) {
}

err = RunMain([]string{cmdName, "enroll", "-c",
"../../testdata/fabric-ca-client-config.yaml", "-u",
filepath.Join(tdDir, "fabric-ca-client-config.yaml"), "-u",
tlsEnrollURL, "-d"})
if err != nil {
t.Errorf("client enroll -c -u failed: %s", err)
Expand Down Expand Up @@ -1980,7 +1980,7 @@ func TestMultiCA(t *testing.T) {
cleanMultiCADir()

srv = lib.TestGetServer(serverPort, testdataDir, "", -1, t)
srv.HomeDir = "../../testdata"
srv.HomeDir = tdDir
srv.Config.CAfiles = []string{"ca/rootca/ca1/fabric-ca-server-config.yaml",
"ca/rootca/ca2/fabric-ca-server-config.yaml"}
srv.CA.Config.CSR.Hosts = []string{"hostname"}
Expand Down Expand Up @@ -2076,8 +2076,9 @@ func TestHomeDirectory(t *testing.T) {
configFilePath := util.GetDefaultConfigFile(clientCMD)
defaultClientConfigDir, defaultClientConfigFile := filepath.Split(configFilePath)

os.RemoveAll("../../testdata/testhome")
defer os.RemoveAll("../../testdata/testhome")
dir := filepath.Join(tdDir, "testhome")
os.RemoveAll(dir)
defer os.RemoveAll(dir)

RunMain([]string{cmdName, "enroll", "-u", enrollURL, "-c", ""})
if !util.FileExists(configFilePath) {
Expand All @@ -2098,19 +2099,19 @@ func TestHomeDirectory(t *testing.T) {
t.Errorf("Failed to correctly created the default config (fabric-ca-client-config) in the default home directory")
}

RunMain([]string{cmdName, "enroll", "-u", enrollURL, "-H", "../../testdata/testhome/testclientcmd"})
if !util.FileExists(filepath.Join("../../testdata/testhome/testclientcmd", defaultClientConfigFile)) {
t.Errorf("Failed to correctly created the default config (fabric-ca-client-config.yaml) in the '../../testdata/testhome/testclientcmd' directory")
RunMain([]string{cmdName, "enroll", "-u", enrollURL, "-H", filepath.Join(tdDir, "testhome/testclientcmd")})
if !util.FileExists(filepath.Join(tdDir, "testhome/testclientcmd", defaultClientConfigFile)) {
t.Errorf("Failed to correctly created the default config (fabric-ca-client-config.yaml) in the '../../../testdata/testhome/testclientcmd' directory")
}

RunMain([]string{cmdName, "enroll", "-u", enrollURL, "-d", "-c", "../../testdata/testhome/testclientcmd2/testconfig2.yaml"})
if !util.FileExists("../../testdata/testhome/testclientcmd2/testconfig2.yaml") {
t.Errorf("Failed to correctly created the config (testconfig2.yaml) in the '../../testdata/testhome/testclientcmd2' directory")
RunMain([]string{cmdName, "enroll", "-u", enrollURL, "-d", "-c", filepath.Join(tdDir, "testhome/testclientcmd2/testconfig2.yaml")})
if !util.FileExists(filepath.Join(tdDir, "testhome/testclientcmd2/testconfig2.yaml")) {
t.Errorf("Failed to correctly created the config (testconfig2.yaml) in the '../../../testdata/testhome/testclientcmd2' directory")
}

RunMain([]string{cmdName, "enroll", "-u", enrollURL, "-d", "-H", "../../testdata/testclientcmd3", "-c", "../../testdata/testhome/testclientcmd3/testconfig3.yaml"})
if !util.FileExists("../../testdata/testhome/testclientcmd3/testconfig3.yaml") {
t.Errorf("Failed to correctly created the config (testconfig3.yaml) in the '../../testdata/testhome/testclientcmd3' directory")
RunMain([]string{cmdName, "enroll", "-u", enrollURL, "-d", "-H", filepath.Join(tdDir, "testclientcmd3"), "-c", filepath.Join(tdDir, "testhome/testclientcmd3/testconfig3.yaml")})
if !util.FileExists(filepath.Join(tdDir, "testhome/testclientcmd3/testconfig3.yaml")) {
t.Errorf("Failed to correctly created the config (testconfig3.yaml) in the '../../../testdata/testhome/testclientcmd3' directory")
}

}
Expand Down Expand Up @@ -2145,8 +2146,8 @@ func TestDebugSetting(t *testing.T) {
}

func TestCleanUp(t *testing.T) {
os.Remove("../../testdata/ca-cert.pem")
os.Remove("../../testdata/ca-key.pem")
os.Remove(filepath.Join(tdDir, "ca-cert.pem"))
os.Remove(filepath.Join(tdDir, "ca-key.pem"))
os.Remove(testYaml)
os.Remove(fabricCADB)
os.RemoveAll(mspDir)
Expand All @@ -2155,7 +2156,7 @@ func TestCleanUp(t *testing.T) {
}

func cleanMultiCADir() {
caFolder := "../../testdata/ca/rootca"
caFolder := filepath.Join(tdDir, "ca/rootca")
nestedFolders := []string{"ca1", "ca2"}
removeFiles := []string{"msp", "ca-cert.pem",
"fabric-ca-server.db", "fabric-ca2-server.db", "ca-chain.pem"}
Expand All @@ -2178,7 +2179,7 @@ func TestRegisterWithoutEnroll(t *testing.T) {

func testGetCACertEnvVar(t *testing.T) error {
t.Log("testGetCACertEnvVar - Entered")
os.Setenv(rootCertEnvVar, "../../testdata/root.pem")
os.Setenv(rootCertEnvVar, filepath.Join(tdDir, "root.pem"))
defer os.Unsetenv(rootCertEnvVar)

defer os.RemoveAll("msp")
Expand All @@ -2193,7 +2194,7 @@ func testGetCACertEnvVar(t *testing.T) error {

func testGetCACertConfigFile(t *testing.T) error {
t.Log("testGetCACertConfigFile - Entered")
configFile := "../../testdata/fabric-ca-client-config.yaml"
configFile := filepath.Join(tdDir, "fabric-ca-client-config.yaml")

err := RunMain([]string{cmdName, "getcacert", "-d", "-c", configFile, "-u", tlsServerURL, "--tls.certfiles", rootCert})
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package command

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package command

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package command

import (
"path/filepath"
Expand Down
39 changes: 39 additions & 0 deletions cmd/fabric-ca-client/command/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright IBM Corp. 2018 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package command

import "os"

// RunMain is the fabric-ca client main
func RunMain(args []string) error {
// Save the os.Args
saveOsArgs := os.Args
os.Args = args

// Execute the command
cmdName := ""
if len(args) > 1 {
cmdName = args[1]
}
ccmd := NewCommand(cmdName)
err := ccmd.Execute()

// Restore original os.Args
os.Args = saveOsArgs

return err
}
24 changes: 3 additions & 21 deletions cmd/fabric-ca-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,13 @@ package main

import (
"os"

"github.com/hyperledger/fabric-ca/cmd/fabric-ca-client/command"
)

// The fabric-ca client main
func main() {
if err := RunMain(os.Args); err != nil {
if err := command.RunMain(os.Args); err != nil {
os.Exit(1)
}
}

// RunMain is the fabric-ca client main
func RunMain(args []string) error {
// Save the os.Args
saveOsArgs := os.Args
os.Args = args

// Execute the command
cmdName := ""
if len(args) > 1 {
cmdName = args[1]
}
ccmd := NewCommand(cmdName)
err := ccmd.Execute()

// Restore original os.Args
os.Args = saveOsArgs

return err
}
2 changes: 1 addition & 1 deletion scripts/run_tests
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export PATH=$PATH:$GOPATH/bin
go get github.com/axw/gocov/...
go get github.com/AlekSi/gocov-xml

PKGS=`go list github.com/hyperledger/fabric-ca/... | grep -Ev '/vendor/|/api|/dbutil|/ldap|/mocks|/test/fabric-ca-load-tester'`
PKGS=`go list github.com/hyperledger/fabric-ca/... | grep -Ev '/vendor/|/api|/dbutil|/ldap|/mocks|/test/fabric-ca-load-tester|/fabric-ca-client$'`

gocov test -timeout 15m $PKGS | gocov-xml > coverage.xml

Expand Down

0 comments on commit 403f2f7

Please sign in to comment.