-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathzzwalletcli_kill_sharder_test.go
84 lines (69 loc) · 2.66 KB
/
zzwalletcli_kill_sharder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package cli_tests
import (
"fmt"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/0chain/system_test/internal/api/util/test"
cliutil "github.com/0chain/system_test/internal/cli/util"
cliutils "github.com/0chain/system_test/internal/cli/util"
)
const (
minShardersForKillSharderTest = 2
)
func TestKillSharder(testSetup *testing.T) { // nolint:gocyclo // team preference is to have codes all within test.
t := test.NewSystemTest(testSetup)
createWallet(t)
sharderUrl := getSharderUrl(t)
startSharders := getNodeSlice(t, "getSharderList", sharderUrl)
if len(startSharders) < minShardersForKillSharderTest {
t.Skipf("not enough sharders in blockchain, found %d need %d", len(startSharders), minShardersForKillSharderTest)
}
var sharderToKill string
for i := range startSharders {
if !startSharders[i].IsKilled {
sharderToKill = startSharders[i].ID
break
}
}
if sharderToKill == "" {
t.Skip("all sharders in the blockchain have been killed")
}
t.RunSequentially("kill sharder by non-smartcontract owner should fail", func(t *test.SystemTest) {
createWallet(t)
output, err := killSharder(t, escapedTestName(t), configPath, createParams(map[string]interface{}{
"id": sharderToKill,
}), true)
require.Error(t, err, "kill sharder by non-smartcontract owner should fail")
require.Len(t, output, 1)
require.True(t, strings.Contains(output[0], "unauthorized access - only the owner can access"), "")
})
t.RunSequentially("Killed sharder does not receive rewards", func(t *test.SystemTest) {
createWallet(t)
output, err := killSharder(t, scOwnerWallet, configPath, createParams(map[string]interface{}{
"id": sharderToKill,
}), true)
require.NoError(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)
cliutil.Wait(t, time.Second)
sharderAfterKill := getMinersDetail(t, sharderToKill)
require.True(t, sharderAfterKill.IsKilled, "sharder should be killed")
// ------------------------------------
cliutil.Wait(t, 10*time.Second)
// ------------------------------------
sharderAfterRewardTest := getMinersDetail(t, sharderToKill)
require.Equalf(t, sharderAfterKill.TotalReward, sharderAfterRewardTest.TotalReward,
"killed sharder %s should not receive any more rewards", sharderToKill)
})
}
func killSharder(t *test.SystemTest, wallet, cliConfigFilename, params string, retry bool) ([]string, error) {
t.Log("kill sharder...")
cmd := fmt.Sprintf("./zwallet sh-kill %s --silent --wallet %s_wallet.json --configDir ./config --config %s",
params, wallet, cliConfigFilename)
if retry {
return cliutils.RunCommand(t, cmd, 3, time.Second*2)
} else {
return cliutils.RunCommandWithoutRetry(cmd)
}
}