github.com/demonoid81/containerd@v1.3.4/content_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  	"context"
    21  	"fmt"
    22  	"sync/atomic"
    23  	"testing"
    24  
    25  	"github.com/containerd/containerd/content"
    26  	"github.com/containerd/containerd/content/testsuite"
    27  	"github.com/containerd/containerd/errdefs"
    28  	"github.com/containerd/containerd/namespaces"
    29  	"github.com/pkg/errors"
    30  )
    31  
    32  func newContentStore(ctx context.Context, root string) (context.Context, content.Store, func() error, error) {
    33  	client, err := New(address)
    34  	if err != nil {
    35  		return nil, nil, nil, err
    36  	}
    37  
    38  	var (
    39  		count uint64
    40  		cs    = client.ContentStore()
    41  		name  = testsuite.Name(ctx)
    42  	)
    43  
    44  	wrap := func(ctx context.Context) (context.Context, func(context.Context) error, error) {
    45  		n := atomic.AddUint64(&count, 1)
    46  		ctx = namespaces.WithNamespace(ctx, fmt.Sprintf("%s-n%d", name, n))
    47  		return client.WithLease(ctx)
    48  	}
    49  
    50  	ctx = testsuite.SetContextWrapper(ctx, wrap)
    51  
    52  	return ctx, cs, func() error {
    53  		for i := uint64(1); i <= count; i++ {
    54  			ctx = namespaces.WithNamespace(ctx, fmt.Sprintf("%s-n%d", name, i))
    55  			statuses, err := cs.ListStatuses(ctx)
    56  			if err != nil {
    57  				return err
    58  			}
    59  			for _, st := range statuses {
    60  				if err := cs.Abort(ctx, st.Ref); err != nil && !errdefs.IsNotFound(err) {
    61  					return errors.Wrapf(err, "failed to abort %s", st.Ref)
    62  				}
    63  			}
    64  			err = cs.Walk(ctx, func(info content.Info) error {
    65  				if err := cs.Delete(ctx, info.Digest); err != nil {
    66  					if errdefs.IsNotFound(err) {
    67  						return nil
    68  					}
    69  
    70  					return err
    71  				}
    72  				return nil
    73  			})
    74  			if err != nil {
    75  				return err
    76  			}
    77  		}
    78  		return nil
    79  
    80  	}, nil
    81  }
    82  
    83  func TestContentClient(t *testing.T) {
    84  	if testing.Short() {
    85  		t.Skip()
    86  	}
    87  	testsuite.ContentSuite(t, "ContentClient", newContentStore)
    88  }