Skip to content

Commit

Permalink
FAB-6099 Add support for experimental build tag
Browse files Browse the repository at this point in the history
In order to support conditional compilation,
this CR adds support for an "experimental"
tag in the build system. The default is
to build with experimental features.

In order to use this, one simply adds
"// +build experimental" plus a blank line
at the top of Go source files.  This files
will only be compiled if the experimental tag
is set.

Added examples/experimental to demonstrate the
use of Go build tags

Change-Id: If9bbfc718ee1e470433c42f0447e5adb6d1c59b3
Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
  • Loading branch information
mastersingh24 committed Sep 11, 2017
1 parent 833b24a commit 1582472
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 15 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ PROJECT_NAME = hyperledger/fabric
BASE_VERSION = 1.1.0
PREV_VERSION = 1.0.0
IS_RELEASE = false
EXPERIMENTAL ?= true

ifeq ($(EXPERIMENTAL),true)
GO_TAGS += experimental
endif

ifneq ($(IS_RELEASE),true)
EXTRA_VERSION ?= snapshot-$(shell git rev-parse --short HEAD)
Expand All @@ -60,14 +65,15 @@ METADATA_VAR += BaseVersion=$(BASEIMAGE_RELEASE)
METADATA_VAR += BaseDockerLabel=$(BASE_DOCKER_LABEL)
METADATA_VAR += DockerNamespace=$(DOCKER_NS)
METADATA_VAR += BaseDockerNamespace=$(BASE_DOCKER_NS)
METADATA_VAR += Experimental=$(EXPERIMENTAL)

GO_LDFLAGS = $(patsubst %,-X $(PKGNAME)/common/metadata.%,$(METADATA_VAR))

GO_TAGS ?=

CHAINTOOL_URL ?= https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/chaintool-$(CHAINTOOL_RELEASE)/hyperledger-fabric-chaintool-$(CHAINTOOL_RELEASE).jar

export GO_LDFLAGS
export GO_LDFLAGS GO_TAGS

EXECUTABLES = go docker git curl
K := $(foreach exec,$(EXECUTABLES),\
Expand Down Expand Up @@ -167,7 +173,7 @@ verify: unit-test-clean peer-docker testenv couchdb

# Generates a string to the terminal suitable for manual augmentation / re-issue, useful for running tests by hand
test-cmd:
@echo "go test -ldflags \"$(GO_LDFLAGS)\""
@echo "go test -tags \"$(GO_TAGS)\" -ldflags \"$(GO_LDFLAGS)\""

docker: $(patsubst %,build/image/%/$(DUMMY), $(IMAGES))
native: peer orderer configtxgen cryptogen configtxlator
Expand Down Expand Up @@ -200,7 +206,7 @@ build/docker/bin/%: $(PROJECT_FILES)
-v $(abspath build/docker/bin):/opt/gopath/bin \
-v $(abspath build/docker/$(TARGET)/pkg):/opt/gopath/pkg \
$(BASE_DOCKER_NS)/fabric-baseimage:$(BASE_DOCKER_TAG) \
go install -ldflags "$(DOCKER_GO_LDFLAGS)" $(pkgmap.$(@F))
go install -tags "$(GO_TAGS)" -ldflags "$(DOCKER_GO_LDFLAGS)" $(pkgmap.$(@F))
@touch $@

build/bin:
Expand Down
1 change: 1 addition & 0 deletions common/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ var BaseVersion string
var BaseDockerLabel string
var DockerNamespace string
var BaseDockerNamespace string
var Experimental string
17 changes: 17 additions & 0 deletions examples/experimental/experimental.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build experimental

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

type MyInterface interface {
BaseInterface
DoSomethingExperimental() string
}

func (m *MyType) DoSomethingExperimental() string {
return "did something experimental"
}
24 changes: 24 additions & 0 deletions examples/experimental/experimental_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// +build experimental

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

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestExperimental(t *testing.T) {
m := MyType{}

something := "did something"
somethingExperimental := "did something experimental"

assert.Equal(t, something, m.DoSomething())
assert.Equal(t, somethingExperimental, m.DoSomethingExperimental())
}
10 changes: 10 additions & 0 deletions examples/experimental/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package experimental

type BaseInterface interface {
DoSomething() string
}
12 changes: 12 additions & 0 deletions examples/experimental/interface_stable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// +build !experimental

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

type MyInterface interface {
BaseInterface
}
12 changes: 12 additions & 0 deletions examples/experimental/stable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package experimental

type MyType struct{}

func (m *MyType) DoSomething() string {
return "did something"
}
34 changes: 34 additions & 0 deletions examples/experimental/stable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// +build !experimental

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

import (
"testing"

"github.com/stretchr/testify/assert"
)

type testIntf interface {
DoSomethingExperimental() string
}

func TestStable(t *testing.T) {
var m MyInterface

m = &MyType{}

something := "did something"

assert.Equal(t, something, m.DoSomething())

// make sure that MyType does not implement testIntf
_, ok := m.(testIntf)
if ok {
t.Error("MyType (stable) should not have DoSomethingExperimental method")
}
}
6 changes: 3 additions & 3 deletions orderer/common/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func GetVersionInfo() string {
Version = "development build"
}

return fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s",
ProgramName, Version, runtime.Version(),
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
return fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s\n"+
" Experimental features: %s\n", ProgramName, Version, runtime.Version(),
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), common.Experimental)
}
7 changes: 4 additions & 3 deletions orderer/common/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ func TestGetVersionInfo(t *testing.T) {
common.Version = "testVersion"
}

