|
| 1 | +// MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2020 Plamen Petrov, Amory Hoste and EASE lab |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +package devmapper_test |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "fmt" |
| 28 | + "github.com/containerd/containerd" |
| 29 | + ctrdlog "github.com/containerd/containerd/log" |
| 30 | + "github.com/containerd/containerd/namespaces" |
| 31 | + "github.com/ease-lab/vhive/ctriface/image" |
| 32 | + "github.com/ease-lab/vhive/devmapper" |
| 33 | + log "github.com/sirupsen/logrus" |
| 34 | + "github.com/stretchr/testify/require" |
| 35 | + "os" |
| 36 | + "sync" |
| 37 | + "testing" |
| 38 | + "time" |
| 39 | +) |
| 40 | + |
| 41 | +const ( |
| 42 | + containerdAddress = "/run/firecracker-containerd/containerd.sock" |
| 43 | + poolName = "fc-dev-thinpool" |
| 44 | + NamespaceName = "firecracker-containerd" |
| 45 | + TestImageName = "ghcr.io/ease-lab/helloworld:var_workload" |
| 46 | +) |
| 47 | + |
| 48 | +func getAllImages() map[string]string { |
| 49 | + return map[string]string{ |
| 50 | + "helloworld": "ghcr.io/ease-lab/helloworld:var_workload", |
| 51 | + "chameleon": "ghcr.io/ease-lab/chameleon:var_workload", |
| 52 | + "pyaes": "ghcr.io/ease-lab/pyaes:var_workload", |
| 53 | + "image_rotate": "ghcr.io/ease-lab/image_rotate:var_workload", |
| 54 | + "lr_training": "ghcr.io/ease-lab/lr_training:var_workload", |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestMain(m *testing.M) { |
| 59 | + // call flag.Parse() here if TestMain uses flags |
| 60 | + |
| 61 | + log.SetFormatter(&log.TextFormatter{ |
| 62 | + TimestampFormat: ctrdlog.RFC3339NanoFixed, |
| 63 | + FullTimestamp: true, |
| 64 | + }) |
| 65 | + |
| 66 | + log.SetOutput(os.Stdout) |
| 67 | + |
| 68 | + log.SetLevel(log.InfoLevel) |
| 69 | + |
| 70 | + os.Exit(m.Run()) |
| 71 | +} |
| 72 | + |
| 73 | +func testDevmapper(t *testing.T, mgr *image.ImageManager, dmpr *devmapper.DeviceMapper, snapKey, imageName string) { |
| 74 | + // Pull image |
| 75 | + testTimeout := 120 * time.Second |
| 76 | + ctx, cancel := context.WithTimeout(namespaces.WithNamespace(context.Background(), NamespaceName), testTimeout) |
| 77 | + defer cancel() |
| 78 | + |
| 79 | + img, err := mgr.GetImage(ctx, imageName) |
| 80 | + require.NoError(t, err, fmt.Sprintf("Failed to pull image %s", imageName)) |
| 81 | + |
| 82 | + // Test devmapper |
| 83 | + err = dmpr.CreateDeviceSnapshotFromImage(ctx, snapKey, *img) |
| 84 | + require.NoError(t, err, fmt.Sprintf("Failed to create snapshot from image %s", imageName)) |
| 85 | + |
| 86 | + _, err = dmpr.GetDeviceSnapshot(ctx, snapKey) |
| 87 | + if err != nil { |
| 88 | + _ = dmpr.RemoveDeviceSnapshot(ctx, snapKey) |
| 89 | + } |
| 90 | + require.NoError(t, err, fmt.Sprintf("Failed to fetch previously created snapshot %s", snapKey)) |
| 91 | + |
| 92 | + err = dmpr.RemoveDeviceSnapshot(ctx, snapKey) |
| 93 | + require.NoError(t, err, fmt.Sprintf("Failed to remove snapshot %s", snapKey)) |
| 94 | +} |
| 95 | + |
| 96 | +func TestDevmapper(t *testing.T) { |
| 97 | + snapKey := "testsnap-1" |
| 98 | + |
| 99 | + // Create containerd client |
| 100 | + client, err := containerd.New(containerdAddress) |
| 101 | + defer client.Close() |
| 102 | + require.NoError(t, err, "Containerd client creation returned error") |
| 103 | + |
| 104 | + // Create image manager |
| 105 | + mgr := image.NewImageManager(client, "devmapper") |
| 106 | + |
| 107 | + // Create devmapper |
| 108 | + dmpr := devmapper.NewDeviceMapper(client, poolName, "") |
| 109 | + |
| 110 | + testDevmapper(t, mgr, dmpr, snapKey, TestImageName) |
| 111 | +} |
| 112 | + |
| 113 | +func TestDevmapperConcurrent(t *testing.T) { |
| 114 | + // Create containerd client |
| 115 | + client, err := containerd.New(containerdAddress) |
| 116 | + defer client.Close() |
| 117 | + require.NoError(t, err, "Containerd client creation returned error") |
| 118 | + |
| 119 | + // Create image manager |
| 120 | + mgr := image.NewImageManager(client, "devmapper") |
| 121 | + |
| 122 | + // Create devmapper |
| 123 | + dmpr := devmapper.NewDeviceMapper(client, poolName, "") |
| 124 | + |
| 125 | + // Test concurrent devmapper |
| 126 | + var wg sync.WaitGroup |
| 127 | + wg.Add(len(getAllImages())) |
| 128 | + |
| 129 | + for _, imgName := range getAllImages() { |
| 130 | + go func(imgName string) { |
| 131 | + snapKey := fmt.Sprintf("testsnap-%s", imgName) |
| 132 | + testDevmapper(t, mgr, dmpr, snapKey, imgName) |
| 133 | + wg.Done() |
| 134 | + }(imgName) |
| 135 | + } |
| 136 | + wg.Wait() |
| 137 | +} |
0 commit comments