|
| 1 | +package instances |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/kernel/hypeman/lib/hypervisor" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// TestInstallForkSharedMemFile_SymlinksSourceMemFile verifies that the helper |
| 15 | +// creates a symlink at the fork's snapshot mem-file path pointing back to the |
| 16 | +// source instance's mem-file. |
| 17 | +func TestInstallForkSharedMemFile_SymlinksSourceMemFile(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + mgr, _ := newStorageOnlyManager(t) |
| 21 | + sourceID := "shared-memfile-source" |
| 22 | + |
| 23 | + srcSnapshotDir := mgr.paths.InstanceSnapshotLatest(sourceID) |
| 24 | + require.NoError(t, os.MkdirAll(srcSnapshotDir, 0o755)) |
| 25 | + srcMem := filepath.Join(srcSnapshotDir, templateSharedMemFileName) |
| 26 | + require.NoError(t, os.WriteFile(srcMem, []byte("guest memory bytes"), 0o644)) |
| 27 | + |
| 28 | + forkDir := filepath.Join(t.TempDir(), "fork-data") |
| 29 | + |
| 30 | + require.NoError(t, mgr.installForkSharedMemFile(forkDir, sourceID)) |
| 31 | + |
| 32 | + forkMem := filepath.Join(forkDir, "snapshots", "snapshot-latest", templateSharedMemFileName) |
| 33 | + info, err := os.Lstat(forkMem) |
| 34 | + require.NoError(t, err) |
| 35 | + assert.NotZero(t, info.Mode()&os.ModeSymlink, "fork mem-file must be a symlink, not a regular file") |
| 36 | + |
| 37 | + target, err := os.Readlink(forkMem) |
| 38 | + require.NoError(t, err) |
| 39 | + assert.Equal(t, srcMem, target) |
| 40 | +} |
| 41 | + |
| 42 | +// TestInstallForkSharedMemFile_ErrorsWhenSourceMissing makes sure the helper |
| 43 | +// refuses to silently create a dangling symlink when the source mem-file does |
| 44 | +// not exist. |
| 45 | +func TestInstallForkSharedMemFile_ErrorsWhenSourceMissing(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + |
| 48 | + mgr, _ := newStorageOnlyManager(t) |
| 49 | + forkDir := filepath.Join(t.TempDir(), "fork-data") |
| 50 | + |
| 51 | + err := mgr.installForkSharedMemFile(forkDir, "no-such-source") |
| 52 | + require.Error(t, err) |
| 53 | +} |
| 54 | + |
| 55 | +// TestForkFirecrackerSharesMemFile_FromStandby verifies the end-to-end fork |
| 56 | +// path: when the source is a Firecracker standby instance, the fork's |
| 57 | +// mem-file is a symlink to the source's mem-file instead of a copy. This |
| 58 | +// preserves the firecracker MAP_PRIVATE COW semantics that let multiple forks |
| 59 | +// share the heavy backing file. |
| 60 | +func TestForkFirecrackerSharesMemFile_FromStandby(t *testing.T) { |
| 61 | + t.Parallel() |
| 62 | + |
| 63 | + mgr, _ := setupTestManager(t) |
| 64 | + ctx := context.Background() |
| 65 | + |
| 66 | + sourceID := "shared-memfile-fc-src" |
| 67 | + createStandbySnapshotSourceFixture(t, mgr, sourceID, "shared-memfile-fc-src", hypervisor.TypeFirecracker) |
| 68 | + |
| 69 | + srcSnapshotDir := mgr.paths.InstanceSnapshotLatest(sourceID) |
| 70 | + srcMem := filepath.Join(srcSnapshotDir, templateSharedMemFileName) |
| 71 | + require.NoError(t, os.WriteFile(srcMem, []byte("firecracker mem-file contents"), 0o644)) |
| 72 | + snapshotConfigPath := mgr.paths.InstanceSnapshotConfig(sourceID) |
| 73 | + require.NoError(t, os.MkdirAll(filepath.Dir(snapshotConfigPath), 0o755)) |
| 74 | + require.NoError(t, os.WriteFile(snapshotConfigPath, []byte(`{}`), 0o644)) |
| 75 | + |
| 76 | + forked, err := mgr.forkInstanceFromStoppedOrStandby(ctx, sourceID, ForkInstanceRequest{ |
| 77 | + Name: "shared-memfile-fc-fork", |
| 78 | + TargetState: StateStopped, |
| 79 | + }, true, false) |
| 80 | + require.NoError(t, err) |
| 81 | + require.NotNil(t, forked) |
| 82 | + |
| 83 | + forkMem := filepath.Join(mgr.paths.InstanceSnapshotLatest(forked.Id), templateSharedMemFileName) |
| 84 | + info, err := os.Lstat(forkMem) |
| 85 | + require.NoError(t, err) |
| 86 | + assert.NotZero(t, info.Mode()&os.ModeSymlink, "fork mem-file must be a symlink for firecracker fan-out") |
| 87 | + |
| 88 | + target, err := os.Readlink(forkMem) |
| 89 | + require.NoError(t, err) |
| 90 | + assert.Equal(t, srcMem, target) |
| 91 | +} |
| 92 | + |
| 93 | +// TestForkFirecrackerRunningSourceDoesNotShareMemFile guards the |
| 94 | +// running-fork carve-out: when the source will be restored afterward, the |
| 95 | +// fork must own its mem-file outright. Sharing would let the source's restore |
| 96 | +// mutate the fork's backing memory. |
| 97 | +func TestForkFirecrackerRunningSourceDoesNotShareMemFile(t *testing.T) { |
| 98 | + t.Parallel() |
| 99 | + |
| 100 | + mgr, _ := setupTestManager(t) |
| 101 | + ctx := context.Background() |
| 102 | + |
| 103 | + sourceID := "running-fork-fc-src" |
| 104 | + createStandbySnapshotSourceFixture(t, mgr, sourceID, "running-fork-fc-src", hypervisor.TypeFirecracker) |
| 105 | + |
| 106 | + srcSnapshotDir := mgr.paths.InstanceSnapshotLatest(sourceID) |
| 107 | + srcMem := filepath.Join(srcSnapshotDir, templateSharedMemFileName) |
| 108 | + require.NoError(t, os.WriteFile(srcMem, []byte("firecracker mem-file contents"), 0o644)) |
| 109 | + snapshotConfigPath := mgr.paths.InstanceSnapshotConfig(sourceID) |
| 110 | + require.NoError(t, os.MkdirAll(filepath.Dir(snapshotConfigPath), 0o755)) |
| 111 | + require.NoError(t, os.WriteFile(snapshotConfigPath, []byte(`{}`), 0o644)) |
| 112 | + |
| 113 | + forked, err := mgr.forkInstanceFromStoppedOrStandby(ctx, sourceID, ForkInstanceRequest{ |
| 114 | + Name: "running-fork-fc-fork", |
| 115 | + TargetState: StateStopped, |
| 116 | + }, true, true) |
| 117 | + require.NoError(t, err) |
| 118 | + require.NotNil(t, forked) |
| 119 | + |
| 120 | + forkMem := filepath.Join(mgr.paths.InstanceSnapshotLatest(forked.Id), templateSharedMemFileName) |
| 121 | + info, err := os.Lstat(forkMem) |
| 122 | + require.NoError(t, err) |
| 123 | + assert.Zero(t, info.Mode()&os.ModeSymlink, "running-fork mem-file must be a regular file copy, not a symlink") |
| 124 | +} |
0 commit comments