expected := fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s",
metadata.ProgramName, common.Version, runtime.Version(),
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
expected := fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s\n"+
" Experimental features: %s\n", metadata.ProgramName, common.Version,
runtime.Version(), fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
common.Experimental)
assert.Equal(t, expected, metadata.GetVersionInfo())
}
5 changes: 3 additions & 2 deletions peer/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func GetInfo() string {
metadata.BaseDockerLabel, metadata.DockerNamespace)

return fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s\n"+
" Chaincode:\n %s\n",
" Experimental features: %s\n Chaincode:\n %s\n",
ProgramName, metadata.Version, runtime.Version(),
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), ccinfo)
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
metadata.Experimental, ccinfo)
}
1 change: 1 addition & 0 deletions unit-test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ unit-tests:
environment:
- UNIT_TEST_PEER_IP=vp
- GO_LDFLAGS
- GO_TAGS
- OUTPUT
- TEST_PKGS=${TEST_PKGS}
- CORE_VM_DOCKER_ATTACHSTDOUT=true
Expand Down
9 changes: 5 additions & 4 deletions unit-test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ if [ "$JOB_TYPE" = "VERIFY" ]; then
if [[ ! -z "$TEST_PKGS" ]]; then
echo "Testing packages:"
echo $TEST_PKGS
echo " with tags " $GO_TAGS
# use go test -cover as this is much more efficient than gocov
time go test -cover -ldflags "$GO_LDFLAGS" $TEST_PKGS -p 1 -timeout=20m
time go test -cover -tags "$GO_TAGS" -ldflags "$GO_LDFLAGS" $TEST_PKGS -p 1 -timeout=20m
else
echo "Nothing changed in unit test!!!"
fi
Expand Down Expand Up @@ -72,9 +73,9 @@ PKGS=`echo $PKGS | sed 's@'github.com/hyperledger/fabric/core/chaincode/platfor
PKGS=`echo $PKGS | sed 's@'github.com/hyperledger/fabric/core/chaincode/platforms/java'@@g'`
fi

echo " DONE!"
echo -e "\nDONE!"
echo -e "Running tests with tags ${GO_TAGS} ..."

echo "Running tests..."
#go test -cover -ldflags "$GO_LDFLAGS" $PKGS -p 1 -timeout=20m
gocov test -ldflags "$GO_LDFLAGS" $PKGS -p 1 -timeout=20m | gocov-xml > report.xml
gocov test -tags "$GO_TAGS" -ldflags "$GO_LDFLAGS" $PKGS -p 1 -timeout=20m | gocov-xml > report.xml
fi

0 comments on commit 1582472

Please sign in to comment.