github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/remote/registry/regserver/mock.go (about) 1 // Package regserver provides a mock registry server for testing purposes 2 package regserver 3 4 import ( 5 "context" 6 "net/http/httptest" 7 8 "github.com/qri-io/dataset" 9 "github.com/qri-io/qri/auth/key" 10 "github.com/qri-io/qri/base" 11 "github.com/qri-io/qri/config" 12 "github.com/qri-io/qri/dsref" 13 "github.com/qri-io/qri/p2p" 14 "github.com/qri-io/qri/remote" 15 "github.com/qri-io/qri/remote/registry" 16 "github.com/qri-io/qri/remote/registry/regclient" 17 "github.com/qri-io/qri/remote/registry/regserver/handlers" 18 "github.com/qri-io/qri/repo" 19 repotest "github.com/qri-io/qri/repo/test" 20 ) 21 22 // TODO (b5) - this value is used all over the plcae need a better strategy for 23 const registryPeerID = "QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt" 24 25 func init() { 26 // don't need verbose logging when working with mock servers 27 handlers.SetLogLevel("error") 28 } 29 30 // NewMockServer creates an in-memory mock server & matching registry client 31 func NewMockServer() (*regclient.Client, *httptest.Server) { 32 return NewMockServerRegistry(NewMemRegistry(nil)) 33 } 34 35 // NewMockServerRegistry creates a mock server & client with a passed-in registry 36 func NewMockServerRegistry(reg registry.Registry) (*regclient.Client, *httptest.Server) { 37 s := httptest.NewServer(handlers.NewRoutes(reg)) 38 c := regclient.NewClient(®client.Config{Location: s.URL}) 39 return c, s 40 } 41 42 // NewMemRegistry creates a new in-memory registry 43 func NewMemRegistry(rem *remote.Server) registry.Registry { 44 return registry.Registry{ 45 Remote: rem, 46 Profiles: registry.NewMemProfiles(), 47 } 48 } 49 50 // NewTempRegistry creates a functioning registry with a teardown function 51 // TODO(b5) - the tempRepo.Repo call in this func *requires* the passed-in 52 // context be cancelled at some point. drop the cleanup function return in 53 // favour of listening for ctx.Done and running the cleanup routine internally 54 func NewTempRegistry(ctx context.Context, peername, tmpDirPrefix string, g key.CryptoGenerator) (*registry.Registry, func(), error) { 55 tempRepo, err := repotest.NewTempRepo(peername, tmpDirPrefix, g) 56 if err != nil { 57 return nil, nil, err 58 } 59 60 r, err := tempRepo.Repo(ctx) 61 if err != nil { 62 return nil, nil, err 63 } 64 65 teardown := func() { 66 <-r.Done() 67 tempRepo.Delete() 68 } 69 70 p2pCfg := config.DefaultP2P() 71 p2pCfg.PeerID = registryPeerID 72 73 localResolver := dsref.SequentialResolver(r.Dscache(), r) 74 node, err := p2p.NewQriNode(r, p2pCfg, r.Bus(), localResolver) 75 if err != nil { 76 return nil, nil, err 77 } 78 79 remoteCfg := &config.RemoteServer{ 80 Enabled: true, 81 AcceptSizeMax: -1, 82 AcceptTimeoutMs: -1, 83 RequireAllBlocks: false, 84 AllowRemoves: true, 85 } 86 87 rem, err := remote.NewServer(node, remoteCfg, node.Repo.Logbook(), node.Repo.Bus()) 88 if err != nil { 89 return nil, nil, err 90 } 91 92 reg := ®istry.Registry{ 93 Remote: rem, 94 Profiles: registry.NewMemProfiles(), 95 Search: MockRepoSearch{Repo: r}, 96 } 97 98 return reg, teardown, nil 99 } 100 101 // MockRepoSearch proxies search to base.ListDatasets' "term" argument for 102 // simple-but-real search 103 type MockRepoSearch struct { 104 repo.Repo 105 } 106 107 // Search implements the registry.Searchable interface 108 func (ss MockRepoSearch) Search(p registry.SearchParams) ([]*dataset.Dataset, error) { 109 ctx := context.Background() 110 infos, err := base.ListDatasets(ctx, ss.Repo, p.Q, "", 0, 1000, true, false) 111 if err != nil { 112 return nil, err 113 } 114 115 var res []*dataset.Dataset 116 for _, info := range infos { 117 ds := dsref.ConvertVersionInfoToDataset(&info) 118 res = append(res, ds) 119 } 120 return res, nil 121 }