Skip to content

Commit f6fc193

Browse files
Adding unit tests for profiles
1 parent da7546d commit f6fc193

14 files changed

+385
-15
lines changed

src/WebJobs.Script.WebHost/WebScriptHostBuilderExtension.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
using Microsoft.Azure.WebJobs.Script.WebHost.Management;
2323
using Microsoft.Azure.WebJobs.Script.WebHost.Middleware;
2424
using Microsoft.Azure.WebJobs.Script.WebHost.Storage;
25-
using Microsoft.Azure.WebJobs.Script.Workers;
26-
using Microsoft.Azure.WebJobs.Script.Workers.Profiles;
2725
using Microsoft.Extensions.DependencyInjection;
2826
using Microsoft.Extensions.DependencyInjection.Extensions;
2927
using Microsoft.Extensions.Hosting;

src/WebJobs.Script/Workers/Profiles/EnvironmentCondition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public bool Evaluate()
5454
}
5555

5656
// Validates if condition parametrs meet expected values, fail if they don't
57-
internal void Validate()
57+
private void Validate()
5858
{
5959
if (string.IsNullOrEmpty(Name))
6060
{

src/WebJobs.Script/Workers/Profiles/HostPropertyCondition.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Microsoft.Azure.WebJobs.Script.Workers
1313
{
1414
// HostPropertycondition checks if host match the expected output for properties such as Sku, Platform, HostVersion
15-
internal class HostPropertyCondition : IWorkerProfileCondition
15+
public class HostPropertyCondition : IWorkerProfileCondition
1616
{
1717
private readonly ILogger _logger;
1818
private readonly ISystemRuntimeInformation _systemRuntimeInformation;
@@ -50,7 +50,7 @@ public enum HostProperty
5050
/// <inheritdoc />
5151
public bool Evaluate()
5252
{
53-
Enum.TryParse(Name, out HostProperty hostPropertyName);
53+
var hostPropertyName = Enum.Parse(typeof(HostProperty), Name, true);
5454

5555
string value = hostPropertyName switch
5656
{
@@ -71,14 +71,14 @@ public bool Evaluate()
7171
}
7272

7373
// Validates if condition parametrs meet expected values, fail if they don't
74-
internal void Validate()
74+
private void Validate()
7575
{
7676
if (string.IsNullOrEmpty(Name))
7777
{
7878
throw new ValidationException($"HostPropertyCondition {nameof(Name)} cannot be empty.");
7979
}
8080

81-
if (!Enum.GetNames(typeof(HostProperty)).Any(x => x.ToLower().Contains(Name)))
81+
if (!Enum.GetNames(typeof(HostProperty)).Any(x => x.ToLower().Contains(Name.ToLower())))
8282
{
8383
throw new ValidationException($"HostPropertyCondition {nameof(Name)} is not a valid host property name.");
8484
}

src/WebJobs.Script/Workers/Profiles/SystemConditionProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ namespace Microsoft.Azure.WebJobs.Script.Workers
99
{
1010
internal sealed class SystemConditionProvider : IWorkerProfileConditionProvider
1111
{
12-
private readonly ILogger<SystemConditionProvider> _logger;
12+
private readonly ILogger _logger;
1313
private readonly IEnvironment _environment;
1414
private readonly ISystemRuntimeInformation _systemRuntimeInformation;
1515

16-
public SystemConditionProvider(ILogger<SystemConditionProvider> logger, ISystemRuntimeInformation systemRuntimeInfo, IEnvironment environment)
16+
public SystemConditionProvider(ILogger logger, ISystemRuntimeInformation systemRuntimeInfo, IEnvironment environment)
1717
{
1818
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
1919
_environment = environment ?? throw new ArgumentNullException(nameof(environment));

src/WebJobs.Script/Workers/Profiles/WorkerDescriptionProfile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public WorkerDescriptionProfile(string name, List<IWorkerProfileCondition> condi
3636

3737
public string ProfileId { get; }
3838

39-
public void Validate()
39+
private void Validate()
4040
{
4141
if (string.IsNullOrEmpty(Name))
4242
{

src/WebJobs.Script/Workers/Profiles/WorkerProfileConditionDescriptor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public sealed class WorkerProfileConditionDescriptor
1717

1818
private IDictionary<string, string> _properties;
1919

20+
[JsonProperty(Required = Required.Always)]
2021
public string Type { get; set; }
2122

2223
public IDictionary<string, string> Properties

src/WebJobs.Script/Workers/Rpc/Configuration/RpcWorkerConfigFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ internal void AddProvider(string workerDir)
128128
workerDescription.WorkerDirectory = workerDir;
129129

130130
//Read the profiles from worker description and load the profile for which the conditions match
131-
var profilesJToken = workerConfig.GetValue(WorkerConstants.WorkerDescriptionProfiles);
132-
if (profilesJToken != null)
131+
JToken profiles = workerConfig.GetValue(WorkerConstants.WorkerDescriptionProfiles);
132+
if (profiles != null)
133133
{
134-
List<WorkerDescriptionProfile> workerDescriptionProfiles = ReadWorkerDescriptionProfiles(profilesJToken);
134+
List<WorkerDescriptionProfile> workerDescriptionProfiles = ReadWorkerDescriptionProfiles(profiles);
135135
if (workerDescriptionProfiles.Count > 0)
136136
{
137137
_profileManager.SaveWorkerDescriptionProfiles(workerDescriptionProfiles, workerDescription.Language);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.ComponentModel.DataAnnotations;
6+
using Microsoft.Azure.WebJobs.Script.Tests;
7+
using Xunit;
8+
9+
namespace Microsoft.Azure.WebJobs.Script.Workers.Profiles
10+
{
11+
public class EnvironmentConditionTests
12+
{
13+
private TestEnvironment _testEnvironment = new TestEnvironment();
14+
15+
[Theory]
16+
[InlineData(null, null)]
17+
[InlineData("", "")]
18+
[InlineData("", null)]
19+
[InlineData(null, "")]
20+
[InlineData("APPLICATIONINSIGHTS_ENABLE_AGENT", null)]
21+
[InlineData("APPLICATIONINSIGHTS_ENABLE_AGENT", "")]
22+
[InlineData(null, "true")]
23+
[InlineData("", "true")]
24+
public void EnvironmentConditionTest_ThrowsValidationException(string name, string expression)
25+
{
26+
var testLogger = new TestLogger("test");
27+
var descriptor = new WorkerProfileConditionDescriptor();
28+
descriptor.Type = WorkerConstants.WorkerDescriptionProfileEnvironmentCondition;
29+
30+
descriptor.Properties[WorkerConstants.WorkerDescriptionProfileConditionName] = name;
31+
descriptor.Properties[WorkerConstants.WorkerDescriptionProfileConditionExpression] = expression;
32+
33+
Assert.Throws<ValidationException>(() => new EnvironmentCondition(testLogger, _testEnvironment, descriptor));
34+
}
35+
36+
[Theory]
37+
[InlineData("APPLICATIONINSIGHTS_ENABLE_AGENT", "true", "true")]
38+
[InlineData("APPLICATIONINSIGHTS_ENABLE_AGENT", "^((?!true).)*$", "false")]
39+
public void EnvironmentConditionTest_EvaluateTrue(string name, string testExpression, string environmentSetting)
40+
{
41+
_testEnvironment.SetEnvironmentVariable(name, environmentSetting);
42+
43+
var testLogger = new TestLogger("test");
44+
45+
var descriptor = new WorkerProfileConditionDescriptor();
46+
descriptor.Type = WorkerConstants.WorkerDescriptionProfileEnvironmentCondition;
47+
descriptor.Properties[WorkerConstants.WorkerDescriptionProfileConditionName] = name;
48+
descriptor.Properties[WorkerConstants.WorkerDescriptionProfileConditionExpression] = testExpression;
49+
50+
var environmentCondition = new EnvironmentCondition(testLogger, _testEnvironment, descriptor);
51+
52+
Assert.True(environmentCondition.Evaluate());
53+
}
54+
55+
[Theory]
56+
[InlineData("APPLICATIONINSIGHTS_ENABLE_AGENT", "true", "false")]
57+
[InlineData("APPLICATIONINSIGHTS_ENABLE_AGENT", "^((?!true).)*$", "true")]
58+
public void EnvironmentConditionTest_EvaluateFalse(string name, string testExpression, string environmentSetting)
59+
{
60+
_testEnvironment.SetEnvironmentVariable(name, environmentSetting);
61+
62+
var testLogger = new TestLogger("test");
63+
64+
var descriptor = new WorkerProfileConditionDescriptor();
65+
descriptor.Type = WorkerConstants.WorkerDescriptionProfileEnvironmentCondition;
66+
descriptor.Properties[WorkerConstants.WorkerDescriptionProfileConditionName] = name;
67+
descriptor.Properties[WorkerConstants.WorkerDescriptionProfileConditionExpression] = testExpression;
68+
69+
var environmentCondition = new EnvironmentCondition(testLogger, _testEnvironment, descriptor);
70+
71+
Assert.False(environmentCondition.Evaluate(), "Expression evaluates to false");
72+
}
73+
}
74+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Xunit;
5+
6+
namespace Microsoft.Azure.WebJobs.Script.Workers.Profiles
7+
{
8+
public class FalseConditionTests
9+
{
10+
[Fact]
11+
public void FalseCondition_EvaluesFalse()
12+
{
13+
var falseCondition = new FalseCondition();
14+
Assert.False(falseCondition.Evaluate(), "False condition must always return false");
15+
}
16+
}
17+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.ComponentModel.DataAnnotations;
5+
using Microsoft.Azure.WebJobs.Script.Tests;
6+
using Xunit;
7+
8+
namespace Microsoft.Azure.WebJobs.Script.Workers.Profiles
9+
{
10+
public class HostPropertyConditionTests
11+
{
12+
private TestSystemRuntimeInformation _testSystemRuntimeInfo = new TestSystemRuntimeInformation();
13+
14+
[Theory]
15+
[InlineData(null, null)]
16+
[InlineData("", "")]
17+
[InlineData("", null)]
18+
[InlineData(null, "")]
19+
[InlineData("sku", null)]
20+
[InlineData("Platform", "")]
21+
[InlineData("HostVersion", null)]
22+
[InlineData("APPLICATIONINSIGHTS_ENABLE_AGENT", "")]
23+
[InlineData(null, "true")]
24+
[InlineData("", "true")]
25+
public void HostPropertyConditionTest_ThrowsValidationException(string name, string expression)
26+
{
27+
var testLogger = new TestLogger("test");
28+
var descriptor = new WorkerProfileConditionDescriptor();
29+
descriptor.Type = WorkerConstants.WorkerDescriptionProfileHostPropertyCondition;
30+
31+
descriptor.Properties[WorkerConstants.WorkerDescriptionProfileConditionName] = name;
32+
descriptor.Properties[WorkerConstants.WorkerDescriptionProfileConditionExpression] = expression;
33+
34+
Assert.Throws<ValidationException>(() => new HostPropertyCondition(testLogger, _testSystemRuntimeInfo, descriptor));
35+
}
36+
37+
[Theory]
38+
//[InlineData("sku", "Dynamic")] TODO: Add test case
39+
[InlineData("platForm", "LINUX")]
40+
[InlineData("HostVersion", "4.*")]
41+
public void HostPropertyConditionTest_EvaluateTrue(string name, string testExpression)
42+
{
43+
var testLogger = new TestLogger("test");
44+
45+
var descriptor = new WorkerProfileConditionDescriptor();
46+
descriptor.Type = WorkerConstants.WorkerDescriptionProfileHostPropertyCondition;
47+
descriptor.Properties[WorkerConstants.WorkerDescriptionProfileConditionName] = name;
48+
descriptor.Properties[WorkerConstants.WorkerDescriptionProfileConditionExpression] = testExpression;
49+
50+
var hostPropertyCondition = new HostPropertyCondition(testLogger, _testSystemRuntimeInfo, descriptor);
51+
52+
Assert.True(hostPropertyCondition.Evaluate());
53+
}
54+
55+
[Theory]
56+
//[InlineData("sku", "Dynamic")] TODO: Add test case
57+
[InlineData("platForm", "Windows")]
58+
[InlineData("HostVersion", "3.*")]
59+
public void HostPropertyConditionTest_EvaluateFalse(string name, string testExpression)
60+
{
61+
var testLogger = new TestLogger("test");
62+
63+
var descriptor = new WorkerProfileConditionDescriptor();
64+
descriptor.Type = WorkerConstants.WorkerDescriptionProfileHostPropertyCondition;
65+
descriptor.Properties[WorkerConstants.WorkerDescriptionProfileConditionName] = name;
66+
descriptor.Properties[WorkerConstants.WorkerDescriptionProfileConditionExpression] = testExpression;
67+
68+
var hostPropertyCondition = new HostPropertyCondition(testLogger, _testSystemRuntimeInfo, descriptor);
69+
70+
Assert.False(hostPropertyCondition.Evaluate(), "Expression evaluates to false");
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)