diff --git a/cluster_test.go b/cluster_test.go index 784e90e5..0a27d11a 100644 --- a/cluster_test.go +++ b/cluster_test.go @@ -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) } diff --git a/ipfscluster_test.go b/ipfscluster_test.go index a3b3e462..c889f1fe 100644 --- a/ipfscluster_test.go +++ b/ipfscluster_test.go @@ -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) { diff --git a/test/ipfs_mock.go b/test/ipfs_mock.go index 5428b1f6..1948acd4 100644 --- a/test/ipfs_mock.go +++ b/test/ipfs_mock.go @@ -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")) +}