Skip to content

Commit 4ec2896

Browse files
committed
[WIP] Improve integration tests
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 #228 Signed-off-by: James Taylor <[email protected]>
1 parent 3e24250 commit 4ec2896

File tree

14 files changed

+497
-238
lines changed

14 files changed

+497
-238
lines changed

.github/workflows/go.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ jobs:
4444
run: go test -v ./...
4545
env:
4646
FABRIC_K8S_BUILDER_DEBUG: 'true'
47-
INCLUDE_KIND_TESTS: ${{ matrix.os == 'ubuntu-latest' && 'true' || 'false' }}
4847

4948
- name: Package
5049
run: |

cmd/build.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package cmd

cmd/detect.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package cmd
4+
5+
import (
6+
"context"
7+
"errors"
8+
"os"
9+
10+
"github.com/hyperledger-labs/fabric-builder-k8s/internal/builder"
11+
"github.com/hyperledger-labs/fabric-builder-k8s/internal/log"
12+
"github.com/hyperledger-labs/fabric-builder-k8s/internal/util"
13+
)
14+
15+
func Detect() int {
16+
17+
const (
18+
expectedArgsLength = 3
19+
chaincodeSourceDirectoryArg = 1
20+
chaincodeMetadataDirectoryArg = 2
21+
)
22+
23+
debug := util.GetOptionalEnv(util.DebugVariable, "false")
24+
ctx := log.NewCmdContext(context.Background(), debug == "true")
25+
logger := log.New(ctx)
26+
27+
if len(os.Args) != expectedArgsLength {
28+
logger.Println("Expected CHAINCODE_SOURCE_DIR and CHAINCODE_METADATA_DIR arguments")
29+
30+
return 1
31+
}
32+
33+
chaincodeSourceDirectory := os.Args[chaincodeSourceDirectoryArg]
34+
chaincodeMetadataDirectory := os.Args[chaincodeMetadataDirectoryArg]
35+
36+
logger.Debugf("Chaincode source directory: %s", chaincodeSourceDirectory)
37+
logger.Debugf("Chaincode metadata directory: %s", chaincodeMetadataDirectory)
38+
39+
detect := &builder.Detect{
40+
ChaincodeSourceDirectory: chaincodeSourceDirectory,
41+
ChaincodeMetadataDirectory: chaincodeMetadataDirectory,
42+
}
43+
44+
if err := detect.Run(ctx); err != nil {
45+
if !errors.Is(err, builder.ErrUnsupportedChaincodeType) {
46+
// don't spam the peer log if it's just chaincode we don't recognise
47+
logger.Printf("Error detecting chaincode: %+v", err)
48+
}
49+
50+
return 1
51+
}
52+
53+
return 0
54+
}

cmd/detect/main.go

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,11 @@
33
package main
44

55
import (
6-
"context"
7-
"errors"
86
"os"
97

10-
"github.com/hyperledger-labs/fabric-builder-k8s/internal/builder"
11-
"github.com/hyperledger-labs/fabric-builder-k8s/internal/log"
12-
"github.com/hyperledger-labs/fabric-builder-k8s/internal/util"
13-
)
14-
15-
const (
16-
expectedArgsLength = 3
17-
chaincodeSourceDirectoryArg = 1
18-
chaincodeMetadataDirectoryArg = 2
8+
"github.com/hyperledger-labs/fabric-builder-k8s/cmd"
199
)
2010

2111
func main() {
22-
debug := util.GetOptionalEnv(util.DebugVariable, "false")
23-
ctx := log.NewCmdContext(context.Background(), debug == "true")
24-
logger := log.New(ctx)
25-
26-
if len(os.Args) != expectedArgsLength {
27-
logger.Println("Expected CHAINCODE_SOURCE_DIR and CHAINCODE_METADATA_DIR arguments")
28-
os.Exit(1)
29-
}
30-
31-
chaincodeSourceDirectory := os.Args[chaincodeSourceDirectoryArg]
32-
chaincodeMetadataDirectory := os.Args[chaincodeMetadataDirectoryArg]
33-
34-
logger.Debugf("Chaincode source directory: %s", chaincodeSourceDirectory)
35-
logger.Debugf("Chaincode metadata directory: %s", chaincodeMetadataDirectory)
36-
37-
detect := &builder.Detect{
38-
ChaincodeSourceDirectory: chaincodeSourceDirectory,
39-
ChaincodeMetadataDirectory: chaincodeMetadataDirectory,
40-
}
41-
42-
if err := detect.Run(ctx); err != nil {
43-
if !errors.Is(err, builder.ErrUnsupportedChaincodeType) {
44-
// don't spam the peer log if it's just chaincode we don't recognise
45-
logger.Printf("Error detecting chaincode: %+v", err)
46-
}
47-
48-
os.Exit(1)
49-
}
12+
os.Exit(cmd.Detect())
5013
}

