1/*
2   Copyright The containerd Authors.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17package containerd
18
19import (
20	"runtime"
21	"testing"
22
23	"github.com/containerd/containerd/errdefs"
24	"github.com/containerd/containerd/images"
25	"github.com/containerd/containerd/leases"
26	"github.com/opencontainers/image-spec/identity"
27	"github.com/pkg/errors"
28)
29
30func TestLeaseResources(t *testing.T) {
31	if runtime.GOOS == "windows" {
32		t.Skip()
33	}
34
35	ctx, cancel := testContext(t)
36	defer cancel()
37
38	client, err := newClient(t, address)
39	if err != nil {
40		t.Fatal(err)
41	}
42	defer client.Close()
43
44	var (
45		ls     = client.LeasesService()
46		cs     = client.ContentStore()
47		imgSrv = client.ImageService()
48		sn     = client.SnapshotService("native")
49	)
50
51	l, err := ls.Create(ctx, leases.WithRandomID())
52	if err != nil {
53		t.Fatal(err)
54	}
55	defer ls.Delete(ctx, l, leases.SynchronousDelete)
56
57	// step 1: download image
58	imageName := "docker.io/library/busybox:1.25"
59
60	image, err := client.Pull(ctx, imageName, WithPullUnpack, WithPullSnapshotter("native"))
61	if err != nil {
62		t.Fatal(err)
63	}
64	defer imgSrv.Delete(ctx, imageName)
65
66	// both the config and snapshotter should exist
67	cfgDesc, err := image.Config(ctx)
68	if err != nil {
69		t.Fatal(err)
70	}
71
72	if _, err := cs.Info(ctx, cfgDesc.Digest); err != nil {
73		t.Fatal(err)
74	}
75
76	dgsts, err := image.RootFS(ctx)
77	if err != nil {
78		t.Fatal(err)
79	}
80	chainID := identity.ChainID(dgsts)
81
82	if _, err := sn.Stat(ctx, chainID.String()); err != nil {
83		t.Fatal(err)
84	}
85
86	// step 2: reference snapshotter with lease
87	r := leases.Resource{
88		ID:   chainID.String(),
89		Type: "snapshots/native",
90	}
91
92	if err := ls.AddResource(ctx, l, r); err != nil {
93		t.Fatal(err)
94	}
95
96	list, err := ls.ListResources(ctx, l)
97	if err != nil {
98		t.Fatal(err)
99	}
100
101	if len(list) != 1 || list[0] != r {
102		t.Fatalf("expected (%v), but got (%v)", []leases.Resource{r}, list)
103	}
104
105	// step 3: remove image and check the status of snapshotter and content
106	if err := imgSrv.Delete(ctx, imageName, images.SynchronousDelete()); err != nil {
107		t.Fatal(err)
108	}
109
110	// config should be removed but the snapshotter should exist
111	if _, err := cs.Info(ctx, cfgDesc.Digest); errors.Cause(err) != errdefs.ErrNotFound {
112		t.Fatalf("expected error(%v), but got(%v)", errdefs.ErrNotFound, err)
113	}
114
115	if _, err := sn.Stat(ctx, chainID.String()); err != nil {
116		t.Fatal(err)
117	}
118
119	// step 4: remove resource from the lease and check the list API
120	if err := ls.DeleteResource(ctx, l, r); err != nil {
121		t.Fatal(err)
122	}
123
124	list, err = ls.ListResources(ctx, l)
125	if err != nil {
126		t.Fatal(err)
127	}
128
129	if len(list) != 0 {
130		t.Fatalf("expected nothing, but got (%v)", list)
131	}
132
133	// step 5: remove the lease to check the status of snapshotter
134	if err := ls.Delete(ctx, l, leases.SynchronousDelete); err != nil {
135		t.Fatal(err)
136	}
137
138	if _, err := sn.Stat(ctx, chainID.String()); errors.Cause(err) != errdefs.ErrNotFound {
139		t.Fatalf("expected error(%v), but got(%v)", errdefs.ErrNotFound, err)
140	}
141}
142