| 1 | package cage |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "reflect" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | type storageRuntimeFixture struct { |
| 13 | events []string |
| 14 | begin func(StorageTransition) error |
| 15 | switchVM func(StorageTransition) error |
| 16 | finish func(StorageTransition, bool) error |
| 17 | } |
| 18 | |
| 19 | func (*storageRuntimeFixture) Name() string { return "test-native-vm" } |
| 20 | func (*storageRuntimeFixture) Snapshot(context.Context, string) (StorageRuntimeSnapshot, error) { |
| 21 | return StorageRuntimeSnapshot{State: []byte(`{"vm":"original"}`)}, nil |
| 22 | } |
| 23 | func (runtime *storageRuntimeFixture) Begin(_ context.Context, move StorageTransition) error { |
| 24 | runtime.events = append(runtime.events, "begin") |
| 25 | if runtime.begin != nil { |
| 26 | return runtime.begin(move) |
| 27 | } |
| 28 | return nil |
| 29 | } |
| 30 | func (runtime *storageRuntimeFixture) Switch(_ context.Context, move StorageTransition) error { |
| 31 | runtime.events = append(runtime.events, "switch") |
| 32 | if runtime.switchVM != nil { |
| 33 | return runtime.switchVM(move) |
| 34 | } |
| 35 | return nil |
| 36 | } |
| 37 | func (runtime *storageRuntimeFixture) Recover(_ context.Context, move StorageTransition, committed bool) error { |
| 38 | action := "rollback" |
| 39 | if committed { |
| 40 | action = "commit" |
| 41 | } |
| 42 | runtime.events = append(runtime.events, action) |
| 43 | if runtime.finish != nil { |
| 44 | return runtime.finish(move, committed) |
| 45 | } |
| 46 | return nil |
| 47 | } |
| 48 | |
| 49 | func TestStorageRuntimeUsesVerifiedCopyAndRetainsOriginal(t *testing.T) { |
| 50 | profile, root := newStorageFixture(t) |
| 51 | payload := []byte("original VM disk and account state") |
| 52 | file := filepath.Join("native-vm", "system.vhdx") |
| 53 | writeStorageFixture(t, filepath.Join(profile, "state", file), payload) |
| 54 | runtime := &storageRuntimeFixture{} |
| 55 | runtime.switchVM = func(move StorageTransition) error { |
| 56 | assertStorageFile(t, filepath.Join(move.Source, file), payload) |
| 57 | assertStorageFile(t, filepath.Join(move.Destination, file), payload) |
| 58 | if _, err := os.Readlink(storageAnchor(profile)); err == nil { |
| 59 | t.Fatal("stable path changed before the runtime accepted its new data") |
| 60 | } |
| 61 | return nil |
| 62 | } |
| 63 | runtime.finish = func(move StorageTransition, committed bool) error { |
| 64 | if !committed { |
| 65 | t.Fatal("successful move rolled back") |
| 66 | } |
| 67 | assertStorageFile(t, filepath.Join(move.Backup, file), payload) |
| 68 | actual, err := os.Stat(storageAnchor(profile)) |
| 69 | destination, targetErr := os.Stat(move.Destination) |
| 70 | if err != nil || targetErr != nil || !os.SameFile(actual, destination) { |
| 71 | t.Fatalf("runtime committed before directory: anchor %v, destination %v", err, targetErr) |
| 72 | } |
| 73 | return nil |
| 74 | } |
| 75 | location, err := MoveStorageWithRuntime(context.Background(), profile, filepath.Join(root, "moved"), storageExpectation(t, profile), runtime, nil) |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | if !reflect.DeepEqual(runtime.events, []string{"begin", "switch", "commit"}) { |
| 80 | t.Fatal(runtime.events) |
| 81 | } |
| 82 | assertStorageFile(t, filepath.Join(location.Backup, file), payload) |
| 83 | } |
| 84 | |
| 85 | func TestStorageRuntimeFailureRetainsBothCopiesUntilRecovery(t *testing.T) { |
| 86 | profile, root := newStorageFixture(t) |
| 87 | payload := []byte("original") |
| 88 | file := filepath.Join("native-vm", "system.vhdx") |
| 89 | writeStorageFixture(t, filepath.Join(profile, "state", file), payload) |
| 90 | runtime := &storageRuntimeFixture{switchVM: func(StorageTransition) error { return errors.New("runtime attach failed") }} |
| 91 | var transition StorageTransition |
| 92 | runtime.finish = func(move StorageTransition, committed bool) error { |
| 93 | transition = move |
| 94 | if committed { |
| 95 | t.Fatal("failed runtime attached as committed") |
| 96 | } |
| 97 | assertStorageFile(t, filepath.Join(storageAnchor(profile), file), payload) |
| 98 | assertStorageFile(t, filepath.Join(move.Destination, file), payload) |
| 99 | return errors.New("helper disconnected") |
| 100 | } |
| 101 | if _, err := MoveStorageWithRuntime(context.Background(), profile, filepath.Join(root, "moved"), storageExpectation(t, profile), runtime, nil); err == nil { |
| 102 | t.Fatal("failed migration succeeded") |
| 103 | } |
| 104 | if _, err := GetStorageLocation(profile); !errors.Is(err, ErrStorageRuntimeRecovery) { |
| 105 | t.Fatalf("plain caller resolved native transaction: %v", err) |
| 106 | } |
| 107 | if _, _, err := AcquireStorageUse(profile); !errors.Is(err, ErrStorageRuntimeRecovery) { |
| 108 | t.Fatalf("CLI started unresolved VM: %v", err) |
| 109 | } |
| 110 | assertStorageFile(t, filepath.Join(transition.Destination, file), payload) |
| 111 | runtime.finish = func(move StorageTransition, committed bool) error { |
| 112 | assertStorageFile(t, filepath.Join(storageAnchor(profile), file), payload) |
| 113 | assertStorageFile(t, filepath.Join(move.Destination, file), payload) |
| 114 | return nil |
| 115 | } |
| 116 | if err := RecoverStorage(context.Background(), profile, runtime); err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | if _, err := os.Stat(transition.Destination); !errors.Is(err, os.ErrNotExist) { |
| 120 | t.Fatal("recovered failed copy retained", err) |
| 121 | } |
| 122 | assertStorageFile(t, filepath.Join(storageAnchor(profile), file), payload) |
| 123 | } |
| 124 | |
| 125 | func TestStorageRuntimeCrashRecoveryUsesDurableCommit(t *testing.T) { |
| 126 | for _, committed := range []bool{false, true} { |
| 127 | name := "before_commit" |
| 128 | if committed { |
| 129 | name = "after_commit" |
| 130 | } |
| 131 | t.Run(name, func(t *testing.T) { |
| 132 | profile, root := newStorageFixture(t) |
| 133 | id := "0123456789abcdef0123456789abcdef" |
| 134 | target := filepath.Join(root, "new") |
| 135 | payload := []byte("disk data") |
| 136 | writeStorageFixture(t, filepath.Join(storageAnchor(profile), "system.vhdx"), payload) |
| 137 | writeStorageFixture(t, filepath.Join(target, "state", "system.vhdx"), payload) |
| 138 | if err := claimStorageTarget(target, id); err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | if err := claimStorageTarget(filepath.Join(target, "state"), id); err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | backup := filepath.Join(profile, "state-backup-"+id) |
| 145 | move := storageMove{Next: storageRecord{Directory: target, ID: id, Backups: []string{backup}}, Runtime: "test-native-vm", Transition: StorageTransition{ID: id, Source: storageAnchor(profile), Destination: filepath.Join(target, "state"), Backup: backup, State: []byte(`{"vm":"original"}`)}} |
| 146 | if err := writeStorageJSON(filepath.Join(profile, pendingFile), move); err != nil { |
| 147 | t.Fatal(err) |
| 148 | } |
| 149 | if err := renameStorage(storageAnchor(profile), backup); err != nil { |
| 150 | t.Fatal(err) |
| 151 | } |
| 152 | if err := linkStorage(storageAnchor(profile), move.Transition.Destination); err != nil { |
| 153 | t.Fatal(err) |
| 154 | } |
| 155 | if committed { |
| 156 | if err := writeStorageJSON(filepath.Join(profile, locationFile), move.Next); err != nil { |
| 157 | t.Fatal(err) |
| 158 | } |
| 159 | } |
| 160 | runtime := &storageRuntimeFixture{finish: func(transition StorageTransition, actual bool) error { |
| 161 | if actual != committed { |
| 162 | t.Fatal("runtime used a different commit decision") |
| 163 | } |
| 164 | assertStorageFile(t, filepath.Join(storageAnchor(profile), "system.vhdx"), payload) |
| 165 | assertStorageFile(t, filepath.Join(transition.Destination, "system.vhdx"), payload) |
| 166 | _, linkErr := os.Readlink(storageAnchor(profile)) |
| 167 | if committed != (linkErr == nil) { |
| 168 | t.Fatal("directory rollback happened after runtime recovery") |
| 169 | } |
| 170 | return nil |
| 171 | }} |
| 172 | if err := RecoverStorage(context.Background(), profile, runtime); err != nil { |
| 173 | t.Fatal(err) |
| 174 | } |
| 175 | if err := RecoverStorage(context.Background(), profile, runtime); err != nil { |
| 176 | t.Fatal("idempotent recovery failed", err) |
| 177 | } |
| 178 | if len(runtime.events) != 1 { |
| 179 | t.Fatal("completed recovery repeated a VM operation", runtime.events) |
| 180 | } |
| 181 | }) |
| 182 | } |
| 183 | } |