cmd/release.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package cmd

cmd/run.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package cmd
4+
5+
import (
6+
"context"
7+
"os"
8+
9+
"github.com/hyperledger-labs/fabric-builder-k8s/internal/builder"
10+
"github.com/hyperledger-labs/fabric-builder-k8s/internal/log"
11+
"github.com/hyperledger-labs/fabric-builder-k8s/internal/util"
12+
"k8s.io/apimachinery/pkg/api/validation"
13+
)
14+
15+
func Run() int {
16+
17+
const (
18+
expectedArgsLength = 3
19+
buildOutputDirectoryArg = 1
20+
runMetadataDirectoryArg = 2
21+
maximumKubeNamePrefixLength = 30
22+
)
23+
24+
debug := util.GetOptionalEnv(util.DebugVariable, "false")
25+
ctx := log.NewCmdContext(context.Background(), debug == "true")
26+
logger := log.New(ctx)
27+
28+
if len(os.Args) != expectedArgsLength {
29+
logger.Println("Expected BUILD_OUTPUT_DIR and RUN_METADATA_DIR arguments")
30+
31+
return 1
32+
}
33+
34+
buildOutputDirectory := os.Args[buildOutputDirectoryArg]
35+
runMetadataDirectory := os.Args[runMetadataDirectoryArg]
36+
37+
logger.Debugf("Build output directory: %s", buildOutputDirectory)
38+
logger.Debugf("Run metadata directory: %s", runMetadataDirectory)
39+
40+
peerID, err := util.GetRequiredEnv(util.PeerIDVariable)
41+
if err != nil {
42+
logger.Printf("Expected %s environment variable\n", util.PeerIDVariable)
43+
44+
return 1
45+
}
46+
47+
logger.Debugf("%s=%s", util.PeerIDVariable, peerID)
48+
49+
kubeconfigPath := util.GetOptionalEnv(util.KubeconfigPathVariable, "")
50+
logger.Debugf("%s=%s", util.KubeconfigPathVariable, kubeconfigPath)
51+
52+
kubeNamespace := util.GetOptionalEnv(util.ChaincodeNamespaceVariable, "")
53+
logger.Debugf("%s=%s", util.ChaincodeNamespaceVariable, kubeNamespace)
54+
55+
if kubeNamespace == "" {
56+
kubeNamespace, err = util.GetKubeNamespace()
57+
if err != nil {
58+
kubeNamespace = util.DefaultNamespace
59+
}
60+
}
61+
62+
kubeServiceAccount := util.GetOptionalEnv(util.ChaincodeServiceAccountVariable, util.DefaultServiceAccountName)
63+
logger.Debugf("%s=%s", util.ChaincodeServiceAccountVariable, kubeServiceAccount)
64+
65+
kubeNamePrefix := util.GetOptionalEnv(util.ObjectNamePrefixVariable, util.DefaultObjectNamePrefix)
66+
logger.Debugf("%s=%s", util.ObjectNamePrefixVariable, kubeNamePrefix)
67+
68+
if len(kubeNamePrefix) > maximumKubeNamePrefixLength {
69+
logger.Printf("The FABRIC_K8S_BUILDER_OBJECT_NAME_PREFIX environment variable must be a maximum of 30 characters")
70+
71+
return 1
72+
}
73+
74+
if msgs := validation.NameIsDNS1035Label(kubeNamePrefix, true); len(msgs) > 0 {
75+
logger.Printf("The FABRIC_K8S_BUILDER_OBJECT_NAME_PREFIX environment variable must be a valid DNS-1035 label: %s", msgs[0])
76+
77+
return 1
78+
}
79+
80+
run := &builder.Run{
81+
BuildOutputDirectory: buildOutputDirectory,
82+
RunMetadataDirectory: runMetadataDirectory,
83+
PeerID: peerID,
84+
KubeconfigPath: kubeconfigPath,
85+
KubeNamespace: kubeNamespace,
86+
KubeServiceAccount: kubeServiceAccount,
87+
KubeNamePrefix: kubeNamePrefix,
88+
}
89+
90+
if err := run.Run(ctx); err != nil {
91+
logger.Printf("Error running chaincode: %+v", err)
92+
93+
return 1
94+
}
95+
96+
return 0
97+
}

cmd/run/main.go

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,11 @@
33
package main
44

