1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-12-12 19:59:03 +08:00
Files
kubo/core/coreapi/test/api_test.go
Marcin Rataj 71e883440e refactor(config): migration 17-to-18 to unify Provider/Reprovider into Provide.DHT (#10951)
* refactor: consolidate Provider/Reprovider into unified Provide config

- merge Provider and Reprovider configs into single Provide section
- add fs-repo-17-to-18 migration for config consolidation
- improve migration ergonomics with common package utilities
- convert deprecated "flat" strategy to "all" during migration
- improve Provide docs

* docs: add total_provide_count metric guidance

- document how to monitor provide success rates via prometheus metrics
- add performance comparison section to changelog
- explain how to evaluate sweep vs legacy provider effectiveness

* fix: add OpenTelemetry meter provider for metrics

- set up meter provider with Prometheus exporter in daemon
- enables metrics from external libs like go-libp2p-kad-dht
- fixes missing total_provide_count_total when SweepEnabled=true
- update docs to reflect actual metric names

---------

Co-authored-by: gammazero <11790789+gammazero@users.noreply.github.com>
Co-authored-by: guillaumemichel <guillaume@michel.id>
Co-authored-by: Daniel Norman <1992255+2color@users.noreply.github.com>
Co-authored-by: Hector Sanjuan <code@hector.link>
2025-09-18 22:17:43 +02:00

129 lines
2.9 KiB
Go

package test
import (
"context"
"encoding/base64"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/ipfs/boxo/bootstrap"
"github.com/ipfs/boxo/filestore"
keystore "github.com/ipfs/boxo/keystore"
"github.com/ipfs/kubo/core"
"github.com/ipfs/kubo/core/coreapi"
mock "github.com/ipfs/kubo/core/mock"
"github.com/ipfs/kubo/core/node/libp2p"
"github.com/ipfs/kubo/repo"
"github.com/ipfs/go-datastore"
syncds "github.com/ipfs/go-datastore/sync"
"github.com/ipfs/kubo/config"
coreiface "github.com/ipfs/kubo/core/coreiface"
"github.com/ipfs/kubo/core/coreiface/tests"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
)
const testPeerID = "QmTFauExutTsy4XP6JbMFcw2Wa9645HJt2bTqL6qYDCKfe"
type NodeProvider struct{}
func (NodeProvider) MakeAPISwarm(t *testing.T, ctx context.Context, fullIdentity bool, online bool, n int) ([]coreiface.CoreAPI, error) {
mn := mocknet.New()
nodes := make([]*core.IpfsNode, n)
apis := make([]coreiface.CoreAPI, n)
for i := 0; i < n; i++ {
var ident config.Identity
if fullIdentity {
sk, pk, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
if err != nil {
return nil, err
}
id, err := peer.IDFromPublicKey(pk)
if err != nil {
return nil, err
}
kbytes, err := crypto.MarshalPrivateKey(sk)
if err != nil {
return nil, err
}
ident = config.Identity{
PeerID: id.String(),
PrivKey: base64.StdEncoding.EncodeToString(kbytes),
}
} else {
ident = config.Identity{
PeerID: testPeerID,
}
}
c := config.Config{}
c.Addresses.Swarm = []string{fmt.Sprintf("/ip4/18.0.%d.1/tcp/4001", i)}
c.Identity = ident
c.Experimental.FilestoreEnabled = true
c.AutoTLS.Enabled = config.False // disable so no /ws listener is added
// For provider tests, avoid that content gets
// auto-provided without calling "provide" (unless pinned).
c.Provide.Strategy = config.NewOptionalString("roots")
ds := syncds.MutexWrap(datastore.NewMapDatastore())
r := &repo.Mock{
C: c,
D: ds,
K: keystore.NewMemKeystore(),
F: filestore.NewFileManager(ds, filepath.Dir(os.TempDir())),
}
node, err := core.NewNode(ctx, &core.BuildCfg{
Routing: libp2p.DHTServerOption,
Repo: r,
Host: mock.MockHostOption(mn),
Online: online,
ExtraOpts: map[string]bool{
"pubsub": true,
},
})
if err != nil {
return nil, err
}
nodes[i] = node
apis[i], err = coreapi.NewCoreAPI(node)
if err != nil {
return nil, err
}
}
err := mn.LinkAll()
if err != nil {
return nil, err
}
if online {
bsinf := bootstrap.BootstrapConfigWithPeers(
[]peer.AddrInfo{
nodes[0].Peerstore.PeerInfo(nodes[0].Identity),
},
)
for _, n := range nodes[1:] {
if err := n.Bootstrap(bsinf); err != nil {
return nil, err
}
}
}
return apis, nil
}
func TestIface(t *testing.T) {
tests.TestApi(NodeProvider{})(t)
}