Skip to content

Commit

Permalink
[FAB-5919] Refactor CI to run unit tests without docker
Browse files Browse the repository at this point in the history
Change-Id: I0b18532a75a49184dd4840e3bc5c99999470275c
Signed-off-by: Aleksandar Likic <aleksandar.likic@securekey.com>
  • Loading branch information
Aleksandar Likic committed Aug 25, 2017
1 parent 6648c81 commit 7570207
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 116 deletions.
21 changes: 11 additions & 10 deletions Makefile
Expand Up @@ -70,18 +70,20 @@ restore-docker-file:
&& sed -i.bak -e 's/$(DOCKER_TAG)/_TAG_/g' Dockerfile\
&& rm -rf Dockerfile.bak

unit-test: clean depend populate edit-docker build-softhsm2-image restore-docker-file
@cd ./test/fixtures && docker-compose -f docker-compose-unit.yaml up --abort-on-container-exit
@test/scripts/check_status.sh "./test/fixtures/docker-compose-unit.yaml"

unit-test: checks depend populate
@test/scripts/unit.sh

unit-tests: unit-test

integration-test: clean depend populate edit-docker build-softhsm2-image restore-docker-file
@cd ./test/fixtures && docker-compose up --force-recreate --abort-on-container-exit
@test/scripts/check_status.sh "./test/fixtures/docker-compose.yaml"
integration-tests: clean depend populate edit-docker build-softhsm2-image restore-docker-file
@cd ./test/fixtures && docker-compose -f docker-compose.yaml -f docker-compose-integration-test.yaml up --force-recreate --abort-on-container-exit
@test/scripts/check_status.sh "-f ./test/fixtures/docker-compose.yaml -f ./test/fixtures/docker-compose-integration-test.yaml"

integration-tests-pkcs11: clean depend populate edit-docker build-softhsm2-image restore-docker-file
@cd ./test/fixtures && docker-compose -f docker-compose.yaml -f docker-compose-pkcs11-test.yaml up --force-recreate --abort-on-container-exit
@test/scripts/check_status.sh "-f ./test/fixtures/docker-compose.yaml -f ./test/fixtures/docker-compose-pkcs11-test.yaml"

integration-tests: integration-test
integration-test: integration-tests integration-tests-pkcs11

mock-gen:
mockgen -build_flags '$(LDFLAGS)' github.com/hyperledger/fabric-sdk-go/api/apitxn ProposalProcessor | sed "s/github.com\/hyperledger\/fabric-sdk-go\/vendor\///g" > api/apitxn/mocks/mockapitxn.gen.go
Expand All @@ -100,5 +102,4 @@ populate-clean:
clean:
rm -Rf /tmp/enroll_user /tmp/msp /tmp/keyvaluestore
rm -f integration-report.xml report.xml
cd test/fixtures && docker-compose down
cd test/fixtures && docker-compose -f docker-compose-unit.yaml down
cd test/fixtures && docker-compose -f docker-compose.yaml -f docker-compose-integration-test.yaml -f docker-compose-pkcs11-test.yaml down
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -38,8 +38,8 @@ repository.
### Compatibility

This client SDK was last tested and found to be compatible with the following Hyperledger Fabric commit levels:
- fabric: v1.0.1
- fabric-ca: v1.0.1
- fabric v1.0.1 & fabric-ca v1.0.1
- fabric v1.0.0 & fabric-ca v1.0.0

### Running the test suite

Expand Down Expand Up @@ -139,14 +139,14 @@ Alternatively you can build and run Fabric on your own box using the following c
cd $GOPATH/src/github.com/hyperledger/
git clone https://github.com/hyperledger/fabric
cd $GOPATH/src/github.com/hyperledger/fabric/
git checkout v1.0.0
git checkout v1.0.1
make docker
# Build fabric-ca:
cd $GOPATH/src/github.com/hyperledger/
git clone https://github.com/hyperledger/fabric-ca
cd $GOPATH/src/github.com/hyperledger/fabric-ca/
git checkout v1.0.0
git checkout v1.0.1
make docker
# Start fabric - latest-env.sh overrides the default docker tags in .env
Expand Down
6 changes: 4 additions & 2 deletions pkg/config/config.go
Expand Up @@ -446,7 +446,7 @@ func (c *Config) CSPConfig() *bccspFactory.FactoryOpts {
Ephemeral: c.Ephemeral(),
},
}
log.Debug("Initialized PKCS11 ")
log.Debug("Initialized SW ")
bccspFactory.InitFactories(opts)
return opts

