|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using Azure.Functions.Cli.Kubernetes.KEDA.Models; |
| 4 | +using Azure.Functions.Cli.Kubernetes.KEDA.V2; |
| 5 | +using Azure.Functions.Cli.Kubernetes.KEDA.V2.Models; |
| 6 | +using Azure.Functions.Cli.Kubernetes.Models; |
| 7 | +using Azure.Functions.Cli.Kubernetes.Models.Kubernetes; |
| 8 | +using Newtonsoft.Json.Linq; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace Azure.Functions.Cli.Tests |
| 12 | +{ |
| 13 | + public class KedaV2ResourceTests |
| 14 | + { |
| 15 | + private readonly Dictionary<string, JObject> _functions = new Dictionary<string, JObject>(); |
| 16 | + |
| 17 | + public KedaV2ResourceTests() |
| 18 | + { |
| 19 | + _functions.Add( |
| 20 | + "HelloOrchestration", |
| 21 | + JObject.Parse(@" |
| 22 | +{ |
| 23 | + ""generatedBy"": ""Microsoft.NET.Sdk.Functions-3.0.13"", |
| 24 | + ""configurationSource"": ""attributes"", |
| 25 | + ""bindings"": [ |
| 26 | + { |
| 27 | + ""type"": ""orchestrationTrigger"", |
| 28 | + ""name"": ""context"" |
| 29 | + } |
| 30 | + ], |
| 31 | + ""disabled"": false, |
| 32 | + ""scriptFile"": ""../bin/Functions.HelloWorld.dll"", |
| 33 | + ""entryPoint"": ""Functions.HelloWorld.HelloOrchestration"" |
| 34 | +} |
| 35 | +")); |
| 36 | + } |
| 37 | + |
| 38 | + [Theory] |
| 39 | + [InlineData(null)] |
| 40 | + [InlineData("{ }")] |
| 41 | + [InlineData("{ \"type\": \"vnext\" }")] |
| 42 | + public void GetUnsupportedDurableScalar(string providerJson) |
| 43 | + { |
| 44 | + string hostSnippet = @" |
| 45 | +{ |
| 46 | + ""extensions"": { |
| 47 | + ""maxConcurrentOrchestratorFunctions"": 5, |
| 48 | + ""maxConcurrentActivityFunctions"": 10, |
| 49 | + ""durableTask"": { |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | +"; |
| 54 | + |
| 55 | + JObject hostConfig = JObject.Parse(hostSnippet); |
| 56 | + JObject durableTaskConfig = hostConfig.SelectToken("extensions.durableTask") as JObject; |
| 57 | + |
| 58 | + if (providerJson != null) |
| 59 | + durableTaskConfig.Add("storageProvider", JObject.Parse(providerJson)); |
| 60 | + |
| 61 | + KedaV2Resource resource = new KedaV2Resource(); |
| 62 | + ScaledObjectKedaV2 scaledObject = resource.GetKubernetesResource( |
| 63 | + "HelloWorld", |
| 64 | + "default", |
| 65 | + new TriggersPayload { HostJson = hostConfig, FunctionsJson = _functions }, |
| 66 | + new DeploymentV1Apps { Metadata = new ObjectMetadataV1 { Name = "HelloDeployment" } }, |
| 67 | + 30, |
| 68 | + 300, |
| 69 | + 1, |
| 70 | + 8) as ScaledObjectKedaV2; |
| 71 | + |
| 72 | + Assert.NotNull(scaledObject); |
| 73 | + Assert.Empty(scaledObject.Spec.Triggers); |
| 74 | + } |
| 75 | + |
| 76 | + [Theory] |
| 77 | + [InlineData(null, null, 10, 1)] |
| 78 | + [InlineData(3, 6, 3, 6)] |
| 79 | + public void GetMsSqlDurableScalar( |
| 80 | + int? configuredMaxOrchestrations, |
| 81 | + int? configuredMaxActivities, |
| 82 | + int expectedMaxOrchestrations, |
| 83 | + int expectedMaxActivities) |
| 84 | + { |
| 85 | + string hostSnippet = @" |
| 86 | +{ |
| 87 | + ""extensions"": { |
| 88 | + ""durableTask"": { |
| 89 | + ""storageProvider"": { |
| 90 | + ""type"": ""mssql"", |
| 91 | + ""connectionStringName"": ""MySqlConnection"", |
| 92 | + ""taskEventLockTimeout"": ""00:01:00"", |
| 93 | + ""partitionCount"": 5 |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +"; |
| 99 | + |
| 100 | + JObject hostConfig = JObject.Parse(hostSnippet); |
| 101 | + JObject durableTaskConfig = hostConfig.SelectToken("extensions.durableTask") as JObject; |
| 102 | + |
| 103 | + if (configuredMaxOrchestrations.HasValue) |
| 104 | + durableTaskConfig.Add("maxConcurrentOrchestratorFunctions", configuredMaxOrchestrations); |
| 105 | + |
| 106 | + if (configuredMaxActivities.HasValue) |
| 107 | + durableTaskConfig.Add("maxConcurrentActivityFunctions", configuredMaxActivities); |
| 108 | + |
| 109 | + KedaV2Resource resource = new KedaV2Resource(); |
| 110 | + ScaledObjectKedaV2 scaledObject = resource.GetKubernetesResource( |
| 111 | + "HelloWorld", |
| 112 | + "default", |
| 113 | + new TriggersPayload { HostJson = hostConfig, FunctionsJson = _functions }, |
| 114 | + new DeploymentV1Apps { Metadata = new ObjectMetadataV1 { Name = "HelloDeployment" } }, |
| 115 | + 30, |
| 116 | + 300, |
| 117 | + 1, |
| 118 | + 8) as ScaledObjectKedaV2; |
| 119 | + |
| 120 | + Assert.NotNull(scaledObject); |
| 121 | + AssertMsSqlDurableScalar(scaledObject.Spec.Triggers.Single(), expectedMaxOrchestrations, expectedMaxActivities, "MySqlConnection"); |
| 122 | + } |
| 123 | + |
| 124 | + private static void AssertMsSqlDurableScalar(ScaledObjectTriggerV1Alpha1 actual, int maxOrchestrations, int maxActivities, string connectionString) |
| 125 | + { |
| 126 | + Assert.NotNull(actual); |
| 127 | + Assert.Equal("mssql", actual.Type); |
| 128 | + Assert.Equal(3, actual.Metadata.Count); |
| 129 | + Assert.Equal($"SELECT dt.GetScaleRecommendation({maxOrchestrations}, {maxActivities})", actual.Metadata["query"]); |
| 130 | + Assert.Equal("1", actual.Metadata["targetValue"]); |
| 131 | + Assert.Equal(connectionString, actual.Metadata["connectionStringFromEnv"]); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments