|
| 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 | + "io/ioutil" |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/containerd/containerd/labels" |
| 25 | + digest "github.com/opencontainers/go-digest" |
| 26 | + "github.com/pkg/errors" |
| 27 | + bolt "go.etcd.io/bbolt" |
| 28 | +) |
| 29 | + |
| 30 | +func TestHasSharedLabel(t *testing.T) { |
| 31 | + tmpdir, err := ioutil.TempDir("", "bucket-testing-") |
| 32 | + if err != nil { |
| 33 | + t.Error(err) |
| 34 | + } |
| 35 | + |
| 36 | + db, err := bolt.Open(filepath.Join(tmpdir, "metadata.db"), 0660, nil) |
| 37 | + if err != nil { |
| 38 | + t.Error(err) |
| 39 | + } |
| 40 | + |
| 41 | + err = createNamespaceLabelsBucket(db, "testing-with-shareable", true) |
| 42 | + if err != nil { |
| 43 | + t.Error(err) |
| 44 | + } |
| 45 | + |
| 46 | + err = createNamespaceLabelsBucket(db, "testing-without-shareable", false) |
| 47 | + if err != nil { |
| 48 | + t.Error(err) |
| 49 | + } |
| 50 | + |
| 51 | + err = db.View(func(tx *bolt.Tx) error { |
| 52 | + if !hasSharedLabel(tx, "testing-with-shareable") { |
| 53 | + return errors.New("hasSharedLabel should return true when label is set") |
| 54 | + } |
| 55 | + if hasSharedLabel(tx, "testing-without-shareable") { |
| 56 | + return errors.New("hasSharedLabel should return false when label is not set") |
| 57 | + } |
| 58 | + return nil |
| 59 | + }) |
| 60 | + |
| 61 | + if err != nil { |
| 62 | + t.Error(err) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestGetShareableBucket(t *testing.T) { |
| 67 | + tmpdir, err := ioutil.TempDir("", "bucket-testing-") |
| 68 | + if err != nil { |
| 69 | + t.Error(err) |
| 70 | + } |
| 71 | + |
| 72 | + db, err := bolt.Open(filepath.Join(tmpdir, "metadata.db"), 0660, nil) |
| 73 | + if err != nil { |
| 74 | + t.Error(err) |
| 75 | + } |
| 76 | + |
| 77 | + goodDigest := digest.FromString("gooddigest") |
| 78 | + imagePresentNS := "has-image-is-shareable" |
| 79 | + imageAbsentNS := "image-absent" |
| 80 | + |
| 81 | + // Create two namespaces, empty for now |
| 82 | + err = db.Update(func(tx *bolt.Tx) error { |
| 83 | + _, err := createImagesBucket(tx, imagePresentNS) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + _, err = createImagesBucket(tx, imageAbsentNS) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | + }) |
| 95 | + |
| 96 | + if err != nil { |
| 97 | + t.Error(err) |
| 98 | + } |
| 99 | + |
| 100 | + // Test that getShareableBucket is correctly returning nothing when a |
| 101 | + // a bucket with that digest is not present in any namespace. |
| 102 | + err = db.View(func(tx *bolt.Tx) error { |
| 103 | + if bkt := getShareableBucket(tx, goodDigest); bkt != nil { |
| 104 | + return errors.New("getShareableBucket should return nil if digest is not present") |
| 105 | + } |
| 106 | + return nil |
| 107 | + }) |
| 108 | + |
| 109 | + if err != nil { |
| 110 | + t.Error(err) |
| 111 | + } |
| 112 | + |
| 113 | + // Create a blob bucket in one of the namespaces with a well-known digest |
| 114 | + err = db.Update(func(tx *bolt.Tx) error { |
| 115 | + _, err = createBlobBucket(tx, imagePresentNS, goodDigest) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + return nil |
| 120 | + }) |
| 121 | + |
| 122 | + if err != nil { |
| 123 | + t.Error(err) |
| 124 | + } |
| 125 | + |
| 126 | + // Verify that it is still not retrievable if the shareable label is not present |
| 127 | + err = db.View(func(tx *bolt.Tx) error { |
| 128 | + if bkt := getShareableBucket(tx, goodDigest); bkt != nil { |
| 129 | + return errors.New("getShareableBucket should return nil if digest is present but doesn't have shareable label") |
| 130 | + } |
| 131 | + return nil |
| 132 | + }) |
| 133 | + |
| 134 | + if err != nil { |
| 135 | + t.Error(err) |
| 136 | + } |
| 137 | + |
| 138 | + // Create the namespace labels bucket and mark it as shareable |
| 139 | + err = createNamespaceLabelsBucket(db, imagePresentNS, true) |
| 140 | + if err != nil { |
| 141 | + t.Error(err) |
| 142 | + } |
| 143 | + |
| 144 | + // Verify that this digest is retrievable from getShareableBucket |
| 145 | + err = db.View(func(tx *bolt.Tx) error { |
| 146 | + if bkt := getShareableBucket(tx, goodDigest); bkt == nil { |
| 147 | + return errors.New("getShareableBucket should not return nil if digest is present") |
| 148 | + } |
| 149 | + return nil |
| 150 | + }) |
| 151 | + |
| 152 | + if err != nil { |
| 153 | + t.Error(err) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func createNamespaceLabelsBucket(db transactor, ns string, shareable bool) error { |
| 158 | + err := db.Update(func(tx *bolt.Tx) error { |
| 159 | + err := withNamespacesLabelsBucket(tx, ns, func(bkt *bolt.Bucket) error { |
| 160 | + if shareable { |
| 161 | + err := bkt.Put([]byte(labels.LabelSharedNamespace), []byte("true")) |
| 162 | + if err != nil { |
| 163 | + return err |
| 164 | + } |
| 165 | + } |
| 166 | + return nil |
| 167 | + }) |
| 168 | + return err |
| 169 | + }) |
| 170 | + return err |
| 171 | +} |
0 commit comments