|
| 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 client |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/containerd/containerd/v2/core/containers" |
| 25 | + "github.com/containerd/containerd/v2/core/content" |
| 26 | + "github.com/opencontainers/go-digest" |
| 27 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | +) |
| 31 | + |
| 32 | +// fakeImage implements the subset of Image used by WithImageConfigLabels: |
| 33 | +// Config returns a descriptor with the config blob inlined in Data, so the |
| 34 | +// content store is never consulted. |
| 35 | +type fakeImage struct { |
| 36 | + Image |
| 37 | + config ocispec.Descriptor |
| 38 | +} |
| 39 | + |
| 40 | +func (i fakeImage) Config(context.Context) (ocispec.Descriptor, error) { |
| 41 | + return i.config, nil |
| 42 | +} |
| 43 | + |
| 44 | +func (i fakeImage) ContentStore() content.Store { |
| 45 | + return nil |
| 46 | +} |
| 47 | + |
| 48 | +func TestWithImageConfigLabels(t *testing.T) { |
| 49 | + blob, err := json.Marshal(ocispec.Image{ |
| 50 | + Config: ocispec.ImageConfig{ |
| 51 | + Labels: map[string]string{ |
| 52 | + "foo": "bar", |
| 53 | + "containerd.io/restart.policy": "always", |
| 54 | + "io.cri-containerd.kind": "sandbox", |
| 55 | + }, |
| 56 | + }, |
| 57 | + }) |
| 58 | + require.NoError(t, err) |
| 59 | + |
| 60 | + img := fakeImage{ |
| 61 | + config: ocispec.Descriptor{ |
| 62 | + MediaType: ocispec.MediaTypeImageConfig, |
| 63 | + Digest: digest.FromBytes(blob), |
| 64 | + Size: int64(len(blob)), |
| 65 | + Data: blob, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + var c containers.Container |
| 70 | + require.NoError(t, WithImageConfigLabels(img)(t.Context(), nil, &c)) |
| 71 | + |
| 72 | + // labels in the namespaces reserved for containerd and the CRI plugin |
| 73 | + // are not copied from the image config |
| 74 | + assert.Equal(t, map[string]string{"foo": "bar"}, c.Labels) |
| 75 | +} |
0 commit comments