| 1 | |
| 2 | |
| 3 | |
| 4 | package store |
| 5 | |
| 6 | import ( |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "testing" |
| 12 | |
| 13 | "gotest.tools/v3/assert" |
| 14 | |
| 15 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 16 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 17 | "github.com/lima-vm/lima/v2/pkg/osutil" |
| 18 | ) |
| 19 | |
| 20 | |
| 21 | func fakeBootSessionID(t *testing.T, id string, err error) { |
| 22 | t.Helper() |
| 23 | orig := bootSessionID |
| 24 | bootSessionID = func() (string, error) { return id, err } |
| 25 | t.Cleanup(func() { bootSessionID = orig }) |
| 26 | } |
| 27 | |
| 28 | func TestWritePIDFile(t *testing.T) { |
| 29 | fakeBootSessionID(t, "boot-1", nil) |
| 30 | dir := t.TempDir() |
| 31 | path := filepath.Join(dir, filenames.HostAgentPID) |
| 32 | assert.NilError(t, WritePIDFile(path, os.Getpid())) |
| 33 | |
| 34 | assert.Assert(t, osutil.FileExists(filepath.Join(dir, filenames.BootSession))) |
| 35 | |
| 36 | pid, err := ReadPIDFile(path) |
| 37 | assert.NilError(t, err) |
| 38 | assert.Equal(t, pid, os.Getpid()) |
| 39 | } |
| 40 | |
| 41 | func TestWritePIDFileRemovesPIDFilesFromPreviousBoot(t *testing.T) { |
| 42 | fakeBootSessionID(t, "boot-1", nil) |
| 43 | dir := t.TempDir() |
| 44 | haPath := filepath.Join(dir, filenames.HostAgentPID) |
| 45 | driverPath := filepath.Join(dir, filenames.PIDFile(limatype.QEMU)) |
| 46 | assert.NilError(t, WritePIDFile(haPath, os.Getpid())) |
| 47 | assert.NilError(t, WritePIDFile(driverPath, os.Getpid())) |
| 48 | |
| 49 | |
| 50 | |
| 51 | fakeBootSessionID(t, "boot-2", nil) |
| 52 | assert.NilError(t, WritePIDFile(haPath, os.Getpid())) |
| 53 | |
| 54 | _, err := os.Stat(driverPath) |
| 55 | assert.Assert(t, errors.Is(err, os.ErrNotExist)) |
| 56 | |
| 57 | pid, err := ReadPIDFile(haPath) |
| 58 | assert.NilError(t, err) |
| 59 | assert.Equal(t, pid, os.Getpid()) |
| 60 | } |
| 61 | |
| 62 | func TestReadPIDFileFromPreviousBoot(t *testing.T) { |
| 63 | fakeBootSessionID(t, "boot-1", nil) |
| 64 | dir := t.TempDir() |
| 65 | path := filepath.Join(dir, filenames.HostAgentPID) |
| 66 | assert.NilError(t, WritePIDFile(path, os.Getpid())) |
| 67 | |
| 68 | |
| 69 | |
| 70 | fakeBootSessionID(t, "boot-2", nil) |
| 71 | |
| 72 | pid, err := ReadPIDFile(path) |
| 73 | assert.NilError(t, err) |
| 74 | assert.Equal(t, pid, 0) |
| 75 | |
| 76 | |
| 77 | assert.Assert(t, osutil.FileExists(path)) |
| 78 | } |
| 79 | |
| 80 | func TestReadPIDFileCorruptedFromPreviousBoot(t *testing.T) { |
| 81 | fakeBootSessionID(t, "boot-1", nil) |
| 82 | dir := t.TempDir() |
| 83 | path := filepath.Join(dir, filenames.HostAgentPID) |
| 84 | assert.NilError(t, WritePIDFile(path, os.Getpid())) |
| 85 | |
| 86 | assert.NilError(t, os.WriteFile(path, nil, 0o644)) |
| 87 | |
| 88 | fakeBootSessionID(t, "boot-2", nil) |
| 89 | |
| 90 | pid, err := ReadPIDFile(path) |
| 91 | assert.NilError(t, err) |
| 92 | assert.Equal(t, pid, 0) |
| 93 | } |
| 94 | |
| 95 | func TestReadPIDFileWithoutBootSessionMarker(t *testing.T) { |
| 96 | |
| 97 | |
| 98 | fakeBootSessionID(t, "boot-1", nil) |
| 99 | dir := t.TempDir() |
| 100 | path := filepath.Join(dir, filenames.HostAgentPID) |
| 101 | assert.NilError(t, os.WriteFile(path, fmt.Appendf(nil, "%d\n", os.Getpid()), 0o644)) |
| 102 | |
| 103 | pid, err := ReadPIDFile(path) |
| 104 | assert.NilError(t, err) |
| 105 | assert.Equal(t, pid, os.Getpid()) |
| 106 | } |
| 107 | |
| 108 | func TestWritePIDFileWhenBootSessionIDFails(t *testing.T) { |
| 109 | |
| 110 | |
| 111 | fakeBootSessionID(t, "", errors.New("sysctl failed")) |
| 112 | dir := t.TempDir() |
| 113 | path := filepath.Join(dir, filenames.HostAgentPID) |
| 114 | |
| 115 | assert.ErrorContains(t, WritePIDFile(path, os.Getpid()), "sysctl failed") |
| 116 | assert.Assert(t, !osutil.FileExists(path)) |
| 117 | } |
| 118 | |
| 119 | func TestReadPIDFileWhenBootSessionIDFails(t *testing.T) { |
| 120 | fakeBootSessionID(t, "boot-1", nil) |
| 121 | dir := t.TempDir() |
| 122 | path := filepath.Join(dir, filenames.HostAgentPID) |
| 123 | assert.NilError(t, WritePIDFile(path, os.Getpid())) |
| 124 | |
| 125 | fakeBootSessionID(t, "", errors.New("sysctl failed")) |
| 126 | _, err := ReadPIDFile(path) |
| 127 | assert.ErrorContains(t, err, "sysctl failed") |
| 128 | } |
| 129 | |
| 130 | func TestWritePIDFileWithoutBootSessionSupport(t *testing.T) { |
| 131 | fakeBootSessionID(t, "", osutil.ErrBootSessionNotSupported) |
| 132 | dir := t.TempDir() |
| 133 | path := filepath.Join(dir, filenames.HostAgentPID) |
| 134 | assert.NilError(t, WritePIDFile(path, os.Getpid())) |
| 135 | |
| 136 | assert.Assert(t, !osutil.FileExists(filepath.Join(dir, filenames.BootSession))) |
| 137 | |
| 138 | pid, err := ReadPIDFile(path) |
| 139 | assert.NilError(t, err) |
| 140 | assert.Equal(t, pid, os.Getpid()) |
| 141 | } |