Skip to content

Commit

Permalink
Add tests for quorum package
Browse files Browse the repository at this point in the history
Signed-off-by: rodion <rodion.lim@partior.com>
  • Loading branch information
rodion-lim-partior committed Jul 2, 2024
1 parent 19cb418 commit 2a12af0
Show file tree
Hide file tree
Showing 11 changed files with 759 additions and 43 deletions.
5 changes: 5 additions & 0 deletions internal/blockchain/ethereum/connector/ethconnect/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package ethconnect
import (
"fmt"
"os"
"path/filepath"

"github.com/hyperledger/firefly-cli/internal/blockchain/ethereum/connector"
"github.com/hyperledger/firefly-cli/pkg/types"
Expand Down Expand Up @@ -58,6 +59,10 @@ type HTTP struct {

func (e *Config) WriteConfig(filename string, extraConnectorConfigPath string) error {
configYamlBytes, _ := yaml.Marshal(e)
basedir := filepath.Dir(filename)
if err := os.MkdirAll(basedir, 0755); err != nil {
return err
}
if err := os.WriteFile(filename, configYamlBytes, 0755); err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions internal/blockchain/ethereum/connector/evmconnect/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package evmconnect
import (
"fmt"
"os"
"path/filepath"

"github.com/hyperledger/firefly-cli/internal/blockchain/ethereum/connector"
"github.com/hyperledger/firefly-cli/pkg/types"
Expand Down Expand Up @@ -75,6 +76,10 @@ type GasOracleConfig struct {

func (e *Config) WriteConfig(filename string, extraEvmconnectConfigPath string) error {
configYamlBytes, _ := yaml.Marshal(e)
basedir := filepath.Dir(filename)
if err := os.MkdirAll(basedir, 0755); err != nil {
return err
}
if err := os.WriteFile(filename, configYamlBytes, 0755); err != nil {
return err
}
Expand Down
81 changes: 81 additions & 0 deletions internal/blockchain/ethereum/quorum/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package quorum

import (
"encoding/json"
"fmt"
"testing"

"github.com/hyperledger/firefly-cli/internal/utils"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)

func TestUnlockAccount(t *testing.T) {
tests := []struct {
Name string
RPCUrl string
Address string
Password string
StatusCode int
ApiResponse *JSONRPCResponse
}{
{
Name: "TestUnlockAccount-1",
RPCUrl: "http://127.0.0.1:8545",
Address: "user-1",
Password: "POST",
StatusCode: 200,
ApiResponse: &JSONRPCResponse{
JSONRPC: "2.0",
ID: 0,
Error: nil,
Result: "mock result",
},
},
{
Name: "TestUnlockAccountError-2",
RPCUrl: "http://127.0.0.1:8545",
Address: "user-1",
Password: "POST",
StatusCode: 200,
ApiResponse: &JSONRPCResponse{
JSONRPC: "2.0",
ID: 0,
Error: &JSONRPCError{500, "invalid account"},
Result: "mock result",
},
},
{
Name: "TestUnlockAccountHTTPError-3",
RPCUrl: "http://localhost:8545",
Address: "user-1",
Password: "POST",
StatusCode: 500,
ApiResponse: &JSONRPCResponse{
JSONRPC: "2.0",
ID: 0,
Error: nil,
Result: "mock result",
},
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
apiResponse, _ := json.Marshal(tc.ApiResponse)
// mockResponse
httpmock.RegisterResponder("POST", tc.RPCUrl,
httpmock.NewStringResponder(tc.StatusCode, string(apiResponse)))
client := NewQuorumClient(tc.RPCUrl)
utils.StartMockServer(t)
err := client.UnlockAccount(tc.Address, tc.Password)
utils.StopMockServer(t)

// expect errors when returned status code != 200 or ApiResponse comes back with non nil error
if tc.StatusCode != 200 || tc.ApiResponse.Error != nil {
assert.NotNil(t, err, "expects error to be returned when either quorum returns an application error or non 200 http response")
} else {
assert.NoError(t, err, fmt.Sprintf("unable to unlock account: %v", err))
}
})
}
}
5 changes: 5 additions & 0 deletions internal/blockchain/ethereum/quorum/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -107,6 +108,10 @@ func CreateGenesis(addresses []string, blockPeriod int, chainID int64) *Genesis

func (g *Genesis) WriteGenesisJSON(filename string) error {
genesisJSONBytes, _ := json.MarshalIndent(g, "", " ")
basedir := filepath.Dir(filename)
if err := os.MkdirAll(basedir, 0755); err != nil {
return err
}
if err := os.WriteFile(filename, genesisJSONBytes, 0755); err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ EOF
/tessera/bin/tessera -configfile ${DDIR}/tessera-config-09.json
`, TmTpPort, TmQ2tPort, TmP2pPort, peerList)
filename := filepath.Join(outputDirectory, DockerEntrypoint)
if err := os.MkdirAll(outputDirectory, 0755); err != nil {
return err
}
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package quorum

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/hyperledger/firefly-cli/internal/log"
"github.com/hyperledger/firefly-cli/pkg/types"
"github.com/stretchr/testify/assert"
)

func TestCreateTesseraKeys(t *testing.T) {
ctx := log.WithVerbosity(log.WithLogger(context.Background(), &log.StdoutLogger{}), false)
testCases := []struct {
Name string
Stack *types.Stack
TesseraImage string
KeysPrefix string
KeysName string
}{
{
Name: "testcase1",
Stack: &types.Stack{
Name: "Org-1_quorum",
InitDir: t.TempDir(),
},
TesseraImage: "quorumengineering/tessera:24.4",
KeysPrefix: "",
KeysName: "tm",
},
{
Name: "testcase2",
Stack: &types.Stack{
Name: "Org-1_quorum",
InitDir: t.TempDir(),
},
TesseraImage: "quorumengineering/tessera:24.4",
KeysPrefix: "xyz",
KeysName: "tm",
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
privateKey, publicKey, tesseraKeysPath, err := CreateTesseraKeys(ctx, tc.TesseraImage, filepath.Join(tc.Stack.InitDir, "tessera", "tessera_0", "keystore"), tc.KeysPrefix, tc.KeysName)
if err != nil {
t.Log("unable to create tessera keys", err)
}
//validate properties of tessera keys
assert.NotEmpty(t, privateKey)
assert.NotEmpty(t, publicKey)
assert.NotEmpty(t, tesseraKeysPath)

expectedOutputName := tc.KeysName
if tc.KeysPrefix != "" {
expectedOutputName = fmt.Sprintf("%s_%s", tc.KeysPrefix, expectedOutputName)
}
assert.Equal(t, tesseraKeysPath, filepath.Join(tc.Stack.InitDir, "tessera", "tessera_0", "keystore", expectedOutputName), "invalid output path")

assert.Nil(t, err)
})
}
}

func TestCreateTesseraEntrypoint(t *testing.T) {
ctx := log.WithVerbosity(log.WithLogger(context.Background(), &log.StdoutLogger{}), false)
testCases := []struct {
Name string
Stack *types.Stack
StackName string
MemberCount int
}{
{
Name: "testcase1",
Stack: &types.Stack{
Name: "Org-1_quorum",
InitDir: t.TempDir(),
},
StackName: "org1",
MemberCount: 4,
},
{
Name: "testcase2",
Stack: &types.Stack{
Name: "Org-2_quorum",
InitDir: t.TempDir(),
},
StackName: "org2",
MemberCount: 0,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
err := CreateTesseraEntrypoint(ctx, tc.Stack.InitDir, tc.StackName, tc.MemberCount)
if err != nil {
t.Log("unable to create tessera docker entrypoint", err)
}
path := filepath.Join(tc.Stack.InitDir, "docker-entrypoint.sh")
_, err = os.Stat(path)
assert.NoError(t, err, "docker entrypoint file not created")

b, err := os.ReadFile(path)
assert.NoError(t, err, "unable to read docker entrypoint file")
for i := 0; i < tc.MemberCount; i++ {
strings.Contains(string(b), fmt.Sprintf("member%dtessera", i))
}
})
}
}
Loading

0 comments on commit 2a12af0

Please sign in to comment.