-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: rodion <rodion.lim@partior.com>
- Loading branch information
1 parent
19cb418
commit c33fd98
Showing
4 changed files
with
270 additions
and
3 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,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) | ||
} |
112 changes: 112 additions & 0 deletions
112
internal/blockchain/ethereum/quorum/private_transaction_manager_test.go
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,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)) | ||
} | ||
}) | ||
} | ||
} |
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,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)) | ||
}) | ||
} | ||
} |