Skip to content

Commit

Permalink
[FAB-9312] Resolve metalinter warnings
Browse files Browse the repository at this point in the history
- fix fabsdk package

Change-Id: I3154e331f38de89975e6b5335fdf59c5c720a512
Signed-off-by: Firas Qutishat <[email protected]>
  • Loading branch information
fqutishat committed Apr 13, 2018
1 parent f8fb6c2 commit c8911f3
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 57 deletions.
31 changes: 31 additions & 0 deletions gometalinter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"Deadline": "5m",
"Exclude": [
".*seekInfo can be .*proto.Message.*"
],
"EnableGC": true,
"WarnUnmatchedDirective": true,
"Enable": [
"deadcode",
"gocyclo",
"gotype",
"goimports",
"golint",
"gosimple",
"ineffassign",
"interfacer",
"misspell",
"unconvert",
"unused",
"vet",
"varcheck",
"maligned",
"errcheck",
"megacheck",
"goconst",
"gas",
"structcheck"
],
"Cyclo": 10,
"Aggregate": true
}
2 changes: 1 addition & 1 deletion pkg/fab/events/deliverclient/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (c *DeliverConnection) deliverStream() deliverStream {
}

// Send sends a seek request to the deliver server
func (c *DeliverConnection) Send(seekInfo *ab.SeekInfo) error { //nolint
func (c *DeliverConnection) Send(seekInfo *ab.SeekInfo) error {
if c.Closed() {
return errors.New("connection is closed")
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/fabsdk/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func WithOrg(org string) ContextOption {
// don't include neither username nor identity
var ErrAnonymousIdentity = errors.New("missing credentials")

func (sdk *FabricSDK) newIdentity(options ...ContextOption) (msp.SigningIdentity, error) {
func (sdk *FabricSDK) newIdentity(options ...ContextOption) (msp.SigningIdentity, error) { //nolint
clientConfig, err := sdk.provider.IdentityConfig().Client()
if err != nil {
return nil, errors.WithMessage(err, "retrieving client configuration failed")
Expand All @@ -59,9 +59,9 @@ func (sdk *FabricSDK) newIdentity(options ...ContextOption) (msp.SigningIdentity
}

for _, option := range options {
err := option(&opts)
if err != nil {
return nil, errors.WithMessage(err, "error in option passed to create identity")
err1 := option(&opts)
if err1 != nil {
return nil, errors.WithMessage(err1, "error in option passed to create identity")
}
}

Expand Down
26 changes: 13 additions & 13 deletions pkg/fabsdk/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,36 +99,36 @@ func TestFabricSDKContext(t *testing.T) {
}

// Valid user, invalid org
ctxProvider = sdk.Context(WithUser(identityValidOptUser), WithOrg("INVALID_ORG_NAME"))
checkValidUserAndInvalidOrg(sdk, t)

ctx, err = ctxProvider()
// Valid user and org
checkValidUserAndOrg(sdk, t)

}

func checkValidUserAndInvalidOrg(sdk *FabricSDK, t *testing.T) {
ctxProvider := sdk.Context(WithUser(identityValidOptUser), WithOrg("INVALID_ORG_NAME"))
ctx, err := ctxProvider()
if err == nil || err.Error() != "invalid options to create identity, invalid org name" {
t.Fatalf("getting context client supposed to fail with idenity error, err: %v", err)
}

if ctx == nil {
t.Fatal("context client will have providers even if idenity fails")
}
}

// Valid user and org
ctxProvider = sdk.Context(WithUser(identityValidOptUser), WithOrg(identityValidOptOrg))

_, err = ctxProvider()
func checkValidUserAndOrg(sdk *FabricSDK, t *testing.T) {
ctxProvider := sdk.Context(WithUser(identityValidOptUser), WithOrg(identityValidOptOrg))
_, err := ctxProvider()
if err != nil {
t.Fatalf("getting context supposed to succeed")
}

ctxProvider = sdk.Context(WithUser(identityValidOptUser))

ctx, err = ctxProvider()

ctx, err := ctxProvider()
if err != nil {
t.Fatalf("getting context supposed to succeed")
}

if ctx == nil || ctx.Identifier().MSPID == "" {
t.Fatalf("supposed to get valid context")
}

}
10 changes: 5 additions & 5 deletions pkg/fabsdk/fabsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func New(configProvider core.ConfigProvider, opts ...Option) (*FabricSDK, error)
// fromPkgSuite creates an SDK based on the implementations in the provided pkg suite.
// TODO: For now leaving this method as private until we have more usage.
func fromPkgSuite(configProvider core.ConfigProvider, pkgSuite pkgSuite, opts ...Option) (*FabricSDK, error) {
core, err := pkgSuite.Core()
coreProv, err := pkgSuite.Core()
if err != nil {
return nil, errors.WithMessage(err, "Unable to initialize core pkg")
}

msp, err := pkgSuite.MSP()
mspProv, err := pkgSuite.MSP()
if err != nil {
return nil, errors.WithMessage(err, "Unable to initialize core pkg")
}
Expand All @@ -85,8 +85,8 @@ func fromPkgSuite(configProvider core.ConfigProvider, pkgSuite pkgSuite, opts ..

sdk := FabricSDK{
opts: options{
Core: core,
MSP: msp,
Core: coreProv,
MSP: mspProv,
Service: svc,
Logger: lg,
},
Expand Down Expand Up @@ -162,7 +162,7 @@ type providerInit interface {
Initialize(providers contextApi.Providers) error
}

func initSDK(sdk *FabricSDK, configProvider core.ConfigProvider, opts []Option) error {
func initSDK(sdk *FabricSDK, configProvider core.ConfigProvider, opts []Option) error { //nolint
for _, option := range opts {
err := option(&sdk.opts)
if err != nil {
Expand Down
44 changes: 16 additions & 28 deletions pkg/fabsdk/fabsdk_chconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,39 +98,13 @@ func TestNewDefaultTwoValidSDK(t *testing.T) {
if err != nil {
t.Fatalf("Error getting config backend from sdk: %s", err)
}

identityConfig, err := msp.ConfigFromBackend(configBackend)
if err != nil {
t.Fatalf("Error getting identity config: %s", err)
}

client1, err := identityConfig.Client()
if err != nil {
t.Fatalf("Error getting client from config: %s", err)
}

if client1.Organization != sdkValidClientOrg1 {
t.Fatalf("Unexpected org in config: %s", client1.Organization)
}
checkClientOrg(configBackend, t, sdkValidClientOrg1)

configBackend, err = sdk2.Config()
if err != nil {
t.Fatalf("Error getting config backend from sdk: %s", err)
}

identityConfig, err = msp.ConfigFromBackend(configBackend)
if err != nil {
t.Fatalf("Error getting identity config : %s", err)
}

client2, err := identityConfig.Client()
if err != nil {
t.Fatalf("Error getting client from config: %s", err)
}

if client2.Organization != sdkValidClientOrg2 {
t.Fatalf("Unexpected org in config: %s", client2.Organization)
}
checkClientOrg(configBackend, t, sdkValidClientOrg2)

// Get a common client context for the following tests
//cc1 := sdk1.NewClient(WithUser(sdkValidClientUser))
Expand Down Expand Up @@ -159,6 +133,20 @@ func TestNewDefaultTwoValidSDK(t *testing.T) {
}
}

func checkClientOrg(configBackend core.ConfigBackend, t *testing.T, orgName string) {
identityConfig, err := msp.ConfigFromBackend(configBackend)
if err != nil {
t.Fatalf("Error getting identity config : %s", err)
}
client, err := identityConfig.Client()
if err != nil {
t.Fatalf("Error getting client from config: %s", err)
}
if client.Organization != orgName {
t.Fatalf("Unexpected org in config: %s", client.Organization)
}
}

func getCustomBackend() (*mockCore.MockConfigBackend, error) {
backend, err := config.FromFile(sdkConfigFile)()
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions pkg/fabsdk/provider/fabpvdr/cachekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ func (p *params) PermitBlockEvents() {
p.permitBlockEvents = true
}

type permitBlockEventsSetter interface {
PermitBlockEvents()
}

func (p *params) getOptKey() string {
// Construct opts portion
optKey := "blockEvents:" + strconv.FormatBool(p.permitBlockEvents)
Expand Down
2 changes: 1 addition & 1 deletion test/scripts/check_license.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
function filterExcludedFiles {
CHECK=`echo "$CHECK" | grep -v .png$ | grep -v .rst$ | grep -v ^.git/ \
| grep -v .pem$ | grep -v .block$ | grep -v .tx$ | grep -v ^LICENSE$ | grep -v _sk$ \
| grep -v .key$ | grep -v .crt$ | grep -v \\.gen.go$ | grep -v Gopkg.lock$ \
| grep -v .key$ | grep -v .crt$ | grep -v \\.gen.go$ | grep -v \\.json$ | grep -v Gopkg.lock$ \
| grep -v .md$ | grep -v ^vendor/ | grep -v ^build/ | grep -v .pb.go$ | grep -v ci.properties$ | sort -u`
}

Expand Down
3 changes: 2 additions & 1 deletion test/scripts/check_lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ declare -a arr1=(
"./pkg/context"
"./pkg/core"
"./pkg/fab"
"./pkg/fabsdk"
)


echo "Running metalinters..."
for i in "${arr1[@]}"
do
echo "Checking $i"
$GOMETALINT_CMD --deadline=300s $i/...
$GOMETALINT_CMD --config=./gometalinter.json $i/...
done

0 comments on commit c8911f3

Please sign in to comment.