55
import (
6-
"context"
76
"os"
87

9-
"github.com/hyperledger-labs/fabric-builder-k8s/internal/builder"
10-
"github.com/hyperledger-labs/fabric-builder-k8s/internal/log"
11-
"github.com/hyperledger-labs/fabric-builder-k8s/internal/util"
12-
"k8s.io/apimachinery/pkg/api/validation"
13-
)
14-
15-
const (
16-
expectedArgsLength = 3
17-
buildOutputDirectoryArg = 1
18-
runMetadataDirectoryArg = 2
19-
maximumKubeNamePrefixLength = 30
8+
"github.com/hyperledger-labs/fabric-builder-k8s/cmd"
209
)
2110

2211
func main() {
23-
debug := util.GetOptionalEnv(util.DebugVariable, "false")
24-
ctx := log.NewCmdContext(context.Background(), debug == "true")
25-
logger := log.New(ctx)
26-
27-
if len(os.Args) != expectedArgsLength {
28-
logger.Println("Expected BUILD_OUTPUT_DIR and RUN_METADATA_DIR arguments")
29-
os.Exit(1)
30-
}
31-
32-
buildOutputDirectory := os.Args[buildOutputDirectoryArg]
33-
runMetadataDirectory := os.Args[runMetadataDirectoryArg]
34-
35-
logger.Debugf("Build output directory: %s", buildOutputDirectory)
36-
logger.Debugf("Run metadata directory: %s", runMetadataDirectory)
37-
38-
peerID, err := util.GetRequiredEnv(util.PeerIDVariable)
39-
if err != nil {
40-
logger.Printf("Expected %s environment variable\n", util.PeerIDVariable)
41-
os.Exit(1)
42-
}
43-
44-
logger.Debugf("%s=%s", util.PeerIDVariable, peerID)
45-
46-
kubeconfigPath := util.GetOptionalEnv(util.KubeconfigPathVariable, "")
47-
logger.Debugf("%s=%s", util.KubeconfigPathVariable, kubeconfigPath)
48-
49-
kubeNamespace := util.GetOptionalEnv(util.ChaincodeNamespaceVariable, "")
50-
logger.Debugf("%s=%s", util.ChaincodeNamespaceVariable, kubeNamespace)
51-
52-
if kubeNamespace == "" {
53-
kubeNamespace, err = util.GetKubeNamespace()
54-
if err != nil {
55-
kubeNamespace = util.DefaultNamespace
56-
}
57-
}
58-
59-
kubeServiceAccount := util.GetOptionalEnv(util.ChaincodeServiceAccountVariable, util.DefaultServiceAccountName)
60-
logger.Debugf("%s=%s", util.ChaincodeServiceAccountVariable, kubeServiceAccount)
61-
62-
kubeNamePrefix := util.GetOptionalEnv(util.ObjectNamePrefixVariable, util.DefaultObjectNamePrefix)
63-
logger.Debugf("%s=%s", util.ObjectNamePrefixVariable, kubeNamePrefix)
64-
65-
if len(kubeNamePrefix) > maximumKubeNamePrefixLength {
66-
logger.Printf("The FABRIC_K8S_BUILDER_OBJECT_NAME_PREFIX environment variable must be a maximum of 30 characters")
67-
os.Exit(1)
68-
}
69-
70-
if msgs := validation.NameIsDNS1035Label(kubeNamePrefix, true); len(msgs) > 0 {
71-
logger.Printf("The FABRIC_K8S_BUILDER_OBJECT_NAME_PREFIX environment variable must be a valid DNS-1035 label: %s", msgs[0])
72-
os.Exit(1)
73-
}
74-
75-
run := &builder.Run{
76-
BuildOutputDirectory: buildOutputDirectory,
77-
RunMetadataDirectory: runMetadataDirectory,
78-
PeerID: peerID,
79-
KubeconfigPath: kubeconfigPath,
80-
KubeNamespace: kubeNamespace,
81-
KubeServiceAccount: kubeServiceAccount,
82-
KubeNamePrefix: kubeNamePrefix,
83-
}
84-
85-
if err := run.Run(ctx); err != nil {
86-
logger.Printf("Error running chaincode: %+v", err)
87-
os.Exit(1)
88-
}
12+
os.Exit(cmd.Run())
8913
}

cmd/run/main_test.go

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package main_test
22

