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 1, 2024
1 parent 19cb418 commit c33fd98
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 3 deletions.
83 changes: 83 additions & 0 deletions internal/blockchain/ethereum/quorum/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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) {
utils.StartMockServer(t)

tests := []struct {
Name string
RPCUrl string
Address string
Password string
StatusCode int
ApiResponse *JSONRPCResponse
}{
{
Name: "TestUnlockAccount-1",
RPCUrl: "http://localhost: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://localhost: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)
err := client.UnlockAccount(tc.Address, tc.Password)

// 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))
}

})
}
utils.StopMockServer(t)
}
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))
}
})
}
}
6 changes: 3 additions & 3 deletions internal/blockchain/ethereum/quorum/quorum_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,10 @@ func TestCreateAccount(t *testing.T) {
InitDir: t.TempDir(),
RuntimeDir: t.TempDir(),
},
Args: []string{"Org-1_quorum", "Org-1_quorum", "0", "1"},
Args: []string{"Org-1_quorum", "Org-1_quorum", "0"},
},
{
Name: "testcase1",
Name: "testcase2",
Ctx: ctx,
Stack: &types.Stack{
Name: "Org-2_quorum",
Expand All @@ -315,7 +315,7 @@ func TestCreateAccount(t *testing.T) {
InitDir: t.TempDir(),
RuntimeDir: t.TempDir(),
},
Args: []string{"Org-2_quorum", "Org-2_quorum", "1", "2"},
Args: []string{"Org-2_quorum", "Org-2_quorum", "1"},
},
}
for _, tc := range testcases {
Expand Down
72 changes: 72 additions & 0 deletions internal/blockchain/ethereum/quorum/quorum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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 TestCreateQuorumEntrypoint(t *testing.T) {
ctx := log.WithVerbosity(log.WithLogger(context.Background(), &log.StdoutLogger{}), false)
testCases := []struct {
Name string
Stack *types.Stack
Consensus string
StackName string
MemberIndex int
ChainID int
BlockPeriodInSeconds int
TesseraEnabled bool
}{
{
Name: "testcase1",
Stack: &types.Stack{
Name: "Org-1_quorum",
InitDir: t.TempDir(),
},
Consensus: "ibft",
StackName: "org1",
MemberIndex: 0,
ChainID: 1337,
BlockPeriodInSeconds: -1,
TesseraEnabled: true,
},
{
Name: "testcase2",
Stack: &types.Stack{
Name: "Org-2_quorum",
InitDir: t.TempDir(),
},
Consensus: "clique",
StackName: "org2",
MemberIndex: 1,
ChainID: 1337,
BlockPeriodInSeconds: 3,
TesseraEnabled: false,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
err := CreateQuorumEntrypoint(ctx, tc.Stack.InitDir, tc.Consensus, tc.StackName, tc.MemberIndex, tc.ChainID, tc.BlockPeriodInSeconds, tc.TesseraEnabled)
if err != nil {
t.Log("unable to create quorum 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")
output := string(b)
strings.Contains(output, fmt.Sprintf("member%dtessera", tc.MemberIndex))
strings.Contains(output, fmt.Sprintf("GOQUORUM_CONS_ALGO=%s", tc.Consensus))
})
}
}

0 comments on commit c33fd98

Please sign in to comment.