blob: 9dc230ad894b4a91ba63d7433386a3253c93035f [file] [log] [blame]
Serge Bazanskia42844a2024-05-16 18:06:19 +02001package qcow2
2
3import (
4 "fmt"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "testing"
9
10 "github.com/bazelbuild/rules_go/go/runfiles"
11)
12
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000013var (
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
20func 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 Bazanskia42844a2024-05-16 18:06:19 +020032// TestGenerate exercises the Generate function for a variety of image sizes.
33func TestGenerate(t *testing.T) {
Serge Bazanskia42844a2024-05-16 18:06:19 +020034 // 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 Windelschmidt82e6af72024-07-23 00:05:42 +000050 cmd := exec.Command(xQemuImgPath, "check", path)
Serge Bazanskia42844a2024-05-16 18:06:19 +020051 if err := cmd.Run(); err != nil {
52 t.Fatalf("qemu-img check failed: %v", err)
53 }
54 })
55 }
56}