Direct pins: fix add tests for direct pinning

This commit is contained in:
Hector Sanjuan 2020-04-23 18:05:05 +02:00
parent 75cc2e5be1
commit 84eddf4ac8
3 changed files with 72 additions and 20 deletions

View File

@ -76,7 +76,7 @@ func (ipfs *mockConnector) PinLsCid(ctx context.Context, pin *api.Pin) (api.IPFS
if !ok {
return api.IPFSPinStatusUnpinned, nil
}
depth := dI.(int)
depth := dI.(api.PinDepth)
if depth == 0 {
return api.IPFSPinStatusDirect, nil
}
@ -87,7 +87,7 @@ func (ipfs *mockConnector) PinLs(ctx context.Context, filter string) (map[string
m := make(map[string]api.IPFSPinStatus)
var st api.IPFSPinStatus
ipfs.pins.Range(func(k, v interface{}) bool {
switch v.(int) {
switch v.(api.PinDepth) {
case 0:
st = api.IPFSPinStatusDirect
default:
@ -477,7 +477,7 @@ func TestUnpinShard(t *testing.T) {
t.Errorf("%s should have been unpinned but is %s", c, st.Status)
}
st2, err := cl.ipfs.PinLsCid(context.Background(), c)
st2, err := cl.ipfs.PinLsCid(context.Background(), api.PinCid(c))
if err != nil {
t.Fatal(err)
}

View File

@ -758,7 +758,49 @@ func TestClustersPinUpdate(t *testing.T) {
}
}
runF(t, clusters, f)
}
func TestClustersPinDirect(t *testing.T) {
ctx := context.Background()
clusters, mock := createClusters(t)
defer shutdownClusters(t, clusters, mock)
prefix := test.Cid1.Prefix()
ttlDelay()
h, _ := prefix.Sum(randomBytes()) // create random cid
_, err := clusters[0].Pin(ctx, h, api.PinOptions{Mode: api.PinModeDirect})
if err != nil {
t.Fatal(err)
}
pinDelay()
f := func(t *testing.T, c *Cluster) {
pinget, err := c.PinGet(ctx, h)
if err != nil {
t.Fatal(err)
}
if pinget.Mode != api.PinModeDirect {
t.Error("pin should be pinned in direct mode")
}
if pinget.MaxDepth != 0 {
t.Error("pin should have max-depth = 0")
}
pInfo := c.StatusLocal(ctx, h)
if pInfo.Error != "" {
t.Error(pInfo.Error)
}
if pInfo.Status != api.TrackerStatusPinned {
t.Error(pInfo.Error)
t.Error("the status should show the hash as pinned")
}
}
runF(t, clusters, f)
}
func TestClustersStatusAll(t *testing.T) {

View File

@ -191,7 +191,12 @@ func (m *IpfsMock) handler(w http.ResponseWriter, r *http.Request) {
if err != nil {
goto ERROR
}
m.pinMap.Add(ctx, api.PinCid(c))
mode := extractMode(r.URL)
opts := api.PinOptions{
Mode: mode,
}
pinObj := api.PinWithOpts(c, opts)
m.pinMap.Add(ctx, pinObj)
resp := mockPinResp{
Pins: []string{arg},
}
@ -267,7 +272,7 @@ func (m *IpfsMock) handler(w http.ResponseWriter, r *http.Request) {
goto ERROR
}
for _, p := range pins {
rMap[p.Cid.String()] = mockPinType{"recursive"}
rMap[p.Cid.String()] = mockPinType{p.Mode.String()}
}
j, _ := json.Marshal(mockPinLsResp{rMap})
w.Write(j)
@ -280,27 +285,28 @@ func (m *IpfsMock) handler(w http.ResponseWriter, r *http.Request) {
goto ERROR
}
ok, err = m.pinMap.Has(ctx, c)
if err != nil {
pinObj, err := m.pinMap.Get(ctx, c)
if err != nil && err != state.ErrNotFound {
goto ERROR
}
if ok {
if c.Equals(Cid4) {
// this a v1 cid. Do not return default-base32 but base58btc encoding of it
w.Write([]byte(`{ "Keys": { "zCT5htkdztJi3x4zBNHo8TRvGHPLTdHUdCLKgTGMgQcRKSLoWxK1": { "Type": "recursive" }}}`))
return
}
rMap := make(map[string]mockPinType)
rMap[cidStr] = mockPinType{"recursive"}
j, _ := json.Marshal(mockPinLsResp{rMap})
w.Write(j)
} else {
if err == state.ErrNotFound {
w.WriteHeader(http.StatusInternalServerError)
resp := ipfsErr{0, fmt.Sprintf("Path '%s' is not pinned", cidStr)}
j, _ := json.Marshal(resp)
w.Write(j)
return
}
if c.Equals(Cid4) {
// this a v1 cid. Do not return default-base32 but base58btc encoding of it
w.Write([]byte(`{ "Keys": { "zCT5htkdztJi3x4zBNHo8TRvGHPLTdHUdCLKgTGMgQcRKSLoWxK1": { "Type": "recursive" }}}`))
return
}
rMap := make(map[string]mockPinType)
rMap[cidStr] = mockPinType{pinObj.Mode.String()}
j, _ := json.Marshal(mockPinLsResp{rMap})
w.Write(j)
case "swarm/connect":
arg, ok := extractCid(r.URL)
if !ok {
@ -480,7 +486,7 @@ func (m *IpfsMock) Close() {
}
}
// extractCid extracts the cid argument from a url.URL, either via
// extractCidAndMode extracts the cid argument from a url.URL, either via
// the query string parameters or from the url path itself.
func extractCid(u *url.URL) (string, bool) {
arg := u.Query().Get("arg")
@ -496,3 +502,7 @@ func extractCid(u *url.URL) (string, bool) {
}
return "", false
}
func extractMode(u *url.URL) api.PinMode {
return api.PinModeFromString(u.Query().Get("type"))
}