-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use github.com/rogpeppe/go-internal/testscript instead of ginkgo to test builder commands? Signed-off-by: James Taylor <[email protected]>
- Loading branch information
Showing
15 changed files
with
392 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
exec bork | ||
stdout 'hello world\n' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
exec detect | ||
stdout 'hello world\n' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
exec release | ||
stdout 'hello world\n' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
exec run | ||
stdout 'hello world\n' |
Oops, something went wrong.