Skip to content

Commit c9ea8c0

Browse files
committed
test: add unit tests for the cardano package
1 parent a5bcb32 commit c9ea8c0

File tree

3 files changed

+545
-0
lines changed

3 files changed

+545
-0
lines changed
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
package cardanocli
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"errors"
7+
"os"
8+
"path/filepath"
9+
"strconv"
10+
"testing"
11+
"time"
12+
13+
"github.com/blockfrost/blockfrost-go"
14+
blockfrostmocks "github.com/kilnfi/cardano-validator-watcher/internal/blockfrost/mocks"
15+
"github.com/kilnfi/cardano-validator-watcher/internal/cardano"
16+
mocks "github.com/kilnfi/cardano-validator-watcher/internal/cardano/cardanocli/mocks"
17+
"github.com/kilnfi/cardano-validator-watcher/internal/pools"
18+
"github.com/stretchr/testify/assert"
19+
"github.com/stretchr/testify/mock"
20+
"github.com/stretchr/testify/require"
21+
)
22+
23+
func TestPing(t *testing.T) {
24+
t.Run("GoodPath_PingOK", func(t *testing.T) {
25+
clientopts := ClientOptions{
26+
Network: "preprod",
27+
SocketPath: "/tmp/cardano.socket",
28+
}
29+
30+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
31+
defer cancel()
32+
33+
exec := &mocks.MockCommandExecutor{}
34+
exec.EXPECT().ExecCommand(
35+
ctx,
36+
mock.Anything,
37+
"cardano-cli",
38+
"ping",
39+
"-u",
40+
clientopts.SocketPath,
41+
"-t",
42+
"-c",
43+
"1",
44+
"-m",
45+
"1",
46+
).Return(nil, nil)
47+
48+
client := NewClient(clientopts, nil, exec)
49+
err := client.Ping(ctx)
50+
require.NoError(t, err)
51+
})
52+
53+
t.Run("SadPath_PingKO", func(t *testing.T) {
54+
clientopts := ClientOptions{
55+
Network: "preprod",
56+
SocketPath: "/tmp/cardano.socket",
57+
}
58+
59+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
60+
defer cancel()
61+
62+
exec := &mocks.MockCommandExecutor{}
63+
exec.EXPECT().ExecCommand(
64+
ctx,
65+
mock.Anything,
66+
"cardano-cli",
67+
"ping",
68+
"-u",
69+
clientopts.SocketPath,
70+
"-t",
71+
"-c",
72+
"1",
73+
"-m",
74+
"1",
75+
).Return(nil, errors.New("connection refused"))
76+
77+
client := NewClient(clientopts, nil, exec)
78+
err := client.Ping(ctx)
79+
assert.Equal(t, "unable to ping cardano RPC node: connection refused", err.Error())
80+
})
81+
}
82+
83+
func TestStakeSnapshot(t *testing.T) {
84+
clientopts := ClientOptions{
85+
Network: "preprod",
86+
SocketPath: "/tmp/cardano.socket",
87+
}
88+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
89+
defer cancel()
90+
91+
exec := &mocks.MockCommandExecutor{}
92+
expected := cardano.ClientQueryStakeSnapshotResponse{
93+
Pools: map[string]cardano.PoolStakeInfo{
94+
"pool-0": {
95+
StakeGo: 100,
96+
StakeMark: 100,
97+
StakeSet: 100,
98+
},
99+
},
100+
Total: cardano.TotalStakeInfo{
101+
StakeGo: 200,
102+
StakeMark: 200,
103+
StakeSet: 200,
104+
},
105+
}
106+
expectedByte, err := json.Marshal(expected)
107+
require.NoError(t, err)
108+
exec.EXPECT().ExecCommand(
109+
ctx,
110+
mock.Anything,
111+
"cardano-cli",
112+
"query",
113+
"stake-snapshot",
114+
"--stake-pool-id",
115+
"pool-0",
116+
"--socket-path",
117+
clientopts.SocketPath,
118+
"--testnet-magic",
119+
"1",
120+
).Return(expectedByte, nil)
121+
122+
client := NewClient(clientopts, nil, exec)
123+
response, err := client.StakeSnapshot(ctx, "pool-0")
124+
require.NoError(t, err)
125+
assert.Equal(t, expected, response)
126+
}
127+
128+
func TestLeaderLogs(t *testing.T) {
129+
pool := pools.Pool{
130+
Instance: "pool-0",
131+
ID: "pool-0",
132+
Name: "pool-0",
133+
Key: "pool-0.vrf.skey",
134+
}
135+
clientopts := ClientOptions{
136+
Network: "preprod",
137+
SocketPath: "/tmp/cardano.socket",
138+
DBPath: "db.db",
139+
Timezone: "UTC",
140+
}
141+
bf := blockfrostmocks.NewMockClient(t)
142+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
143+
defer cancel()
144+
145+
// Temporary file
146+
dir, err := os.MkdirTemp("", "")
147+
if err != nil {
148+
t.Fatal(err)
149+
}
150+
clientopts.ConfigDir = dir
151+
byronGenesis, _ := os.Create(filepath.Join(clientopts.ConfigDir, "byron.json"))
152+
shelleyGenesis, _ := os.Create(filepath.Join(clientopts.ConfigDir, "shelley.json"))
153+
vrf, _ := os.Create("pool-0.vrf.skey")
154+
defer func() {
155+
os.RemoveAll(clientopts.ConfigDir)
156+
os.Remove(vrf.Name())
157+
}()
158+
159+
bf.EXPECT().GetPoolInfo(ctx, "pool-0").Return(blockfrost.Pool{
160+
PoolID: "pool-0",
161+
Hex: "pool-0-hex",
162+
}, nil)
163+
164+
expected := cardano.ClientQueryStakeSnapshotResponse{
165+
Pools: map[string]cardano.PoolStakeInfo{
166+
"pool-0": {
167+
StakeGo: 100,
168+
StakeMark: 100,
169+
StakeSet: 100,
170+
},
171+
},
172+
Total: cardano.TotalStakeInfo{
173+
StakeGo: 200,
174+
StakeMark: 200,
175+
StakeSet: 200,
176+
},
177+
}
178+
expectedByte, err := json.Marshal(expected)
179+
require.NoError(t, err)
180+
181+
exec := &mocks.MockCommandExecutor{}
182+
exec.EXPECT().ExecCommand(
183+
ctx,
184+
mock.Anything,
185+
"cardano-cli",
186+
"query",
187+
"stake-snapshot",
188+
"--stake-pool-id",
189+
"pool-0",
190+
"--socket-path",
191+
clientopts.SocketPath,
192+
"--testnet-magic",
193+
"1",
194+
).Return(expectedByte, nil)
195+
196+
expectedOutput := cardano.ClientLeaderLogsResponse{
197+
Status: "ok",
198+
Epoch: 100,
199+
EpochNonce: "nonce",
200+
EpochSlots: 10,
201+
PoolID: "pool-0",
202+
AssignedSlots: []cardano.SlotSchedule{
203+
{
204+
No: 1,
205+
Slot: 2,
206+
SlotInEpoch: 2,
207+
},
208+
},
209+
}
210+
expectedOutputByte, err := json.Marshal(expectedOutput)
211+
require.NoError(t, err)
212+
exec.EXPECT().ExecCommand(
213+
ctx,
214+
mock.Anything,
215+
"cncli",
216+
"leaderlog",
217+
"--byron-genesis",
218+
byronGenesis.Name(),
219+
"--shelley-genesis",
220+
shelleyGenesis.Name(),
221+
"--ledger-set",
222+
"current",
223+
"--nonce",
224+
"nonce",
225+
"--pool-id",
226+
pool.ID,
227+
"--pool-vrf-skey",
228+
vrf.Name(),
229+
"--tz",
230+
clientopts.Timezone,
231+
"--db",
232+
clientopts.DBPath,
233+
"--pool-stake",
234+
strconv.Itoa(expected.Pools["pool-0-hex"].StakeSet),
235+
"--active-stake",
236+
strconv.Itoa(expected.Total.StakeSet),
237+
).Return(expectedOutputByte, nil)
238+
239+
client := NewClient(clientopts, bf, exec)
240+
err = client.LeaderLogs(ctx, "current", "nonce", pool)
241+
require.NoError(t, err)
242+
}

internal/cardano/cardanocli/mocks/mock_CommandExecutor.go

Lines changed: 111 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)