forked from akash-network/provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostname_test.go
220 lines (173 loc) · 6.73 KB
/
hostname_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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package cluster
import (
"context"
"errors"
"github.com/akash-network/node/testutil"
"github.com/stretchr/testify/require"
"testing"
"time"
)
type scaffold struct {
service *hostnameService
ctx context.Context
cancel context.CancelFunc
}
const testWait = time.Second * time.Duration(15)
func makeHostnameScaffold(t *testing.T, blockedHostnames []string) *scaffold {
// Create a context with no more than 15 seconds of wait here. Tests should not
// take that long to run
ctx, cancel := context.WithTimeout(context.Background(), testWait)
svc, err := newHostnameService(ctx, Config{BlockedHostnames: blockedHostnames}, nil)
require.NoError(t, err)
v := &scaffold{
service: svc,
ctx: ctx,
cancel: cancel,
}
return v
}
func TestBlockedHostname(t *testing.T) {
s := makeHostnameScaffold(t, []string{"foobar.com", "bobsdefi.com"})
ownerAddr := testutil.AccAddress(t)
err := s.service.CanReserveHostnames([]string{"foobar.com", "other.org"}, ownerAddr)
require.Error(t, err)
require.True(t, errors.Is(err, ErrHostnameNotAllowed))
require.Regexp(t, "^.*blocked by this provider.*$", err.Error())
s.cancel()
select {
case <-s.service.lc.Done():
case <-time.After(testWait):
t.Fatal("timed out waiting for service shutdown")
}
}
func TestBlockedDomain(t *testing.T) {
s := makeHostnameScaffold(t, []string{"foobar.com", ".bobsdefi.com"})
ownerAddr := testutil.AccAddress(t)
err := s.service.CanReserveHostnames([]string{"accounts.bobsdefi.com"}, ownerAddr)
require.Error(t, err)
require.True(t, errors.Is(err, ErrHostnameNotAllowed))
require.Regexp(t, "^.*blocked by this provider.*$", err.Error())
s.cancel()
select {
case <-s.service.lc.Done():
case <-time.After(testWait):
t.Fatal("timed out waiting for service shutdown")
}
}
func TestReserveMoreHostnamesSameDeployment(t *testing.T) {
s := makeHostnameScaffold(t, []string{"foobar.com", ".bobsdefi.com"})
leaseID := testutil.LeaseID(t)
result, err := s.service.ReserveHostnames(s.ctx, []string{"meow.com", "kittens.com"}, leaseID)
require.NoError(t, err)
require.Len(t, result, 0)
result, err = s.service.ReserveHostnames(s.ctx, []string{"kittens.com", "meow.com", "cats.com"}, leaseID)
require.NoError(t, err)
require.Len(t, result, 0) // Not withheld because it's the same lease
secondLeaseID := testutil.LeaseID(t)
secondLeaseID.Owner = leaseID.Owner
result, err = s.service.ReserveHostnames(s.ctx, []string{"dogs.com", "meow.com", "ferrets.com"}, secondLeaseID)
require.NoError(t, err)
require.Len(t, result, 1) // Withheld because it's the same owner but a different lease
require.Equal(t, result[0], "meow.com")
s.cancel()
select {
case <-s.service.lc.Done():
case <-time.After(testWait):
t.Fatal("timed out waiting for service shutdown")
}
}
func TestReserveAndReleaseDomain(t *testing.T) {
s := makeHostnameScaffold(t, []string{"foobar.com", ".bobsdefi.com"})
leaseID := testutil.LeaseID(t)
result, err := s.service.ReserveHostnames(s.ctx, []string{"meow.com", "kittens.com"}, leaseID)
require.NoError(t, err)
require.Len(t, result, 0)
secondLeaseID := testutil.LeaseID(t)
result, err = s.service.ReserveHostnames(s.ctx, []string{"KITTENS.com"}, secondLeaseID)
require.Error(t, err)
require.True(t, errors.Is(err, ErrHostnameNotAllowed))
require.Nil(t, result)
err = s.service.ReleaseHostnames(leaseID)
require.NoError(t, err)
result, err = s.service.ReserveHostnames(s.ctx, []string{"KITTENS.com"}, secondLeaseID)
require.NoError(t, err)
require.Len(t, result, 0)
s.cancel()
select {
case <-s.service.lc.Done():
case <-time.After(testWait):
t.Fatal("timed out waiting for service shutdown")
}
}
func TestReserveAndReserve(t *testing.T) {
s := makeHostnameScaffold(t, []string{"foobar.com", ".bobsdefi.com"})
leaseID := testutil.LeaseID(t)
result, err := s.service.ReserveHostnames(s.ctx, []string{"meow.com", "kittens.com"}, leaseID)
require.NoError(t, err)
require.Len(t, result, 0)
secondLeaseID := testutil.LeaseID(t)
result, err = s.service.ReserveHostnames(s.ctx, []string{"kittens.com"}, secondLeaseID)
require.Error(t, err)
require.True(t, errors.Is(err, ErrHostnameNotAllowed))
require.Nil(t, result)
// The first deployment changes. It is no longer using the hostname 'kittens.com'
// so it gets dropped
result, err = s.service.ReserveHostnames(s.ctx, []string{"meow.com"}, leaseID)
require.NoError(t, err)
require.Len(t, result, 0)
result, err = s.service.ReserveHostnames(s.ctx, []string{"KITTENS.com"}, secondLeaseID)
require.NoError(t, err)
require.Len(t, result, 0)
s.cancel()
select {
case <-s.service.lc.Done():
case <-time.After(testWait):
t.Fatal("timed out waiting for service shutdown")
}
}
func TestPrepareHostnamesForTransfer(t *testing.T) {
s := makeHostnameScaffold(t, []string{"challenger.com"})
defer s.cancel()
leaseID := testutil.LeaseID(t)
result, err := s.service.ReserveHostnames(s.ctx, []string{"meow.com", "kittens.org"}, leaseID)
require.NoError(t, err)
require.Len(t, result, 0)
secondLeaseID := testutil.LeaseID(t)
secondLeaseID.Owner = leaseID.Owner // Same owner, different leases
err = s.service.PrepareHostnamesForTransfer(s.ctx, []string{"kittens.org"}, secondLeaseID)
require.NoError(t, err)
}
func TestPrepareHostnamesForTransferSameLease(t *testing.T) {
s := makeHostnameScaffold(t, []string{"challenger.com"})
defer s.cancel()
leaseID := testutil.LeaseID(t)
result, err := s.service.ReserveHostnames(s.ctx, []string{"meow.com", "kittens.org"}, leaseID)
require.NoError(t, err)
require.Len(t, result, 0)
err = s.service.PrepareHostnamesForTransfer(s.ctx, []string{"kittens.org"}, leaseID) // Same lease
require.NoError(t, err)
}
func TestPrepareHostnamesForTransferDifferentOwner(t *testing.T) {
s := makeHostnameScaffold(t, []string{"challenger.com"})
defer s.cancel()
leaseID := testutil.LeaseID(t)
result, err := s.service.ReserveHostnames(s.ctx, []string{"meow.com", "kittens.org"}, leaseID)
require.NoError(t, err)
require.Len(t, result, 0)
secondLeaseID := testutil.LeaseID(t) // Different owner
err = s.service.PrepareHostnamesForTransfer(s.ctx, []string{"kittens.org"}, secondLeaseID)
require.Error(t, err)
require.Regexp(t, `^.*host "kittens.org" in use.*$`, err)
}
func TestPrepareHostnamesForTransferNotReserved(t *testing.T) {
s := makeHostnameScaffold(t, []string{"challenger.com"})
defer s.cancel()
leaseID := testutil.LeaseID(t)
result, err := s.service.ReserveHostnames(s.ctx, []string{"meow.com", "kittens.org"}, leaseID)
require.NoError(t, err)
require.Len(t, result, 0)
secondLeaseID := testutil.LeaseID(t)
secondLeaseID.Owner = leaseID.Owner // Same owner, different leases
err = s.service.PrepareHostnamesForTransfer(s.ctx, []string{"pets.com"}, secondLeaseID) // unreserved hostname
require.NoError(t, err)
}