github.com/ethersphere/bee/v2@v2.2.0/pkg/api/status_test.go (about) 1 // Copyright 2023 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package api_test 6 7 import ( 8 "net/http" 9 "testing" 10 11 "github.com/ethersphere/bee/v2/pkg/api" 12 "github.com/ethersphere/bee/v2/pkg/jsonhttp" 13 "github.com/ethersphere/bee/v2/pkg/jsonhttp/jsonhttptest" 14 "github.com/ethersphere/bee/v2/pkg/log" 15 "github.com/ethersphere/bee/v2/pkg/postage" 16 "github.com/ethersphere/bee/v2/pkg/status" 17 "github.com/ethersphere/bee/v2/pkg/topology" 18 ) 19 20 func TestGetStatus(t *testing.T) { 21 t.Parallel() 22 23 const url = "/status" 24 25 t.Run("node", func(t *testing.T) { 26 t.Parallel() 27 28 mode := api.FullMode 29 ssr := api.StatusSnapshotResponse{ 30 Proximity: 256, 31 BeeMode: mode.String(), 32 ReserveSize: 128, 33 ReserveSizeWithinRadius: 64, 34 PullsyncRate: 64, 35 StorageRadius: 8, 36 ConnectedPeers: 0, 37 NeighborhoodSize: 1, 38 BatchCommitment: 1, 39 IsReachable: true, 40 LastSyncedBlock: 6092500, 41 } 42 43 ssMock := &statusSnapshotMock{ 44 syncRate: ssr.PullsyncRate, 45 reserveSize: int(ssr.ReserveSize), 46 reserveSizeWithinRadius: ssr.ReserveSizeWithinRadius, 47 storageRadius: ssr.StorageRadius, 48 commitment: ssr.BatchCommitment, 49 chainState: &postage.ChainState{Block: ssr.LastSyncedBlock}, 50 } 51 52 statusSvc := status.NewService( 53 log.Noop, 54 nil, 55 new(topologyPeersIterNoopMock), 56 mode.String(), 57 ssMock, 58 ssMock, 59 ) 60 61 statusSvc.SetSync(ssMock) 62 63 client, _, _, _ := newTestServer(t, testServerOptions{ 64 BeeMode: mode, 65 NodeStatus: statusSvc, 66 }) 67 68 jsonhttptest.Request(t, client, http.MethodGet, url, http.StatusOK, 69 jsonhttptest.WithExpectedJSONResponse(ssr), 70 ) 71 }) 72 73 t.Run("bad request", func(t *testing.T) { 74 t.Parallel() 75 76 client, _, _, _ := newTestServer(t, testServerOptions{ 77 BeeMode: api.DevMode, 78 NodeStatus: status.NewService( 79 log.Noop, 80 nil, 81 new(topologyPeersIterNoopMock), 82 "", 83 nil, 84 nil, 85 ), 86 }) 87 88 jsonhttptest.Request(t, client, http.MethodGet, url, http.StatusBadRequest, 89 jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{ 90 Message: api.ErrUnsupportedDevNodeOperation.Error(), 91 Code: http.StatusBadRequest, 92 }), 93 ) 94 }) 95 } 96 97 // topologyPeersIterNoopMock is noop topology.PeerIterator. 98 type topologyPeersIterNoopMock struct{} 99 100 func (m *topologyPeersIterNoopMock) EachConnectedPeer(_ topology.EachPeerFunc, _ topology.Select) error { 101 return nil 102 } 103 104 func (m *topologyPeersIterNoopMock) EachConnectedPeerRev(_ topology.EachPeerFunc, _ topology.Select) error { 105 return nil 106 } 107 func (m *topologyPeersIterNoopMock) IsReachable() bool { 108 return true 109 } 110 111 // statusSnapshotMock satisfies the following interfaces: 112 // - status.Reserve 113 // - status.SyncReporter 114 // - postage.CommitmentGetter 115 type statusSnapshotMock struct { 116 syncRate float64 117 reserveSize int 118 reserveSizeWithinRadius uint64 119 storageRadius uint8 120 commitment uint64 121 chainState *postage.ChainState 122 } 123 124 func (m *statusSnapshotMock) SyncRate() float64 { return m.syncRate } 125 func (m *statusSnapshotMock) ReserveSize() int { return m.reserveSize } 126 func (m *statusSnapshotMock) StorageRadius() uint8 { return m.storageRadius } 127 func (m *statusSnapshotMock) Commitment() (uint64, error) { return m.commitment, nil } 128 func (m *statusSnapshotMock) GetChainState() *postage.ChainState { return m.chainState } 129 func (m *statusSnapshotMock) ReserveSizeWithinRadius() uint64 { 130 return m.reserveSizeWithinRadius 131 }