| 1 | |
| 2 | |
| 3 | |
| 4 | package wsl2 |
| 5 | |
| 6 | import ( |
| 7 | "runtime" |
| 8 | "testing" |
| 9 | |
| 10 | "gotest.tools/v3/assert" |
| 11 | |
| 12 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 13 | ) |
| 14 | |
| 15 | func TestValidateConfigImages(t *testing.T) { |
| 16 | var arch limatype.Arch |
| 17 | switch runtime.GOARCH { |
| 18 | case "amd64": |
| 19 | arch = limatype.X8664 |
| 20 | case "arm64": |
| 21 | arch = limatype.AARCH64 |
| 22 | default: |
| 23 | arch = runtime.GOARCH |
| 24 | } |
| 25 | vmType := limatype.WSL2 |
| 26 | |
| 27 | tests := []struct { |
| 28 | name string |
| 29 | images []limatype.Image |
| 30 | expectedErr string |
| 31 | }{ |
| 32 | { |
| 33 | name: "Valid tarball format .tar", |
| 34 | images: []limatype.Image{ |
| 35 | { |
| 36 | File: limatype.File{ |
| 37 | Location: "/path/to/rootfs.tar", |
| 38 | Arch: arch, |
| 39 | }, |
| 40 | }, |
| 41 | }, |
| 42 | expectedErr: "", |
| 43 | }, |
| 44 | { |
| 45 | name: "Valid tarball format .tar.gz", |
| 46 | images: []limatype.Image{ |
| 47 | { |
| 48 | File: limatype.File{ |
| 49 | Location: "/path/to/rootfs.tar.gz", |
| 50 | Arch: arch, |
| 51 | }, |
| 52 | }, |
| 53 | }, |
| 54 | expectedErr: "", |
| 55 | }, |
| 56 | { |
| 57 | name: "Valid tarball format .tgz", |
| 58 | images: []limatype.Image{ |
| 59 | { |
| 60 | File: limatype.File{ |
| 61 | Location: "/path/to/rootfs.tgz", |
| 62 | Arch: arch, |
| 63 | }, |
| 64 | }, |
| 65 | }, |
| 66 | expectedErr: "", |
| 67 | }, |
| 68 | { |
| 69 | name: "Valid tarball format .tar.xz", |
| 70 | images: []limatype.Image{ |
| 71 | { |
| 72 | File: limatype.File{ |
| 73 | Location: "/path/to/rootfs.tar.xz", |
| 74 | Arch: arch, |
| 75 | }, |
| 76 | }, |
| 77 | }, |
| 78 | expectedErr: "", |
| 79 | }, |
| 80 | { |
| 81 | name: "Unsupported VM image format .qcow2", |
| 82 | images: []limatype.Image{ |
| 83 | { |
| 84 | File: limatype.File{ |
| 85 | Location: "/path/to/rootfs.qcow2", |
| 86 | Arch: arch, |
| 87 | }, |
| 88 | }, |
| 89 | }, |
| 90 | expectedErr: "unsupported image type for wsl2: \"/path/to/rootfs.qcow2\". wsl2 only supports importing tar archive root filesystems, not standard VM disk images", |
| 91 | }, |
| 92 | { |
| 93 | name: "Unsupported VM image format .qcow2.gz", |
| 94 | images: []limatype.Image{ |
| 95 | { |
| 96 | File: limatype.File{ |
| 97 | Location: "/path/to/rootfs.qcow2.gz", |
| 98 | Arch: arch, |
| 99 | }, |
| 100 | }, |
| 101 | }, |
| 102 | expectedErr: "unsupported image type for wsl2: \"/path/to/rootfs.qcow2.gz\". wsl2 only supports importing tar archive root filesystems, not standard VM disk images", |
| 103 | }, |
| 104 | { |
| 105 | name: "Unsupported VM image format .raw", |
| 106 | images: []limatype.Image{ |
| 107 | { |
| 108 | File: limatype.File{ |
| 109 | Location: "/path/to/rootfs.raw", |
| 110 | Arch: arch, |
| 111 | }, |
| 112 | }, |
| 113 | }, |
| 114 | expectedErr: "unsupported image type for wsl2: \"/path/to/rootfs.raw\". wsl2 only supports importing tar archive root filesystems, not standard VM disk images", |
| 115 | }, |
| 116 | { |
| 117 | name: "Unsupported SquashFS image format", |
| 118 | images: []limatype.Image{ |
| 119 | { |
| 120 | File: limatype.File{ |
| 121 | Location: "/path/to/rootfs.squashfs", |
| 122 | Arch: arch, |
| 123 | }, |
| 124 | }, |
| 125 | }, |
| 126 | expectedErr: "unsupported image type for wsl2: \"/path/to/rootfs.squashfs\". wsl2 does not natively support importing SquashFS images; please convert the image to a tar archive before importing", |
| 127 | }, |
| 128 | { |
| 129 | name: "Generic unsupported format", |
| 130 | images: []limatype.Image{ |
| 131 | { |
| 132 | File: limatype.File{ |
| 133 | Location: "/path/to/rootfs.zip", |
| 134 | Arch: arch, |
| 135 | }, |
| 136 | }, |
| 137 | }, |
| 138 | expectedErr: "unsupported image type for wsl2: \"/path/to/rootfs.zip\". A tar archive root filesystem (.tar, .tar.gz, .tar.xz, etc.) is required", |
| 139 | }, |
| 140 | { |
| 141 | name: "Unsupported VM image format .qcow2.zst", |
| 142 | images: []limatype.Image{ |
| 143 | { |
| 144 | File: limatype.File{ |
| 145 | Location: "/path/to/rootfs.qcow2.zst", |
| 146 | Arch: arch, |
| 147 | }, |
| 148 | }, |
| 149 | }, |
| 150 | expectedErr: "unsupported image type for wsl2: \"/path/to/rootfs.qcow2.zst\". wsl2 only supports importing tar archive root filesystems, not standard VM disk images", |
| 151 | }, |
| 152 | { |
| 153 | name: "Unsupported VM image format .raw.bz2", |
| 154 | images: []limatype.Image{ |
| 155 | { |
| 156 | File: limatype.File{ |
| 157 | Location: "/path/to/rootfs.raw.bz2", |
| 158 | Arch: arch, |
| 159 | }, |
| 160 | }, |
| 161 | }, |
| 162 | expectedErr: "unsupported image type for wsl2: \"/path/to/rootfs.raw.bz2\". wsl2 only supports importing tar archive root filesystems, not standard VM disk images", |
| 163 | }, |
| 164 | { |
| 165 | name: "Unsupported SquashFS image format .squashfs.gz", |
| 166 | images: []limatype.Image{ |
| 167 | { |
| 168 | File: limatype.File{ |
| 169 | Location: "/path/to/rootfs.squashfs.gz", |
| 170 | Arch: arch, |
| 171 | }, |
| 172 | }, |
| 173 | }, |
| 174 | expectedErr: "unsupported image type for wsl2: \"/path/to/rootfs.squashfs.gz\". wsl2 does not natively support importing SquashFS images; please convert the image to a tar archive before importing", |
| 175 | }, |
| 176 | { |
| 177 | name: "Different arch is ignored", |
| 178 | images: []limatype.Image{ |
| 179 | { |
| 180 | File: limatype.File{ |
| 181 | Location: "/path/to/rootfs.qcow2", |
| 182 | Arch: "different_arch", |
| 183 | }, |
| 184 | }, |
| 185 | }, |
| 186 | expectedErr: "", |
| 187 | }, |
| 188 | } |
| 189 | |
| 190 | for _, tt := range tests { |
| 191 | t.Run(tt.name, func(t *testing.T) { |
| 192 | cfg := &limatype.LimaYAML{ |
| 193 | VMType: &vmType, |
| 194 | Arch: &arch, |
| 195 | Images: tt.images, |
| 196 | } |
| 197 | err := validateConfig(t.Context(), cfg) |
| 198 | if tt.expectedErr == "" { |
| 199 | assert.NilError(t, err) |
| 200 | } else { |
| 201 | assert.ErrorContains(t, err, tt.expectedErr) |
| 202 | } |
| 203 | }) |
| 204 | } |
| 205 | } |