Skip to content

Commit 1582472

Browse files
committed
FAB-6099 Add support for experimental build tag
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>
1 parent 833b24a commit 1582472

File tree

13 files changed

+135
-15
lines changed

13 files changed

+135
-15
lines changed

Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ PROJECT_NAME = hyperledger/fabric
3939
BASE_VERSION = 1.1.0
4040
PREV_VERSION = 1.0.0
4141
IS_RELEASE = false
42+
EXPERIMENTAL ?= true
43+
44+
ifeq ($(EXPERIMENTAL),true)
45+
GO_TAGS += experimental
46+
endif
4247

4348
ifneq ($(IS_RELEASE),true)
4449
EXTRA_VERSION ?= snapshot-$(shell git rev-parse --short HEAD)
@@ -60,14 +65,15 @@ METADATA_VAR += BaseVersion=$(BASEIMAGE_RELEASE)
6065
METADATA_VAR += BaseDockerLabel=$(BASE_DOCKER_LABEL)
6166
METADATA_VAR += DockerNamespace=$(DOCKER_NS)
6267
METADATA_VAR += BaseDockerNamespace=$(BASE_DOCKER_NS)
68+
METADATA_VAR += Experimental=$(EXPERIMENTAL)
6369

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

6672
GO_TAGS ?=
6773

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

70-
export GO_LDFLAGS
76+
export GO_LDFLAGS GO_TAGS
7177

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

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

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

206212
build/bin:

common/metadata/metadata.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ var BaseVersion string
2222
var BaseDockerLabel string
2323
var DockerNamespace string
2424
var BaseDockerNamespace string
25+
var Experimental string

examples/experimental/experimental.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// +build experimental
2+
3+
/*
4+
Copyright IBM Corp. All Rights Reserved.
5+
6+
SPDX-License-Identifier: Apache-2.0
7+
*/
8+
package experimental
9+
10+
type MyInterface interface {
11+
BaseInterface
12+
DoSomethingExperimental() string
13+
}
14+
15+
func (m *MyType) DoSomethingExperimental() string {
16+
return "did something experimental"
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// +build experimental
2+
3+
/*
4+
Copyright IBM Corp. All Rights Reserved.
5+
6+
SPDX-License-Identifier: Apache-2.0
7+
*/
8+
package experimental
9+
10+
import (
11+
"testing"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestExperimental(t *testing.T) {
17+
m := MyType{}
18+
19+
something := "did something"
20+
somethingExperimental := "did something experimental"
21+
22+
assert.Equal(t, something, m.DoSomething())
23+
assert.Equal(t, somethingExperimental, m.DoSomethingExperimental())
24+
}

examples/experimental/interface.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
package experimental
7+
8+
type BaseInterface interface {
9+
DoSomething() string
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// +build !experimental
2+
3+
/*
4+
Copyright IBM Corp. All Rights Reserved.
5+
6+
SPDX-License-Identifier: Apache-2.0
7+
*/
8+
package experimental
9+
10+
type MyInterface interface {
11+
BaseInterface
12+
}

examples/experimental/stable.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
package experimental
7+
8+
type MyType struct{}
9+
10+
func (m *MyType) DoSomething() string {
11+
return "did something"
12+
}

examples/experimental/stable_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// +build !experimental
2+
3+
/*
4+
Copyright IBM Corp. All Rights Reserved.
5+
6+
SPDX-License-Identifier: Apache-2.0
7+
*/
8+
package experimental
9+
10+
import (
11+
"testing"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
type testIntf interface {
17+
DoSomethingExperimental() string
18+
}
19+
20+
func TestStable(t *testing.T) {
21+
var m MyInterface
22+
23+
m = &MyType{}
24+
25+
something := "did something"
26+
27+
assert.Equal(t, something, m.DoSomething())
28+
29+
// make sure that MyType does not implement testIntf
30+
_, ok := m.(testIntf)
31+
if ok {
32+
t.Error("MyType (stable) should not have DoSomethingExperimental method")
33+
}
34+
}

orderer/common/metadata/metadata.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func GetVersionInfo() string {
2929
Version = "development build"
3030
}
3131

32-
return fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s",
33-
ProgramName, Version, runtime.Version(),
34-
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
32+
return fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s\n"+
33+
" Experimental features: %s\n", ProgramName, Version, runtime.Version(),
34+
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), common.Experimental)
3535
}

orderer/common/metadata/metadata_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ func TestGetVersionInfo(t *testing.T) {
2424
common.Version = "testVersion"
2525
}
2626

27-
expected := fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s",
28-
metadata.ProgramName, common.Version, runtime.Version(),
29-
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
27+
expected := fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s\n"+
28+
" Experimental features: %s\n", metadata.ProgramName, common.Version,
29+
runtime.Version(), fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
30+
common.Experimental)
3031
assert.Equal(t, expected, metadata.GetVersionInfo())
3132
}

peer/version/version.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ func GetInfo() string {
4545
metadata.BaseDockerLabel, metadata.DockerNamespace)
4646

4747
return fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s\n"+
48-
" Chaincode:\n %s\n",
48+
" Experimental features: %s\n Chaincode:\n %s\n",
4949
ProgramName, metadata.Version, runtime.Version(),
50-
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), ccinfo)
50+
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
51+
metadata.Experimental, ccinfo)
5152
}

unit-test/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ unit-tests:
2222
environment:
2323
- UNIT_TEST_PEER_IP=vp
2424
- GO_LDFLAGS
25+
- GO_TAGS
2526
- OUTPUT
2627
- TEST_PKGS=${TEST_PKGS}
2728
- CORE_VM_DOCKER_ATTACHSTDOUT=true

unit-test/run.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ if [ "$JOB_TYPE" = "VERIFY" ]; then
3434
if [[ ! -z "$TEST_PKGS" ]]; then
3535
echo "Testing packages:"
3636
echo $TEST_PKGS
37+
echo " with tags " $GO_TAGS
3738
# use go test -cover as this is much more efficient than gocov
38-
time go test -cover -ldflags "$GO_LDFLAGS" $TEST_PKGS -p 1 -timeout=20m
39+
time go test -cover -tags "$GO_TAGS" -ldflags "$GO_LDFLAGS" $TEST_PKGS -p 1 -timeout=20m
3940
else
4041
echo "Nothing changed in unit test!!!"
4142
fi
@@ -72,9 +73,9 @@ PKGS=`echo $PKGS | sed 's@'github.com/hyperledger/fabric/core/chaincode/platfor
7273
PKGS=`echo $PKGS | sed 's@'github.com/hyperledger/fabric/core/chaincode/platforms/java'@@g'`
7374
fi
7475

75-
echo " DONE!"
76+
echo -e "\nDONE!"
77+
echo -e "Running tests with tags ${GO_TAGS} ..."
7678

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

0 commit comments

Comments
 (0)