-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk_test.go
More file actions
73 lines (61 loc) · 1.62 KB
/
disk_test.go
File metadata and controls
73 lines (61 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package knaller
import (
"os"
"path/filepath"
"testing"
)
func TestPrepareDisk(t *testing.T) {
// Create a fake base rootfs
dir := t.TempDir()
baseRootFS := filepath.Join(dir, "rootfs.ext4")
content := []byte("fake rootfs content for testing")
if err := os.WriteFile(baseRootFS, content, 0o644); err != nil {
t.Fatal(err)
}
// Override home dir for test
origHome := os.Getenv("HOME")
os.Setenv("HOME", dir)
defer os.Setenv("HOME", origHome)
name := "test-vm"
diskPath, err := prepareDisk(name, baseRootFS)
if err != nil {
t.Fatal(err)
}
// Verify the copy exists
got, err := os.ReadFile(diskPath)
if err != nil {
t.Fatal(err)
}
if string(got) != string(content) {
t.Errorf("disk content mismatch: got %q", got)
}
// Verify path is under the expected directory
expectedDir := filepath.Join(dir, ".local", "share", "knaller", "vms", name)
if filepath.Dir(diskPath) != expectedDir {
t.Errorf("disk path = %q, expected dir %q", diskPath, expectedDir)
}
}
func TestRemoveDisk(t *testing.T) {
dir := t.TempDir()
baseRootFS := filepath.Join(dir, "rootfs.ext4")
os.WriteFile(baseRootFS, []byte("fake"), 0o644)
origHome := os.Getenv("HOME")
os.Setenv("HOME", dir)
defer os.Setenv("HOME", origHome)
name := "test-rm-vm"
diskPath, err := prepareDisk(name, baseRootFS)
if err != nil {
t.Fatal(err)
}
// Verify it exists
if _, err := os.Stat(diskPath); err != nil {
t.Fatal("disk should exist before removal")
}
if err := removeDisk(name); err != nil {
t.Fatal(err)
}
// Verify it's gone
if _, err := os.Stat(diskPath); !os.IsNotExist(err) {
t.Error("disk should not exist after removal")
}
}