/
cmd_forget_satellite_test.go
161 lines (142 loc) · 4.64 KB
/
cmd_forget_satellite_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
// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"bytes"
"context"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/zeebo/errs"
"go.uber.org/zap/zaptest"
"storj.io/common/memory"
"storj.io/common/testcontext"
"storj.io/common/testrand"
"storj.io/storj/private/testplanet"
"storj.io/storj/storagenode/blobstore"
"storj.io/storj/storagenode/reputation"
"storj.io/storj/storagenode/satellites"
)
func Test_newForgetSatelliteCmd_Error(t *testing.T) {
tests := []struct {
name string
args string
wantErr string
}{
{
name: "no args",
args: "",
wantErr: "must specify either satellite ID(s) as arguments or --all-untrusted flag",
},
{
name: "Both satellite ID and --all-untrusted flag specified",
args: "--all-untrusted 1234567890123456789012345678901234567890123456789012345678901234",
wantErr: "cannot specify both satellite IDs and --all-untrusted",
},
{
name: "--all-untrusted and --force specified",
args: "--all-untrusted --force",
wantErr: "cannot specify both --all-untrusted and --force",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := newForgetSatelliteCmd(&Factory{})
cmd.SetArgs(strings.Fields(tt.args))
err := cmd.ExecuteContext(testcontext.New(t))
if tt.wantErr == "" {
require.NoError(t, err)
return
}
require.Equal(t, tt.wantErr, err.Error())
})
}
}
func Test_cmdForgetSatellite(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 2, StorageNodeCount: 1, UplinkCount: 0,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
// pause the forget satellite chore
planet.StorageNodes[0].ForgetSatellite.Chore.Loop.Pause()
address := planet.StorageNodes[0].Server.PrivateAddr().String()
log := zaptest.NewLogger(t)
store := planet.StorageNodes[0].Storage2.BlobsCache
defer ctx.Check(store.Close)
satellite := planet.Satellites[0]
blobSize := memory.KB
blobRef := blobstore.BlobRef{
Namespace: satellite.ID().Bytes(),
Key: testrand.PieceID().Bytes(),
}
w, err := store.Create(ctx, blobRef, -1)
require.NoError(t, err)
_, err = w.Write(testrand.Bytes(blobSize))
require.NoError(t, err)
require.NoError(t, w.Commit(ctx))
// create a new satellite reputation
timestamp := time.Now().UTC()
reputationDB := planet.StorageNodes[0].DB.Reputation()
stats := reputation.Stats{
SatelliteID: satellite.ID(),
Audit: reputation.Metric{
TotalCount: 6,
SuccessCount: 7,
Alpha: 8,
Beta: 9,
Score: 10,
UnknownAlpha: 11,
UnknownBeta: 12,
UnknownScore: 13,
},
OnlineScore: 14,
UpdatedAt: timestamp,
JoinedAt: timestamp,
}
err = reputationDB.Store(ctx, stats)
require.NoError(t, err)
// test that the reputation was stored correctly
rstats, err := reputationDB.Get(ctx, satellite.ID())
require.NoError(t, err)
require.NotNil(t, rstats)
require.Equal(t, stats, *rstats)
satelliteDB := planet.StorageNodes[0].DB.Satellites()
// insert a new untrusted satellite in the database
err = satelliteDB.SetAddressAndStatus(ctx, satellite.ID(), satellite.URL(), satellites.Untrusted)
require.NoError(t, err)
// test that the satellite was inserted correctly
satelliteInfo, err := satelliteDB.GetSatellite(ctx, satellite.ID())
require.NoError(t, err)
require.Equal(t, satellites.Untrusted, satelliteInfo.Status)
// run the forget satellite command with All flag
var stdout bytes.Buffer
client, err := dialForgetSatelliteClient(ctx, address)
require.NoError(t, err)
defer ctx.Check(client.close)
cmdCtx, cmdCancel := context.WithCancel(ctx)
defer cmdCancel()
err = startForgetSatellite(cmdCtx, log, client, ForgetSatelliteOptions{
AllUntrusted: true,
Stdout: &stdout,
})
require.NoError(t, err)
require.Contains(t, stdout.String(), satellite.ID().String())
require.Contains(t, stdout.String(), "In Progress")
// trigger the chore to run
planet.StorageNodes[0].ForgetSatellite.Chore.Loop.TriggerWait()
// confirm cleanup succeeded
satelliteInfo, err = satelliteDB.GetSatellite(ctx, satellite.ID())
require.NoError(t, err)
require.Equal(t, satellites.CleanupSucceeded, satelliteInfo.Status)
// check that the blob was deleted
blobInfo, err := store.Stat(ctx, blobRef)
require.Error(t, err)
require.True(t, errs.Is(err, os.ErrNotExist))
require.Nil(t, blobInfo)
// check that the reputation was deleted
rstats, err = reputationDB.Get(ctx, satellite.ID())
require.NoError(t, err)
require.Equal(t, &reputation.Stats{SatelliteID: satellite.ID()}, rstats)
})
}