|
14 | 14 |
|
15 | 15 | using Google.Api; |
16 | 16 | using Google.Api.Gax; |
| 17 | +using Google.Apis.Auth.OAuth2; |
17 | 18 | using Google.Cloud.Logging.V2; |
18 | 19 | using Google.Protobuf.WellKnownTypes; |
19 | 20 | using Newtonsoft.Json.Linq; |
|
29 | 30 | using System.Collections.Concurrent; |
30 | 31 | using System.Collections.Generic; |
31 | 32 | using System.Diagnostics; |
| 33 | +using System.IO; |
32 | 34 | using System.Linq; |
33 | 35 | using System.Reflection; |
34 | 36 | using System.Runtime.CompilerServices; |
@@ -56,6 +58,19 @@ private void LogInfo(IEnumerable<string> messages) |
56 | 58 | private const string s_projectId = "projectId"; |
57 | 59 | private const string s_logId = "logId"; |
58 | 60 |
|
| 61 | + private const string s_DummyServiceAccountCredentialJson = @"{ |
| 62 | + ""type"": ""service_account"", |
| 63 | + ""project_id"": ""test-project"", |
| 64 | + ""private_key_id"": ""a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2"", |
| 65 | + ""private_key"": ""-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"", |
| 66 | + ""client_email"": ""test-service-account@test-project.iam.gserviceaccount.com"", |
| 67 | + ""client_id"": ""123456789012"", |
| 68 | + ""auth_uri"": ""https://accounts.google.com/o/oauth2/auth"", |
| 69 | + ""token_uri"": ""https://oauth2.googleapis.com/token"", |
| 70 | + ""auth_provider_x509_cert_url"": ""https://www.googleapis.com/oauth2/v1/certs"", |
| 71 | + ""client_x509_cert_url"": ""https://www.googleapis.com/robot/v1/metadata/x509/test-service-account%40test-project.iam.gserviceaccount.com"" |
| 72 | + }"; |
| 73 | + |
59 | 74 | private async Task RunTest( |
60 | 75 | Func<IEnumerable<LogEntry>, Task<WriteLogEntriesResponse>> handlerFn, |
61 | 76 | Func<GoogleStackdriverTarget, Task> testFn, |
@@ -152,6 +167,41 @@ public async Task InvalidOptions_MultipleCredentials() |
152 | 167 | Assert.True(innerEx.Message.Contains("CredentialFile") && innerEx.Message.Contains("CredentialJson")); |
153 | 168 | } |
154 | 169 |
|
| 170 | + [Fact] |
| 171 | + public async Task GoogleStackdriverTarget_FromFile_InvalidType_ThrowsArgumentException() |
| 172 | + { |
| 173 | + var tempFile = Path.GetTempFileName(); |
| 174 | + File.WriteAllText(tempFile, s_DummyServiceAccountCredentialJson); |
| 175 | + try |
| 176 | + { |
| 177 | + var target = new GoogleStackdriverTarget |
| 178 | + { |
| 179 | + ProjectId = "a_project_id", |
| 180 | + CredentialFile = Layout.FromString(tempFile), |
| 181 | + CredentialType = Layout.FromString("invalid_type"), |
| 182 | + }; |
| 183 | + var nlogEx = await Assert.ThrowsAsync<NLogRuntimeException>(() => ActivateTargetAsync(target)); |
| 184 | + Assert.IsType<InvalidOperationException>(nlogEx.InnerException); |
| 185 | + } |
| 186 | + finally |
| 187 | + { |
| 188 | + File.Delete(tempFile); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + [Fact] |
| 193 | + public async Task GoogleStackdriverTarget_FromJson_InvalidType_ThrowsArgumentException() |
| 194 | + { |
| 195 | + var target = new GoogleStackdriverTarget |
| 196 | + { |
| 197 | + ProjectId = "a_project_id", |
| 198 | + CredentialJson = Layout.FromString(s_DummyServiceAccountCredentialJson), |
| 199 | + CredentialType = Layout.FromString("invalid_type"), |
| 200 | + }; |
| 201 | + var nlogEx = await Assert.ThrowsAsync<NLogRuntimeException>(() => ActivateTargetAsync(target)); |
| 202 | + Assert.IsType<InvalidOperationException>(nlogEx.InnerException); |
| 203 | + } |
| 204 | + |
155 | 205 | [Fact] |
156 | 206 | public async Task InvalidOptions_MultipleJsonConverters() |
157 | 207 | { |
|
0 commit comments