github.com/demonoid81/containerd@v1.3.4/metadata/snapshot_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 metadata
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"path/filepath"
    23  	"reflect"
    24  	"runtime"
    25  	"testing"
    26  
    27  	"github.com/containerd/containerd/pkg/testutil"
    28  	"github.com/containerd/containerd/snapshots"
    29  	"github.com/containerd/containerd/snapshots/native"
    30  	"github.com/containerd/containerd/snapshots/testsuite"
    31  	bolt "go.etcd.io/bbolt"
    32  )
    33  
    34  func newTestSnapshotter(ctx context.Context, root string) (snapshots.Snapshotter, func() error, error) {
    35  	nativeRoot := filepath.Join(root, "native")
    36  	if err := os.Mkdir(nativeRoot, 0770); err != nil {
    37  		return nil, nil, err
    38  	}
    39  	snapshotter, err := native.NewSnapshotter(nativeRoot)
    40  	if err != nil {
    41  		return nil, nil, err
    42  	}
    43  
    44  	db, err := bolt.Open(filepath.Join(root, "metadata.db"), 0660, nil)
    45  	if err != nil {
    46  		return nil, nil, err
    47  	}
    48  
    49  	sn := NewDB(db, nil, map[string]snapshots.Snapshotter{"native": snapshotter}).Snapshotter("native")
    50  
    51  	return sn, func() error {
    52  		if err := sn.Close(); err != nil {
    53  			return err
    54  		}
    55  		return db.Close()
    56  	}, nil
    57  }
    58  
    59  func TestMetadata(t *testing.T) {
    60  	if runtime.GOOS == "windows" {
    61  		t.Skip("snapshotter not implemented on windows")
    62  	}
    63  	// Snapshot tests require mounting, still requires root
    64  	testutil.RequiresRoot(t)
    65  	testsuite.SnapshotterSuite(t, "Metadata", newTestSnapshotter)
    66  }
    67  
    68  func TestFilterInheritedLabels(t *testing.T) {
    69  	tests := []struct {
    70  		labels   map[string]string
    71  		expected map[string]string
    72  	}{
    73  		{
    74  			nil,
    75  			nil,
    76  		},
    77  		{
    78  			map[string]string{},
    79  			map[string]string{},
    80  		},
    81  		{
    82  			map[string]string{"": ""},
    83  			map[string]string{},
    84  		},
    85  		{
    86  			map[string]string{"foo": "bar"},
    87  			map[string]string{},
    88  		},
    89  		{
    90  			map[string]string{inheritedLabelsPrefix + "foo": "bar"},
    91  			map[string]string{inheritedLabelsPrefix + "foo": "bar"},
    92  		},
    93  		{
    94  			map[string]string{inheritedLabelsPrefix + "foo": "bar", "qux": "qaz"},
    95  			map[string]string{inheritedLabelsPrefix + "foo": "bar"},
    96  		},
    97  	}
    98  
    99  	for _, test := range tests {
   100  		if actual := filterInheritedLabels(test.labels); !reflect.DeepEqual(actual, test.expected) {
   101  			t.Fatalf("expected %v but got %v", test.expected, actual)
   102  		}
   103  	}
   104  }