| Serge Bazanski | a42844a | 2024-05-16 18:06:19 +0200 | [diff] [blame^] | 1 | package qcow2 |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/bazelbuild/rules_go/go/runfiles" |
| 11 | ) |
| 12 | |
| 13 | // TestGenerate exercises the Generate function for a variety of image sizes. |
| 14 | func TestGenerate(t *testing.T) { |
| 15 | qemuImg, err := runfiles.Rlocation("qemu/qemu-img") |
| 16 | if err != nil { |
| 17 | t.Fatalf("Could not locate qemu-img: %v", err) |
| 18 | } |
| 19 | |
| 20 | // Test all orders of magnitude from 1KiB to 1PiB. |
| 21 | for i := 20; i < 50; i++ { |
| 22 | t.Run(fmt.Sprintf("%d", 1<<i), func(t *testing.T) { |
| 23 | path := filepath.Join(t.TempDir(), "test.qcow2") |
| 24 | |
| 25 | f, err := os.Create(path) |
| 26 | if err != nil { |
| 27 | t.Fatalf("Could not create test image file: %v", err) |
| 28 | } |
| 29 | if err := Generate(f, GenerateWithFileSize(1<<i)); err != nil { |
| 30 | t.Fatalf("Generate(%d bytes): %v", 1<<i, err) |
| 31 | } |
| 32 | if err := f.Close(); err != nil { |
| 33 | t.Fatalf("Close: %v", err) |
| 34 | } |
| 35 | |
| 36 | cmd := exec.Command(qemuImg, "check", path) |
| 37 | if err := cmd.Run(); err != nil { |
| 38 | t.Fatalf("qemu-img check failed: %v", err) |
| 39 | } |
| 40 | }) |
| 41 | } |
| 42 | } |