|
| 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 integration |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "syscall" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/containerd/errdefs" |
| 29 | + "github.com/opencontainers/image-spec/identity" |
| 30 | + "github.com/stretchr/testify/assert" |
| 31 | + "github.com/stretchr/testify/require" |
| 32 | + criruntime "k8s.io/cri-api/pkg/apis/runtime/v1" |
| 33 | + |
| 34 | + containerd "github.com/containerd/containerd/v2/client" |
| 35 | + "github.com/containerd/containerd/v2/integration/images" |
| 36 | + snpkg "github.com/containerd/containerd/v2/pkg/snapshotters" |
| 37 | + "github.com/containerd/containerd/v2/plugins" |
| 38 | +) |
| 39 | + |
| 40 | +func TestRuntimeHandlerUnpackWithSnapshotLabels(t *testing.T) { |
| 41 | + workDir := t.TempDir() |
| 42 | + cfgPath := filepath.Join(workDir, "config.toml") |
| 43 | + cfg := ` |
| 44 | +version = 3 |
| 45 | +
|
| 46 | +[plugins.'io.containerd.cri.v1.images'] |
| 47 | + snapshotter = "overlayfs" |
| 48 | + disable_snapshot_annotations = false |
| 49 | +
|
| 50 | +[plugins.'io.containerd.cri.v1.runtime'.containerd] |
| 51 | + default_runtime_name = "runc" |
| 52 | +
|
| 53 | +[plugins.'io.containerd.cri.v1.runtime'.containerd.runtimes.runc] |
| 54 | + runtime_type = "io.containerd.runc.v2" |
| 55 | + snapshotter = "overlayfs" |
| 56 | +
|
| 57 | +[plugins.'io.containerd.cri.v1.runtime'.containerd.runtimes.erofs] |
| 58 | + runtime_type = "io.containerd.runc.v2" |
| 59 | + snapshotter = "erofs" |
| 60 | +` |
| 61 | + require.NoError(t, os.WriteFile(cfgPath, []byte(cfg), 0o600)) |
| 62 | + |
| 63 | + ctrd := newCtrdProc(t, *containerdBin, workDir, nil) |
| 64 | + require.NoError(t, ctrd.isReady()) |
| 65 | + |
| 66 | + rSvc := ctrd.criRuntimeService(t) |
| 67 | + iSvc := ctrd.criImageService(t) |
| 68 | + |
| 69 | + ctrdClient, err := containerd.New(ctrd.grpcAddress(), containerd.WithDefaultNamespace(k8sNamespace)) |
| 70 | + require.NoError(t, err) |
| 71 | + |
| 72 | + t.Cleanup(func() { |
| 73 | + if t.Failed() { |
| 74 | + t.Log("Dumping containerd config and logs due to test failure") |
| 75 | + dumpFileContent(t, ctrd.configPath()) |
| 76 | + dumpFileContent(t, ctrd.logPath()) |
| 77 | + } |
| 78 | + assert.NoError(t, ctrdClient.Close()) |
| 79 | + cleanupPods(t, rSvc) |
| 80 | + assert.NoError(t, ctrd.kill(syscall.SIGTERM)) |
| 81 | + assert.NoError(t, ctrd.wait(5*time.Minute)) |
| 82 | + }) |
| 83 | + |
| 84 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 85 | + defer cancel() |
| 86 | + resp, err := ctrdClient.IntrospectionService().Plugins(ctx, fmt.Sprintf("type==%s,id==%s", plugins.SnapshotPlugin, "erofs")) |
| 87 | + require.NoError(t, err) |
| 88 | + if len(resp.Plugins) == 0 { |
| 89 | + t.Skip("erofs snapshotter plugin is not registered") |
| 90 | + } |
| 91 | + if initErr := resp.Plugins[0].InitErr; initErr != nil { |
| 92 | + t.Skipf("erofs snapshotter plugin is not ready: %s", initErr.Message) |
| 93 | + } |
| 94 | + |
| 95 | + nginxImage := images.Get(images.Nginx) |
| 96 | + pullImagesByCRI(t, iSvc, nginxImage) |
| 97 | + |
| 98 | + img, err := ctrdClient.GetImage(context.Background(), nginxImage) |
| 99 | + require.NoError(t, err) |
| 100 | + diffIDs, err := img.RootFS(context.Background()) |
| 101 | + require.NoError(t, err) |
| 102 | + chainIDs := identity.ChainIDs(diffIDs) |
| 103 | + |
| 104 | + // First pod uses default runtime handler (overlayfs). No containers created. |
| 105 | + sb1Cfg := PodSandboxConfig("overlay-pod", "runtime-handler-unpack") |
| 106 | + _, err = rSvc.RunPodSandbox(sb1Cfg, "") |
| 107 | + require.NoError(t, err) |
| 108 | + |
| 109 | + // Image is pulled with overlayfs; nginx snapshots should not exist on erofs yet. |
| 110 | + erofsSn := ctrdClient.SnapshotService("erofs") |
| 111 | + for _, chainID := range chainIDs { |
| 112 | + _, err := erofsSn.Stat(context.Background(), chainID.String()) |
| 113 | + assert.Truef(t, errdefs.IsNotFound(err), "expected no erofs snapshot for chainID %s before erofs container creation, got err=%v", chainID, err) |
| 114 | + } |
| 115 | + |
| 116 | + // Second pod uses erofs runtime handler. Creating nginx container should trigger |
| 117 | + // automatic unpack for erofs with snapshot labels. |
| 118 | + sb2Cfg := PodSandboxConfig("erofs-pod", "runtime-handler-unpack") |
| 119 | + sb2ID, err := rSvc.RunPodSandbox(sb2Cfg, "erofs") |
| 120 | + require.NoError(t, err) |
| 121 | + cn2Cfg := ContainerConfig("erofs-container", nginxImage, WithCommand("sleep", "1d")) |
| 122 | + cn2ID, err := rSvc.CreateContainer(sb2ID, cn2Cfg, sb2Cfg) |
| 123 | + require.NoError(t, err) |
| 124 | + |
| 125 | + for _, chainID := range chainIDs { |
| 126 | + snInfo, err := erofsSn.Stat(context.Background(), chainID.String()) |
| 127 | + require.NoErrorf(t, err, "failed to stat erofs snapshot for chainID %s", chainID) |
| 128 | + require.NotNil(t, snInfo.Labels) |
| 129 | + assert.NotEmpty(t, snInfo.Labels[snpkg.TargetRefLabel], "missing %s on chainID %s", snpkg.TargetRefLabel, chainID) |
| 130 | + assert.NotEmpty(t, snInfo.Labels[snpkg.TargetManifestDigestLabel], "missing %s on chainID %s", snpkg.TargetManifestDigestLabel, chainID) |
| 131 | + assert.NotEmpty(t, snInfo.Labels[snpkg.TargetLayerDigestLabel], "missing %s on chainID %s", snpkg.TargetLayerDigestLabel, chainID) |
| 132 | + assert.NotEmpty(t, snInfo.Labels[snpkg.TargetImageLayersLabel], "missing %s on chainID %s", snpkg.TargetImageLayersLabel, chainID) |
| 133 | + } |
| 134 | + |
| 135 | + // Make sure the second pod really used the erofs runtime handler path. |
| 136 | + sb2Status, err := rSvc.PodSandboxStatus(sb2ID) |
| 137 | + require.NoError(t, err) |
| 138 | + assert.Equal(t, "erofs", sb2Status.RuntimeHandler) |
| 139 | + |
| 140 | + // Container should be created. |
| 141 | + cn2Status, err := rSvc.ContainerStatus(cn2ID) |
| 142 | + require.NoError(t, err) |
| 143 | + assert.Equal(t, criruntime.ContainerState_CONTAINER_CREATED, cn2Status.State) |
| 144 | +} |
0 commit comments