|
1 | 1 | package hypervisor |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "archive/tar" |
| 5 | + "bytes" |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "io" |
| 9 | + "os" |
| 10 | + "path/filepath" |
4 | 11 | "strings" |
5 | 12 | "testing" |
6 | 13 |
|
7 | 14 | "github.com/cocoonstack/cocoon/types" |
8 | 15 | ) |
9 | 16 |
|
| 17 | +func TestFailRestoreSparesStoppedOrigin(t *testing.T) { |
| 18 | + b, _ := newMeteringTestBackend(t) |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + origin types.VMState |
| 22 | + want types.VMState |
| 23 | + }{ |
| 24 | + {"stopped origin stays stopped (wake retryable)", types.VMStateStopped, types.VMStateStopped}, |
| 25 | + {"running origin marks error", types.VMStateRunning, types.VMStateError}, |
| 26 | + } |
| 27 | + for _, tt := range tests { |
| 28 | + t.Run(tt.name, func(t *testing.T) { |
| 29 | + id := "vm-fail-" + string(tt.origin) |
| 30 | + seedVMRecord(t, b, id, 1, 512, 1024, true) |
| 31 | + if err := b.DB.Update(t.Context(), func(idx *VMIndex) error { |
| 32 | + idx.VMs[id].State = tt.origin |
| 33 | + return nil |
| 34 | + }); err != nil { |
| 35 | + t.Fatalf("seed state: %v", err) |
| 36 | + } |
| 37 | + |
| 38 | + b.FailRestore(t.Context(), id, tt.origin) |
| 39 | + |
| 40 | + rec, err := b.LoadRecord(t.Context(), id) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("load record: %v", err) |
| 43 | + } |
| 44 | + if rec.State != tt.want { |
| 45 | + t.Errorf("state %s, want %s", rec.State, tt.want) |
| 46 | + } |
| 47 | + }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestDirectRestoreFailureKeepsOriginContract(t *testing.T) { |
| 52 | + failAfterExtract := func(spec *DirectRestoreSpec) { |
| 53 | + spec.AfterExtract = func(context.Context, string, *types.VMConfig, *VMRecord) (*types.VM, error) { |
| 54 | + return nil, errors.New("launch boom") |
| 55 | + } |
| 56 | + } |
| 57 | + failPopulate := func(spec *DirectRestoreSpec) { |
| 58 | + spec.Populate = func(*VMRecord, string) error { return errors.New("populate boom") } |
| 59 | + } |
| 60 | + tests := []struct { |
| 61 | + name string |
| 62 | + origin types.VMState |
| 63 | + fail func(*DirectRestoreSpec) |
| 64 | + want types.VMState |
| 65 | + }{ |
| 66 | + {"stopped origin survives a post-populate failure", types.VMStateStopped, failAfterExtract, types.VMStateStopped}, |
| 67 | + {"running origin quarantines on a post-populate failure", types.VMStateRunning, failAfterExtract, types.VMStateError}, |
| 68 | + {"partial populate quarantines even a stopped origin", types.VMStateStopped, failPopulate, types.VMStateError}, |
| 69 | + } |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + b, _ := newMeteringTestBackend(t) |
| 73 | + const id = "vm-direct-fail" |
| 74 | + seedVMRecord(t, b, id, 1, 512, 1024, true) |
| 75 | + if err := b.DB.Update(t.Context(), func(idx *VMIndex) error { |
| 76 | + idx.VMs[id].State = tt.origin |
| 77 | + return nil |
| 78 | + }); err != nil { |
| 79 | + t.Fatalf("seed state: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + spec := DirectRestoreSpec{ |
| 83 | + VMCfg: &types.VMConfig{Config: types.Config{CPU: 1, Memory: 512, Storage: 1024}}, |
| 84 | + SrcDir: t.TempDir(), |
| 85 | + Preflight: func(string, *VMRecord) error { return nil }, |
| 86 | + Kill: func(context.Context, string, *VMRecord) error { return nil }, |
| 87 | + Populate: func(*VMRecord, string) error { return nil }, |
| 88 | + } |
| 89 | + tt.fail(&spec) |
| 90 | + if _, err := b.DirectRestoreSequence(t.Context(), id, spec); err == nil { |
| 91 | + t.Fatal("expected restore failure") |
| 92 | + } |
| 93 | + rec, err := b.LoadRecord(t.Context(), id) |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("load record: %v", err) |
| 96 | + } |
| 97 | + if rec.State != tt.want { |
| 98 | + t.Errorf("state %s, want %s", rec.State, tt.want) |
| 99 | + } |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestRestorePartialMergeQuarantinesEvenStoppedOrigin(t *testing.T) { |
| 105 | + b, _ := newMeteringTestBackend(t) |
| 106 | + const id = "vm-merge-fail" |
| 107 | + runDir := t.TempDir() |
| 108 | + seedVMRecord(t, b, id, 1, 512, 1024, true) |
| 109 | + if err := b.DB.Update(t.Context(), func(idx *VMIndex) error { |
| 110 | + idx.VMs[id].State = types.VMStateStopped |
| 111 | + idx.VMs[id].RunDir = runDir |
| 112 | + return nil |
| 113 | + }); err != nil { |
| 114 | + t.Fatalf("seed state: %v", err) |
| 115 | + } |
| 116 | + |
| 117 | + // Staged "a" merges, then staged file "b" cannot rename over the run |
| 118 | + // dir's directory "b": the merge fails halfway through, which must |
| 119 | + // quarantine even a stopped origin. |
| 120 | + if err := os.MkdirAll(filepath.Join(runDir, "b"), 0o750); err != nil { |
| 121 | + t.Fatalf("setup: %v", err) |
| 122 | + } |
| 123 | + spec := RestoreSpec{ |
| 124 | + VMCfg: &types.VMConfig{Config: types.Config{CPU: 1, Memory: 512, Storage: 1024}}, |
| 125 | + Snapshot: tarWithFiles(t, "a", "b"), |
| 126 | + Preflight: func(string, *VMRecord) error { return nil }, |
| 127 | + Kill: func(context.Context, string, *VMRecord) error { return nil }, |
| 128 | + AfterExtract: func(context.Context, string, *types.VMConfig, *VMRecord) (*types.VM, error) { |
| 129 | + t.Fatal("AfterExtract must not run after a failed merge") |
| 130 | + return nil, nil |
| 131 | + }, |
| 132 | + } |
| 133 | + if _, err := b.RestoreSequence(t.Context(), id, spec); err == nil { |
| 134 | + t.Fatal("expected merge failure") |
| 135 | + } |
| 136 | + rec, err := b.LoadRecord(t.Context(), id) |
| 137 | + if err != nil { |
| 138 | + t.Fatalf("load record: %v", err) |
| 139 | + } |
| 140 | + if rec.State != types.VMStateError { |
| 141 | + t.Errorf("state %s, want error (mixed run dir must quarantine)", rec.State) |
| 142 | + } |
| 143 | + if _, err := os.Stat(filepath.Join(runDir, "a")); err != nil { |
| 144 | + t.Errorf("merge was not partial: %v", err) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func tarWithFiles(t *testing.T, names ...string) io.Reader { |
| 149 | + t.Helper() |
| 150 | + var buf bytes.Buffer |
| 151 | + tw := tar.NewWriter(&buf) |
| 152 | + for _, name := range names { |
| 153 | + if err := tw.WriteHeader(&tar.Header{Name: name, Mode: 0o600, Size: 1}); err != nil { |
| 154 | + t.Fatalf("tar hdr %s: %v", name, err) |
| 155 | + } |
| 156 | + if _, err := tw.Write([]byte("y")); err != nil { |
| 157 | + t.Fatalf("tar body %s: %v", name, err) |
| 158 | + } |
| 159 | + } |
| 160 | + if err := tw.Close(); err != nil { |
| 161 | + t.Fatalf("tar close: %v", err) |
| 162 | + } |
| 163 | + return &buf |
| 164 | +} |
| 165 | + |
10 | 166 | func TestResolveForRestoreStates(t *testing.T) { |
11 | 167 | b, _ := newMeteringTestBackend(t) |
12 | 168 | tests := []struct { |
|
0 commit comments