|
| 1 | +// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package driver |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/codes" |
| 11 | + "github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/status" |
| 12 | + "github.com/gophercloud/gophercloud/v2" |
| 13 | + . "github.com/onsi/ginkgo/v2" |
| 14 | + . "github.com/onsi/gomega" |
| 15 | + |
| 16 | + "github.com/gardener/machine-controller-manager-provider-openstack/pkg/driver/executor" |
| 17 | +) |
| 18 | + |
| 19 | +var _ = Describe("Utils", func() { |
| 20 | + |
| 21 | + Context("mapErrorToCode", func() { |
| 22 | + It("should map executor.ErrFlavorNotFound error to ResourceExhausted error code", func() { |
| 23 | + err1 := executor.ErrFlavorNotFound{} |
| 24 | + err2 := status.Error(mapErrorToCode(err1), err1.Error()) |
| 25 | + Expect(err1).To(HaveOccurred()) |
| 26 | + Expect(err2).To(HaveOccurred()) |
| 27 | + Expect(mapErrorToCode(err1)).To(Equal(codes.ResourceExhausted)) |
| 28 | + }) |
| 29 | + It("should map executor.ErrFlavorNotFound error with specific flavor to ResourceExhausted error code", func() { |
| 30 | + err1 := executor.ErrFlavorNotFound{Flavor: "flavor"} |
| 31 | + err2 := status.Error(mapErrorToCode(err1), err1.Error()) |
| 32 | + Expect(err1).To(HaveOccurred()) |
| 33 | + Expect(err2).To(HaveOccurred()) |
| 34 | + Expect(mapErrorToCode(err1)).To(Equal(codes.ResourceExhausted)) |
| 35 | + }) |
| 36 | + It("should map executor.ErrNotFound error to NotFound error code", func() { |
| 37 | + err1 := executor.ErrNotFound |
| 38 | + err2 := status.Error(mapErrorToCode(err1), err1.Error()) |
| 39 | + Expect(err1).To(HaveOccurred()) |
| 40 | + Expect(err2).To(HaveOccurred()) |
| 41 | + Expect(mapErrorToCode(err1)).To(Equal(codes.NotFound)) |
| 42 | + }) |
| 43 | + It("should map gophercloud.ErrResourceNotFound error to Internal error code", func() { |
| 44 | + err1 := gophercloud.ErrResourceNotFound{} |
| 45 | + err2 := status.Error(mapErrorToCode(err1), err1.Error()) |
| 46 | + Expect(err1).To(HaveOccurred()) |
| 47 | + Expect(err2).To(HaveOccurred()) |
| 48 | + Expect(mapErrorToCode(err1)).To(Equal(codes.Internal)) |
| 49 | + }) |
| 50 | + It("should map error containing executor.NoValidHost to ResourceExhausted error code", func() { |
| 51 | + err1 := fmt.Errorf("error: %s", executor.NoValidHost) |
| 52 | + err2 := status.Error(mapErrorToCode(err1), err1.Error()) |
| 53 | + Expect(err1).To(HaveOccurred()) |
| 54 | + Expect(err2).To(HaveOccurred()) |
| 55 | + Expect(mapErrorToCode(err1)).To(Equal(codes.ResourceExhausted)) |
| 56 | + }) |
| 57 | + It("should map error containing random string to Internal error code", func() { |
| 58 | + err1 := fmt.Errorf("error: Random provider issue") |
| 59 | + err2 := status.Error(mapErrorToCode(err1), err1.Error()) |
| 60 | + Expect(err1).To(HaveOccurred()) |
| 61 | + Expect(err2).To(HaveOccurred()) |
| 62 | + Expect(mapErrorToCode(err1)).To(Equal(codes.Internal)) |
| 63 | + }) |
| 64 | + }) |
| 65 | +}) |
0 commit comments