|
5 | 5 | package api_test |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "bytes" |
| 9 | + "context" |
8 | 10 | "encoding/hex" |
| 11 | + "fmt" |
9 | 12 | "net/http" |
| 13 | + "strconv" |
10 | 14 | "testing" |
| 15 | + "time" |
11 | 16 |
|
12 | 17 | "github.com/ethersphere/bee/v2/pkg/api" |
| 18 | + "github.com/ethersphere/bee/v2/pkg/file/redundancy" |
13 | 19 | "github.com/ethersphere/bee/v2/pkg/jsonhttp" |
14 | 20 | "github.com/ethersphere/bee/v2/pkg/jsonhttp/jsonhttptest" |
15 | 21 | "github.com/ethersphere/bee/v2/pkg/log" |
16 | 22 | mockpost "github.com/ethersphere/bee/v2/pkg/postage/mock" |
| 23 | + "github.com/ethersphere/bee/v2/pkg/steward" |
17 | 24 | "github.com/ethersphere/bee/v2/pkg/steward/mock" |
| 25 | + "github.com/ethersphere/bee/v2/pkg/storage" |
18 | 26 | mockstorer "github.com/ethersphere/bee/v2/pkg/storer/mock" |
19 | 27 | "github.com/ethersphere/bee/v2/pkg/swarm" |
| 28 | + "gitlab.com/nolash/go-mockbytes" |
20 | 29 | ) |
21 | 30 |
|
22 | 31 | // nolint:paralleltest |
@@ -60,6 +69,58 @@ func TestStewardship(t *testing.T) { |
60 | 69 | }) |
61 | 70 | } |
62 | 71 |
|
| 72 | +type localRetriever struct { |
| 73 | + getter storage.Getter |
| 74 | +} |
| 75 | + |
| 76 | +func (lr *localRetriever) RetrieveChunk(ctx context.Context, addr, sourceAddr swarm.Address) (chunk swarm.Chunk, err error) { |
| 77 | + ch, err := lr.getter.Get(ctx, addr) |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("retrieve chunk %s: %w", addr, err) |
| 80 | + } |
| 81 | + return ch, nil |
| 82 | +} |
| 83 | + |
| 84 | +func TestStewardshipWithRedundancy(t *testing.T) { |
| 85 | + t.Parallel() |
| 86 | + |
| 87 | + var ( |
| 88 | + storerMock = mockstorer.New() |
| 89 | + localRetrieval = &localRetriever{getter: storerMock.ChunkStore()} |
| 90 | + s = steward.New(storerMock, localRetrieval, storerMock.Cache()) |
| 91 | + client, _, _, _ = newTestServer(t, testServerOptions{ |
| 92 | + Storer: storerMock, |
| 93 | + Logger: log.Noop, |
| 94 | + Steward: s, |
| 95 | + Post: mockpost.New(mockpost.WithAcceptAll()), |
| 96 | + }) |
| 97 | + ) |
| 98 | + |
| 99 | + g := mockbytes.New(0, mockbytes.MockTypeStandard).WithModulus(255) |
| 100 | + content, err := g.SequentialBytes(512000) // 500KB |
| 101 | + if err != nil { |
| 102 | + t.Fatal(err) |
| 103 | + } |
| 104 | + |
| 105 | + for _, l := range []redundancy.Level{redundancy.NONE, redundancy.MEDIUM, redundancy.STRONG, redundancy.INSANE, redundancy.PARANOID} { |
| 106 | + t.Run(fmt.Sprintf("rLevel-%d", l), func(t *testing.T) { |
| 107 | + res := new(api.BytesPostResponse) |
| 108 | + jsonhttptest.Request(t, client, http.MethodPost, "/bytes", http.StatusCreated, |
| 109 | + jsonhttptest.WithRequestHeader(api.SwarmDeferredUploadHeader, "true"), |
| 110 | + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), |
| 111 | + jsonhttptest.WithRequestHeader(api.SwarmRedundancyLevelHeader, strconv.Itoa(int(l))), |
| 112 | + jsonhttptest.WithRequestBody(bytes.NewReader(content)), |
| 113 | + jsonhttptest.WithUnmarshalJSONResponse(res), |
| 114 | + ) |
| 115 | + |
| 116 | + time.Sleep(2 * time.Second) |
| 117 | + jsonhttptest.Request(t, client, http.MethodGet, "/stewardship/"+res.Reference.String(), http.StatusOK, |
| 118 | + jsonhttptest.WithExpectedJSONResponse(api.IsRetrievableResponse{IsRetrievable: true}), |
| 119 | + ) |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
| 123 | + |
63 | 124 | func TestStewardshipInvalidInputs(t *testing.T) { |
64 | 125 | t.Parallel() |
65 | 126 |
|
|
0 commit comments