Skip to content

Commit ab7760c

Browse files
authored
Fix tag for health check endpoints (#11363)
* Fix tag for health check endpoints * Add tests for health check expand
1 parent c9609a3 commit ab7760c

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/WebJobs.Script.WebHost/WebJobsApplicationBuilderExtension.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.AspNetCore.Hosting;
88
using Microsoft.AspNetCore.Http;
99
using Microsoft.Azure.WebJobs.Extensions.Http;
10+
using Microsoft.Azure.WebJobs.Script.Diagnostics.HealthChecks;
1011
using Microsoft.Azure.WebJobs.Script.Extensions;
1112
using Microsoft.Azure.WebJobs.Script.WebHost.Configuration;
1213
using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.HealthChecks;
@@ -151,13 +152,13 @@ static bool Predicate(HttpContext context)
151152

152153
app.UseHealthChecks($"{healthPrefix}/live", new HealthCheckOptions
153154
{
154-
Predicate = r => r.Tags.Contains("az.functions.liveness"),
155+
Predicate = r => r.Tags.Contains(HealthCheckTags.Liveness),
155156
ResponseWriter = HealthCheckResponseWriter.WriteResponseAsync,
156157
});
157158

158159
app.UseHealthChecks($"{healthPrefix}/ready", new HealthCheckOptions
159160
{
160-
Predicate = r => r.Tags.Contains("az.functions.readiness"),
161+
Predicate = r => r.Tags.Contains(HealthCheckTags.Readiness),
161162
ResponseWriter = HealthCheckResponseWriter.WriteResponseAsync,
162163
});
163164
});

test/WebJobs.Script.Tests.Integration/WebHostEndToEnd/SamplesEndToEndTests_CSharp.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
using System.Net.Http.Json;
1212
using System.Reflection;
1313
using System.Text;
14+
using System.Text.Json;
1415
using System.Threading;
1516
using System.Threading.Tasks;
1617
using System.Xml.Linq;
1718
using Microsoft.Azure.Storage.Blob;
1819
using Microsoft.Azure.WebJobs.Script.Config;
20+
using Microsoft.Azure.WebJobs.Script.Diagnostics.HealthChecks;
1921
using Microsoft.Azure.WebJobs.Script.Management.Models;
2022
using Microsoft.Azure.WebJobs.Script.Models;
2123
using Microsoft.Azure.WebJobs.Script.WebHost;
@@ -331,6 +333,43 @@ public async Task HealthCheck_AdminToken_Succeeds(string uri)
331333
Assert.Equal("{\"status\":\"Healthy\"}", body);
332334
}
333335

336+
[Theory]
337+
[InlineData("/admin/health?expand=true", null)]
338+
[InlineData("/admin/health/live?expand=true", HealthCheckTags.Liveness)]
339+
[InlineData("/admin/health/ready?expand=true", HealthCheckTags.Readiness)]
340+
public async Task HealthCheck_AdminToken_ExpandSucceeds(string uri, string tag)
341+
{
342+
// token specified as bearer token
343+
HttpRequestMessage request = new(HttpMethod.Get, uri);
344+
string token = _fixture.Host.GenerateAdminJwtToken();
345+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
346+
HttpResponseMessage response = await _fixture.Host.HttpClient.SendAsync(request);
347+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
348+
349+
// Not doing a deep validation of the response, just ensuring we get the correct tags.
350+
// The full response body is validated in other tests.
351+
using Stream stream = await response.Content.ReadAsStreamAsync();
352+
JsonDocument json = await JsonDocument.ParseAsync(stream);
353+
JsonElement entries = json.RootElement.GetProperty("entries");
354+
355+
if (string.IsNullOrEmpty(tag))
356+
{
357+
Assert.NotEmpty(entries.EnumerateObject());
358+
}
359+
else
360+
{
361+
bool atLeastOne = false;
362+
foreach (JsonProperty entry in entries.EnumerateObject())
363+
{
364+
atLeastOne = true;
365+
HashSet<string> tags = entry.Value.GetProperty("tags").Deserialize<HashSet<string>>();
366+
Assert.Contains(tag, tags);
367+
}
368+
369+
Assert.True(atLeastOne, "Expected at least one entry to have the specified tag.");
370+
}
371+
}
372+
334373
[Theory]
335374
[InlineData("/admin/health")]
336375
[InlineData("/admin/health/live")]

0 commit comments

Comments
 (0)