| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Serge Bazanski | a42844a | 2024-05-16 18:06:19 +0200 | [diff] [blame] | 4 | package qcow2 |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "testing" |
| Tim Windelschmidt | 3a7a67e | 2025-03-27 21:59:36 +0100 | [diff] [blame] | 12 | |
| 13 | "github.com/bazelbuild/rules_go/go/runfiles" |
| Serge Bazanski | a42844a | 2024-05-16 18:06:19 +0200 | [diff] [blame] | 14 | ) |
| 15 | |
| Tim Windelschmidt | 3a7a67e | 2025-03-27 21:59:36 +0100 | [diff] [blame] | 16 | var ( |
| 17 | // These are filled by bazel at linking time with the canonical path of |
| 18 | // their corresponding file. Inside the init function we resolve it |
| 19 | // with the rules_go runfiles package to the real path. |
| 20 | xQEMUImgPath string |
| 21 | ) |
| 22 | |
| 23 | func init() { |
| 24 | var err error |
| 25 | for _, path := range []*string{ |
| 26 | &xQEMUImgPath, |
| 27 | } { |
| 28 | *path, err = runfiles.Rlocation(*path) |
| 29 | if err != nil { |
| 30 | panic(err) |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| Serge Bazanski | a42844a | 2024-05-16 18:06:19 +0200 | [diff] [blame] | 35 | // TestGenerate exercises the Generate function for a variety of image sizes. |
| 36 | func TestGenerate(t *testing.T) { |
| Serge Bazanski | a42844a | 2024-05-16 18:06:19 +0200 | [diff] [blame] | 37 | // Test all orders of magnitude from 1KiB to 1PiB. |
| 38 | for i := 20; i < 50; i++ { |
| 39 | t.Run(fmt.Sprintf("%d", 1<<i), func(t *testing.T) { |
| 40 | path := filepath.Join(t.TempDir(), "test.qcow2") |
| 41 | |
| 42 | f, err := os.Create(path) |
| 43 | if err != nil { |
| 44 | t.Fatalf("Could not create test image file: %v", err) |
| 45 | } |
| 46 | if err := Generate(f, GenerateWithFileSize(1<<i)); err != nil { |
| 47 | t.Fatalf("Generate(%d bytes): %v", 1<<i, err) |
| 48 | } |
| 49 | if err := f.Close(); err != nil { |
| 50 | t.Fatalf("Close: %v", err) |
| 51 | } |
| 52 | |
| Tim Windelschmidt | 3a7a67e | 2025-03-27 21:59:36 +0100 | [diff] [blame] | 53 | cmd := exec.Command(xQEMUImgPath, "check", path) |
| Serge Bazanski | a42844a | 2024-05-16 18:06:19 +0200 | [diff] [blame] | 54 | if err := cmd.Run(); err != nil { |
| 55 | t.Fatalf("qemu-img check failed: %v", err) |
| 56 | } |
| 57 | }) |
| 58 | } |
| 59 | } |