33
import (
4-
"fmt"
54
"os"
65
"os/exec"
7-
"time"
86

97
. "github.com/onsi/ginkgo/v2"
108
. "github.com/onsi/gomega"
@@ -66,73 +64,4 @@ var _ = Describe("Main", func() {
6664
Entry("When the FABRIC_K8S_BUILDER_OBJECT_NAME_PREFIX starts with a number", "1prefix", `run \[\d+\]: The FABRIC_K8S_BUILDER_OBJECT_NAME_PREFIX environment variable must be a valid DNS-1035 label: a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character`),
6765
Entry("When the FABRIC_K8S_BUILDER_OBJECT_NAME_PREFIX starts with a dash", "-prefix", `run \[\d+\]: The FABRIC_K8S_BUILDER_OBJECT_NAME_PREFIX environment variable must be a valid DNS-1035 label: a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character`),
6866
)
69-
70-
It(
71-
"should start a chaincode job using the supplied configuration environment variables",
72-
Label("kind"),
73-
func() {
74-
homedir, err := os.UserHomeDir()
75-
Expect(err).NotTo(HaveOccurred())
76-
77-
args := []string{"./testdata/validimage", "./testdata/validchaincode"}
78-
command := exec.Command(runCmdPath, args...)
79-
command.Env = append(os.Environ(),
80-
fmt.Sprintf("KUBECONFIG_PATH=%s/.kube/config", homedir),
81-
"CORE_PEER_ID=core-peer-id-abcdefghijklmnopqrstuvwxyz-0123456789",
82-
"FABRIC_K8S_BUILDER_DEBUG=true",
83-
"FABRIC_K8S_BUILDER_NAMESPACE=chaincode",
84-
"FABRIC_K8S_BUILDER_SERVICE_ACCOUNT=chaincode",
85-
)
86-
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
87-
Expect(err).NotTo(HaveOccurred())
88-
89-
Eventually(session).ShouldNot(gexec.Exit())
90-
Eventually(
91-
session.Err,
92-
).Should(gbytes.Say(`run \[\d+\] DEBUG: FABRIC_K8S_BUILDER_NAMESPACE=chaincode`))
93-
Eventually(
94-
session.Err,
95-
).Should(gbytes.Say(`run \[\d+\] DEBUG: FABRIC_K8S_BUILDER_SERVICE_ACCOUNT=chaincode`))
96-
Eventually(
97-
session.Err,
98-
).Should(gbytes.Say(`run \[\d+\]: Running chaincode ID CHAINCODE_LABEL:6f98c4bb29414771312eddd1a813eef583df2121c235c4797792f141a46d4b45 with kubernetes job chaincode/hlfcc-chaincodelabel-piihcaj6ryttc`))
99-
100-
waitArgs := []string{
101-
"wait",
102-
"--for=jsonpath=.status.ready=1",
103-
"job",
104-
"--timeout=120s",
105-
"--namespace=chaincode",
106-
"-l",
107-
"fabric-builder-k8s-cclabel=CHAINCODE_LABEL",
108-
}
109-
waitCommand := exec.Command("kubectl", waitArgs...)
110-
waitSession, err := gexec.Start(waitCommand, GinkgoWriter, GinkgoWriter)
111-
Expect(err).NotTo(HaveOccurred())
112-
Eventually(waitSession).WithTimeout(240 * time.Second).Should(gexec.Exit(0))
113-
114-
descArgs := []string{
115-
"describe",
116-
"job",
117-
"--namespace=chaincode",
118-
"-l",
119-
"fabric-builder-k8s-cclabel=CHAINCODE_LABEL",
120-
}
121-
descCommand := exec.Command("kubectl", descArgs...)
122-
descSession, err := gexec.Start(descCommand, GinkgoWriter, GinkgoWriter)
123-
Expect(err).NotTo(HaveOccurred())
124-
125-
Eventually(descSession).Should(gexec.Exit(0))
126-
Eventually(descSession.Out).Should(gbytes.Say(`Namespace:\s+chaincode`))
127-
Eventually(
128-
descSession.Out,
129-
).Should(gbytes.Say(`fabric-builder-k8s-ccid:\s+CHAINCODE_LABEL:6f98c4bb29414771312eddd1a813eef583df2121c235c4797792f141a46d4b45`))
130-
Eventually(descSession.Out).Should(gbytes.Say(`fabric-builder-k8s-mspid:\s+MSPID`))
131-
Eventually(descSession.Out).Should(gbytes.Say(`fabric-builder-k8s-peeraddress:\s+PEER_ADDRESS`))
132-
Eventually(descSession.Out).Should(gbytes.Say(`fabric-builder-k8s-peerid:\s+core-peer-id-abcdefghijklmnopqrstuvwxyz-0123456789`))
133-
Eventually(descSession.Out).Should(gbytes.Say(`CORE_CHAINCODE_ID_NAME:\s+CHAINCODE_LABEL:6f98c4bb29414771312eddd1a813eef583df2121c235c4797792f141a46d4b45`))
134-
Eventually(descSession.Out).Should(gbytes.Say(`CORE_PEER_ADDRESS:\s+PEER_ADDRESS`))
135-
Eventually(descSession.Out).Should(gbytes.Say(`CORE_PEER_LOCALMSPID:\s+MSPID`))
136-
},
137-
)
13867
})

0 commit comments

Comments
 (0)