|
1 | 1 | package instances |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "errors" |
| 6 | + "os" |
5 | 7 | "testing" |
| 8 | + "time" |
6 | 9 |
|
| 10 | + "github.com/kernel/hypeman/lib/hypervisor" |
7 | 11 | snapshotstore "github.com/kernel/hypeman/lib/snapshot" |
8 | 12 | "github.com/stretchr/testify/assert" |
9 | 13 | "github.com/stretchr/testify/require" |
@@ -206,3 +210,111 @@ func TestValidateCreateSnapshotRequestRejectsStoppedCompression(t *testing.T) { |
206 | 210 | require.Error(t, err) |
207 | 211 | assert.True(t, errors.Is(err, ErrInvalidRequest)) |
208 | 212 | } |
| 213 | + |
| 214 | +func TestDeleteInstanceCancelsCompressionJob(t *testing.T) { |
| 215 | + t.Parallel() |
| 216 | + |
| 217 | + mgr, _ := setupTestManager(t) |
| 218 | + ctx := context.Background() |
| 219 | + const instanceID = "delete-instance-compression" |
| 220 | + |
| 221 | + require.NoError(t, mgr.ensureDirectories(instanceID)) |
| 222 | + now := time.Now() |
| 223 | + require.NoError(t, mgr.saveMetadata(&metadata{StoredMetadata: StoredMetadata{ |
| 224 | + Id: instanceID, |
| 225 | + Name: instanceID, |
| 226 | + DataDir: mgr.paths.InstanceDir(instanceID), |
| 227 | + HypervisorType: hypervisor.TypeCloudHypervisor, |
| 228 | + CreatedAt: now, |
| 229 | + StoppedAt: &now, |
| 230 | + }})) |
| 231 | + |
| 232 | + target := installCancelableCompressionJob(mgr, compressionTarget{ |
| 233 | + Key: mgr.snapshotJobKeyForInstance(instanceID), |
| 234 | + OwnerID: instanceID, |
| 235 | + SnapshotDir: mgr.paths.InstanceSnapshotLatest(instanceID), |
| 236 | + Source: snapshotCompressionSourceStandby, |
| 237 | + Policy: snapshotstore.SnapshotCompressionConfig{ |
| 238 | + Enabled: true, |
| 239 | + Algorithm: snapshotstore.SnapshotCompressionAlgorithmZstd, |
| 240 | + Level: intPtr(1), |
| 241 | + }, |
| 242 | + }) |
| 243 | + |
| 244 | + require.NoError(t, mgr.DeleteInstance(ctx, instanceID)) |
| 245 | + assertCompressionJobCanceled(t, mgr, target) |
| 246 | + _, err := os.Stat(mgr.paths.InstanceDir(instanceID)) |
| 247 | + require.Error(t, err) |
| 248 | + assert.True(t, os.IsNotExist(err)) |
| 249 | +} |
| 250 | + |
| 251 | +func TestDeleteSnapshotCancelsCompressionJob(t *testing.T) { |
| 252 | + t.Parallel() |
| 253 | + |
| 254 | + mgr, _ := setupTestManager(t) |
| 255 | + ctx := context.Background() |
| 256 | + const snapshotID = "delete-snapshot-compression" |
| 257 | + |
| 258 | + snapshotDir := mgr.paths.SnapshotGuestDir(snapshotID) |
| 259 | + require.NoError(t, os.MkdirAll(snapshotDir, 0o755)) |
| 260 | + require.NoError(t, mgr.saveSnapshotRecord(&snapshotRecord{ |
| 261 | + Snapshot: Snapshot{ |
| 262 | + Id: snapshotID, |
| 263 | + Name: snapshotID, |
| 264 | + Kind: SnapshotKindStandby, |
| 265 | + CreatedAt: time.Now(), |
| 266 | + }, |
| 267 | + })) |
| 268 | + |
| 269 | + target := installCancelableCompressionJob(mgr, compressionTarget{ |
| 270 | + Key: mgr.snapshotJobKeyForSnapshot(snapshotID), |
| 271 | + SnapshotID: snapshotID, |
| 272 | + SnapshotDir: snapshotDir, |
| 273 | + Source: snapshotCompressionSourceSnapshot, |
| 274 | + Policy: snapshotstore.SnapshotCompressionConfig{ |
| 275 | + Enabled: true, |
| 276 | + Algorithm: snapshotstore.SnapshotCompressionAlgorithmLz4, |
| 277 | + Level: intPtr(0), |
| 278 | + }, |
| 279 | + }) |
| 280 | + |
| 281 | + require.NoError(t, mgr.DeleteSnapshot(ctx, snapshotID)) |
| 282 | + assertCompressionJobCanceled(t, mgr, target) |
| 283 | + _, err := os.Stat(mgr.paths.SnapshotDir(snapshotID)) |
| 284 | + require.Error(t, err) |
| 285 | + assert.True(t, os.IsNotExist(err)) |
| 286 | +} |
| 287 | + |
| 288 | +func installCancelableCompressionJob(mgr *manager, target compressionTarget) *compressionTarget { |
| 289 | + ctx, cancel := context.WithCancel(context.Background()) |
| 290 | + done := make(chan struct{}) |
| 291 | + |
| 292 | + mgr.compressionMu.Lock() |
| 293 | + mgr.compressionJobs[target.Key] = &compressionJob{ |
| 294 | + cancel: cancel, |
| 295 | + done: done, |
| 296 | + target: target, |
| 297 | + } |
| 298 | + mgr.compressionMu.Unlock() |
| 299 | + |
| 300 | + go func() { |
| 301 | + <-ctx.Done() |
| 302 | + mgr.compressionMu.Lock() |
| 303 | + delete(mgr.compressionJobs, target.Key) |
| 304 | + mgr.compressionMu.Unlock() |
| 305 | + close(done) |
| 306 | + }() |
| 307 | + |
| 308 | + return &target |
| 309 | +} |
| 310 | + |
| 311 | +func assertCompressionJobCanceled(t *testing.T, mgr *manager, target *compressionTarget) { |
| 312 | + t.Helper() |
| 313 | + |
| 314 | + require.Eventually(t, func() bool { |
| 315 | + mgr.compressionMu.Lock() |
| 316 | + defer mgr.compressionMu.Unlock() |
| 317 | + _, ok := mgr.compressionJobs[target.Key] |
| 318 | + return !ok |
| 319 | + }, time.Second, 10*time.Millisecond) |
| 320 | +} |
0 commit comments