Skip to content

Commit

Permalink
[FAB-9312] Resolve metalinter warnings
Browse files Browse the repository at this point in the history
Change-Id: I68d740f03825136b13ea36cdbd58a0268416b3bd
Signed-off-by: Firas Qutishat <firas.qutishat@securekey.com>
  • Loading branch information
fqutishat committed Apr 12, 2018
1 parent b16df6d commit a5c9ea3
Show file tree
Hide file tree
Showing 19 changed files with 414 additions and 330 deletions.
452 changes: 256 additions & 196 deletions pkg/fab/endpointconfig.go

Large diffs are not rendered by default.

69 changes: 46 additions & 23 deletions pkg/fab/endpointconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,25 @@ func TestCAConfigFailsByNetworkConfig(t *testing.T) {
t.Fatal("Testing PeersConfig supposed to fail")
}

checkCAConfigFailsByNetworkConfig(sampleEndpointConfig, t)
}

func checkCAConfigFailsByNetworkConfig(sampleEndpointConfig *EndpointConfig, t *testing.T) {
//Testing PeersConfig failure scenario
pConfig, err := sampleEndpointConfig.PeerConfig("peerorg1", "peer1")
if pConfig != nil || err == nil {
t.Fatal("Testing PeerConfig supposed to fail")
}

//Testing ChannelConfig failure scenario
chConfig, err := sampleEndpointConfig.ChannelConfig("invalid")
if chConfig != nil || err == nil {
t.Fatal("Testing ChannelConfig supposed to fail")
}

//Testing ChannelPeers failure scenario
cpConfigs, err := sampleEndpointConfig.ChannelPeers("invalid")
if cpConfigs != nil || err == nil {
t.Fatal("Testing ChannelPeeers supposed to fail")
}

//Testing ChannelOrderers failure scenario
coConfigs, err := sampleEndpointConfig.ChannelOrderers("invalid")
if coConfigs != nil || err == nil {
Expand All @@ -143,12 +144,12 @@ func TestTLSCAConfig(t *testing.T) {
t.Fatalf("Failed to get TLS CA Cert, reason: %v", err)
}

config, err := ConfigFromBackend(configBackend)
configBackend1, err := ConfigFromBackend(configBackend)
if err != nil {
t.Fatalf("Failed to get endpoint config, reason: %v", err)
}

endpointConfig := config.(*EndpointConfig)
endpointConfig := configBackend1.(*EndpointConfig)

_, err = endpointConfig.TLSCACertPool(cert)
if err != nil {
Expand All @@ -175,6 +176,10 @@ func TestTLSCAConfig(t *testing.T) {

_, err = endpointConfig.TLSCACertPool(badCert)

if err != nil {
t.Fatalf(err.Error())
}

keyConfig := endpoint.TLSConfig{Path: keyPath}

key, err := keyConfig.TLSCert()
Expand All @@ -184,6 +189,9 @@ func TestTLSCAConfig(t *testing.T) {
}

_, err = endpointConfig.TLSCACertPool(key)
if err != nil {
t.Fatalf(err.Error())
}
}

func TestTimeouts(t *testing.T) {
Expand Down Expand Up @@ -233,7 +241,11 @@ func TestTimeouts(t *testing.T) {
if t1 != time.Millisecond*2 {
t.Fatalf(errStr, "OrdererConnection", t1)
}
t1 = endpointConfig.Timeout(fab.OrdererResponse)
checkTimeouts(endpointConfig, t, errStr)
}

func checkTimeouts(endpointConfig fab.EndpointConfig, t *testing.T, errStr string) {
t1 := endpointConfig.Timeout(fab.OrdererResponse)
if t1 != time.Second*6 {
t.Fatalf(errStr, "OrdererResponse", t1)
}
Expand Down Expand Up @@ -314,7 +326,11 @@ func TestDefaultTimeouts(t *testing.T) {
if t1 != defaultOrdererConnectionTimeout {
t.Fatalf(errStr, "OrdererConnection", t1)
}
t1 = endpointConfig.Timeout(fab.OrdererResponse)
checkDefaultTimeout(endpointConfig, t, errStr)
}

func checkDefaultTimeout(endpointConfig fab.EndpointConfig, t *testing.T, errStr string) {
t1 := endpointConfig.Timeout(fab.OrdererResponse)
if t1 != defaultOrdererResponseTimeout {
t.Fatalf(errStr, "OrdererResponse", t1)
}
Expand Down Expand Up @@ -406,19 +422,22 @@ func TestPeerConfigByUrl_entityMatchers(t *testing.T) {
}

func testCommonConfigPeerByURL(t *testing.T, expectedConfigURL string, fetchedConfigURL string) {
config, err := ConfigFromBackend(configBackend)
config1, err := ConfigFromBackend(configBackend)
if err != nil {
t.Fatal("Failed to get endpoint config from backend")
}

endpointConfig := config.(*EndpointConfig)
endpointConfig := config1.(*EndpointConfig)

expectedConfig, err := endpointConfig.peerConfig(expectedConfigURL)
if err != nil {
t.Fatalf(err.Error())
}

fetchedConfig, err := endpointConfig.PeerConfigByURL(fetchedConfigURL)
if err != nil {
t.Fatalf(err.Error())
}

if fetchedConfig.URL == "" {
t.Fatalf("Url value for the host is empty")
Expand Down Expand Up @@ -617,12 +636,12 @@ func TestPeerConfig(t *testing.T) {

func testCommonConfigPeer(t *testing.T, expectedConfigHost string, fetchedConfigHost string) (expectedConfig *fab.PeerConfig, fetchedConfig *fab.PeerConfig) {

config, err := ConfigFromBackend(configBackend)
config1, err := ConfigFromBackend(configBackend)
if err != nil {
t.Fatal("Failed to get endpoint config from backend")
}

endpointConfig := config.(*EndpointConfig)
endpointConfig := config1.(*EndpointConfig)

expectedConfig, err = endpointConfig.peerConfig(expectedConfigHost)
if err != nil {
Expand Down Expand Up @@ -781,19 +800,19 @@ func TestInitConfigFromRawWithPem(t *testing.T) {
t.Fatalf("Failed to initialize config from bytes array. Error: %s", err)
}

config, err := ConfigFromBackend(backend)
config1, err := ConfigFromBackend(backend)
if err != nil {
t.Fatalf("Failed to initialize config from bytes array. Error: %s", err)
}

endpointConfig := config.(*EndpointConfig)
endpointConfig := config1.(*EndpointConfig)

o, err := endpointConfig.OrderersConfig()
if err != nil {
t.Fatalf("Failed to load orderers from config. Error: %s", err)
}

if o == nil || len(o) == 0 {
if len(o) == 0 {
t.Fatalf("orderer cannot be nil or empty")
}

Expand All @@ -820,9 +839,14 @@ SQtE5YgdxkUCIHReNWh/pluHTxeGu2jNCH1eh6o2ajSGeeizoapvdJbN
if err != nil {
t.Fatalf(err.Error())
}
if pc == nil || len(pc) == 0 {
if len(pc) == 0 {
t.Fatalf("peers list of %s cannot be nil or empty", org1)
}
checkPem(endpointConfig, t)

}

func checkPem(endpointConfig *EndpointConfig, t *testing.T) {
peer0 := "peer0.org1.example.com"
p0, err := endpointConfig.PeerConfig(org1, peer0)
if err != nil {
Expand All @@ -846,12 +870,11 @@ V842OVjxCYYQwCjPIY+5e9ORR+8pxVzcMAoGCCqGSM49BAMCA0cAMEQCIGZ+KTfS
eezqv0ml1VeQEmnAEt5sJ2RJA58+LegUYMd6AiAfEe6BKqdY03qFUgEYmtKG+3Dr
O94CDp7l2k7hMQI0zQ==
-----END CERTIFICATE-----`

loadedPPem := strings.TrimSpace(p0.TLSCACerts.Pem) // viper's unmarshall adds a \n to the end of a string, hence the TrimeSpace
loadedPPem := strings.TrimSpace(p0.TLSCACerts.Pem)
// viper's unmarshall adds a \n to the end of a string, hence the TrimeSpace
if loadedPPem != pPem {
t.Fatalf("%s Pem doesn't match. Expected \n'%s'\n, but got \n'%s'\n", peer0, pPem, loadedPPem)
}

}

func loadConfigBytesFromFile(t *testing.T, filePath string) ([]byte, error) {
Expand All @@ -866,7 +889,7 @@ func loadConfigBytesFromFile(t *testing.T, filePath string) ([]byte, error) {
t.Fatalf("Failed to read config file stat. Error: %s", err)
}
s := fi.Size()
cBytes := make([]byte, s, s)
cBytes := make([]byte, s)
n, err := f.Read(cBytes)
if err != nil {
t.Fatalf("Failed to read test config for bytes array testing. Error: %s", err)
Expand All @@ -879,12 +902,12 @@ func loadConfigBytesFromFile(t *testing.T, filePath string) ([]byte, error) {

func TestLoadConfigWithEmbeddedUsersWithPems(t *testing.T) {
// get a config file with embedded users
configBackend, err := config.FromFile(configEmbeddedUsersTestFilePath)()
configBackend1, err := config.FromFile(configEmbeddedUsersTestFilePath)()
if err != nil {
t.Fatal(err)
}

endpointConfig, err := ConfigFromBackend(configBackend)
endpointConfig, err := ConfigFromBackend(configBackend1)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -914,12 +937,12 @@ func TestLoadConfigWithEmbeddedUsersWithPems(t *testing.T) {

func TestLoadConfigWithEmbeddedUsersWithPaths(t *testing.T) {
// get a config file with embedded users
configBackend, err := config.FromFile(configEmbeddedUsersTestFilePath)()
configBackend1, err := config.FromFile(configEmbeddedUsersTestFilePath)()
if err != nil {
t.Fatal(err)
}

endpointConfig, err := ConfigFromBackend(configBackend)
endpointConfig, err := ConfigFromBackend(configBackend1)
if err != nil {
t.Fatal(err)
}
Expand Down
22 changes: 17 additions & 5 deletions pkg/fab/mocks/mockbroadcastserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,21 @@ func (m *MockBroadcastServer) Deliver(server po.AtomicBroadcast_DeliverServer) e
}

if m.DeliverResponse != nil {
server.Recv()
server.SendMsg(m.DeliverResponse)
if _, err := server.Recv(); err != nil {
return err
}
if err := server.SendMsg(m.DeliverResponse); err != nil {
return err
}
return nil
}

server.Recv()
server.Send(TestBlock)
if _, err := server.Recv(); err != nil {
return err
}
if err := server.Send(TestBlock); err != nil {
return err
}

return nil
}
Expand All @@ -92,7 +100,11 @@ func StartMockBroadcastServer(broadcastTestURL string, grpcServer *grpc.Server)

broadcastServer := new(MockBroadcastServer)
po.RegisterAtomicBroadcastServer(grpcServer, broadcastServer)
go grpcServer.Serve(lis)
go func() {
if err := grpcServer.Serve(lis); err != nil {
panic(err.Error())
}
}()

return broadcastServer, addr
}
3 changes: 1 addition & 2 deletions pkg/fab/mocks/mockchprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package mocks

import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
)
Expand All @@ -29,7 +28,7 @@ type MockChannelService struct {
}

// NewMockChannelProvider returns a mock ChannelProvider
func NewMockChannelProvider(ctx context.Client) (*MockChannelProvider, error) {
func NewMockChannelProvider(ctx core.Providers) (*MockChannelProvider, error) {
// Create a mock client with the mock channel
cp := MockChannelProvider{
ctx: ctx,
Expand Down
2 changes: 1 addition & 1 deletion pkg/fab/mocks/mockcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ type MockTransactionHeader struct {

// TransactionID returns the transaction's computed identifier.
func (th *MockTransactionHeader) TransactionID() fab.TransactionID {
return fab.TransactionID(th.MockID)
return th.MockID
}

// Creator returns the transaction creator's identity bytes.
Expand Down
6 changes: 5 additions & 1 deletion pkg/fab/mocks/mockendorserserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func StartEndorserServer(endorserTestURL string) *MockEndorserServer {
endorserServer := &MockEndorserServer{}
pb.RegisterEndorserServer(grpcServer, endorserServer)
fmt.Printf("Test endorser server started\n")
go grpcServer.Serve(lis)
go func() {
if err := grpcServer.Serve(lis); err != nil {
panic(err.Error())
}
}()
return endorserServer
}
21 changes: 16 additions & 5 deletions pkg/fab/mocks/mockeventserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ func StartMockEventServer(testAddress string) (*MockEventServer, error) {
eventServer := &MockEventServer{grpcServer: grpcServer}
pb.RegisterEventsServer(grpcServer, eventServer)
fmt.Printf("Starting mock event server\n")
go grpcServer.Serve(lis)
go func() {
if err := grpcServer.Serve(lis); err != nil {
panic(err.Error())
}
}()

return eventServer, nil
}
Expand All @@ -42,19 +46,26 @@ func StartMockEventServer(testAddress string) (*MockEventServer, error) {
func (m *MockEventServer) Chat(srv pb.Events_ChatServer) error {
m.server = srv
m.channel = make(chan *pb.Event)
in, _ := srv.Recv()
in, err := srv.Recv()
if err != nil {
return err
}
evt := &pb.Event{}
err := proto.Unmarshal(in.EventBytes, evt)
err = proto.Unmarshal(in.EventBytes, evt)
if err != nil {
return fmt.Errorf("error unmarshaling the event bytes in the SignedEvent: %s", err)
}
switch evt.Event.(type) {
case *pb.Event_Register:
srv.Send(&pb.Event{Event: &pb.Event_Register{Register: &pb.Register{}}})
if err := srv.Send(&pb.Event{Event: &pb.Event_Register{Register: &pb.Register{}}}); err != nil {
return err
}
}
for {
event := <-m.channel
srv.Send(event)
if err := srv.Send(event); err != nil {
return err
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/fab/mocks/mockfabricprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ import (
reqContext "context"

"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
)

// MockInfraProvider represents the default implementation of Fabric objects.
type MockInfraProvider struct {
providerContext context.Providers
customOrderer fab.Orderer
customTransactor fab.Transactor
}
Expand Down
6 changes: 0 additions & 6 deletions pkg/fab/mocks/mockidentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
msp_protos "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/msp"
)

Expand Down Expand Up @@ -74,11 +73,6 @@ func (id *MockIdentity) Serialize() ([]byte, error) {
return nil, nil
}

func (id *MockIdentity) getHashOpt(hashFamily string) (core.HashOpts, error) {

return nil, nil
}

// MockSigningIdentity ...
type MockSigningIdentity struct {
// we embed everything from a base identity
Expand Down
2 changes: 1 addition & 1 deletion pkg/fab/mocks/mocktransactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (t *MockTransactor) CreateTransactionHeader() (fab.TransactionHeader, error

// SendTransactionProposal sends a TransactionProposal to the target peers.
func (t *MockTransactor) SendTransactionProposal(proposal *fab.TransactionProposal, targets []fab.ProposalProcessor) ([]*fab.TransactionProposalResponse, error) {
response := make([]*fab.TransactionProposalResponse, 1, 1)
response := make([]*fab.TransactionProposalResponse, 1)
response[0] = &fab.TransactionProposalResponse{Endorser: "example.com", Status: 99,
ProposalResponse: &pb.ProposalResponse{Response: &pb.Response{Payload: []byte("abc")}},
}
Expand Down
Loading

0 comments on commit a5c9ea3

Please sign in to comment.