|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/containerd/containerd" |
| 22 | + "github.com/containerd/containerd/namespaces" |
| 23 | + "github.com/containerd/containerd/oci" |
| 24 | + "github.com/firecracker-microvm/firecracker-containerd/internal/integtest" |
| 25 | + "github.com/firecracker-microvm/firecracker-containerd/proto" |
| 26 | + "github.com/firecracker-microvm/firecracker-containerd/runtime/firecrackeroci" |
| 27 | + "github.com/firecracker-microvm/firecracker-containerd/snapshotter/internal/integtest/stargz/fs/source" |
| 28 | + "github.com/firecracker-microvm/firecracker-containerd/volume" |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | + "github.com/stretchr/testify/require" |
| 31 | +) |
| 32 | + |
| 33 | +const mib = 1024 * 1024 |
| 34 | + |
| 35 | +func TestGuestVolumeFrom_Isolated(t *testing.T) { |
| 36 | + integtest.Prepare(t, integtest.WithDefaultNetwork()) |
| 37 | + const ( |
| 38 | + vmID = "default" |
| 39 | + postgres = "docker.io/library/postgres:14.3" |
| 40 | + alpine = "docker.io/library/alpine:3.10.1" |
| 41 | + runtime = "aws.firecracker" |
| 42 | + ) |
| 43 | + |
| 44 | + ctx := namespaces.WithNamespace(context.Background(), vmID) |
| 45 | + |
| 46 | + client, err := containerd.New(integtest.ContainerdSockPath, containerd.WithDefaultRuntime(runtime)) |
| 47 | + require.NoError(t, err, "unable to create client to containerd service at %s, is containerd running?", integtest.ContainerdSockPath) |
| 48 | + defer client.Close() |
| 49 | + fcClient, err := integtest.NewFCControlClient(integtest.ContainerdSockPath) |
| 50 | + require.NoError(t, err, "Failed to create fccontrol client") |
| 51 | + |
| 52 | + vs := volume.NewSet(runtime) |
| 53 | + |
| 54 | + // Add a non-stargz image with volumes. |
| 55 | + localImage := volume.FromImage(client, postgres, "postgres-snapshot", volume.WithSnapshotter("devmapper")) |
| 56 | + err = vs.AddFrom(ctx, localImage) |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + // Add a stargz image. |
| 60 | + // The volume directories must be specified since the host's containerd doesn't know about the image. |
| 61 | + remoteImage := volume.FromGuestImage( |
| 62 | + client, vmID, al2stargz, "al2-snapshot", []string{"/etc/yum"}, |
| 63 | + volume.WithSnapshotter("demux"), |
| 64 | + volume.WithPullOptions(containerd.WithImageHandlerWrapper( |
| 65 | + source.AppendDefaultLabelsHandlerWrapper(al2stargz, 10*mib), |
| 66 | + )), |
| 67 | + ) |
| 68 | + err = vs.AddFrom(ctx, remoteImage) |
| 69 | + require.NoError(t, err) |
| 70 | + |
| 71 | + // PrepareDriveMount only copies images that are available before starting the VM. |
| 72 | + // In this case, only postgres. |
| 73 | + mount, err := vs.PrepareDriveMount(ctx, 10*mib) |
| 74 | + require.NoError(t, err) |
| 75 | + |
| 76 | + _, err = fcClient.CreateVM(ctx, &proto.CreateVMRequest{ |
| 77 | + VMID: vmID, |
| 78 | + RootDrive: &proto.FirecrackerRootDrive{ |
| 79 | + HostPath: "/var/lib/firecracker-containerd/runtime/rootfs-stargz.img", |
| 80 | + }, |
| 81 | + NetworkInterfaces: []*proto.FirecrackerNetworkInterface{ |
| 82 | + { |
| 83 | + AllowMMDS: true, |
| 84 | + CNIConfig: &proto.CNIConfiguration{ |
| 85 | + NetworkName: "fcnet", |
| 86 | + InterfaceName: "veth0", |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + MachineCfg: &proto.FirecrackerMachineConfiguration{ |
| 91 | + VcpuCount: 2, |
| 92 | + MemSizeMib: 2048, |
| 93 | + }, |
| 94 | + ContainerCount: 1, |
| 95 | + DriveMounts: []*proto.FirecrackerDriveMount{mount}, |
| 96 | + }) |
| 97 | + require.NoErrorf(t, err, "Failed to create microVM[%s]", vmID) |
| 98 | + defer fcClient.StopVM(ctx, &proto.StopVMRequest{VMID: vmID}) |
| 99 | + |
| 100 | + _, err = fcClient.SetVMMetadata(ctx, &proto.SetVMMetadataRequest{ |
| 101 | + VMID: vmID, |
| 102 | + Metadata: fmt.Sprintf(dockerMetadataTemplate, "ghcr.io", noAuth, noAuth), |
| 103 | + }) |
| 104 | + require.NoError(t, err, "Failed to configure VM metadata for registry resolution") |
| 105 | + |
| 106 | + // PrepareGuestVolumes only copies images that are only available after starting the VM. |
| 107 | + // In this case, only al2stargz. |
| 108 | + err = vs.PrepareInGuest(ctx, "prepare-in-guest") |
| 109 | + require.NoError(t, err) |
| 110 | + |
| 111 | + image, err := client.Pull(ctx, |
| 112 | + alpine, |
| 113 | + containerd.WithPullUnpack, |
| 114 | + containerd.WithPullSnapshotter("devmapper"), |
| 115 | + ) |
| 116 | + require.NoError(t, err) |
| 117 | + |
| 118 | + mountsFromAL2, err := vs.WithMountsFromProvider(al2stargz) |
| 119 | + require.NoError(t, err) |
| 120 | + |
| 121 | + mountsFromPostgres, err := vs.WithMountsFromProvider(postgres) |
| 122 | + require.NoError(t, err) |
| 123 | + |
| 124 | + name := "cat" |
| 125 | + snapshotName := fmt.Sprintf("%s-snapshot", name) |
| 126 | + container, err := client.NewContainer(ctx, |
| 127 | + name, |
| 128 | + containerd.WithSnapshotter("devmapper"), |
| 129 | + containerd.WithNewSnapshot(snapshotName, image), |
| 130 | + containerd.WithNewSpec( |
| 131 | + firecrackeroci.WithVMID(vmID), |
| 132 | + oci.WithProcessArgs("sh", "-c", "ls -d /var/lib/postgresql/data; ls /etc/yum"), |
| 133 | + oci.WithDefaultPathEnv, |
| 134 | + mountsFromAL2, |
| 135 | + mountsFromPostgres, |
| 136 | + ), |
| 137 | + ) |
| 138 | + require.NoError(t, err, "failed to create container %s", name) |
| 139 | + defer container.Delete(ctx, containerd.WithSnapshotCleanup) |
| 140 | + |
| 141 | + result, err := integtest.RunTask(ctx, container) |
| 142 | + require.NoError(t, err) |
| 143 | + |
| 144 | + assert.Equal(t, uint32(0), result.ExitCode) |
| 145 | + assert.Equal(t, "/var/lib/postgresql/data\nfssnap.d\npluginconf.d\nprotected.d\nvars\nversion-groups.conf\n", result.Stdout) |
| 146 | + assert.Equal(t, "", result.Stderr) |
| 147 | +} |
0 commit comments