|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Text.Json; |
| 6 | +using AwesomeAssertions; |
| 7 | +using Microsoft.Azure.WebJobs.Script.Diagnostics.OpenTelemetry; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Microsoft.Azure.WebJobs.Script.Tests.Configuration |
| 11 | +{ |
| 12 | + public class ScriptJobHostOptionsTests |
| 13 | + { |
| 14 | + [Theory] |
| 15 | + [InlineData(FileLoggingMode.Always, 10, true, "Always", "00:10:00", "ApplicationInsights")] |
| 16 | + [InlineData(FileLoggingMode.Never, 5, false, "Never", "00:05:00", "OpenTelemetry")] |
| 17 | + [InlineData(FileLoggingMode.DebugOnly, 0.5, true, "DebugOnly", "00:00:30", "ApplicationInsights")] |
| 18 | + public void Format_SerializesOptionsToJson( |
| 19 | + FileLoggingMode loggingMode, |
| 20 | + double timeoutMinutes, |
| 21 | + bool fileWatchingEnabled, |
| 22 | + string expectedLoggingMode, |
| 23 | + string expectedTimeout, |
| 24 | + string expectedTelemetryMode) |
| 25 | + { |
| 26 | + var options = new ScriptJobHostOptions |
| 27 | + { |
| 28 | + FileLoggingMode = loggingMode, |
| 29 | + FunctionTimeout = TimeSpan.FromMinutes(timeoutMinutes), |
| 30 | + FileWatchingEnabled = fileWatchingEnabled, |
| 31 | + TelemetryMode = Enum.Parse<TelemetryMode>(expectedTelemetryMode) |
| 32 | + }; |
| 33 | + |
| 34 | + string json = options.Format(); |
| 35 | + var root = JsonDocument.Parse(json).RootElement; |
| 36 | + |
| 37 | + root.TryGetProperty("FileWatchingEnabled", out var fileWatchingProperty).Should().BeTrue(); |
| 38 | + fileWatchingProperty.GetBoolean().Should().Be(fileWatchingEnabled); |
| 39 | + |
| 40 | + root.TryGetProperty("FileLoggingMode", out var fileLoggingProperty).Should().BeTrue(); |
| 41 | + fileLoggingProperty.GetString().Should().Be(expectedLoggingMode); |
| 42 | + |
| 43 | + root.TryGetProperty("FunctionTimeout", out var timeoutProperty).Should().BeTrue(); |
| 44 | + timeoutProperty.GetString().Should().Be(expectedTimeout); |
| 45 | + |
| 46 | + root.TryGetProperty("TelemetryMode", out var telemetryModeProperty).Should().BeTrue(); |
| 47 | + telemetryModeProperty.GetString().Should().Be(expectedTelemetryMode); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void Format_WithNullFunctionTimeout_SerializesAsNull() |
| 52 | + { |
| 53 | + var options = new ScriptJobHostOptions |
| 54 | + { |
| 55 | + FileLoggingMode = FileLoggingMode.Never, |
| 56 | + FileWatchingEnabled = true, |
| 57 | + FunctionTimeout = null |
| 58 | + }; |
| 59 | + |
| 60 | + string json = options.Format(); |
| 61 | + var root = JsonDocument.Parse(json).RootElement; |
| 62 | + |
| 63 | + root.TryGetProperty("FunctionTimeout", out var timeoutProperty).Should().BeTrue(); |
| 64 | + timeoutProperty.ValueKind.Should().Be(JsonValueKind.Null); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void Format_ReturnsValidIndentedJson() |
| 69 | + { |
| 70 | + var options = new ScriptJobHostOptions |
| 71 | + { |
| 72 | + FileLoggingMode = FileLoggingMode.Always, |
| 73 | + FileWatchingEnabled = true, |
| 74 | + FunctionTimeout = TimeSpan.FromMinutes(5) |
| 75 | + }; |
| 76 | + |
| 77 | + string json = options.Format(); |
| 78 | + |
| 79 | + // Should be valid JSON |
| 80 | + var exception = Record.Exception(() => JsonDocument.Parse(json)); |
| 81 | + exception.Should().BeNull(); |
| 82 | + |
| 83 | + // Should be indented (contains newlines) |
| 84 | + json.Should().Contain(Environment.NewLine); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments