-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathzboxcli_cancel_allocation_test.go
115 lines (89 loc) · 3.69 KB
/
zboxcli_cancel_allocation_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package cli_tests
import (
"fmt"
"regexp"
"strings"
"testing"
"time"
"github.com/0chain/system_test/internal/api/util/test"
"github.com/stretchr/testify/require"
cliutils "github.com/0chain/system_test/internal/cli/util"
)
var (
cancelAllocationRegex = regexp.MustCompile(`^Allocation canceled with txId : [a-f0-9]{64}$`)
)
func TestCancelAllocation(testSetup *testing.T) {
t := test.NewSystemTest(testSetup)
t.SetSmokeTests("Cancel allocation immediately should work")
t.Parallel()
t.Run("Cancel allocation immediately should work", func(t *test.SystemTest) {
allocationID := setupAllocation(t, configPath)
output, err := cancelAllocation(t, configPath, allocationID, true)
require.NoError(t, err, "cancel allocation failed but should succeed", strings.Join(output, "\n"))
require.Len(t, output, 1)
assertOutputMatchesAllocationRegex(t, cancelAllocationRegex, output[0])
})
t.RunWithTimeout("Cancel allocation after upload should work", 5*time.Minute, func(t *test.SystemTest) {
allocationID := setupAllocation(t, configPath)
filename := generateRandomTestFileName(t)
err := createFileWithSize(filename, 1*MB)
require.Nil(t, err)
output, err := uploadFile(t, configPath, map[string]interface{}{
"allocation": allocationID,
"remotepath": "/",
"localpath": filename,
}, true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 2)
time.Sleep(1 * time.Minute)
output, err = cancelAllocation(t, configPath, allocationID, true)
require.NoError(t, err, "cancel allocation failed but should succeed", strings.Join(output, "\n"))
require.Len(t, output, 1)
assertOutputMatchesAllocationRegex(t, cancelAllocationRegex, output[0])
})
t.Run("No allocation param should fail", func(t *test.SystemTest) {
// create wallet
createWallet(t)
cmd := fmt.Sprintf(
"./zbox alloc-cancel --silent "+
"--wallet %s --configDir ./config --config %s",
escapedTestName(t)+"_wallet.json",
configPath,
)
output, err := cliutils.RunCommandWithoutRetry(cmd)
require.Error(t, err, "expected error canceling allocation", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, "Error: allocation flag is missing", output[0])
})
t.Run("Cancel Other's Allocation Should Fail", func(t *test.SystemTest) {
otherAllocationID := setupAllocationWithWallet(t, escapedTestName(t)+"_other_wallet.json", configPath)
createWallet(t)
// otherAllocationID should not be cancelable from this level
output, err := cancelAllocation(t, configPath, otherAllocationID, false)
require.Error(t, err, "expected error canceling allocation", strings.Join(output, "\n"))
require.True(t, len(output) > 0, "expected output length be at least 1", strings.Join(output, "\n"))
require.Equal(t, "Error canceling allocation:alloc_cancel_failed: only owner can cancel an allocation", output[len(output)-1])
})
t.Run("Cancel Non-existent Allocation Should Fail", func(t *test.SystemTest) {
createWallet(t)
allocationID := "123abc"
output, err := cancelAllocation(t, configPath, allocationID, false)
require.Error(t, err, "expected error updating allocation", strings.Join(output, "\n"))
require.Equal(t, "Error canceling allocation:alloc_cancel_failed: value not present", output[0])
})
}
func cancelAllocation(t *test.SystemTest, cliConfigFilename, allocationID string, retry bool) ([]string, error) {
t.Logf("Canceling allocation...")
cmd := fmt.Sprintf(
"./zbox alloc-cancel --allocation %s --silent "+
"--wallet %s --configDir ./config --config %s",
allocationID,
escapedTestName(t)+"_wallet.json",
cliConfigFilename,
)
if retry {
return cliutils.RunCommand(t, cmd, 3, time.Second*2)
} else {
return cliutils.RunCommandWithoutRetry(cmd)
}
}