-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge "[FAB-6069] Add capabilities package to common"
- Loading branch information
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package capabilities | ||
|
||
import ( | ||
"github.com/hyperledger/fabric/common/flogging" | ||
cb "github.com/hyperledger/fabric/protos/common" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
const pkgLogID = "common/capabilities" | ||
|
||
var logger = flogging.MustGetLogger(pkgLogID) | ||
|
||
// provider is the 'plugin' parameter for registry. | ||
type provider interface { | ||
// HasCapability should report whether the binary supports this capability. | ||
HasCapability(capability string) bool | ||
|
||
// Type is used to make error messages more legible. | ||
Type() string | ||
} | ||
|
||
// registry is a common structure intended to be used to support specific aspects of capabilities | ||
// such as orderer, application, and channel. | ||
type registry struct { | ||
provider provider | ||
capabilities map[string]*cb.Capability | ||
} | ||
|
||
func newRegistry(p provider, capabilities map[string]*cb.Capability) *registry { | ||
return ®istry{ | ||
provider: p, | ||
capabilities: capabilities, | ||
} | ||
} | ||
|
||
// Supported checks that all of the required capabilities are supported by this binary. | ||
func (r *registry) Supported() error { | ||
for capabilityName, capability := range r.capabilities { | ||
if r.provider.HasCapability(capabilityName) { | ||
continue | ||
} | ||
|
||
if capability.Required { | ||
return errors.Errorf("%s capability %s is required but not supported", r.provider.Type(), capabilityName) | ||
} | ||
logger.Debugf("Found unknown %s capability %s but it is not required", r.provider.Type(), capabilityName) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package capabilities | ||
|
||
import ( | ||
"testing" | ||
|
||
cb "github.com/hyperledger/fabric/protos/common" | ||
|
||
"github.com/op/go-logging" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func init() { | ||
logging.SetLevel(logging.DEBUG, pkgLogID) | ||
} | ||
|
||
func TestSatisfied(t *testing.T) { | ||
t.Run("NilCapabilities", func(t *testing.T) { | ||
var capsMap map[string]*cb.Capability | ||
for _, provider := range []*registry{ | ||
NewChannelProvider(capsMap).registry, | ||
} { | ||
assert.Nil(t, provider.Supported()) | ||
} | ||
}) | ||
|
||
t.Run("NotRequiredCapabilities", func(t *testing.T) { | ||
capsMap := map[string]*cb.Capability{ | ||
"FakeCapability1": &cb.Capability{ | ||
Required: false, | ||
}, | ||
"FakeCapability2": &cb.Capability{ | ||
Required: false, | ||
}, | ||
} | ||
for _, provider := range []*registry{ | ||
NewChannelProvider(capsMap).registry, | ||
} { | ||
assert.Nil(t, provider.Supported()) | ||
} | ||
}) | ||
} | ||
|
||
func TestNotSatisfied(t *testing.T) { | ||
capsMap := map[string]*cb.Capability{ | ||
"FakeCapability": &cb.Capability{ | ||
Required: true, | ||
}, | ||
} | ||
for _, provider := range []*registry{ | ||
NewChannelProvider(capsMap).registry, | ||
} { | ||
assert.Error(t, provider.Supported()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package capabilities | ||
|
||
import ( | ||
cb "github.com/hyperledger/fabric/protos/common" | ||
) | ||
|
||
const ( | ||
channelTypeName = "Channel" | ||
) | ||
|
||
// ChannelProvider provides capabilities information for channel level config. | ||
type ChannelProvider struct { | ||
*registry | ||
} | ||
|
||
// NewChannelProvider creates a channel capabilities provider. | ||
func NewChannelProvider(capabilities map[string]*cb.Capability) *ChannelProvider { | ||
cp := &ChannelProvider{} | ||
cp.registry = newRegistry(cp, capabilities) | ||
return cp | ||
} | ||
|
||
// Type returns a descriptive string for logging purposes. | ||
func (cp *ChannelProvider) Type() string { | ||
return channelTypeName | ||
} | ||
|
||
// HasCapability returns true if the capability is supported by this binary. | ||
func (cp *ChannelProvider) HasCapability(capability string) bool { | ||
switch capability { | ||
// Add new capability names here | ||
default: | ||
return false | ||
} | ||
} |