-
Notifications
You must be signed in to change notification settings - Fork 993
/
Copy pathcmd_test.go
200 lines (180 loc) · 5.47 KB
/
cmd_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"bytes"
"context"
"io"
"os"
"reflect"
"testing"
"github.com/cristalhq/jwt/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/celestiaorg/celestia-node/api/rpc/perms"
"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/libs/authtoken"
"github.com/celestiaorg/celestia-node/libs/keystore"
nodemod "github.com/celestiaorg/celestia-node/nodebuilder/node"
)
func TestCompletionHelpString(t *testing.T) {
type TestFields struct {
NoInputOneOutput func(context.Context) (*header.ExtendedHeader, error)
TwoInputsOneOutputArray func(
context.Context,
*header.ExtendedHeader,
uint64,
) ([]*header.ExtendedHeader, error)
OneInputOneOutput func(context.Context, uint64) (*header.ExtendedHeader, error)
NoInputsNoOutputs func(ctx context.Context) error
NoInputsChanOutput func(ctx context.Context) (<-chan *header.ExtendedHeader, error)
}
testOutputs := []string{
"() -> (*header.ExtendedHeader)",
"(*header.ExtendedHeader, uint64) -> ([]*header.ExtendedHeader)",
"(uint64) -> (*header.ExtendedHeader)",
"() -> ()",
"() -> (<-chan *header.ExtendedHeader)",
}
methods := reflect.VisibleFields(reflect.TypeOf(TestFields{}))
for i, method := range methods {
require.Equal(t, testOutputs[i], parseSignatureForHelpString(method))
}
}
func TestLight(t *testing.T) {
// Run the tests in a temporary directory
tmpDir := t.TempDir()
testDir, err := os.Getwd()
require.NoError(t, err, "error getting the current working directory")
err = os.Chdir(tmpDir)
require.NoError(t, err, "error changing to the temporary test directory")
t.Run("init", func(t *testing.T) {
output := &bytes.Buffer{}
rootCmd.SetOut(output)
rootCmd.SetArgs([]string{
"light",
"--node.store", ".celestia-light",
"init",
})
err := rootCmd.ExecuteContext(context.Background())
require.NoError(t, err)
})
t.Cleanup(func() {
if err := os.Chdir(testDir); err != nil {
t.Error("error resetting:", err)
}
})
// TODO @jbowen93: Commented out until a dry-run option can be implemented
/*
t.Run("start", func(t *testing.T) {
output := &bytes.Buffer{}
rootCmd.SetOut(output)
rootCmd.SetArgs([]string{
"light",
"--node.store", ".celestia-light",
"start",
"--headers.trusted-peer",
"/ip4/192.167.10.6/tcp/2121/p2p/12D3KooWL8z3KARAYJcmExhDsGwKbjChKeGaJpFPENyADdxmEHzw",
"--headers.trusted-hash",
"54A8B66D2BEF13850D67C8D474E196BD7485FE5A79989E31B17169371B0A9C96",
})
err := rootCmd.ExecuteContext(cmdnode.WithEnv(context.Background()))
require.NoError(t, err)
})
*/
}
func TestBridge(t *testing.T) {
// Run the tests in a temporary directory
tmpDir := t.TempDir()
testDir, err := os.Getwd()
require.NoError(t, err, "error getting the current working directory")
err = os.Chdir(tmpDir)
require.NoError(t, err, "error changing to the temporary test directory")
t.Run("init", func(t *testing.T) {
output := &bytes.Buffer{}
rootCmd.SetOut(output)
rootCmd.SetArgs([]string{
"bridge",
"--node.store", ".celestia-bridge",
"init",
})
err := rootCmd.ExecuteContext(context.Background())
require.NoError(t, err)
})
// tests that auth admin token generated by node via CLI
// can be verified + contains expected perms
t.Run("auth", func(t *testing.T) {
rootCmd.SetOut(&bytes.Buffer{})
rootCmd.SetArgs([]string{
"bridge",
"--node.store", ".celestia-bridge",
"init",
})
err := rootCmd.ExecuteContext(context.Background())
require.NoError(t, err)
ogStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
rootCmd.SetArgs([]string{
"bridge",
"auth", "admin",
"--node.store", ".celestia-bridge",
})
err = rootCmd.ExecuteContext(context.Background())
require.NoError(t, err)
err = w.Close()
require.NoError(t, err)
os.Stdout = ogStdout
ks, err := keystore.NewFSKeystore(".celestia-bridge/keys", nil)
require.NoError(t, err)
key, err := ks.Get(nodemod.SecretName)
require.NoError(t, err)
verifier, err := jwt.NewVerifierHS(jwt.HS256, key.Body)
require.NoError(t, err)
token, err := io.ReadAll(r)
require.NoError(t, err)
permissions, err := authtoken.ExtractSignedPermissions(verifier, string(token))
require.NoError(t, err)
assert.Equal(t, permissions, perms.AllPerms)
})
t.Cleanup(func() {
if err := os.Chdir(testDir); err != nil {
t.Error("error resetting:", err)
}
})
// TODO @jbowen93: Commented out until a dry-run option can be implemented
/*
t.Run("start", func(t *testing.T) {
output := &bytes.Buffer{}
rootCmd.SetOut(output)
rootCmd.SetArgs([]string{
"bridge",
"--node.store", ".celestia-bridge",
"start",
"--core.remote",
"tcp://192.167.10.2:26657",
"--headers.trusted-hash",
"54A8B66D2BEF13850D67C8D474E196BD7485FE5A79989E31B17169371B0A9C96",
})
err := rootCmd.ExecuteContext(cmdnode.WithEnv(context.Background()))
require.NoError(t, err)
})
*/
}
func parseSignatureForHelpString(methodSig reflect.StructField) string {
simplifiedSignature := "("
in, out := methodSig.Type.NumIn(), methodSig.Type.NumOut()
for i := 1; i < in; i++ {
simplifiedSignature += methodSig.Type.In(i).String()
if i != in-1 {
simplifiedSignature += ", "
}
}
simplifiedSignature += ") -> ("
for i := 0; i < out-1; i++ {
simplifiedSignature += methodSig.Type.Out(i).String()
if i != out-2 {
simplifiedSignature += ", "
}
}
simplifiedSignature += ")"
return simplifiedSignature
}