forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_test.go
169 lines (139 loc) · 4.53 KB
/
service_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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package admin
import (
"net/http"
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms/registry/registrymock"
"github.com/ava-labs/avalanchego/vms/vmsmock"
rpcdbpb "github.com/ava-labs/avalanchego/proto/pb/rpcdb"
)
type loadVMsTest struct {
admin *Admin
ctrl *gomock.Controller
mockVMManager *vmsmock.Manager
mockVMRegistry *registrymock.VMRegistry
}
func initLoadVMsTest(t *testing.T) *loadVMsTest {
ctrl := gomock.NewController(t)
mockVMRegistry := registrymock.NewVMRegistry(ctrl)
mockVMManager := vmsmock.NewManager(ctrl)
return &loadVMsTest{
admin: &Admin{Config: Config{
Log: logging.NoLog{},
VMRegistry: mockVMRegistry,
VMManager: mockVMManager,
}},
ctrl: ctrl,
mockVMManager: mockVMManager,
mockVMRegistry: mockVMRegistry,
}
}
// Tests behavior for LoadVMs if everything succeeds.
func TestLoadVMsSuccess(t *testing.T) {
require := require.New(t)
resources := initLoadVMsTest(t)
id1 := ids.GenerateTestID()
id2 := ids.GenerateTestID()
newVMs := []ids.ID{id1, id2}
failedVMs := map[ids.ID]error{
ids.GenerateTestID(): errTest,
}
// every vm is at least aliased to itself.
alias1 := []string{id1.String(), "vm1-alias-1", "vm1-alias-2"}
alias2 := []string{id2.String(), "vm2-alias-1", "vm2-alias-2"}
// we expect that we dedup the redundant alias of vmId.
expectedVMRegistry := map[ids.ID][]string{
id1: alias1[1:],
id2: alias2[1:],
}
resources.mockVMRegistry.EXPECT().Reload(gomock.Any()).Times(1).Return(newVMs, failedVMs, nil)
resources.mockVMManager.EXPECT().Aliases(id1).Times(1).Return(alias1, nil)
resources.mockVMManager.EXPECT().Aliases(id2).Times(1).Return(alias2, nil)
// execute test
reply := LoadVMsReply{}
require.NoError(resources.admin.LoadVMs(&http.Request{}, nil, &reply))
require.Equal(expectedVMRegistry, reply.NewVMs)
}
// Tests behavior for LoadVMs if we fail to reload vms.
func TestLoadVMsReloadFails(t *testing.T) {
require := require.New(t)
resources := initLoadVMsTest(t)
// Reload fails
resources.mockVMRegistry.EXPECT().Reload(gomock.Any()).Times(1).Return(nil, nil, errTest)
reply := LoadVMsReply{}
err := resources.admin.LoadVMs(&http.Request{}, nil, &reply)
require.ErrorIs(err, errTest)
}
// Tests behavior for LoadVMs if we fail to fetch our aliases
func TestLoadVMsGetAliasesFails(t *testing.T) {
require := require.New(t)
resources := initLoadVMsTest(t)
id1 := ids.GenerateTestID()
id2 := ids.GenerateTestID()
newVMs := []ids.ID{id1, id2}
failedVMs := map[ids.ID]error{
ids.GenerateTestID(): errTest,
}
// every vm is at least aliased to itself.
alias1 := []string{id1.String(), "vm1-alias-1", "vm1-alias-2"}
resources.mockVMRegistry.EXPECT().Reload(gomock.Any()).Times(1).Return(newVMs, failedVMs, nil)
resources.mockVMManager.EXPECT().Aliases(id1).Times(1).Return(alias1, nil)
resources.mockVMManager.EXPECT().Aliases(id2).Times(1).Return(nil, errTest)
reply := LoadVMsReply{}
err := resources.admin.LoadVMs(&http.Request{}, nil, &reply)
require.ErrorIs(err, errTest)
}
func TestServiceDBGet(t *testing.T) {
a := &Admin{Config: Config{
Log: logging.NoLog{},
DB: memdb.New(),
}}
helloBytes := []byte("hello")
helloHex, err := formatting.Encode(formatting.HexNC, helloBytes)
require.NoError(t, err)
worldBytes := []byte("world")
worldHex, err := formatting.Encode(formatting.HexNC, worldBytes)
require.NoError(t, err)
require.NoError(t, a.DB.Put(helloBytes, worldBytes))
tests := []struct {
name string
key string
expectedValue string
expectedErrorCode rpcdbpb.Error
}{
{
name: "key exists",
key: helloHex,
expectedValue: worldHex,
expectedErrorCode: rpcdbpb.Error_ERROR_UNSPECIFIED,
},
{
name: "key doesn't exist",
key: "",
expectedValue: "",
expectedErrorCode: rpcdbpb.Error_ERROR_NOT_FOUND,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require := require.New(t)
reply := &DBGetReply{}
require.NoError(a.DbGet(
nil,
&DBGetArgs{
Key: test.key,
},
reply,
))
require.Equal(test.expectedValue, reply.Value)
require.Equal(test.expectedErrorCode, reply.ErrorCode)
})
}
}