Skip to content

Commit

Permalink
[FAB-12099] build plugins with race when needed
Browse files Browse the repository at this point in the history
This allows us to run tests with and without the race detector.

Change-Id: If20a850c6e9d5484bf9f4b39415e0cabe8410d1c
Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
  • Loading branch information
sykesm committed Sep 24, 2018
1 parent 2c2f65d commit c592f15
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
12 changes: 10 additions & 2 deletions bccsp/factory/pluginfactory_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build linux,go1.9,cgo
// +build go1.9,linux,cgo go1.10,darwin,cgo
// +build !ppc64le

/*
Expand All @@ -16,12 +16,20 @@ import (
"github.com/stretchr/testify/assert"
)

// raceEnabled is set to true when the race build tag is enabled.
// see race_test.go
var raceEnabled bool

func buildPlugin(lib string, t *testing.T) {
t.Helper()
// check to see if the example plugin exists
if _, err := os.Stat(lib); err != nil {
// build the example plugin
cmd := exec.Command("go", "build", "-buildmode=plugin", "github.com/hyperledger/fabric/examples/plugins/bccsp")
cmd := exec.Command("go", "build", "-buildmode=plugin")
if raceEnabled {
cmd.Args = append(cmd.Args, "-race")
}
cmd.Args = append(cmd.Args, "github.com/hyperledger/fabric/examples/plugins/bccsp")
err := cmd.Run()
if err != nil {
t.Fatalf("Could not build plugin: [%s]", err)
Expand Down
14 changes: 14 additions & 0 deletions bccsp/factory/race_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// +build race
// +build go1.9,linux,cgo go1.10,darwin,cgo
// +build !ppc64le

/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package factory

func init() {
raceEnabled = true
}
14 changes: 14 additions & 0 deletions core/handlers/library/race_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// +build race
// +build go1.9,linux,cgo go1.10,darwin,cgo
// +build !ppc64le

/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package library

func init() {
raceEnabled = true
}
50 changes: 25 additions & 25 deletions core/handlers/library/registry_plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build go1.9,linux,cgo
// +build go1.9,linux,cgo go1.10,darwin,cgo
// +build !ppc64le

/*
Expand All @@ -14,7 +14,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"strings"
"path/filepath"
"testing"

"github.com/golang/protobuf/proto"
Expand All @@ -31,18 +31,29 @@ const (
validationTestPlugin = "github.com/hyperledger/fabric/core/handlers/validation/testdata/"
)

// raceEnabled is set to true when the race build tag is enabled.
// see race_test.go
var raceEnabled bool

func buildPlugin(t *testing.T, dest, pkg string) {
cmd := exec.Command("go", "build", "-o", dest, "-buildmode=plugin")
if raceEnabled {
cmd.Args = append(cmd.Args, "-race")
}
cmd.Args = append(cmd.Args, pkg)
output, err := cmd.CombinedOutput()
assert.NoError(t, err, "Could not build plugin: "+string(output))
}

func TestLoadAuthPlugin(t *testing.T) {
endorser := &mockEndorserServer{}

testDir, err := ioutil.TempDir("", "")
assert.NoError(t, err, "Could not create temp directory for plugins")
defer os.Remove(testDir)
pluginPath := strings.Join([]string{testDir, "/", "authplugin.so"}, "")

cmd := exec.Command("go", "build", "-o", pluginPath, "-buildmode=plugin",
authPluginPackage)
output, err := cmd.CombinedOutput()
assert.NoError(t, err, "Could not build plugin: "+string(output))
pluginPath := filepath.Join(testDir, "authplugin.so")
buildPlugin(t, pluginPath, authPluginPackage)

testReg := registry{}
testReg.loadPlugin(pluginPath, Auth)
Expand All @@ -60,12 +71,9 @@ func TestLoadDecoratorPlugin(t *testing.T) {
testDir, err := ioutil.TempDir("", "")
assert.NoError(t, err, "Could not create temp directory for plugins")
defer os.Remove(testDir)
pluginPath := strings.Join([]string{testDir, "/", "decoratorplugin.so"}, "")

cmd := exec.Command("go", "build", "-o", pluginPath, "-buildmode=plugin",
decoratorPluginPackage)
output, err := cmd.CombinedOutput()
assert.NoError(t, err, "Could not build plugin: "+string(output))
pluginPath := filepath.Join(testDir, "decoratorplugin.so")
buildPlugin(t, pluginPath, decoratorPluginPackage)

testReg := registry{}
testReg.loadPlugin(pluginPath, Decoration)
Expand All @@ -80,12 +88,8 @@ func TestEndorsementPlugin(t *testing.T) {
assert.NoError(t, err, "Could not create temp directory for plugins")
defer os.Remove(testDir)

pluginPath := strings.Join([]string{testDir, "/", "endorsementplugin.so"}, "")

cmd := exec.Command("go", "build", "-o", pluginPath, "-buildmode=plugin",
endorsementTestPlugin)
output, err := cmd.CombinedOutput()
assert.NoError(t, err, "Could not build plugin: "+string(output))
pluginPath := filepath.Join(testDir, "endorsementplugin.so")
buildPlugin(t, pluginPath, endorsementTestPlugin)

testReg := registry{endorsers: make(map[string]endorsement.PluginFactory)}
testReg.loadPlugin(pluginPath, Endorsement, "escc")
Expand All @@ -95,7 +99,7 @@ func TestEndorsementPlugin(t *testing.T) {
instance := factory.New()
assert.NotNil(t, instance)
assert.NoError(t, instance.Init())
_, output, err = instance.Endorse([]byte{1, 2, 3}, nil)
_, output, err := instance.Endorse([]byte{1, 2, 3}, nil)
assert.NoError(t, err)
assert.Equal(t, []byte{1, 2, 3}, output)
}
Expand All @@ -105,12 +109,8 @@ func TestValidationPlugin(t *testing.T) {
assert.NoError(t, err, "Could not create temp directory for plugins")
defer os.Remove(testDir)

pluginPath := strings.Join([]string{testDir, "/", "validationplugin.so"}, "")

cmd := exec.Command("go", "build", "-o", pluginPath, "-buildmode=plugin",
validationTestPlugin)
output, err := cmd.CombinedOutput()
assert.NoError(t, err, "Could not build plugin: "+string(output))
pluginPath := filepath.Join(testDir, "validationplugin.so")
buildPlugin(t, pluginPath, validationTestPlugin)

testReg := registry{validators: make(map[string]validation.PluginFactory)}
testReg.loadPlugin(pluginPath, Validation, "vscc")
Expand Down

0 comments on commit c592f15

Please sign in to comment.