|
| 1 | +package nativeimgutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "gotest.tools/v3/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func createImg(name, format, size string) error { |
| 14 | + return exec.Command("qemu-img", "create", name, "-f", format, size).Run() |
| 15 | +} |
| 16 | + |
| 17 | +func TestConvertToRaw(t *testing.T) { |
| 18 | + _, err := exec.LookPath("qemu-img") |
| 19 | + if err != nil { |
| 20 | + t.Skipf("qemu-img does not seem installed: %v", err) |
| 21 | + } |
| 22 | + tmpDir := t.TempDir() |
| 23 | + |
| 24 | + qcowImage, err := os.Create(filepath.Join(tmpDir, "qcow.img")) |
| 25 | + assert.NilError(t, err) |
| 26 | + defer qcowImage.Close() |
| 27 | + err = createImg(qcowImage.Name(), "qcow2", "1M") |
| 28 | + assert.NilError(t, err) |
| 29 | + |
| 30 | + rawImage, err := os.Create(filepath.Join(tmpDir, "raw.img")) |
| 31 | + assert.NilError(t, err) |
| 32 | + defer rawImage.Close() |
| 33 | + err = createImg(rawImage.Name(), "raw", "1M") |
| 34 | + assert.NilError(t, err) |
| 35 | + |
| 36 | + rawImageExtended, err := os.Create(filepath.Join(tmpDir, "raw_extended.img")) |
| 37 | + assert.NilError(t, err) |
| 38 | + defer rawImageExtended.Close() |
| 39 | + err = createImg(rawImageExtended.Name(), "raw", "2M") |
| 40 | + assert.NilError(t, err) |
| 41 | + |
| 42 | + t.Run("qcow without backing file", func(t *testing.T) { |
| 43 | + resultImage := filepath.Join(tmpDir, strings.ReplaceAll(t.Name(), string(os.PathSeparator), "_")) |
| 44 | + assert.NilError(t, err) |
| 45 | + |
| 46 | + err = ConvertToRaw(qcowImage.Name(), resultImage, nil, false) |
| 47 | + assert.NilError(t, err) |
| 48 | + assertFileEquals(t, rawImage.Name(), resultImage) |
| 49 | + }) |
| 50 | + |
| 51 | + t.Run("qcow with backing file", func(t *testing.T) { |
| 52 | + resultImage := filepath.Join(tmpDir, strings.ReplaceAll(t.Name(), string(os.PathSeparator), "_")) |
| 53 | + assert.NilError(t, err) |
| 54 | + |
| 55 | + err = ConvertToRaw(qcowImage.Name(), resultImage, nil, true) |
| 56 | + assert.NilError(t, err) |
| 57 | + assertFileEquals(t, rawImage.Name(), resultImage) |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("qcow with extra size", func(t *testing.T) { |
| 61 | + resultImage := filepath.Join(tmpDir, strings.ReplaceAll(t.Name(), string(os.PathSeparator), "_")) |
| 62 | + assert.NilError(t, err) |
| 63 | + size := int64(2_097_152) // 2mb |
| 64 | + err = ConvertToRaw(qcowImage.Name(), resultImage, &size, false) |
| 65 | + assert.NilError(t, err) |
| 66 | + assertFileEquals(t, rawImageExtended.Name(), resultImage) |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("raw", func(t *testing.T) { |
| 70 | + resultImage := filepath.Join(tmpDir, strings.ReplaceAll(t.Name(), string(os.PathSeparator), "_")) |
| 71 | + assert.NilError(t, err) |
| 72 | + |
| 73 | + err = ConvertToRaw(rawImage.Name(), resultImage, nil, false) |
| 74 | + assert.NilError(t, err) |
| 75 | + assertFileEquals(t, rawImage.Name(), resultImage) |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +func assertFileEquals(t *testing.T, expected, actual string) { |
| 80 | + expectedContent, err := os.ReadFile(expected) |
| 81 | + assert.NilError(t, err) |
| 82 | + actualContent, err := os.ReadFile(actual) |
| 83 | + assert.NilError(t, err) |
| 84 | + assert.DeepEqual(t, expectedContent, actualContent) |
| 85 | +} |
0 commit comments