1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
// Copyright (c) 2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package oci
import (
"os/user"
"reflect"
"testing"
"github.com/sylabs/singularity/v4/internal/pkg/runtime/launcher"
"github.com/sylabs/singularity/v4/internal/pkg/test"
"github.com/sylabs/singularity/v4/internal/pkg/util/fs/fuse"
"github.com/sylabs/singularity/v4/pkg/util/singularityconf"
)
func TestNewLauncher(t *testing.T) {
test.DropPrivilege(t)
defer test.ResetPrivilege(t)
sc, err := singularityconf.GetConfig(nil)
if err != nil {
t.Fatalf("while initializing singularityconf: %s", err)
}
singularityconf.SetCurrentConfig(sc)
u, err := user.Current()
if err != nil {
t.Fatalf("while getting current user: %s", err)
}
tests := []struct {
name string
opts []launcher.Option
want *Launcher
wantErr bool
}{
{
name: "default",
want: &Launcher{
cfg: launcher.Options{WritableTmpfs: true},
singularityConf: sc,
homeHost: u.HomeDir,
homeSrc: "",
homeDest: u.HomeDir,
imageMountsByImagePath: make(map[string]*fuse.ImageMount),
imageMountsByMountpoint: make(map[string]*fuse.ImageMount),
},
},
{
name: "homeDest",
opts: []launcher.Option{
launcher.OptHome("/home/dest", true, false),
},
want: &Launcher{
cfg: launcher.Options{HomeDir: "/home/dest", CustomHome: true, WritableTmpfs: true},
singularityConf: sc,
homeHost: u.HomeDir,
homeSrc: "",
homeDest: "/home/dest",
imageMountsByImagePath: make(map[string]*fuse.ImageMount),
imageMountsByMountpoint: make(map[string]*fuse.ImageMount),
},
wantErr: false,
},
{
name: "homeSrcDest",
opts: []launcher.Option{
launcher.OptHome("/home/src:/home/dest", true, false),
},
want: &Launcher{
cfg: launcher.Options{HomeDir: "/home/src:/home/dest", CustomHome: true, WritableTmpfs: true},
singularityConf: sc,
homeHost: u.HomeDir,
homeSrc: "/home/src",
homeDest: "/home/dest",
imageMountsByImagePath: make(map[string]*fuse.ImageMount),
imageMountsByMountpoint: make(map[string]*fuse.ImageMount),
},
wantErr: false,
},
{
name: "no-compat",
opts: []launcher.Option{
launcher.OptNoCompat(true),
},
want: &Launcher{
cfg: launcher.Options{NoCompat: true, WritableTmpfs: false},
singularityConf: sc,
homeHost: u.HomeDir,
homeSrc: "",
homeDest: u.HomeDir,
imageMountsByImagePath: make(map[string]*fuse.ImageMount),
imageMountsByMountpoint: make(map[string]*fuse.ImageMount),
},
wantErr: false,
},
{
name: "no-compat_writable-tmpfs",
opts: []launcher.Option{
launcher.OptNoCompat(true),
launcher.OptWritableTmpfs(true),
},
want: &Launcher{
cfg: launcher.Options{NoCompat: true, WritableTmpfs: true},
singularityConf: sc,
homeHost: u.HomeDir,
homeSrc: "",
homeDest: u.HomeDir,
imageMountsByImagePath: make(map[string]*fuse.ImageMount),
imageMountsByMountpoint: make(map[string]*fuse.ImageMount),
},
wantErr: false,
},
{
name: "unsupportedOption",
opts: []launcher.Option{
launcher.OptSecurity([]string{"seccomp:example.json"}),
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewLauncher(tt.opts...)
if (err != nil) != tt.wantErr {
t.Errorf("NewLauncher() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewLauncher() = %v, want %v", got, tt.want)
}
})
}
}
func Test_normalizeImageRef(t *testing.T) {
tests := []struct {
name string
imageRef string
want string
wantErr bool
}{
{
name: "ext3 image",
imageRef: "../../../../../test/images/extfs-for-overlay.img",
want: "",
wantErr: true,
},
{
name: "squashfs image",
imageRef: "../../../../../test/images/squashfs-for-overlay.img",
want: "",
wantErr: true,
},
{
name: "sif image",
imageRef: "../../../../../test/images/empty.sif",
want: "sif:../../../../../test/images/empty.sif",
wantErr: false,
},
{
name: "oci ref",
imageRef: "oci:/my/layout",
want: "oci:/my/layout",
wantErr: false,
},
{
name: "oci sif",
imageRef: "../../../../../test/images/empty.oci.sif",
want: "oci-sif:../../../../../test/images/empty.oci.sif",
wantErr: false,
},
{
name: "oci sif prefixed",
imageRef: "oci-sif:../../../../../test/images/empty.oci.sif",
want: "oci-sif:../../../../../test/images/empty.oci.sif",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := normalizeImageRef(tt.imageRef)
if (err != nil) != tt.wantErr {
t.Errorf("normalizeImageRef() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("normalizeImageRef() = %v, want %v", got, tt.want)
}
})
}
}
|