blob: 10967ae08d9db7e2646707bee82abc0883b37a3a [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Serge Bazanskia42844a2024-05-16 18:06:19 +02004package qcow2
5
6import (
7 "fmt"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "testing"
Tim Windelschmidt3a7a67e2025-03-27 21:59:36 +010012
13 "github.com/bazelbuild/rules_go/go/runfiles"
Serge Bazanskia42844a2024-05-16 18:06:19 +020014)
15
Tim Windelschmidt3a7a67e2025-03-27 21:59:36 +010016var (
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
23func 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 Bazanskia42844a2024-05-16 18:06:19 +020035// TestGenerate exercises the Generate function for a variety of image sizes.
36func TestGenerate(t *testing.T) {
Serge Bazanskia42844a2024-05-16 18:06:19 +020037 // 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 Windelschmidt3a7a67e2025-03-27 21:59:36 +010053 cmd := exec.Command(xQEMUImgPath, "check", path)
Serge Bazanskia42844a2024-05-16 18:06:19 +020054 if err := cmd.Run(); err != nil {
55 t.Fatalf("qemu-img check failed: %v", err)
56 }
57 })
58 }
59}