Skip to content

Commit

Permalink
[FAB-11040] Organize PKCS11 integration tests
Browse files Browse the repository at this point in the history
This change seperates the PKCS11 tests into its own script. The folder
is also moved below e2e as it is simply running the e2e test.

Change-Id: Ibec4366cb58c21899100dded864d935583804f04
Signed-off-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
troyronda committed Jul 9, 2018
1 parent 7a65fa4 commit 32bd578
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -26,7 +26,7 @@ You're good to go, happy coding! Check out the examples for usage demonstrations
- [Ledger Query Test](test/integration/sdk/ledger_queries_test.go): Basic example that uses SDK to query a channel's underlying ledger
- [Multi Org Test](test/integration/orgs/multiple_orgs_test.go): An example that has multiple organisations involved in transaction
- [Dynamic Endorser Selection](test/integration/sdk/sdk_provider_test.go): An example that uses dynamic endorser selection (based on chaincode policy)
- [E2E PKCS11 Test](test/integration/pkcs11/e2e_test.go): E2E Test using a PKCS11 crypto suite and configuration
- [E2E PKCS11 Test](test/integration/e2e/pkcs11/e2e_test.go): E2E Test using a PKCS11 crypto suite and configuration
- [CLI](https://github.com/securekey/fabric-examples/tree/master/fabric-cli/): An example CLI for Fabric built with the Go SDK.
- More examples needed!

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/dockerenv/docker-compose-pkcs11-test.yaml
Expand Up @@ -20,7 +20,7 @@ services:
- TEST_CHANGED_ONLY
volumes:
- ../../../:/opt/gopath/src/github.com/hyperledger/fabric-sdk-go
command: bash -c "/opt/gopath/src/github.com/hyperledger/fabric-sdk-go/test/scripts/unit-pkcs11.sh && /opt/gopath/src/github.com/hyperledger/fabric-sdk-go/test/scripts/integration.sh"
command: bash -c "/opt/gopath/src/github.com/hyperledger/fabric-sdk-go/test/scripts/unit-pkcs11.sh && /opt/gopath/src/github.com/hyperledger/fabric-sdk-go/test/scripts/integration-pkcs11.sh"
depends_on:
- org1ca1
- org2ca1
Expand Down
9 changes: 3 additions & 6 deletions test/integration/e2e/end_to_end.go
Expand Up @@ -7,8 +7,8 @@ SPDX-License-Identifier: Apache-2.0
package e2e

import (
"path"
"strconv"
"strings"
"testing"
"time"

Expand All @@ -18,16 +18,13 @@ import (
"github.com/stretchr/testify/require"

"github.com/hyperledger/fabric-sdk-go/test/integration"
"github.com/hyperledger/fabric-sdk-go/test/metadata"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/cauthdsl"

"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"

mspclient "github.com/hyperledger/fabric-sdk-go/pkg/client/msp"

"strings"

"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
packager "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/gopackager"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
Expand Down Expand Up @@ -189,7 +186,7 @@ func queryCC(client *channel.Client, t *testing.T, targetEndpoints ...string) []
}

func createCC(t *testing.T, orgResMgmt *resmgmt.Client) {
ccPkg, err := packager.NewCCPackage("github.com/example_cc", "../../fixtures/testdata")
ccPkg, err := packager.NewCCPackage("github.com/example_cc", integration.GetDeployPath())
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -221,7 +218,7 @@ func createChannel(sdk *fabsdk.FabricSDK, t *testing.T, resMgmtClient *resmgmt.C
t.Fatal(err)
}
req := resmgmt.SaveChannelRequest{ChannelID: channelID,
ChannelConfigPath: path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"),
ChannelConfigPath: integration.GetChannelConfigPath("mychannel.tx"),
SigningIdentities: []msp.SigningIdentity{adminIdentity}}
txID, err := resMgmtClient.SaveChannel(req, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint("orderer.example.com"))
require.Nil(t, err, "error should be nil")
Expand Down
5 changes: 3 additions & 2 deletions test/integration/e2e/end_to_end_test.go
Expand Up @@ -10,13 +10,14 @@ import (
"testing"

"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/test/integration"
)

func TestE2E(t *testing.T) {
configPath := "../../fixtures/config/config_test.yaml"
configPath := integration.GetConfigPath("config_test.yaml")
//End to End testing
Run(t, config.FromFile(configPath))

//Using setup done set above by end to end test, run below test with new config which has no orderer config inside
runWithNoOrdererConfig(t, config.FromFile("../../fixtures/config/config_test_no_orderer.yaml"))
runWithNoOrdererConfig(t, config.FromFile(integration.GetConfigPath("config_test_no_orderer.yaml")))
}
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/hyperledger/fabric-sdk-go/test/integration"

"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
Expand All @@ -18,10 +19,15 @@ import (
"github.com/hyperledger/fabric-sdk-go/test/integration/e2e"
)

const (
// ConfigTestFile contains the path and filename of the config for integration tests
ConfigTestFilename = "config_pkcs11_test.yaml"
)

func TestE2E(t *testing.T) {
// Create SDK setup for the integration tests
e2e.Run(t,
config.FromFile("../"+ConfigTestFile),
config.FromFile(integration.GetConfigPath(ConfigTestFilename)),
fabsdk.WithCorePkg(&CustomCryptoSuiteProviderFactory{}))
}

Expand Down
12 changes: 0 additions & 12 deletions test/integration/pkcs11/env.go

This file was deleted.

102 changes: 102 additions & 0 deletions test/scripts/integration-pkcs11.sh
@@ -0,0 +1,102 @@
#!/bin/bash
#
# Copyright SecureKey Technologies Inc. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# Environment variables that affect this script:
# GO_TESTFLAGS: Flags are added to the go test command.
# GO_LDFLAGS: Flags are added to the go test command (example: -s).
# TEST_CHANGED_ONLY: Boolean on whether to only run tests on changed packages.
# TEST_RACE_CONDITIONS: Boolean on whether to test for race conditions.
# FABRIC_SDKGO_CODELEVEL_TAG: Go tag that represents the fabric code target
# FABRIC_SDKGO_CODELEVEL_VER: Version that represents the fabric code target
# FABRIC_FIXTURE_VERSION: Version of fabric fixtures
# FABRIC_CRYPTOCONFIG_VERSION: Version of cryptoconfig fixture to use
# CONFIG_FILE: config file to use

set -e

GO_CMD="${GO_CMD:-go}"
GOPATH="${GOPATH:-$HOME/go}"
FABRIC_SDKGO_CODELEVEL_TAG="${FABRIC_SDKGO_CODELEVEL_TAG:-stable}"
FABRIC_CRYPTOCONFIG_VERSION="${FABRIC_CRYPTOCONFIG_VERSION:-v1}"
FABRIC_FIXTURE_VERSION="${FABRIC_FIXTURE_VERSION:-v1.2}"
CONFIG_FILE="${CONFIG_FILE:-config_test.yaml}"
TEST_LOCAL="${TEST_LOCAL:-false}"
TEST_CHANGED_ONLY="${TEST_CHANGED_ONLY:-false}"
TEST_RACE_CONDITIONS="${TEST_RACE_CONDITIONS:-true}"
SCRIPT_DIR="$(dirname "$0")"
# TODO: better default handling for FABRIC_CRYPTOCONFIG_VERSION

REPO="github.com/hyperledger/fabric-sdk-go"

source ${SCRIPT_DIR}/lib/find_packages.sh

echo "Running" $(basename "$0")

# Packages to include in test run
PKGS=($(${GO_CMD} list ${REPO}/test/integration/... 2> /dev/null | \
grep ^${REPO}/test/integration/e2e/pkcs11 | \
tr '\n' ' '))

# Reduce tests to changed packages.
if [ "${TEST_CHANGED_ONLY}" = true ]; then
# findChangedFiles assumes that the working directory contains the repo; so change to the repo directory.
PWD=$(pwd)
cd "${GOPATH}/src/${REPO}"
findChangedFiles
cd ${PWD}

if [[ "${CHANGED_FILES[@]}" =~ ( |^)(test/fixtures/|test/metadata/|test/scripts/|Makefile( |$)|Gopkg.lock( |$)|ci.properties( |$)) ]]; then
echo "Test scripts, fixtures or metadata changed - running all tests"
else
findChangedPackages
filterExcludedPackages
appendDepPackages
PKGS=(${DEP_PKGS[@]})
fi
fi

RACEFLAG=""
if [ "${TEST_RACE_CONDITIONS}" = true ]; then
ARCH=$(uname -m)

if [ "${ARCH}" = "x86_64" ]; then
echo "Enabling data race detection"
RACEFLAG="-race"
else
echo "Data race detection not supported on ${ARCH}"
fi
fi

if [ ${#PKGS[@]} -eq 0 ]; then
echo "Skipping integration tests since no packages were changed"
exit 0
fi

#Add entry here below for your key to be imported into softhsm
declare -a PRIVATE_KEYS=(
"github.com/hyperledger/fabric-sdk-go/test/fixtures/config/mutual_tls/client_sdk_go-key.pem"
"github.com/hyperledger/fabric-sdk-go/test/fixtures/fabric/${FABRIC_CRYPTOCONFIG_VERSION}/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/keystore/f4aa194b12d13d7c2b7b275a7115af5e6f728e11710716f2c754df4587891511_sk"
"github.com/hyperledger/fabric-sdk-go/test/fixtures/fabric/${FABRIC_CRYPTOCONFIG_VERSION}/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/ce142124e13093a3e13bc4708b0f2b26e1d4d2ea4d4cc59942790bfc0f3bcc6d_sk"
"github.com/hyperledger/fabric-sdk-go/test/fixtures/fabric/${FABRIC_CRYPTOCONFIG_VERSION}/crypto-config/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/abbe8ee0f86c227b1917d208921497603d2ff28f4ba8e902d703744c4a6fa7b7_sk"
"github.com/hyperledger/fabric-sdk-go/test/fixtures/fabric/${FABRIC_CRYPTOCONFIG_VERSION}/crypto-config/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp/keystore/371ea01078b18f3b92c1fc8233dfa8d209d882ae40aeff4defd118ba9d572a15_sk"
"github.com/hyperledger/fabric-sdk-go/test/fixtures/fabric/${FABRIC_CRYPTOCONFIG_VERSION}/crypto-config/peerOrganizations/org2.example.com/users/User1@org2.example.com/msp/keystore/7777a174c9fe40ab5abe33199a4fe82f1e0a7c45715e395e73a78cc3480d0021_sk"
)

GO_SRC=/opt/gopath/src
for i in "${PRIVATE_KEYS[@]}"
do
echo "Importing key : ${GO_SRC}/${i}"
openssl pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in ${GO_SRC}/${i} -out private.p8
pkcs11helper -action import -keyFile private.p8
rm -rf private.p8
done

echo "Code level ${FABRIC_SDKGO_CODELEVEL_TAG} (Fabric ${FABRIC_FIXTURE_VERSION})"
echo "Running integration tests ..."

GO_TAGS="$GO_TAGS ${FABRIC_SDKGO_CODELEVEL_TAG}"
GO_LDFLAGS="${GO_LDFLAGS} -X github.com/hyperledger/fabric-sdk-go/test/metadata.ChannelConfigPath=test/fixtures/fabric/${FABRIC_FIXTURE_VERSION}/channel -X github.com/hyperledger/fabric-sdk-go/test/metadata.CryptoConfigPath=test/fixtures/fabric/${FABRIC_CRYPTOCONFIG_VERSION}/crypto-config"
$GO_CMD test ${RACEFLAG} -tags "${GO_TAGS}" ${GO_TESTFLAGS} -ldflags="${GO_LDFLAGS}" ${PKGS[@]} -p 1 -timeout=40m configFile=${CONFIG_FILE} testLocal=${TEST_LOCAL}
48 changes: 11 additions & 37 deletions test/scripts/integration.sh
Expand Up @@ -36,24 +36,19 @@ source ${SCRIPT_DIR}/lib/find_packages.sh
echo "Running" $(basename "$0")

# Packages to include in test run
PKGS=($($GO_CMD list $REPO/test/integration/... 2> /dev/null | \
grep -v ^$REPO/test/integration/pkcs11 | \
grep -v ^$REPO/test/integration/negative | \
grep -v ^$REPO/test/integration\$ | \
PKGS=($(${GO_CMD} list ${REPO}/test/integration/... 2> /dev/null | \
grep -v ^${REPO}/test/integration/e2e/pkcs11 | \
grep -v ^${REPO}/test/integration/negative | \
grep -v ^${REPO}/test/integration\$ | \
tr '\n' ' '))

if [ "$E2E_ONLY" == "true" ]; then
echo "Including E2E tests only"
PKGS=(`$GO_CMD list $REPO/test/integration/e2e/... 2> /dev/null`)
fi

if [ "$FABRIC_SDK_CLIENT_BCCSP_SECURITY_DEFAULT_PROVIDER" == "PKCS11" ]; then
echo "Including PKCS11 tests only"
PKGS=("${REPO}/test/integration/pkcs11")
PKGS=($(echo ${PKGS[@]} | tr ' ' '\n' | grep ^${REPO}/test/integration/e2e | tr '\n' ' '))
fi

# Reduce tests to changed packages.
if [ "$TEST_CHANGED_ONLY" = true ]; then
if [ "${TEST_CHANGED_ONLY}" = true ]; then
# findChangedFiles assumes that the working directory contains the repo; so change to the repo directory.
PWD=$(pwd)
cd "${GOPATH}/src/${REPO}"
Expand All @@ -71,7 +66,7 @@ if [ "$TEST_CHANGED_ONLY" = true ]; then
fi

RACEFLAG=""
if [ "$TEST_RACE_CONDITIONS" = true ]; then
if [ "${TEST_RACE_CONDITIONS}" = true ]; then
ARCH=$(uname -m)

if [ "${ARCH}" = "x86_64" ]; then
Expand All @@ -87,30 +82,9 @@ if [ ${#PKGS[@]} -eq 0 ]; then
exit 0
fi

#Add entry here below for your key to be imported into softhsm
declare -a PRIVATE_KEYS=(
"github.com/hyperledger/fabric-sdk-go/test/fixtures/config/mutual_tls/client_sdk_go-key.pem"
"github.com/hyperledger/fabric-sdk-go/test/fixtures/fabric/${FABRIC_CRYPTOCONFIG_VERSION}/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/keystore/f4aa194b12d13d7c2b7b275a7115af5e6f728e11710716f2c754df4587891511_sk"
"github.com/hyperledger/fabric-sdk-go/test/fixtures/fabric/${FABRIC_CRYPTOCONFIG_VERSION}/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/ce142124e13093a3e13bc4708b0f2b26e1d4d2ea4d4cc59942790bfc0f3bcc6d_sk"
"github.com/hyperledger/fabric-sdk-go/test/fixtures/fabric/${FABRIC_CRYPTOCONFIG_VERSION}/crypto-config/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/abbe8ee0f86c227b1917d208921497603d2ff28f4ba8e902d703744c4a6fa7b7_sk"
"github.com/hyperledger/fabric-sdk-go/test/fixtures/fabric/${FABRIC_CRYPTOCONFIG_VERSION}/crypto-config/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp/keystore/371ea01078b18f3b92c1fc8233dfa8d209d882ae40aeff4defd118ba9d572a15_sk"
"github.com/hyperledger/fabric-sdk-go/test/fixtures/fabric/${FABRIC_CRYPTOCONFIG_VERSION}/crypto-config/peerOrganizations/org2.example.com/users/User1@org2.example.com/msp/keystore/7777a174c9fe40ab5abe33199a4fe82f1e0a7c45715e395e73a78cc3480d0021_sk"
)

GO_SRC=/opt/gopath/src
if [ "$FABRIC_SDK_CLIENT_BCCSP_SECURITY_DEFAULT_PROVIDER" == "PKCS11" ]; then
for i in "${PRIVATE_KEYS[@]}"
do
echo "Importing key : ${GO_SRC}/${i}"
openssl pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in ${GO_SRC}/${i} -out private.p8
pkcs11helper -action import -keyFile private.p8
rm -rf private.p8
done
fi

echo "Code level $FABRIC_SDKGO_CODELEVEL_TAG (Fabric ${FABRIC_FIXTURE_VERSION})"
echo "Code level ${FABRIC_SDKGO_CODELEVEL_TAG} (Fabric ${FABRIC_FIXTURE_VERSION})"
echo "Running integration tests ..."

GO_TAGS="$GO_TAGS $FABRIC_SDKGO_CODELEVEL_TAG"
GO_LDFLAGS="$GO_LDFLAGS -X github.com/hyperledger/fabric-sdk-go/test/metadata.ChannelConfigPath=test/fixtures/fabric/${FABRIC_FIXTURE_VERSION}/channel -X github.com/hyperledger/fabric-sdk-go/test/metadata.CryptoConfigPath=test/fixtures/fabric/${FABRIC_CRYPTOCONFIG_VERSION}/crypto-config"
$GO_CMD test $RACEFLAG -tags "$GO_TAGS" $GO_TESTFLAGS -ldflags="$GO_LDFLAGS" ${PKGS[@]} -p 1 -timeout=40m configFile=${CONFIG_FILE} testLocal=${TEST_LOCAL}
GO_TAGS="${GO_TAGS} ${FABRIC_SDKGO_CODELEVEL_TAG}"
GO_LDFLAGS="${GO_LDFLAGS} -X github.com/hyperledger/fabric-sdk-go/test/metadata.ChannelConfigPath=test/fixtures/fabric/${FABRIC_FIXTURE_VERSION}/channel -X github.com/hyperledger/fabric-sdk-go/test/metadata.CryptoConfigPath=test/fixtures/fabric/${FABRIC_CRYPTOCONFIG_VERSION}/crypto-config"
${GO_CMD} test ${RACEFLAG} -tags "${GO_TAGS}" ${GO_TESTFLAGS} -ldflags="${GO_LDFLAGS}" ${PKGS[@]} -p 1 -timeout=40m configFile=${CONFIG_FILE} testLocal=${TEST_LOCAL}

0 comments on commit 32bd578

Please sign in to comment.