|
| 1 | +package cloudrun |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "google.golang.org/grpc/codes" |
| 9 | + "google.golang.org/grpc/status" |
| 10 | +) |
| 11 | + |
| 12 | +func TestClassify_NilStaysNil(t *testing.T) { |
| 13 | + require.NoError(t, Classify(nil, "p", "r")) |
| 14 | +} |
| 15 | + |
| 16 | +func TestClassify_NonGRPCErrorPassesThrough(t *testing.T) { |
| 17 | + original := errors.New("some plain go error") |
| 18 | + got := Classify(original, "p", "r") |
| 19 | + require.Same(t, original, got) |
| 20 | +} |
| 21 | + |
| 22 | +func TestClassify_UnknownGRPCCodePassesThrough(t *testing.T) { |
| 23 | + original := status.Error(codes.ResourceExhausted, "rate limited") |
| 24 | + got := Classify(original, "p", "r") |
| 25 | + require.Same(t, original, got) |
| 26 | +} |
| 27 | + |
| 28 | +func TestClassify_UnauthenticatedReturnsADCAdvice(t *testing.T) { |
| 29 | + original := status.Error(codes.Unauthenticated, "token expired") |
| 30 | + got := Classify(original, "proj-1", "europe-west1") |
| 31 | + |
| 32 | + require.Error(t, got) |
| 33 | + require.Contains(t, got.Error(), "GCP authentication failed") |
| 34 | + require.Contains(t, got.Error(), "GOOGLE_APPLICATION_CREDENTIALS") |
| 35 | + require.Contains(t, got.Error(), "gcloud auth application-default login") |
| 36 | + require.Contains(t, got.Error(), "metadata server") |
| 37 | + require.Contains(t, got.Error(), "Workload Identity") |
| 38 | + require.ErrorIs(t, got, original, "underlying error must be preserved via %%w") |
| 39 | +} |
| 40 | + |
| 41 | +func TestClassify_PermissionDeniedNamesProjectAndRoleViewer(t *testing.T) { |
| 42 | + original := status.Error(codes.PermissionDenied, "missing iam role") |
| 43 | + got := Classify(original, "proj-1", "europe-west1") |
| 44 | + |
| 45 | + require.Error(t, got) |
| 46 | + require.Contains(t, got.Error(), "GCP permission denied") |
| 47 | + require.Contains(t, got.Error(), "roles/run.viewer") |
| 48 | + require.Contains(t, got.Error(), `"proj-1"`) |
| 49 | + require.ErrorIs(t, got, original) |
| 50 | +} |
| 51 | + |
| 52 | +func TestClassify_NotFoundNamesProjectAndRegion(t *testing.T) { |
| 53 | + original := status.Error(codes.NotFound, "no such resource") |
| 54 | + got := Classify(original, "bad-project", "europe-west1") |
| 55 | + |
| 56 | + require.Error(t, got) |
| 57 | + require.Contains(t, got.Error(), "not found or not accessible") |
| 58 | + require.Contains(t, got.Error(), `"bad-project"`) |
| 59 | + require.Contains(t, got.Error(), `"europe-west1"`) |
| 60 | + require.ErrorIs(t, got, original) |
| 61 | +} |
0 commit comments