github.com/lalkh/containerd@v1.4.3/lease_test.go (about)

     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  
    17  package containerd
    18  
    19  import (
    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  )
    28  
    29  func TestLeaseResources(t *testing.T) {
    30  	if runtime.GOOS == "windows" {
    31  		t.Skip()
    32  	}
    33  
    34  	ctx, cancel := testContext(t)
    35  	defer cancel()
    36  
    37  	client, err := newClient(t, address)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	defer client.Close()
    42  
    43  	var (
    44  		ls     = client.LeasesService()
    45  		cs     = client.ContentStore()
    46  		imgSrv = client.ImageService()
    47  		sn     = client.SnapshotService("native")
    48  	)
    49  
    50  	l, err := ls.Create(ctx, leases.WithRandomID())
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	defer ls.Delete(ctx, l, leases.SynchronousDelete)
    55  
    56  	// step 1: download image
    57  	imageName := "docker.io/library/busybox:1.25"
    58  
    59  	image, err := client.Pull(ctx, imageName, WithPullUnpack, WithPullSnapshotter("native"))
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	defer imgSrv.Delete(ctx, imageName)
    64  
    65  	// both the config and snapshotter should exist
    66  	cfgDesc, err := image.Config(ctx)
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  
    71  	if _, err := cs.Info(ctx, cfgDesc.Digest); err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	dgsts, err := image.RootFS(ctx)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	chainID := identity.ChainID(dgsts)
    80  
    81  	if _, err := sn.Stat(ctx, chainID.String()); err != nil {
    82  		t.Fatal(err)
    83  	}
    84  
    85  	// step 2: reference snapshotter with lease
    86  	r := leases.Resource{
    87  		ID:   chainID.String(),
    88  		Type: "snapshots/native",
    89  	}
    90  
    91  	if err := ls.AddResource(ctx, l, r); err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	list, err := ls.ListResources(ctx, l)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  
   100  	if len(list) != 1 || list[0] != r {
   101  		t.Fatalf("expected (%v), but got (%v)", []leases.Resource{r}, list)
   102  	}
   103  
   104  	// step 3: remove image and check the status of snapshotter and content
   105  	if err := imgSrv.Delete(ctx, imageName, images.SynchronousDelete()); err != nil {
   106  		t.Fatal(err)
   107  	}
   108  
   109  	// config should be removed but the snapshotter should exist
   110  	if _, err := cs.Info(ctx, cfgDesc.Digest); !errdefs.IsNotFound(err) {
   111  		t.Fatalf("expected error(%v), but got(%v)", errdefs.ErrNotFound, err)
   112  	}
   113  
   114  	if _, err := sn.Stat(ctx, chainID.String()); err != nil {
   115  		t.Fatal(err)
   116  	}
   117  
   118  	// step 4: remove resource from the lease and check the list API
   119  	if err := ls.DeleteResource(ctx, l, r); err != nil {
   120  		t.Fatal(err)
   121  	}
   122  
   123  	list, err = ls.ListResources(ctx, l)
   124  	if err != nil {
   125  		t.Fatal(err)
   126  	}
   127  
   128  	if len(list) != 0 {
   129  		t.Fatalf("expected nothing, but got (%v)", list)
   130  	}
   131  
   132  	// step 5: remove the lease to check the status of snapshotter
   133  	if err := ls.Delete(ctx, l, leases.SynchronousDelete); err != nil {
   134  		t.Fatal(err)
   135  	}
   136  
   137  	if _, err := sn.Stat(ctx, chainID.String()); !errdefs.IsNotFound(err) {
   138  		t.Fatalf("expected error(%v), but got(%v)", errdefs.ErrNotFound, err)
   139  	}
   140  }