Skip to content

Commit

Permalink
[WIP] testscript experiment
Browse files Browse the repository at this point in the history
Use github.com/rogpeppe/go-internal/testscript instead of ginkgo to
test builder commands?

Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti committed Jan 7, 2025
1 parent 3e24250 commit 397ffbc
Show file tree
Hide file tree
Showing 15 changed files with 392 additions and 120 deletions.
3 changes: 3 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// SPDX-License-Identifier: Apache-2.0

package cmd
44 changes: 44 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: Apache-2.0

package cmd_test

import (
"os"
"testing"

"github.com/hyperledger-labs/fabric-builder-k8s/cmd"
"github.com/rogpeppe/go-internal/testscript"
)

func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
// "build": cmd.Build,
"detect": cmd.Detect,
// "release": cmd.Release,
// "run": cmd.Run,
}))
}

func TestBuildCommand(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/build",
})
}

func TestDetectCommand(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/detect",
})
}

func TestReleaseCommand(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/release",
})
}

func TestRunCommand(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/run",
})
}
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"

"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 {

Check failure on line 15 in cmd/detect.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

const (
expectedArgsLength = 3
chaincodeSourceDirectoryArg = 1
chaincodeMetadataDirectoryArg = 2
)

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")

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())
}
3 changes: 3 additions & 0 deletions cmd/release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// SPDX-License-Identifier: Apache-2.0

package cmd
97 changes: 97 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// SPDX-License-Identifier: Apache-2.0

package cmd

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"
"k8s.io/apimachinery/pkg/api/validation"
)

func Run() int {

Check failure on line 15 in cmd/run.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

const (
expectedArgsLength = 3
buildOutputDirectoryArg = 1
runMetadataDirectoryArg = 2
maximumKubeNamePrefixLength = 30
)

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 RUN_METADATA_DIR arguments")

return 1
}

buildOutputDirectory := os.Args[buildOutputDirectoryArg]
runMetadataDirectory := os.Args[runMetadataDirectoryArg]

logger.Debugf("Build output directory: %s", buildOutputDirectory)
logger.Debugf("Run metadata directory: %s", runMetadataDirectory)

peerID, err := util.GetRequiredEnv(util.PeerIDVariable)
if err != nil {
logger.Printf("Expected %s environment variable\n", util.PeerIDVariable)

return 1
}

logger.Debugf("%s=%s", util.PeerIDVariable, peerID)

kubeconfigPath := util.GetOptionalEnv(util.KubeconfigPathVariable, "")
logger.Debugf("%s=%s", util.KubeconfigPathVariable, kubeconfigPath)

kubeNamespace := util.GetOptionalEnv(util.ChaincodeNamespaceVariable, "")
logger.Debugf("%s=%s", util.ChaincodeNamespaceVariable, kubeNamespace)

if kubeNamespace == "" {
kubeNamespace, err = util.GetKubeNamespace()
if err != nil {
kubeNamespace = util.DefaultNamespace
}
}

kubeServiceAccount := util.GetOptionalEnv(util.ChaincodeServiceAccountVariable, util.DefaultServiceAccountName)
logger.Debugf("%s=%s", util.ChaincodeServiceAccountVariable, kubeServiceAccount)

kubeNamePrefix := util.GetOptionalEnv(util.ObjectNamePrefixVariable, util.DefaultObjectNamePrefix)
logger.Debugf("%s=%s", util.ObjectNamePrefixVariable, kubeNamePrefix)

if len(kubeNamePrefix) > maximumKubeNamePrefixLength {
logger.Printf("The FABRIC_K8S_BUILDER_OBJECT_NAME_PREFIX environment variable must be a maximum of 30 characters")

return 1
}

if msgs := validation.NameIsDNS1035Label(kubeNamePrefix, true); len(msgs) > 0 {
logger.Printf("The FABRIC_K8S_BUILDER_OBJECT_NAME_PREFIX environment variable must be a valid DNS-1035 label: %s", msgs[0])

return 1
}

