Skip to content

Commit

Permalink
fix: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
notanatol committed Feb 9, 2024
1 parent 8251a61 commit 3551a79
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ type Storer interface {
storer.Debugger
}

type PinIntegrity interface {
Check(ctx context.Context, logger log.Logger, pin string, out chan storer.PinStat)
}

type Service struct {
auth auth.Authenticator
storer Storer
Expand Down Expand Up @@ -174,7 +178,7 @@ type Service struct {

batchStore postage.Storer
stamperStore storage.Store
pinIntegrity *storer.PinIntegrity
pinIntegrity PinIntegrity

syncStatus func() (bool, error)

Expand Down Expand Up @@ -244,7 +248,7 @@ type ExtraOptions struct {
Steward steward.Interface
SyncStatus func() (bool, error)
NodeStatus *status.Service
PinIntegrity *storer.PinIntegrity
PinIntegrity PinIntegrity
}

func New(
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type testServerOptions struct {
BeeMode api.BeeNodeMode
RedistributionAgent *storageincentives.Agent
NodeStatus *status.Service
PinIntegrity api.PinIntegrity
}

func newTestServer(t *testing.T, o testServerOptions) (*http.Client, *websocket.Conn, string, *chanStorer) {
Expand Down Expand Up @@ -201,6 +202,7 @@ func newTestServer(t *testing.T, o testServerOptions) (*http.Client, *websocket.
SyncStatus: o.SyncStatus,
Staking: o.StakingContract,
NodeStatus: o.NodeStatus,
PinIntegrity: o.PinIntegrity,
}

// By default bee mode is set to full mode.
Expand Down
1 change: 1 addition & 0 deletions pkg/api/integritycheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (s *Service) pinIntegrityHandler(w http.ResponseWriter, r *http.Request) {
}

w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
flusher.Flush()

Expand Down
67 changes: 67 additions & 0 deletions pkg/api/integritycheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2022 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package api_test

import (
"context"
"net/http"
"testing"

"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
"github.com/ethersphere/bee/pkg/log"
storage "github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/storage/inmemstore"
storer "github.com/ethersphere/bee/pkg/storer"
)

const pinRef = "620fcd78c7ce54da2d1b7cc2274a02e190cbe8fecbc3bd244690ab6517ce8f39"

func TestIntegrityHandler(t *testing.T) {
t.Parallel()

t.Run("ok", func(t *testing.T) {
t.Parallel()
testServer, _, _, _ := newTestServer(t, testServerOptions{
DebugAPI: true,
PinIntegrity: &mockPinIntegrity{
Store: inmemstore.New(),
tester: t,
},
})

endp := "/check/pin?ref=" + pinRef

// When probe is not set health endpoint should indicate that node is not healthy
jsonhttptest.Request(t, testServer, http.MethodGet, endp, http.StatusOK, jsonhttptest.WithExpectedResponse(nil))
})

t.Run("wrong hash format", func(t *testing.T) {
t.Parallel()
testServer, _, _, _ := newTestServer(t, testServerOptions{
DebugAPI: true,
PinIntegrity: &mockPinIntegrity{
Store: inmemstore.New(),
tester: t,
},
})

endp := "/check/pin?ref=0xbadhash"

// When probe is not set health endpoint should indicate that node is not healthy
jsonhttptest.Request(t, testServer, http.MethodGet, endp, http.StatusBadRequest, jsonhttptest.WithExpectedResponse(nil))
})
}

type mockPinIntegrity struct {
tester *testing.T
Store storage.Store
}

func (p *mockPinIntegrity) Check(ctx context.Context, logger log.Logger, pin string, out chan storer.PinStat) {
if pin != pinRef {
p.tester.Fatal("bad pin", pin)
}
close(out)
}

0 comments on commit 3551a79

Please sign in to comment.