| 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 | |
| Tim Windelschmidt | 82e6af7 | 2024-07-23 00:05:42 +0000 | [diff] [blame] | 13 | var ( |
| 14 | // These are filled by bazel at linking time with the canonical path of |
| 15 | // their corresponding file. Inside the init function we resolve it |
| 16 | // with the rules_go runfiles package to the real path. |
| 17 | xQemuImgPath string |
| 18 | ) |
| 19 | |
| 20 | func init() { |
| 21 | var err error |
| 22 | for _, path := range []*string{ |
| 23 | &xQemuImgPath, |
| 24 | } { |
| 25 | *path, err = runfiles.Rlocation(*path) |
| 26 | if err != nil { |
| 27 | panic(err) |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| Serge Bazanski | a42844a | 2024-05-16 18:06:19 +0200 | [diff] [blame] | 32 | // TestGenerate exercises the Generate function for a variety of image sizes. |
| 33 | func TestGenerate(t *testing.T) { |
| Serge Bazanski | a42844a | 2024-05-16 18:06:19 +0200 | [diff] [blame] | 34 | // Test all orders of magnitude from 1KiB to 1PiB. |
| 35 | for i := 20; i < 50; i++ { |
| 36 | t.Run(fmt.Sprintf("%d", 1<<i), func(t *testing.T) { |
| 37 | path := filepath.Join(t.TempDir(), "test.qcow2") |
| 38 | |
| 39 | f, err := os.Create(path) |
| 40 | if err != nil { |
| 41 | t.Fatalf("Could not create test image file: %v", err) |
| 42 | } |
| 43 | if err := Generate(f, GenerateWithFileSize(1<<i)); err != nil { |
| 44 | t.Fatalf("Generate(%d bytes): %v", 1<<i, err) |
| 45 | } |
| 46 | if err := f.Close(); err != nil { |
| 47 | t.Fatalf("Close: %v", err) |
| 48 | } |
| 49 | |
| Tim Windelschmidt | 82e6af7 | 2024-07-23 00:05:42 +0000 | [diff] [blame] | 50 | cmd := exec.Command(xQemuImgPath, "check", path) |
| Serge Bazanski | a42844a | 2024-05-16 18:06:19 +0200 | [diff] [blame] | 51 | if err := cmd.Run(); err != nil { |
| 52 | t.Fatalf("qemu-img check failed: %v", err) |
| 53 | } |
| 54 | }) |
| 55 | } |
| 56 | } |