run := &builder.Run{
BuildOutputDirectory: buildOutputDirectory,
RunMetadataDirectory: runMetadataDirectory,
PeerID: peerID,
KubeconfigPath: kubeconfigPath,
KubeNamespace: kubeNamespace,
KubeServiceAccount: kubeServiceAccount,
KubeNamePrefix: kubeNamePrefix,
}

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

return 1
}

return 0
}
80 changes: 2 additions & 78 deletions cmd/run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,87 +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"
"k8s.io/apimachinery/pkg/api/validation"
)

const (
expectedArgsLength = 3
buildOutputDirectoryArg = 1
runMetadataDirectoryArg = 2
maximumKubeNamePrefixLength = 30
"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 RUN_METADATA_DIR arguments")
os.Exit(1)
}

buildOutputDirectory := os.Args[buildOutputDirectoryArg]
runMetadataDirectory := os.Args[runMetadataDirectoryArg]

logger.Debugf("Build output directory: %s", buildOutputDirectory)
logger.Debugf("Run metadata directory: %s", runMetadataDirectory)

peerID, err := util.GetRequiredEnv(util.PeerIDVariable)
if err != nil {
logger.Printf("Expected %s environment variable\n", util.PeerIDVariable)
os.Exit(1)
}

logger.Debugf("%s=%s", util.PeerIDVariable, peerID)

kubeconfigPath := util.GetOptionalEnv(util.KubeconfigPathVariable, "")
logger.Debugf("%s=%s", util.KubeconfigPathVariable, kubeconfigPath)

kubeNamespace := util.GetOptionalEnv(util.ChaincodeNamespaceVariable, "")
logger.Debugf("%s=%s", util.ChaincodeNamespaceVariable, kubeNamespace)

if kubeNamespace == "" {
kubeNamespace, err = util.GetKubeNamespace()
if err != nil {
kubeNamespace = util.DefaultNamespace
}
}

kubeServiceAccount := util.GetOptionalEnv(util.ChaincodeServiceAccountVariable, util.DefaultServiceAccountName)
logger.Debugf("%s=%s", util.ChaincodeServiceAccountVariable, kubeServiceAccount)

kubeNamePrefix := util.GetOptionalEnv(util.ObjectNamePrefixVariable, util.DefaultObjectNamePrefix)
logger.Debugf("%s=%s", util.ObjectNamePrefixVariable, kubeNamePrefix)

if len(kubeNamePrefix) > maximumKubeNamePrefixLength {
logger.Printf("The FABRIC_K8S_BUILDER_OBJECT_NAME_PREFIX environment variable must be a maximum of 30 characters")
os.Exit(1)
}

if msgs := validation.NameIsDNS1035Label(kubeNamePrefix, true); len(msgs) > 0 {
logger.Printf("The FABRIC_K8S_BUILDER_OBJECT_NAME_PREFIX environment variable must be a valid DNS-1035 label: %s", msgs[0])
os.Exit(1)
}

run := &builder.Run{
BuildOutputDirectory: buildOutputDirectory,
RunMetadataDirectory: runMetadataDirectory,
PeerID: peerID,
KubeconfigPath: kubeconfigPath,
KubeNamespace: kubeNamespace,
KubeServiceAccount: kubeServiceAccount,
KubeNamePrefix: kubeNamePrefix,
}

if err := run.Run(ctx); err != nil {
logger.Printf("Error running chaincode: %+v", err)
os.Exit(1)
}
os.Exit(cmd.Run())
}
2 changes: 2 additions & 0 deletions cmd/testdata/build/build.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exec bork
stdout 'hello world\n'
2 changes: 2 additions & 0 deletions cmd/testdata/detect/detect.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exec detect
stdout 'hello world\n'
2 changes: 2 additions & 0 deletions cmd/testdata/release/release.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exec release
stdout 'hello world\n'
2 changes: 2 additions & 0 deletions cmd/testdata/run/run.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exec run
stdout 'hello world\n'
Loading

0 comments on commit 397ffbc

Please sign in to comment.