(t *testing.T)
| 9 | ) |
| 10 | |
| 11 | func TestGCPCredentialOption(t *testing.T) { |
| 12 | tests := []struct { |
| 13 | name string |
| 14 | json string |
| 15 | wantErr string |
| 16 | }{ |
| 17 | { |
| 18 | name: "service_account", |
| 19 | json: `{"type": "service_account", "project_id": "test"}`, |
| 20 | }, |
| 21 | { |
| 22 | name: "external_account", |
| 23 | json: `{"type": "external_account", "audience": "test"}`, |
| 24 | }, |
| 25 | { |
| 26 | name: "impersonated_service_account", |
| 27 | json: `{"type": "impersonated_service_account"}`, |
| 28 | }, |
| 29 | { |
| 30 | name: "authorized_user", |
| 31 | json: `{"type": "authorized_user"}`, |
| 32 | }, |
| 33 | { |
| 34 | name: "invalid JSON", |
| 35 | json: `not json`, |
| 36 | wantErr: "failed to parse GCP credential JSON", |
| 37 | }, |
| 38 | { |
| 39 | name: "missing type field", |
| 40 | json: `{"project_id": "test"}`, |
| 41 | wantErr: `missing "type" field`, |
| 42 | }, |
| 43 | { |
| 44 | name: "unsupported type", |
| 45 | json: `{"type": "unknown_type"}`, |
| 46 | wantErr: `unsupported GCP credential type: "unknown_type"`, |
| 47 | }, |
| 48 | } |
| 49 | |
| 50 | for _, tc := range tests { |
| 51 | t.Run(tc.name, func(t *testing.T) { |
| 52 | got, err := GCPCredentialOption([]byte(tc.json)) |
| 53 | if tc.wantErr != "" { |
| 54 | if err == nil { |
| 55 | t.Fatalf("expected error containing %q, got nil", tc.wantErr) |
| 56 | } |
| 57 | if !strings.Contains(err.Error(), tc.wantErr) { |
| 58 | t.Fatalf("expected error containing %q, got %q", tc.wantErr, err.Error()) |
| 59 | } |
| 60 | return |
| 61 | } |
| 62 | if err != nil { |
| 63 | t.Fatalf("unexpected error: %v", err) |
| 64 | } |
| 65 | if got == nil { |
| 66 | t.Fatal("expected non-nil ClientOption") |
| 67 | } |
| 68 | }) |
nothing calls this directly
no test coverage detected