|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + migrationv1alpha1 "github.com/openshift/vcf-migration-operator/api/v1alpha1" |
| 9 | +) |
| 10 | + |
| 11 | +func TestIsTransientError(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + err error |
| 15 | + want bool |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "nil error is not transient", |
| 19 | + err: nil, |
| 20 | + want: false, |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "plain error is not transient", |
| 24 | + err: fmt.Errorf("boom"), |
| 25 | + want: false, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "transient error is transient", |
| 29 | + err: newTransientError(fmt.Errorf("cluster operators are not healthy")), |
| 30 | + want: true, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "transient error wrapped by additional context is still transient", |
| 34 | + err: fmt.Errorf("running preflight checks: %w", newTransientError(fmt.Errorf("cluster upgrade is in progress"))), |
| 35 | + want: true, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + for _, tt := range tests { |
| 40 | + t.Run(tt.name, func(t *testing.T) { |
| 41 | + if got := isTransientError(tt.err); got != tt.want { |
| 42 | + t.Fatalf("isTransientError(%v) = %v, want %v", tt.err, got, tt.want) |
| 43 | + } |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestTransientErrorUnwrap(t *testing.T) { |
| 49 | + inner := fmt.Errorf("cluster operators are not healthy") |
| 50 | + transient := newTransientError(inner) |
| 51 | + |
| 52 | + if transient.Error() != inner.Error() { |
| 53 | + t.Fatalf("transient.Error() = %q, want %q", transient.Error(), inner.Error()) |
| 54 | + } |
| 55 | + if !errors.Is(transient, inner) { |
| 56 | + t.Fatalf("errors.Is(transient, inner) = false, want true") |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestReasonForError(t *testing.T) { |
| 61 | + tests := []struct { |
| 62 | + name string |
| 63 | + err error |
| 64 | + want string |
| 65 | + }{ |
| 66 | + { |
| 67 | + name: "transient error retries", |
| 68 | + err: newTransientError(fmt.Errorf("cluster operators are not healthy")), |
| 69 | + want: migrationv1alpha1.ReasonRetrying, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "plain error fails", |
| 73 | + err: fmt.Errorf("spec.failureDomains must not be empty"), |
| 74 | + want: migrationv1alpha1.ReasonFailed, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + for _, tt := range tests { |
| 79 | + t.Run(tt.name, func(t *testing.T) { |
| 80 | + if got := reasonForError(tt.err); got != tt.want { |
| 81 | + t.Fatalf("reasonForError(%v) = %q, want %q", tt.err, got, tt.want) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
0 commit comments