Skip to content

Commit

Permalink
[WIP] Improve integration tests
Browse files Browse the repository at this point in the history
Use github.com/rogpeppe/go-internal/testscript and
github.com/kubernetes-sigs/e2e-framework to test the builder run
command with a kind kubernetes cluster

New integration tests should now be easier to add, e.g. for hyperledger-labs#228

Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti committed Jan 14, 2025
1 parent 3e24250 commit e126535
Show file tree
Hide file tree
Showing 17 changed files with 673 additions and 312 deletions.
1 change: 0 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ jobs:
run: go test -v ./...
env:
FABRIC_K8S_BUILDER_DEBUG: 'true'
INCLUDE_KIND_TESTS: ${{ matrix.os == 'ubuntu-latest' && 'true' || 'false' }}

- name: Package
run: |
Expand Down
56 changes: 56 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"context"
"os"
"strconv"

"github.com/hyperledger-labs/fabric-builder-k8s/internal/builder"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/log"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/util"
)

func Build() int {
const (
expectedArgsLength = 4
chaincodeSourceDirectoryArg = 1
chaincodeMetadataDirectoryArg = 2
buildOutputDirectoryArg = 3
)

debug, _ := strconv.ParseBool(util.GetOptionalEnv(util.DebugVariable, "false"))
ctx := log.NewCmdContext(context.Background(), debug)
logger := log.New(ctx)

if len(os.Args) != expectedArgsLength {
logger.Println(
"Expected CHAINCODE_SOURCE_DIR, CHAINCODE_METADATA_DIR and BUILD_OUTPUT_DIR arguments",
)

return 1
}

chaincodeSourceDirectory := os.Args[chaincodeSourceDirectoryArg]
chaincodeMetadataDirectory := os.Args[chaincodeMetadataDirectoryArg]
buildOutputDirectory := os.Args[buildOutputDirectoryArg]

logger.Debugf("Chaincode source directory: %s", chaincodeSourceDirectory)
logger.Debugf("Chaincode metadata directory: %s", chaincodeMetadataDirectory)
logger.Debugf("Build output directory: %s", buildOutputDirectory)

build := &builder.Build{
ChaincodeSourceDirectory: chaincodeSourceDirectory,
ChaincodeMetadataDirectory: chaincodeMetadataDirectory,
BuildOutputDirectory: buildOutputDirectory,
}

if err := build.Run(ctx); err != nil {
logger.Printf("Error building chaincode: %+v", err)

return 1
}

return 0
}
42 changes: 2 additions & 40 deletions cmd/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,11 @@
package main

import (
"context"
"os"

"github.com/hyperledger-labs/fabric-builder-k8s/internal/builder"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/log"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/util"
)

const (
expectedArgsLength = 4
chaincodeSourceDirectoryArg = 1
chaincodeMetadataDirectoryArg = 2
buildOutputDirectoryArg = 3
"github.com/hyperledger-labs/fabric-builder-k8s/cmd"
)

func main() {
debug := util.GetOptionalEnv(util.DebugVariable, "false")
ctx := log.NewCmdContext(context.Background(), debug == "true")
logger := log.New(ctx)

if len(os.Args) != expectedArgsLength {
logger.Println(
"Expected CHAINCODE_SOURCE_DIR, CHAINCODE_METADATA_DIR and BUILD_OUTPUT_DIR arguments",
)
os.Exit(1)
}

chaincodeSourceDirectory := os.Args[chaincodeSourceDirectoryArg]
chaincodeMetadataDirectory := os.Args[chaincodeMetadataDirectoryArg]
buildOutputDirectory := os.Args[buildOutputDirectoryArg]

logger.Debugf("Chaincode source directory: %s", chaincodeSourceDirectory)
logger.Debugf("Chaincode metadata directory: %s", chaincodeMetadataDirectory)
logger.Debugf("Build output directory: %s", buildOutputDirectory)

build := &builder.Build{
ChaincodeSourceDirectory: chaincodeSourceDirectory,
ChaincodeMetadataDirectory: chaincodeMetadataDirectory,
BuildOutputDirectory: buildOutputDirectory,
}

if err := build.Run(ctx); err != nil {
logger.Printf("Error building chaincode: %+v", err)
os.Exit(1)
}
os.Exit(cmd.Build())
}
54 changes: 54 additions & 0 deletions cmd/detect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"context"
"errors"
"os"
"strconv"

"github.com/hyperledger-labs/fabric-builder-k8s/internal/builder"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/log"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/util"
)

