|
| 1 | +/* |
| 2 | +Copyright © contributors to CloudNativePG, established as |
| 3 | +CloudNativePG a Series of LF Projects, LLC. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +
|
| 17 | +SPDX-License-Identifier: Apache-2.0 |
| 18 | +*/ |
| 19 | + |
| 20 | +package common |
| 21 | + |
| 22 | +import ( |
| 23 | + "errors" |
| 24 | + "fmt" |
| 25 | + |
| 26 | + barmanRestorer "github.com/cloudnative-pg/barman-cloud/pkg/restorer" |
| 27 | + . "github.com/onsi/ginkgo/v2" |
| 28 | + . "github.com/onsi/gomega" |
| 29 | + "google.golang.org/grpc/codes" |
| 30 | + "google.golang.org/grpc/status" |
| 31 | +) |
| 32 | + |
| 33 | +var _ = Describe("classifyWALRestoreError", func() { |
| 34 | + const walName = "000000010000000000000001" |
| 35 | + |
| 36 | + DescribeTable( |
| 37 | + "maps barman restorer sentinels to gRPC status codes", |
| 38 | + func(walErr error, expectedCode codes.Code) { |
| 39 | + got := classifyWALRestoreError(walName, walErr) |
| 40 | + Expect(got).To(HaveOccurred()) |
| 41 | + |
| 42 | + st, ok := status.FromError(got) |
| 43 | + Expect(ok).To(BeTrue(), "returned error must carry a gRPC status") |
| 44 | + Expect(st.Code()).To(Equal(expectedCode)) |
| 45 | + Expect(st.Message()).To(ContainSubstring(walName)) |
| 46 | + }, |
| 47 | + Entry("ErrWALNotFound -> NotFound", |
| 48 | + fmt.Errorf("object storage or file not found: %w", barmanRestorer.ErrWALNotFound), |
| 49 | + codes.NotFound), |
| 50 | + Entry("ErrInvalidWALName -> InvalidArgument", |
| 51 | + fmt.Errorf("invalid name for a WAL file: %w", barmanRestorer.ErrInvalidWALName), |
| 52 | + codes.InvalidArgument), |
| 53 | + Entry("ErrConnectivity -> Unavailable", |
| 54 | + fmt.Errorf("connectivity failure, retrying: %w", barmanRestorer.ErrConnectivity), |
| 55 | + codes.Unavailable), |
| 56 | + Entry("ErrGeneric -> Unavailable (barman uses exit 4 for some retryable cases too)", |
| 57 | + fmt.Errorf("generic error: %w", barmanRestorer.ErrGeneric), |
| 58 | + codes.Unavailable), |
| 59 | + Entry("unknown error -> Internal", |
| 60 | + errors.New("something we did not classify"), |
| 61 | + codes.Internal), |
| 62 | + ) |
| 63 | + |
| 64 | + It("matches the sentinel even through several wrapping layers", func() { |
| 65 | + // The plugin wraps barman errors via fmt.Errorf("...: %w", ...); |
| 66 | + // classification must keep working if more wraps appear above. |
| 67 | + inner := fmt.Errorf("connectivity failure, retrying: %w", barmanRestorer.ErrConnectivity) |
| 68 | + wrapped := fmt.Errorf("while restoring WAL %s: %w", walName, inner) |
| 69 | + |
| 70 | + got := classifyWALRestoreError(walName, wrapped) |
| 71 | + st, ok := status.FromError(got) |
| 72 | + Expect(ok).To(BeTrue()) |
| 73 | + Expect(st.Code()).To(Equal(codes.Unavailable)) |
| 74 | + }) |
| 75 | + |
| 76 | + It("treats ErrWALNotFound as terminal even when the error chain mentions other sentinels in its message", func() { |
| 77 | + // Defensive: if the underlying error stringifies to something |
| 78 | + // resembling another sentinel's message, the switch must still |
| 79 | + // match by identity (errors.Is), not by substring. |
| 80 | + walErr := fmt.Errorf("not found, looks like a connectivity failure: %w", barmanRestorer.ErrWALNotFound) |
| 81 | + |
| 82 | + got := classifyWALRestoreError(walName, walErr) |
| 83 | + st, _ := status.FromError(got) |
| 84 | + Expect(st.Code()).To(Equal(codes.NotFound)) |
| 85 | + }) |
| 86 | +}) |
0 commit comments