Expand All @@ -471,6 +471,8 @@ func (c *Config) CSPConfig() *bccspFactory.FactoryOpts {
bccspFactory.InitFactories(opts)
return opts

default:
panic(fmt.Sprintf("Unsupported BCCSP Provider: %s", c.SecurityProvider()))

}
return nil
}
86 changes: 5 additions & 81 deletions pkg/config/config_test.go
Expand Up @@ -15,8 +15,6 @@ import (
"time"

api "github.com/hyperledger/fabric-sdk-go/api/apiconfig"
pkcsFactory "github.com/hyperledger/fabric/bccsp/factory"
pkcs11 "github.com/hyperledger/fabric/bccsp/pkcs11"
logging "github.com/op/go-logging"
"github.com/spf13/viper"
)
Expand All @@ -28,8 +26,7 @@ var bccspProviderType string
var securityLevel = 256

const (
providerTypeSW = "SW"
providerTypePKCS11 = "PKCS11"
providerTypeSW = "SW"
)

var validRootCA = `-----BEGIN CERTIFICATE-----
Expand Down Expand Up @@ -277,19 +274,19 @@ func TestCSPConfig(t *testing.T) {

if cspconfig != nil && cspconfig.ProviderName == "SW" {
if cspconfig.SwOpts.HashFamily != configImpl.SecurityAlgorithm() {
t.Fatalf("In correct hashfamily found for cspconfig")
t.Fatalf("Incorrect hashfamily found for cspconfig")
}

if cspconfig.SwOpts.SecLevel != configImpl.SecurityLevel() {
t.Fatalf("In correct security level found for cspconfig")
t.Fatalf("Incorrect security level found for cspconfig")
}

if cspconfig.SwOpts.Ephemeral {
t.Fatalf("In correct Ephemeral found for cspconfig")
t.Fatalf("Incorrect Ephemeral found for cspconfig")
}

if cspconfig.SwOpts.FileKeystore.KeyStorePath != configImpl.KeyStorePath() {
t.Fatalf("In correct keystore path found for cspconfig")
t.Fatalf("Incorrect keystore path found for cspconfig")
}
}
}
Expand Down Expand Up @@ -471,79 +468,6 @@ func TestNetworkConfig(t *testing.T) {
}
}

func TestPKCS11CSPConfigWithValidOptions(t *testing.T) {
opts := configurePKCS11Options("SHA2", securityLevel)
f := &pkcsFactory.PKCS11Factory{}
//
csp, err := f.Get(opts)
if err != nil {
t.Fatalf(err.Error())
}
if csp == nil {
t.Fatalf("BCCSP PKCS11 was not configured")
}
fmt.Println("TestPKCS11CSPConfigWithValidOptions passed. BCCSP PKCS11 provider was configured\n", csp)

}

func TestPKCS11CSPConfigWithEmptyHashFamily(t *testing.T) {

opts := configurePKCS11Options("", securityLevel)

f := &pkcsFactory.PKCS11Factory{}
fmt.Println(f.Name())
_, err := f.Get(opts)
if err == nil {
t.Fatalf("Expected error 'Hash Family not supported'")
}
fmt.Println("TestPKCS11CSPConfigWithEmptyHashFamily passed. ")

}

func TestPKCS11CSPConfigWithIncorrectLevel(t *testing.T) {

opts := configurePKCS11Options("SHA2", 100)

f := &pkcsFactory.PKCS11Factory{}
fmt.Println(f.Name())
_, err := f.Get(opts)
if err == nil {
t.Fatalf("Expected error 'Failed initializing configuration'")
}

}

func TestPKCS11CSPConfigWithEmptyProviderName(t *testing.T) {
f := &pkcsFactory.PKCS11Factory{}
if f.Name() != providerTypePKCS11 {
t.Fatalf("Expected default name for PKCS11. Got %s", f.Name())
}
}

func configurePKCS11Options(hashFamily string, securityLevel int) *pkcsFactory.FactoryOpts {
providerLib, softHSMPin, softHSMTokenLabel := pkcs11.FindPKCS11Lib()

pkks := pkcs11.FileKeystoreOpts{KeyStorePath: os.TempDir()}
//PKCS11 options
pkcsOpt := pkcs11.PKCS11Opts{
SecLevel: securityLevel,
HashFamily: hashFamily,
FileKeystore: &pkks,
Library: providerLib,
Pin: softHSMPin,
Label: softHSMTokenLabel,
Ephemeral: false,
}

opts := &pkcsFactory.FactoryOpts{
ProviderName: providerTypePKCS11,
Pkcs11Opts: &pkcsOpt,
}
pkcsFactory.InitFactories(opts)
return opts

}

func TestMain(m *testing.M) {
var err error
configImpl, err = InitConfig("../../test/fixtures/config/config_test.yaml")
Expand Down
4 changes: 1 addition & 3 deletions test/fixtures/config/config_test.yaml
Expand Up @@ -33,7 +33,7 @@ client:
security:
enabled: true
default:
provider: "PKCS11"
provider: "SW"
hashAlgorithm: "SHA2"
softVerify: true
ephemeral: false
Expand All @@ -42,8 +42,6 @@ client:
label: "ForFabric"
library: "/usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so, /usr/lib/softhsm/libsofthsm2.so , /usr/lib/s390x-linux-gnu/softhsm/libsofthsm2.so, /usr/lib/powerpc64le-linux-gnu/softhsm/libsofthsm2.so, /usr/local/Cellar/softhsm/2.1.0/lib/softhsm/libsofthsm2.so"



# network provides a static definition of a Hyperledger Fabric network
network:
# list of ordering service nodes
Expand Down
Expand Up @@ -6,10 +6,13 @@
version: '2'

services:

fabric-sdk-testcase:
container_name: fabric-sdk-softhsm2
fabric-sdk-integration-tests:
container_name: fabric-sdk-integration-test
image: softhsm2-image
volumes:
- ../../:/opt/gopath/src/github.com/hyperledger/fabric-sdk-go
command: /opt/gopath/src/github.com/hyperledger/fabric-sdk-go/test/scripts/unit.sh
command: /opt/gopath/src/github.com/hyperledger/fabric-sdk-go/test/scripts/integration.sh
depends_on:
- orderer.example.com
- builder
20 changes: 20 additions & 0 deletions test/fixtures/docker-compose-pkcs11-test.yaml
@@ -0,0 +1,20 @@
#
# Copyright SecureKey Technologies Inc. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'

services:

fabric-sdk-pkcs11-tests:
container_name: fabric-sdk-pkcs11-test
image: softhsm2-image
environment:
- FABRIC_SDK_CLIENT_BCCSP_SECURITY_DEFAULT_PROVIDER=PKCS11
volumes:
- ../../:/opt/gopath/src/github.com/hyperledger/fabric-sdk-go
command: /opt/gopath/src/github.com/hyperledger/fabric-sdk-go/test/scripts/integration.sh
depends_on:
- orderer.example.com
- builder
12 changes: 1 addition & 11 deletions test/fixtures/docker-compose.yaml
@@ -1,5 +1,5 @@
#
# Copyright IBM Corp, SecureKey Technologies Inc. All Rights Reserved.
# Copyright SecureKey Technologies Inc. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
Expand Down Expand Up @@ -143,16 +143,6 @@ services:
depends_on:
- orderer.example.com
- builder

fabric-sdk-int-tests:
container_name: fabric-sdk-integration-softhsm2
image: softhsm2-image
volumes:
- ../../:/opt/gopath/src/github.com/hyperledger/fabric-sdk-go
command: /opt/gopath/src/github.com/hyperledger/fabric-sdk-go/test/scripts/integration-container.sh
depends_on:
- orderer.example.com
- builder


# builder is only here to create a dependency on the image (not used as part of compose)
Expand Down
97 changes: 97 additions & 0 deletions test/integration/pkcs11_config_test.go
@@ -0,0 +1,97 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package integration

import (
"fmt"
"os"
"testing"

api "github.com/hyperledger/fabric-sdk-go/api/apiconfig"
pkcsFactory "github.com/hyperledger/fabric/bccsp/factory"
pkcs11 "github.com/hyperledger/fabric/bccsp/pkcs11"
)

var configImpl api.Config
var securityLevel = 256

const (
providerTypePKCS11 = "PKCS11"
)

func TestPKCS11CSPConfigWithValidOptions(t *testing.T) {
opts := configurePKCS11Options("SHA2", securityLevel)
f := &pkcsFactory.PKCS11Factory{}
//
csp, err := f.Get(opts)
if err != nil {
t.Fatalf(err.Error())
}
if csp == nil {
t.Fatalf("BCCSP PKCS11 was not configured")
}
fmt.Println("TestPKCS11CSPConfigWithValidOptions passed. BCCSP PKCS11 provider was configured\n", csp)

}

func TestPKCS11CSPConfigWithEmptyHashFamily(t *testing.T) {

opts := configurePKCS11Options("", securityLevel)

f := &pkcsFactory.PKCS11Factory{}
fmt.Println(f.Name())
_, err := f.Get(opts)
if err == nil {
t.Fatalf("Expected error 'Hash Family not supported'")
}
fmt.Println("TestPKCS11CSPConfigWithEmptyHashFamily passed. ")

}

func TestPKCS11CSPConfigWithIncorrectLevel(t *testing.T) {

opts := configurePKCS11Options("SHA2", 100)

f := &pkcsFactory.PKCS11Factory{}
fmt.Println(f.Name())
_, err := f.Get(opts)
if err == nil {
t.Fatalf("Expected error 'Failed initializing configuration'")
}

}

func TestPKCS11CSPConfigWithEmptyProviderName(t *testing.T) {
f := &pkcsFactory.PKCS11Factory{}
if f.Name() != providerTypePKCS11 {
t.Fatalf("Expected default name for PKCS11. Got %s", f.Name())
}
}

func configurePKCS11Options(hashFamily string, securityLevel int) *pkcsFactory.FactoryOpts {
providerLib, softHSMPin, softHSMTokenLabel := pkcs11.FindPKCS11Lib()

pkks := pkcs11.FileKeystoreOpts{KeyStorePath: os.TempDir()}
//PKCS11 options
pkcsOpt := pkcs11.PKCS11Opts{
SecLevel: securityLevel,
HashFamily: hashFamily,
FileKeystore: &pkks,
Library: providerLib,
Pin: softHSMPin,
Label: softHSMTokenLabel,
Ephemeral: false,
}

opts := &pkcsFactory.FactoryOpts{
ProviderName: providerTypePKCS11,
Pkcs11Opts: &pkcsOpt,
}
pkcsFactory.InitFactories(opts)
return opts

}
2 changes: 1 addition & 1 deletion test/scripts/check_status.sh
Expand Up @@ -7,7 +7,7 @@
set -e
file_path=$1

docker-compose --file=$file_path ps -q | xargs docker inspect -f '{{ .Name }},{{ .State.ExitCode }}' | \
docker-compose $file_path ps -q | xargs docker inspect -f '{{ .Name }},{{ .State.ExitCode }}' | \

while read name ; do
if echo "$name" | grep -q "softhsm2"
Expand Down
File renamed without changes.

0 comments on commit 7570207

Please sign in to comment.