Skip to content

Commit

Permalink
More consistent use of test library (#1392)
Browse files Browse the repository at this point in the history
* More consistent use of test library
  • Loading branch information
gammazero authored Mar 13, 2023
1 parent 58cc78b commit 9a50bf7
Show file tree
Hide file tree
Showing 25 changed files with 438 additions and 1,161 deletions.
17 changes: 5 additions & 12 deletions assigner/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,13 @@ const pubsubTopic = "/indexer/ingest/mainnet"

func setupServer(t *testing.T, assigner *core.Assigner) *server.Server {
s, err := server.New("127.0.0.1:0", assigner)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
return s
}

func setupClient(t *testing.T, host string) *client.Client {
c, err := client.New(host)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
return c
}

Expand Down Expand Up @@ -176,9 +172,8 @@ func TestAssignOnAnnounce(t *testing.T) {
assignChan, cancel := assigner.OnAssignment(peerID)
defer cancel()

if err := cl.Announce(context.Background(), ai, cid.NewCidV1(22, mhs[0])); err != nil {
t.Fatalf("Failed to announce to %s: %s", s.URL(), err)
}
err = cl.Announce(context.Background(), ai, cid.NewCidV1(22, mhs[0]))
require.NoErrorf(t, err, "Failed to announce to %s", s.URL())

select {
case indexerNum := <-assignChan:
Expand Down Expand Up @@ -231,9 +226,7 @@ func initAssigner(t *testing.T, trustedID string) (*core.Assigner, config.Assign
PubSubTopic: pubsubTopic,
}
assigner, err := core.NewAssigner(context.Background(), cfg, nil)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

return assigner, cfg
}
Expand Down
7 changes: 2 additions & 5 deletions carstore/carwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,8 @@ func TestWriteExistingAdsInStore(t *testing.T) {
var carFound bool
fc, ec := fileStore.List(ctx, "", false)
for fileInfo := range fc {
if fileInfo.Path == carName {
carFound = true
} else {
t.Fatal("unexpected file")
}
require.Equal(t, carName, fileInfo.Path, "unexpected file")
carFound = true
}
err = <-ec
require.NoError(t, err)
Expand Down
45 changes: 12 additions & 33 deletions command/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/ipni/storetheindex/config"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -34,19 +35,13 @@ func TestInit(t *testing.T) {
)

err := app.RunContext(ctx, []string{"storetheindex", "init", "-listen-admin", badAddr})
if err == nil {
t.Fatal("expected error")
}
require.Error(t, err)

err = app.RunContext(ctx, []string{"storetheindex", "init", "-listen-finder", badAddr})
if err == nil {
t.Fatal("expected error")
}
require.Error(t, err)

err = app.RunContext(ctx, []string{"storetheindex", "init", "-listen-ingest", badAddr})
if err == nil {
t.Fatal("expected error")
}
require.Error(t, err)

args := []string{
"storetheindex", "init",
Expand All @@ -57,33 +52,17 @@ func TestInit(t *testing.T) {
"-store", storeType,
}
err = app.RunContext(ctx, args)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

cfg, err := config.Load("")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

if cfg.Addresses.Finder != goodAddr {
t.Error("finder listen address was not configured")
}
if cfg.Addresses.Ingest != goodAddr2 {
t.Error("ingest listen address was not configured")
}
if cfg.Indexer.CacheSize != cacheSize {
t.Error("cache size was tno configured")
}
if cfg.Indexer.ValueStoreType != storeType {
t.Error("value store type was not configured")
}
if cfg.Ingest.PubSubTopic != topicName {
t.Errorf("expected %s for pubsub topic, got %s", topicName, cfg.Ingest.PubSubTopic)
}
if cfg.Version != config.Version {
t.Error("did not init config with correct version")
}
require.Equal(t, goodAddr, cfg.Addresses.Finder, "finder listen address was not configured")
require.Equal(t, goodAddr2, cfg.Addresses.Ingest, "ingest listen address was not configured")
require.Equal(t, cacheSize, cfg.Indexer.CacheSize, "cache size was tno configured")
require.Equal(t, storeType, cfg.Indexer.ValueStoreType, "value store type was not configured")
require.Equal(t, topicName, cfg.Ingest.PubSubTopic)
require.Equal(t, config.Version, cfg.Version)

t.Log(cfg.String())
}
10 changes: 4 additions & 6 deletions config/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package config
import (
"sort"
"testing"

"github.com/stretchr/testify/require"
)

var testBootstrapAddresses = []string{
Expand All @@ -19,18 +21,14 @@ func TestBoostrapPeers(t *testing.T) {
Peers: testBootstrapAddresses,
}
addrs, err := b.PeerAddrs()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

var b2 Bootstrap
b2.SetPeers(addrs)
sort.Strings(b2.Peers)
sort.Strings(b.Peers)

for i := range b2.Peers {
if b2.Peers[i] != b.Peers[i] {
t.Fatalf("expected %s, %s", b.Peers[i], b2.Peers[i])
}
require.Equal(t, b2.Peers[i], b.Peers[i])
}
}
30 changes: 9 additions & 21 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"path/filepath"
"runtime"
"testing"

"github.com/stretchr/testify/require"
)

func TestPath(t *testing.T) {
Expand All @@ -17,32 +19,18 @@ func TestPath(t *testing.T) {
}

path, err := Path("", dir)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

configRoot, err := PathRoot()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

if path != filepath.Join(configRoot, dir) {
t.Fatalf("wrong path %s:", path)
}
require.Equal(t, filepath.Join(configRoot, dir), path)

path, err = Path("altroot", dir)
if err != nil {
t.Fatal(err)
}
if path != filepath.Join("altroot", dir) {
t.Fatalf("wrong path %s:", path)
}
require.NoError(t, err)
require.Equal(t, filepath.Join("altroot", dir), path)

path, err = Path("altroot", absdir)
if err != nil {
t.Fatal(err)
}
if path != filepath.Clean(absdir) {
t.Fatalf("wrong path %s:", path)
}
require.NoError(t, err)
require.Equal(t, filepath.Clean(absdir), path)
}
66 changes: 18 additions & 48 deletions config/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,83 +8,53 @@ import (
"testing"

crypto_pb "github.com/libp2p/go-libp2p/core/crypto/pb"
"github.com/stretchr/testify/require"
)

func TestCreateIdentity(t *testing.T) {
id, err := CreateIdentity(io.Discard)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
pk, err := id.DecodePrivateKey("")
if err != nil {
t.Fatal(err)
}
if pk.Type() != crypto_pb.KeyType_Ed25519 {
t.Fatal("unexpected type:", pk.Type())
}
require.NoError(t, err)
require.Equal(t, crypto_pb.KeyType_Ed25519, pk.Type())
}

func TestMarshalUnmarshal(t *testing.T) {
cfg, err := Init(io.Discard)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

b, err := json.MarshalIndent(&cfg, " ", " ")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

t.Log("default config:\n", string(b))

cfg2 := Config{}
if err = json.Unmarshal(b, &cfg2); err != nil {
t.Fatal(err)
}
err = json.Unmarshal(b, &cfg2)
require.NoError(t, err)

if cfg.Identity.PeerID != cfg2.Identity.PeerID {
t.Fatal("identity no same")
}
if cfg.Identity.PrivKey != cfg2.Identity.PrivKey {
t.Fatal("private key not same")
}
require.Equal(t, cfg.Identity.PeerID, cfg2.Identity.PeerID)
require.Equal(t, cfg.Identity.PrivKey, cfg2.Identity.PrivKey)
}

func TestSaveLoad(t *testing.T) {
tmpDir := t.TempDir()
cfgFile, err := Filename(tmpDir)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

if filepath.Dir(cfgFile) != tmpDir {
t.Fatal("wrong root dir", cfgFile)
}
require.Equal(t, tmpDir, filepath.Dir(cfgFile), "wrong root dir")

cfg, err := Init(io.Discard)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
cfgBytes, err := Marshal(cfg)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

err = cfg.Save(cfgFile)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

cfg2, err := Load(cfgFile)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
cfg2Bytes, err := Marshal(cfg2)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

if !bytes.Equal(cfgBytes, cfg2Bytes) {
t.Fatal("config data different after being loaded")
}
require.True(t, bytes.Equal(cfgBytes, cfg2Bytes), "config data different after being loaded")
}
37 changes: 12 additions & 25 deletions dagsync/announce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ func TestAnnounceReplace(t *testing.T) {
// Have the subscriber receive an announce. This is the same as if it was
// published by the publisher without having to wait for it to arrive.
err = sub.Announce(context.Background(), firstCid, srcHost.ID(), srcHost.Addrs())
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
t.Log("Sent announce for first CID", firstCid)

// This first announce should start the handler goroutine and clear the
Expand Down Expand Up @@ -113,15 +111,10 @@ func TestAnnounceReplace(t *testing.T) {
case <-time.After(updateTimeout):
t.Fatal("timed out waiting for sync to propagate")
case downstream, open := <-watcher:
if !open {
t.Fatal("event channle closed without receiving event")
}
if !downstream.Cid.Equals(firstCid) {
t.Fatalf("sync returned unexpected first cid %s, expected %s", downstream.Cid, firstCid)
}
if _, err = dstStore.Get(context.Background(), datastore.NewKey(downstream.Cid.String())); err != nil {
t.Fatalf("data not in receiver store: %s", err)
}
require.True(t, open, "event channle closed without receiving event")
require.Equal(t, firstCid, downstream.Cid, "sync returned unexpected first cid")
_, err = dstStore.Get(context.Background(), datastore.NewKey(downstream.Cid.String()))
require.NoError(t, err, "data not in receiver store")
t.Log("Received sync notification for first CID:", firstCid)
}

Expand All @@ -130,26 +123,20 @@ func TestAnnounceReplace(t *testing.T) {
case <-time.After(updateTimeout):
t.Fatal("timed out waiting for sync to propagate")
case downstream, open := <-watcher:
if !open {
t.Fatal("event channle closed without receiving event")
}
if !downstream.Cid.Equals(lastCid) {
t.Fatalf("sync returned unexpected last cid %s, expected %s", downstream.Cid, lastCid)
}
if _, err = dstStore.Get(context.Background(), datastore.NewKey(downstream.Cid.String())); err != nil {
t.Fatalf("data not in receiver store: %s", err)
}
require.True(t, open, "event channle closed without receiving event")
require.Equal(t, lastCid, downstream.Cid, "sync returned unexpected last cid")
_, err = dstStore.Get(context.Background(), datastore.NewKey(downstream.Cid.String()))
require.NoError(t, err, "data not in receiver store")
t.Log("Received sync notification for last CID:", lastCid)
}

// Validate that no additional updates happen.
select {
case <-time.After(3 * time.Second):
case changeEvent, open := <-watcher:
if open {
t.Fatalf("no exchange should have been performed, but got change from peer %s for cid %s",
changeEvent.PeerID, changeEvent.Cid)
}
require.Falsef(t, open,
"no exchange should have been performed, but got change from peer %s for cid %s",
changeEvent.PeerID, changeEvent.Cid)
}
}

Expand Down
Loading

0 comments on commit 9a50bf7

Please sign in to comment.