func Detect() int {
const (
expectedArgsLength = 3
chaincodeSourceDirectoryArg = 1
chaincodeMetadataDirectoryArg = 2
)

debug, _ := strconv.ParseBool(util.GetOptionalEnv(util.DebugVariable, "false"))
ctx := log.NewCmdContext(context.Background(), debug)
logger := log.New(ctx)

if len(os.Args) != expectedArgsLength {
logger.Println("Expected CHAINCODE_SOURCE_DIR and CHAINCODE_METADATA_DIR arguments")

return 1
}

chaincodeSourceDirectory := os.Args[chaincodeSourceDirectoryArg]
chaincodeMetadataDirectory := os.Args[chaincodeMetadataDirectoryArg]

logger.Debugf("Chaincode source directory: %s", chaincodeSourceDirectory)
logger.Debugf("Chaincode metadata directory: %s", chaincodeMetadataDirectory)

detect := &builder.Detect{
ChaincodeSourceDirectory: chaincodeSourceDirectory,
ChaincodeMetadataDirectory: chaincodeMetadataDirectory,
}

if err := detect.Run(ctx); err != nil {
if !errors.Is(err, builder.ErrUnsupportedChaincodeType) {
// don't spam the peer log if it's just chaincode we don't recognise
logger.Printf("Error detecting chaincode: %+v", err)
}

return 1
}

return 0
}
41 changes: 2 additions & 39 deletions cmd/detect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,11 @@
package main

import (
"context"
"errors"
"os"

"github.com/hyperledger-labs/fabric-builder-k8s/internal/builder"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/log"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/util"
)

const (
expectedArgsLength = 3
chaincodeSourceDirectoryArg = 1
chaincodeMetadataDirectoryArg = 2
"github.com/hyperledger-labs/fabric-builder-k8s/cmd"
)

func main() {
debug := util.GetOptionalEnv(util.DebugVariable, "false")
ctx := log.NewCmdContext(context.Background(), debug == "true")
logger := log.New(ctx)

if len(os.Args) != expectedArgsLength {
logger.Println("Expected CHAINCODE_SOURCE_DIR and CHAINCODE_METADATA_DIR arguments")
os.Exit(1)
}

chaincodeSourceDirectory := os.Args[chaincodeSourceDirectoryArg]
chaincodeMetadataDirectory := os.Args[chaincodeMetadataDirectoryArg]

logger.Debugf("Chaincode source directory: %s", chaincodeSourceDirectory)
logger.Debugf("Chaincode metadata directory: %s", chaincodeMetadataDirectory)

detect := &builder.Detect{
ChaincodeSourceDirectory: chaincodeSourceDirectory,
ChaincodeMetadataDirectory: chaincodeMetadataDirectory,
}

if err := detect.Run(ctx); err != nil {
if !errors.Is(err, builder.ErrUnsupportedChaincodeType) {
// don't spam the peer log if it's just chaincode we don't recognise
logger.Printf("Error detecting chaincode: %+v", err)
}

os.Exit(1)
}
os.Exit(cmd.Detect())
}
50 changes: 50 additions & 0 deletions cmd/release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"context"
"os"
"strconv"

"github.com/hyperledger-labs/fabric-builder-k8s/internal/builder"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/log"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/util"
)

func Release() int {
const (
expectedArgsLength = 3
buildOutputDirectoryArg = 1
releaseOutputDirectoryArg = 2
)

debug, _ := strconv.ParseBool(util.GetOptionalEnv(util.DebugVariable, "false"))
ctx := log.NewCmdContext(context.Background(), debug)
logger := log.New(ctx)

if len(os.Args) != expectedArgsLength {
logger.Println("Expected BUILD_OUTPUT_DIR and RELEASE_OUTPUT_DIR arguments")

return 1
}

buildOutputDirectory := os.Args[buildOutputDirectoryArg]
releaseOutputDirectory := os.Args[releaseOutputDirectoryArg]

logger.Debugf("Build output directory: %s", buildOutputDirectory)
logger.Debugf("Release output directory: %s", releaseOutputDirectory)

release := &builder.Release{
BuildOutputDirectory: buildOutputDirectory,
ReleaseOutputDirectory: releaseOutputDirectory,
}

if err := release.Run(ctx); err != nil {
logger.Printf("Error releasing chaincode: %+v", err)

return 1
}

return 0
}
36 changes: 2 additions & 34 deletions cmd/release/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,11 @@
package main

import (
"context"
"os"

"github.com/hyperledger-labs/fabric-builder-k8s/internal/builder"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/log"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/util"
)

const (
expectedArgsLength = 3
buildOutputDirectoryArg = 1
releaseOutputDirectoryArg = 2
"github.com/hyperledger-labs/fabric-builder-k8s/cmd"
)

func main() {
debug := util.GetOptionalEnv(util.DebugVariable, "false")
ctx := log.NewCmdContext(context.Background(), debug == "true")
logger := log.New(ctx)

if len(os.Args) != expectedArgsLength {
logger.Println("Expected BUILD_OUTPUT_DIR and RELEASE_OUTPUT_DIR arguments")
os.Exit(1)
}

buildOutputDirectory := os.Args[buildOutputDirectoryArg]
releaseOutputDirectory := os.Args[releaseOutputDirectoryArg]

logger.Debugf("Build output directory: %s", buildOutputDirectory)
logger.Debugf("Release output directory: %s", releaseOutputDirectory)

release := &builder.Release{
BuildOutputDirectory: buildOutputDirectory,
ReleaseOutputDirectory: releaseOutputDirectory,
}

if err := release.Run(ctx); err != nil {
logger.Printf("Error releasing chaincode: %+v", err)
os.Exit(1)
}
os.Exit(cmd.Release())
}
Loading

0 comments on commit e126535

Please sign in to comment.