Skip to content

Commit

Permalink
[FAB-7097] Fix tests when GOPATH isn't set
Browse files Browse the repository at this point in the history
This patch allows usage of the SDK when the GOPATH env variable
is not set.

Change-Id: I60e66a45c04f93dfd943db6d29eb2904462c4df2
Signed-off-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
troyronda committed Nov 27, 2017
1 parent dd63d01 commit 7053d2c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
46 changes: 24 additions & 22 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ package config
import (
"crypto/x509"
"encoding/pem"
"go/build"
"io/ioutil"
"math/rand"
"os"
"path"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -96,7 +98,7 @@ func loadDefaultConfig(myViper *viper.Viper) error {
return nil
}
// if set, use it to load default config
myViper.AddConfigPath(strings.Replace(defaultPath, "$GOPATH", os.Getenv("GOPATH"), -1))
myViper.AddConfigPath(substGoPath(defaultPath))
err := myViper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
return errors.Wrap(err, "loading config file failed")
Expand Down Expand Up @@ -147,7 +149,7 @@ func (c *Config) CAServerCertFiles(org string) ([]string, error) {

certFileModPath := make([]string, len(certFiles))
for i, v := range certFiles {
certFileModPath[i] = strings.Replace(v, "$GOPATH", os.Getenv("GOPATH"), -1)
certFileModPath[i] = substGoPath(v)
}
return certFileModPath, nil
}
Expand Down Expand Up @@ -187,8 +189,7 @@ func (c *Config) CAClientKeyFile(org string) (string, error) {
if _, ok := config.CertificateAuthorities[strings.ToLower(caName)]; !ok {
return "", errors.Errorf("CA Server Name '%s' not found", caName)
}
return strings.Replace(config.CertificateAuthorities[strings.ToLower(caName)].TLSCACerts.Client.Keyfile,
"$GOPATH", os.Getenv("GOPATH"), -1), nil
return substGoPath(config.CertificateAuthorities[strings.ToLower(caName)].TLSCACerts.Client.Keyfile), nil
}

// CAClientCertFile Read configuration option for the fabric CA client cert file
Expand All @@ -205,8 +206,7 @@ func (c *Config) CAClientCertFile(org string) (string, error) {
if _, ok := config.CertificateAuthorities[strings.ToLower(caName)]; !ok {
return "", errors.Errorf("CA Server Name '%s' not found", caName)
}
return strings.Replace(config.CertificateAuthorities[strings.ToLower(caName)].TLSCACerts.Client.Certfile,
"$GOPATH", os.Getenv("GOPATH"), -1), nil
return substGoPath(config.CertificateAuthorities[strings.ToLower(caName)].TLSCACerts.Client.Certfile), nil
}

// TimeoutOrDefault reads connection timeouts for the given connection type
Expand Down Expand Up @@ -303,8 +303,7 @@ func (c *Config) OrderersConfig() ([]apiconfig.OrdererConfig, error) {

for _, orderer := range config.Orderers {
if orderer.TLSCACerts.Path != "" {
orderer.TLSCACerts.Path = strings.Replace(orderer.TLSCACerts.Path, "$GOPATH",
os.Getenv("GOPATH"), -1)
orderer.TLSCACerts.Path = substGoPath(orderer.TLSCACerts.Path)
}

orderers = append(orderers, orderer)
Expand Down Expand Up @@ -345,8 +344,7 @@ func (c *Config) OrdererConfig(name string) (*apiconfig.OrdererConfig, error) {
}

if orderer.TLSCACerts.Path != "" {
orderer.TLSCACerts.Path = strings.Replace(orderer.TLSCACerts.Path, "$GOPATH",
os.Getenv("GOPATH"), -1)
orderer.TLSCACerts.Path = substGoPath(orderer.TLSCACerts.Path)
}

return &orderer, nil
Expand All @@ -369,8 +367,7 @@ func (c *Config) PeersConfig(org string) ([]apiconfig.PeerConfig, error) {
return nil, err
}
if p.TLSCACerts.Path != "" {
p.TLSCACerts.Path = strings.Replace(p.TLSCACerts.Path, "$GOPATH",
os.Getenv("GOPATH"), -1)
p.TLSCACerts.Path = substGoPath(p.TLSCACerts.Path)
}

peers = append(peers, p)
Expand Down Expand Up @@ -402,8 +399,7 @@ func (c *Config) PeerConfig(org string, name string) (*apiconfig.PeerConfig, err
}

if peerConfig.TLSCACerts.Path != "" {
peerConfig.TLSCACerts.Path = strings.Replace(peerConfig.TLSCACerts.Path, "$GOPATH",
os.Getenv("GOPATH"), -1)
peerConfig.TLSCACerts.Path = substGoPath(peerConfig.TLSCACerts.Path)
}
return &peerConfig, nil
}
Expand Down Expand Up @@ -484,7 +480,7 @@ func (c *Config) ChannelPeers(name string) ([]apiconfig.ChannelPeer, error) {
}

if p.TLSCACerts.Path != "" {
p.TLSCACerts.Path = strings.Replace(p.TLSCACerts.Path, "$GOPATH", os.Getenv("GOPATH"), -1)
p.TLSCACerts.Path = substGoPath(p.TLSCACerts.Path)
}

mspID, err := c.PeerMspID(peerName)
Expand Down Expand Up @@ -519,7 +515,7 @@ func (c *Config) NetworkPeers() ([]apiconfig.NetworkPeer, error) {
}

if p.TLSCACerts.Path != "" {
p.TLSCACerts.Path = strings.Replace(p.TLSCACerts.Path, "$GOPATH", os.Getenv("GOPATH"), -1)
p.TLSCACerts.Path = substGoPath(p.TLSCACerts.Path)
}

mspID, err := c.PeerMspID(name)
Expand Down Expand Up @@ -662,23 +658,20 @@ func (c *Config) SecurityProviderLabel() string {

// KeyStorePath returns the keystore path used by BCCSP
func (c *Config) KeyStorePath() string {
keystorePath := strings.Replace(c.configViper.GetString("client.credentialStore.cryptoStore.path"),
"$GOPATH", os.Getenv("GOPATH"), -1)
keystorePath := substGoPath(c.configViper.GetString("client.credentialStore.cryptoStore.path"))
return path.Join(keystorePath, "keystore")
}

// CAKeyStorePath returns the same path as KeyStorePath() without the
// 'keystore' directory added. This is done because the fabric-ca-client
// adds this to the path
func (c *Config) CAKeyStorePath() string {
return strings.Replace(c.configViper.GetString("client.credentialStore.cryptoStore.path"),
"$GOPATH", os.Getenv("GOPATH"), -1)
return substGoPath(c.configViper.GetString("client.credentialStore.cryptoStore.path"))
}

// CryptoConfigPath ...
func (c *Config) CryptoConfigPath() string {
return strings.Replace(c.configViper.GetString("client.cryptoconfig.path"),
"$GOPATH", os.Getenv("GOPATH"), -1)
return substGoPath(c.configViper.GetString("client.cryptoconfig.path"))
}

// loadCAKey
Expand All @@ -695,3 +688,12 @@ func loadCAKey(rawData []byte) (*x509.Certificate, error) {
}
return nil, errors.New("pem data missing")
}

// substGoPath replaces instances of '$GOPATH' with the GOPATH. If the system
// has multiple GOPATHs then the first is used.
func substGoPath(s string) string {
gpDefault := build.Default.GOPATH
gps := filepath.SplitList(gpDefault)

return strings.Replace(s, "$GOPATH", gps[0], -1)
}
13 changes: 11 additions & 2 deletions pkg/fabric-client/ccpackager/gopackager/packager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"archive/tar"
"bytes"
"compress/gzip"
"go/build"
"io"
"os"
"path"
Expand Down Expand Up @@ -46,8 +47,7 @@ func NewCCPackage(chaincodePath string, goPath string) (*fab.CCPackage, error) {
var projDir string
gp := goPath
if gp == "" {
// TODO: for now use env variable
gp = os.Getenv("GOPATH")
gp = defaultGoPath()
if gp == "" {
return nil, errors.New("GOPATH not defined")
}
Expand Down Expand Up @@ -185,3 +185,12 @@ func packEntry(tw *tar.Writer, gw *gzip.Writer, descriptor *Descriptor) error {
}
return nil
}

// defaultGoPath returns the system's default GOPATH. If the system
// has multiple GOPATHs then the first is used.
func defaultGoPath() string {
gpDefault := build.Default.GOPATH
gps := filepath.SplitList(gpDefault)

return gps[0]
}

0 comments on commit 7053d2c

Please sign in to comment.