|
| 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 | + "bytes" |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "strconv" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/containerd/containerd" |
| 26 | + "github.com/containerd/containerd/cio" |
| 27 | + "github.com/containerd/containerd/namespaces" |
| 28 | + "github.com/containerd/containerd/oci" |
| 29 | + "github.com/firecracker-microvm/firecracker-containerd/proto" |
| 30 | + "github.com/firecracker-microvm/firecracker-containerd/runtime/firecrackeroci" |
| 31 | + "github.com/firecracker-microvm/firecracker-containerd/volume" |
| 32 | + "github.com/stretchr/testify/assert" |
| 33 | + "github.com/stretchr/testify/require" |
| 34 | +) |
| 35 | + |
| 36 | +const mib = 1024 * 1024 |
| 37 | + |
| 38 | +func TestVolumes_Isolated(t *testing.T) { |
| 39 | + prepareIntegTest(t) |
| 40 | + |
| 41 | + const vmID = 0 |
| 42 | + |
| 43 | + ctx := namespaces.WithNamespace(context.Background(), "default") |
| 44 | + |
| 45 | + client, err := containerd.New(containerdSockPath, containerd.WithDefaultRuntime(firecrackerRuntime)) |
| 46 | + require.NoError(t, err, "unable to create client to containerd service at %s, is containerd running?", containerdSockPath) |
| 47 | + defer client.Close() |
| 48 | + |
| 49 | + image, err := alpineImage(ctx, client, defaultSnapshotterName) |
| 50 | + require.NoError(t, err, "failed to get alpine image") |
| 51 | + |
| 52 | + fcClient, err := newFCControlClient(containerdSockPath) |
| 53 | + require.NoError(t, err, "failed to create fccontrol client") |
| 54 | + |
| 55 | + // Make volumes. |
| 56 | + path, err := os.MkdirTemp("", t.Name()) |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + f, err := os.Create(filepath.Join(path, "hello.txt")) |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + _, err = f.Write([]byte("hello from host\n")) |
| 63 | + require.NoError(t, err) |
| 64 | + |
| 65 | + const volName = "volume1" |
| 66 | + vs := volume.NewSet() |
| 67 | + vs.Add(volume.FromHost(volName, path)) |
| 68 | + |
| 69 | + // Since CreateVM doesn't take functional options, we need to explicitly create |
| 70 | + // a FirecrackerDriveMount |
| 71 | + mount, err := vs.PrepareDriveMount(ctx, 10*mib) |
| 72 | + require.NoError(t, err) |
| 73 | + |
| 74 | + containers := []string{"c1", "c2"} |
| 75 | + |
| 76 | + _, err = fcClient.CreateVM(ctx, &proto.CreateVMRequest{ |
| 77 | + VMID: strconv.Itoa(vmID), |
| 78 | + ContainerCount: int32(len(containers)), |
| 79 | + DriveMounts: []*proto.FirecrackerDriveMount{mount}, |
| 80 | + }) |
| 81 | + require.NoError(t, err, "failed to create VM") |
| 82 | + |
| 83 | + // Make containers with the volume. |
| 84 | + dir := "/path/in/container" |
| 85 | + mpOpt, err := vs.WithMounts([]volume.Mount{{Source: volName, Destination: dir, ReadOnly: false}}) |
| 86 | + require.NoError(t, err) |
| 87 | + |
| 88 | + for _, name := range containers { |
| 89 | + snapshotName := fmt.Sprintf("%s-snapshot", name) |
| 90 | + |
| 91 | + sh := fmt.Sprintf("echo hello from %s >> %s/hello.txt", name, dir) |
| 92 | + container, err := client.NewContainer(ctx, |
| 93 | + name, |
| 94 | + containerd.WithSnapshotter(defaultSnapshotterName), |
| 95 | + containerd.WithNewSnapshot(snapshotName, image), |
| 96 | + containerd.WithNewSpec( |
| 97 | + firecrackeroci.WithVMID(strconv.Itoa(vmID)), |
| 98 | + oci.WithProcessArgs("sh", "-c", sh), |
| 99 | + oci.WithDefaultPathEnv, |
| 100 | + mpOpt, |
| 101 | + ), |
| 102 | + ) |
| 103 | + require.NoError(t, err, "failed to create container %s", name) |
| 104 | + |
| 105 | + var stdout, stderr bytes.Buffer |
| 106 | + |
| 107 | + task, err := container.NewTask(ctx, cio.NewCreator(cio.WithStreams(nil, &stdout, &stderr))) |
| 108 | + require.NoError(t, err, "failed to create task for container %s", name) |
| 109 | + |
| 110 | + exitCh, err := task.Wait(ctx) |
| 111 | + require.NoError(t, err, "failed to wait on task for container %s", name) |
| 112 | + |
| 113 | + err = task.Start(ctx) |
| 114 | + require.NoError(t, err, "failed to start task for container %s", name) |
| 115 | + |
| 116 | + exit := <-exitCh |
| 117 | + _, err = task.Delete(ctx) |
| 118 | + require.NoError(t, err) |
| 119 | + |
| 120 | + assert.Equalf(t, uint32(0), exit.ExitCode(), "stdout=%q stderr=%q", stdout.String(), stderr.String()) |
| 121 | + } |
| 122 | + |
| 123 | + name := "cat" |
| 124 | + snapshotName := fmt.Sprintf("%s-snapshot", name) |
| 125 | + container, err := client.NewContainer(ctx, |
| 126 | + name, |
| 127 | + containerd.WithSnapshotter(defaultSnapshotterName), |
| 128 | + containerd.WithNewSnapshot(snapshotName, image), |
| 129 | + containerd.WithNewSpec( |
| 130 | + firecrackeroci.WithVMID(strconv.Itoa(vmID)), |
| 131 | + oci.WithProcessArgs("cat", fmt.Sprintf("%s/hello.txt", dir)), |
| 132 | + oci.WithDefaultPathEnv, |
| 133 | + mpOpt, |
| 134 | + ), |
| 135 | + ) |
| 136 | + require.NoError(t, err, "failed to create container %s", name) |
| 137 | + |
| 138 | + var stdout, stderr bytes.Buffer |
| 139 | + task, err := container.NewTask(ctx, cio.NewCreator(cio.WithStreams(nil, &stdout, &stderr))) |
| 140 | + require.NoError(t, err, "failed to create task for container %s", name) |
| 141 | + |
| 142 | + exitCh, err := task.Wait(ctx) |
| 143 | + require.NoError(t, err, "failed to wait on task for container %s", name) |
| 144 | + |
| 145 | + err = task.Start(ctx) |
| 146 | + require.NoError(t, err, "failed to start task for container %s", name) |
| 147 | + |
| 148 | + exit := <-exitCh |
| 149 | + _, err = task.Delete(ctx) |
| 150 | + require.NoError(t, err) |
| 151 | + |
| 152 | + assert.Equal(t, uint32(0), exit.ExitCode()) |
| 153 | + assert.Equal(t, "hello from host\nhello from c1\nhello from c2\n", stdout.String()) |
| 154 | + assert.Equal(t, "", stderr.String()) |
| 155 | +} |
0 commit comments