Commit dfeca7c2 authored by Janoš Guljaš's avatar Janoš Guljaš Committed by GitHub

move chunks-pin endpoints from debugapi to api (#673)

parent 823f236d
......@@ -450,3 +450,78 @@ paths:
$ref: 'SwarmCommon.yaml#/components/responses/500'
default:
description: Default response
'/pinning/chunks/{address}':
parameters:
- in: path
name: address
schema:
$ref: 'SwarmCommon.yaml#/components/schemas/SwarmAddress'
required: true
description: Swarm address of chunk
post:
summary: Pin chunk with given address
tags:
- Chunk pinning
responses:
'200':
description: Pinning chunk with address
content:
application/json:
schema:
$ref: 'SwarmCommon.yaml#/components/schemas/Response'
'400':
$ref: 'SwarmCommon.yaml#/components/responses/400'
'404':
$ref: 'SwarmCommon.yaml#/components/responses/404'
default:
description: Default response
delete:
summary: Unpin chunk with given address
tags:
- Chunk pinning
responses:
'200':
description: Unpinning chunk with address
content:
application/json:
schema:
$ref: 'SwarmCommon.yaml#/components/schemas/Response'
'400':
$ref: 'SwarmCommon.yaml#/components/responses/400'
'404':
$ref: 'SwarmCommon.yaml#/components/responses/404'
default:
description: Default response
get:
summary: Get pinning status of chunk with given address
tags:
- Chunk pinning
responses:
'200':
description: Pinning state of chunk with address
content:
application/json:
schema:
$ref: 'SwarmCommon.yaml#/components/schemas/PinningState'
'500':
$ref: 'SwarmCommon.yaml#/components/responses/500'
default:
description: Default response
'/pinning/chunks/':
get:
summary: Get list of pinned chunks
tags:
- Chunk pinning
responses:
'200':
description: List of pinned chunks
content:
application/json:
schema:
$ref: 'SwarmCommon.yaml#/components/schemas/BzzChunksPinned'
'500':
$ref: 'SwarmCommon.yaml#/components/responses/500'
default:
description: Default response
......@@ -134,81 +134,6 @@ paths:
default:
description: Default response
'/chunks-pin/{address}':
parameters:
- in: path
name: address
schema:
$ref: 'SwarmCommon.yaml#/components/schemas/SwarmAddress'
required: true
description: Swarm address of chunk
post:
summary: Pin chunk with given address
tags:
- Chunk pinning
responses:
'200':
description: Pinning chunk with address
content:
application/json:
schema:
$ref: 'SwarmCommon.yaml#/components/schemas/Response'
'400':
$ref: 'SwarmCommon.yaml#/components/responses/400'
'404':
$ref: 'SwarmCommon.yaml#/components/responses/404'
default:
description: Default response
delete:
summary: Unpin chunk with given address
tags:
- Chunk pinning
responses:
'200':
description: Unpinning chunk with address
content:
application/json:
schema:
$ref: 'SwarmCommon.yaml#/components/schemas/Response'
'400':
$ref: 'SwarmCommon.yaml#/components/responses/400'
'404':
$ref: 'SwarmCommon.yaml#/components/responses/404'
default:
description: Default response
get:
summary: Get pinning status of chunk with given address
tags:
- Chunk pinning
responses:
'200':
description: Pinning state of chunk with address
content:
application/json:
schema:
$ref: 'SwarmCommon.yaml#/components/schemas/PinningState'
'500':
$ref: 'SwarmCommon.yaml#/components/responses/500'
default:
description: Default response
'/chunks-pin/':
get:
summary: Get list of pinned chunks
tags:
- Chunk pinning
responses:
'200':
description: List of pinned chunks
content:
application/json:
schema:
$ref: 'SwarmCommon.yaml#/components/schemas/BzzChunksPinned'
'500':
$ref: 'SwarmCommon.yaml#/components/responses/500'
default:
description: Default response
'/connect/{multiAddress}':
post:
summary: Connect to address
......
......@@ -13,6 +13,8 @@ type (
FileUploadResponse = fileUploadResponse
TagResponse = tagResponse
TagRequest = tagRequest
PinnedChunk = pinnedChunk
ListPinnedChunksResponse = listPinnedChunksResponse
)
var (
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package debugapi
package api
import (
"errors"
......@@ -52,7 +52,7 @@ func (s *server) pinChunk(w http.ResponseWriter, r *http.Request) {
func (s *server) unpinChunk(w http.ResponseWriter, r *http.Request) {
addr, err := swarm.ParseHexAddress(mux.Vars(r)["address"])
if err != nil {
s.Logger.Debugf("debug api: pin chunk: parse chunk ddress: %v", err)
s.Logger.Debugf("debug api: pin chunk: parse chunk address: %v", err)
jsonhttp.BadRequest(w, "bad address")
return
}
......
......@@ -2,17 +2,18 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package debugapi_test
package api_test
import (
"bytes"
"github.com/ethersphere/bee/pkg/logging"
statestore "github.com/ethersphere/bee/pkg/statestore/mock"
"io/ioutil"
"net/http"
"testing"
"github.com/ethersphere/bee/pkg/debugapi"
"github.com/ethersphere/bee/pkg/logging"
statestore "github.com/ethersphere/bee/pkg/statestore/mock"
"github.com/ethersphere/bee/pkg/api"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
"github.com/ethersphere/bee/pkg/storage/mock"
......@@ -36,21 +37,16 @@ func TestPinChunkHandler(t *testing.T) {
logger = logging.New(ioutil.Discard, 0)
tag = tags.NewTags(mockStatestore, logger)
debugTestServer = newTestServer(t, testServerOptions{
Storer: mockValidatingStorer,
Tags: tag,
})
// This server is used to store chunks
bzzTestServer = newBZZTestServer(t, testServerOptions{
client = newTestServer(t, testServerOptions{
Storer: mockValidatingStorer,
Tags: tag,
Logger: logger,
})
)
// bad chunk address
t.Run("pin-bad-address", func(t *testing.T) {
jsonhttptest.Request(t, debugTestServer.Client, http.MethodPost, "/chunks-pin/abcd1100zz", http.StatusBadRequest,
jsonhttptest.Request(t, client, http.MethodPost, "/pinning/chunks/abcd1100zz", http.StatusBadRequest,
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: "bad address",
Code: http.StatusBadRequest,
......@@ -60,16 +56,16 @@ func TestPinChunkHandler(t *testing.T) {
// list pins without anything pinned
t.Run("list-pins-zero-pins", func(t *testing.T) {
jsonhttptest.Request(t, debugTestServer.Client, http.MethodGet, "/chunks-pin", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(debugapi.ListPinnedChunksResponse{
Chunks: []debugapi.PinnedChunk{},
jsonhttptest.Request(t, client, http.MethodGet, "/pinning/chunks", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(api.ListPinnedChunksResponse{
Chunks: []api.PinnedChunk{},
}),
)
})
// pin a chunk which is not existing
t.Run("pin-absent-chunk", func(t *testing.T) {
jsonhttptest.Request(t, debugTestServer.Client, http.MethodPost, "/chunks-pin/123456", http.StatusNotFound,
jsonhttptest.Request(t, client, http.MethodPost, "/pinning/chunks/123456", http.StatusNotFound,
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: http.StatusText(http.StatusNotFound),
Code: http.StatusNotFound,
......@@ -80,7 +76,7 @@ func TestPinChunkHandler(t *testing.T) {
// unpin on a chunk which is not pinned
t.Run("unpin-while-not-pinned", func(t *testing.T) {
// Post a chunk
jsonhttptest.Request(t, bzzTestServer, http.MethodPost, resource(hash), http.StatusOK,
jsonhttptest.Request(t, client, http.MethodPost, resource(hash), http.StatusOK,
jsonhttptest.WithRequestBody(bytes.NewReader(data)),
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: http.StatusText(http.StatusOK),
......@@ -88,7 +84,7 @@ func TestPinChunkHandler(t *testing.T) {
}),
)
jsonhttptest.Request(t, debugTestServer.Client, http.MethodDelete, "/chunks-pin/"+hash.String(), http.StatusBadRequest,
jsonhttptest.Request(t, client, http.MethodDelete, "/pinning/chunks/"+hash.String(), http.StatusBadRequest,
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: "chunk is not yet pinned",
Code: http.StatusBadRequest,
......@@ -99,7 +95,7 @@ func TestPinChunkHandler(t *testing.T) {
// pin a existing chunk first time
t.Run("pin-chunk-1", func(t *testing.T) {
// Post a chunk
jsonhttptest.Request(t, bzzTestServer, http.MethodPost, resource(hash), http.StatusOK,
jsonhttptest.Request(t, client, http.MethodPost, resource(hash), http.StatusOK,
jsonhttptest.WithRequestBody(bytes.NewReader(data)),
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: http.StatusText(http.StatusOK),
......@@ -107,7 +103,7 @@ func TestPinChunkHandler(t *testing.T) {
}),
)
jsonhttptest.Request(t, debugTestServer.Client, http.MethodPost, "/chunks-pin/"+hash.String(), http.StatusOK,
jsonhttptest.Request(t, client, http.MethodPost, "/pinning/chunks/"+hash.String(), http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: http.StatusText(http.StatusOK),
Code: http.StatusOK,
......@@ -115,8 +111,8 @@ func TestPinChunkHandler(t *testing.T) {
)
// Check is the chunk is pinned once
jsonhttptest.Request(t, debugTestServer.Client, http.MethodGet, "/chunks-pin/"+hash.String(), http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(debugapi.PinnedChunk{
jsonhttptest.Request(t, client, http.MethodGet, "/pinning/chunks/"+hash.String(), http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(api.PinnedChunk{
Address: swarm.MustParseHexAddress("aabbcc"),
PinCounter: 1,
}),
......@@ -126,7 +122,7 @@ func TestPinChunkHandler(t *testing.T) {
// pin a existing chunk second time
t.Run("pin-chunk-2", func(t *testing.T) {
jsonhttptest.Request(t, debugTestServer.Client, http.MethodPost, "/chunks-pin/"+hash.String(), http.StatusOK,
jsonhttptest.Request(t, client, http.MethodPost, "/pinning/chunks/"+hash.String(), http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: http.StatusText(http.StatusOK),
Code: http.StatusOK,
......@@ -134,8 +130,8 @@ func TestPinChunkHandler(t *testing.T) {
)
// Check is the chunk is pinned twice
jsonhttptest.Request(t, debugTestServer.Client, http.MethodGet, "/chunks-pin/"+hash.String(), http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(debugapi.PinnedChunk{
jsonhttptest.Request(t, client, http.MethodGet, "/pinning/chunks/"+hash.String(), http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(api.PinnedChunk{
Address: swarm.MustParseHexAddress("aabbcc"),
PinCounter: 2,
}),
......@@ -144,7 +140,7 @@ func TestPinChunkHandler(t *testing.T) {
// unpin a chunk first time
t.Run("unpin-chunk-1", func(t *testing.T) {
jsonhttptest.Request(t, debugTestServer.Client, http.MethodDelete, "/chunks-pin/"+hash.String(), http.StatusOK,
jsonhttptest.Request(t, client, http.MethodDelete, "/pinning/chunks/"+hash.String(), http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: http.StatusText(http.StatusOK),
Code: http.StatusOK,
......@@ -152,8 +148,8 @@ func TestPinChunkHandler(t *testing.T) {
)
// Check is the chunk is pinned once
jsonhttptest.Request(t, debugTestServer.Client, http.MethodGet, "/chunks-pin/"+hash.String(), http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(debugapi.PinnedChunk{
jsonhttptest.Request(t, client, http.MethodGet, "/pinning/chunks/"+hash.String(), http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(api.PinnedChunk{
Address: swarm.MustParseHexAddress("aabbcc"),
PinCounter: 1,
}),
......@@ -162,7 +158,7 @@ func TestPinChunkHandler(t *testing.T) {
// unpin a chunk second time
t.Run("unpin-chunk-2", func(t *testing.T) {
jsonhttptest.Request(t, debugTestServer.Client, http.MethodDelete, "/chunks-pin/"+hash.String(), http.StatusOK,
jsonhttptest.Request(t, client, http.MethodDelete, "/pinning/chunks/"+hash.String(), http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: http.StatusText(http.StatusOK),
Code: http.StatusOK,
......@@ -170,7 +166,7 @@ func TestPinChunkHandler(t *testing.T) {
)
// Check if the chunk is removed from the pinIndex
jsonhttptest.Request(t, debugTestServer.Client, http.MethodGet, "/chunks-pin/"+hash.String(), http.StatusNotFound,
jsonhttptest.Request(t, client, http.MethodGet, "/pinning/chunks/"+hash.String(), http.StatusNotFound,
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: http.StatusText(http.StatusNotFound),
Code: http.StatusNotFound,
......@@ -181,7 +177,7 @@ func TestPinChunkHandler(t *testing.T) {
// Add 2 chunks, pin it and check if they show up in the list
t.Run("list-chunks", func(t *testing.T) {
// Post a chunk
jsonhttptest.Request(t, bzzTestServer, http.MethodPost, resource(hash), http.StatusOK,
jsonhttptest.Request(t, client, http.MethodPost, resource(hash), http.StatusOK,
jsonhttptest.WithRequestBody(bytes.NewReader(data)),
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: http.StatusText(http.StatusOK),
......@@ -189,7 +185,7 @@ func TestPinChunkHandler(t *testing.T) {
}),
)
jsonhttptest.Request(t, debugTestServer.Client, http.MethodPost, "/chunks-pin/"+hash.String(), http.StatusOK,
jsonhttptest.Request(t, client, http.MethodPost, "/pinning/chunks/"+hash.String(), http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: http.StatusText(http.StatusOK),
Code: http.StatusOK,
......@@ -200,23 +196,23 @@ func TestPinChunkHandler(t *testing.T) {
hash2 := swarm.MustParseHexAddress("ddeeff")
data2 := []byte("eagle")
mockValidator.AddPair(hash2, data2)
jsonhttptest.Request(t, bzzTestServer, http.MethodPost, resource(hash2), http.StatusOK,
jsonhttptest.Request(t, client, http.MethodPost, resource(hash2), http.StatusOK,
jsonhttptest.WithRequestBody(bytes.NewReader(data2)),
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: http.StatusText(http.StatusOK),
Code: http.StatusOK,
}),
)
jsonhttptest.Request(t, debugTestServer.Client, http.MethodPost, "/chunks-pin/"+hash2.String(), http.StatusOK,
jsonhttptest.Request(t, client, http.MethodPost, "/pinning/chunks/"+hash2.String(), http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: http.StatusText(http.StatusOK),
Code: http.StatusOK,
}),
)
jsonhttptest.Request(t, debugTestServer.Client, http.MethodGet, "/chunks-pin", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(debugapi.ListPinnedChunksResponse{
Chunks: []debugapi.PinnedChunk{
jsonhttptest.Request(t, client, http.MethodGet, "/pinning/chunks", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(api.ListPinnedChunksResponse{
Chunks: []api.PinnedChunk{
{
Address: swarm.MustParseHexAddress("aabbcc"),
PinCounter: 1,
......
......@@ -81,6 +81,15 @@ func (s *server) setupRouting() {
),
})
handle(router, "/pinning/chunks/{address}", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.getPinnedChunk),
"POST": http.HandlerFunc(s.pinChunk),
"DELETE": http.HandlerFunc(s.unpinChunk),
})
handle(router, "/pinning/chunks", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.listPinnedChunks),
})
s.Handler = web.ChainHandlers(
logging.NewHTTPAccessLogHandler(s.Logger, logrus.InfoLevel, "api access"),
handlers.CompressHandler,
......
......@@ -12,13 +12,11 @@ import (
"testing"
accountingmock "github.com/ethersphere/bee/pkg/accounting/mock"
"github.com/ethersphere/bee/pkg/api"
"github.com/ethersphere/bee/pkg/debugapi"
"github.com/ethersphere/bee/pkg/logging"
p2pmock "github.com/ethersphere/bee/pkg/p2p/mock"
"github.com/ethersphere/bee/pkg/pingpong"
"github.com/ethersphere/bee/pkg/resolver"
resolverMock "github.com/ethersphere/bee/pkg/resolver/mock"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/tags"
......@@ -67,26 +65,6 @@ func newTestServer(t *testing.T, o testServerOptions) *testServer {
}
}
func newBZZTestServer(t *testing.T, o testServerOptions) *http.Client {
if o.Resolver == nil {
o.Resolver = resolverMock.NewResolver()
}
s := api.New(o.Tags, o.Storer, o.Resolver, nil, logging.New(ioutil.Discard, 0), nil)
ts := httptest.NewServer(s)
t.Cleanup(ts.Close)
return &http.Client{
Transport: web.RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
u, err := url.Parse(ts.URL + r.URL.String())
if err != nil {
return nil, err
}
r.URL = u
return ts.Client().Transport.RoundTrip(r)
}),
}
}
func mustMultiaddr(t *testing.T, s string) multiaddr.Multiaddr {
t.Helper()
......
......@@ -10,8 +10,6 @@ type (
PeerConnectResponse = peerConnectResponse
PeersResponse = peersResponse
AddressesResponse = addressesResponse
PinnedChunk = pinnedChunk
ListPinnedChunksResponse = listPinnedChunksResponse
WelcomeMessageRequest = welcomeMessageRequest
WelcomeMessageResponse = welcomeMessageResponse
BalancesResponse = balancesResponse
......
......@@ -70,14 +70,6 @@ func (s *server) setupRouting() {
"GET": http.HandlerFunc(s.hasChunkHandler),
"DELETE": http.HandlerFunc(s.removeChunk),
})
router.Handle("/chunks-pin/{address}", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.getPinnedChunk),
"POST": http.HandlerFunc(s.pinChunk),
"DELETE": http.HandlerFunc(s.unpinChunk),
})
router.Handle("/chunks-pin", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.listPinnedChunks),
})
router.Handle("/topology", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.topologyHandler),
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment