|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +package fc |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "net" |
| 10 | + "testing" |
| 11 | + |
| 12 | + openapiruntime "github.com/go-openapi/runtime" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + |
| 15 | + "github.com/e2b-dev/infra/packages/shared/pkg/fc/client/operations" |
| 16 | + "github.com/e2b-dev/infra/packages/shared/pkg/fc/models" |
| 17 | +) |
| 18 | + |
| 19 | +// Firecracker's fault for an MSR the reading host's kernel refuses. Kept whole, |
| 20 | +// as it arrives: the classifier matches two nested stages inside it. |
| 21 | +const msrFault = "Load snapshot error: Failed to restore from snapshot: Failed to build microVM from snapshot: Failed to restore vCPUs: Failed to run action on vcpu: Failed to set all KVM MSRs for this vCPU. Only a partial write was done." |
| 22 | + |
| 23 | +func badRequest(fault string) error { |
| 24 | + return &operations.LoadSnapshotBadRequest{Payload: &models.Error{FaultMessage: fault}} |
| 25 | +} |
| 26 | + |
| 27 | +func TestClassifySnapshotLoadFailure(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + |
| 30 | + for _, tc := range []struct { |
| 31 | + name string |
| 32 | + err error |
| 33 | + want snapshotLoadFailureReason |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "msr fault names the vcpu and the msr step", |
| 37 | + err: badRequest(msrFault), |
| 38 | + want: snapshotLoadVcpuMSR, |
| 39 | + }, |
| 40 | + { |
| 41 | + // The reason exists so that an MSR alert does not absorb every |
| 42 | + // other way vCPU restore can fail. |
| 43 | + name: "vcpu restore failing elsewhere is not an msr failure", |
| 44 | + err: badRequest("Load snapshot error: Failed to restore from snapshot: Failed to build microVM from snapshot: Failed to restore vCPUs: Failed to run action on vcpu: Failed to set the TSC frequency."), |
| 45 | + want: snapshotLoadVcpuOther, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "refusal that does not reach the vcpus", |
| 49 | + err: badRequest("Load snapshot error: Failed to restore from snapshot: Cannot deserialize the microVM state: Snapshot file was created by a different version"), |
| 50 | + want: snapshotLoadBadRequest, |
| 51 | + }, |
| 52 | + { |
| 53 | + // A later Firecracker rewording the fault must still count as a |
| 54 | + // refusal, so the total stays complete and only the breakdown |
| 55 | + // loses precision. |
| 56 | + name: "unrecognized fault text still counts as a refusal", |
| 57 | + err: badRequest("Load snapshot error: something we have never seen"), |
| 58 | + want: snapshotLoadBadRequest, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "400 without a payload is still a refusal", |
| 62 | + err: &operations.LoadSnapshotBadRequest{}, |
| 63 | + want: snapshotLoadBadRequest, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "wrapped fault is classified through the wrapping", |
| 67 | + err: fmt.Errorf("error loading snapshot: %w", badRequest(msrFault)), |
| 68 | + want: snapshotLoadVcpuMSR, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "non-400 status never reached the snapshot", |
| 72 | + err: operations.NewLoadSnapshotDefault(500), |
| 73 | + want: snapshotLoadUnavailable, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "api error never reached the snapshot", |
| 77 | + err: openapiruntime.NewAPIError("load snapshot", nil, 503), |
| 78 | + want: snapshotLoadUnavailable, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "unreachable socket says nothing about the snapshot", |
| 82 | + err: fmt.Errorf("post unix socket: %w", &net.OpError{Op: "dial", Err: errors.New("connection refused")}), |
| 83 | + want: snapshotLoadTransport, |
| 84 | + }, |
| 85 | + { |
| 86 | + // The one reason an alert leaves out, so it must not absorb a load |
| 87 | + // that never finished. |
| 88 | + name: "caller cancellation is not a broken snapshot", |
| 89 | + err: fmt.Errorf("load snapshot: %w", context.Canceled), |
| 90 | + want: snapshotLoadCanceled, |
| 91 | + }, |
| 92 | + { |
| 93 | + // The orchestrator's own request timeout: the load overran. |
| 94 | + name: "deadline is a load that never finished", |
| 95 | + err: fmt.Errorf("load snapshot: %w", context.DeadlineExceeded), |
| 96 | + want: snapshotLoadTimeout, |
| 97 | + }, |
| 98 | + { |
| 99 | + // Firecracker can answer while the caller is already gone. Reading |
| 100 | + // the cancel instead would drop a dead template from the total. |
| 101 | + name: "an answer outranks a cancel that arrived with it", |
| 102 | + err: fmt.Errorf("load snapshot: %w", errors.Join(badRequest(msrFault), context.Canceled)), |
| 103 | + want: snapshotLoadVcpuMSR, |
| 104 | + }, |
| 105 | + } { |
| 106 | + t.Run(tc.name, func(t *testing.T) { |
| 107 | + t.Parallel() |
| 108 | + |
| 109 | + assert.Equal(t, tc.want, classifySnapshotLoadFailure(tc.err)) |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments