|
| 1 | +// Copyright (c) MASA Stack All rights reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. |
| 3 | + |
| 4 | +namespace Masa.Contrib.Development.DaprStarter.Tests; |
| 5 | + |
| 6 | +[TestClass] |
| 7 | +public class DaprProcessTest |
| 8 | +{ |
| 9 | + [DataRow(true, "", "AppId", true, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")] |
| 10 | + [DataRow(true, "AppId-ByEnvironment", "AppId-ByEnvironment", true, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")] |
| 11 | + [DataRow(true, "", "AppId", false, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")] |
| 12 | + [DataRow(true, "AppId-ByEnvironment", "AppId-ByEnvironment", false, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")] |
| 13 | + [DataRow(true, "", "AppId", true, "", "", "", "", "")] |
| 14 | + [DataRow(true, "AppId-ByEnvironment", "AppId-ByEnvironment", true, "", "", "", "", "")] |
| 15 | + [DataRow(true, "", "AppId", false, "", "", "", "", "")] |
| 16 | + [DataRow(true, "AppId-ByEnvironment", "AppId-ByEnvironment", false, "", "", "", "", "")] |
| 17 | + [DataRow(false, "", "AppId-AppIdSuffix", true, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")] |
| 18 | + [DataRow(false, "AppId-ByEnvironment", "AppId-ByEnvironment", true, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")] |
| 19 | + [DataRow(false, "", "AppId-AppIdSuffix", false, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")] |
| 20 | + [DataRow(false, "AppId-ByEnvironment", "AppId-ByEnvironment", false, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")] |
| 21 | + [DataRow(false, "", "AppId-AppIdSuffix", true, "", "", "", "", "")] |
| 22 | + [DataRow(false, "AppId-ByEnvironment", "AppId-ByEnvironment", true, "", "", "", "", "")] |
| 23 | + [DataRow(false, "", "AppId-AppIdSuffix", false, "", "", "", "", "")] |
| 24 | + [DataRow(false, "AppId-ByEnvironment", "AppId-ByEnvironment", false, "", "", "", "", "")] |
| 25 | + [DataTestMethod] |
| 26 | + public void TestConvertToSidecarOptions( |
| 27 | + bool disableAppIdSuffix, |
| 28 | + string inputDaprAppIdByEnvironment, |
| 29 | + string expectedDaprAppId, |
| 30 | + bool inputEnableDefaultPlacementHostAddress, |
| 31 | + string inputPlacementHostAddress, |
| 32 | + string inputRootPath, |
| 33 | + string inputDaprRootPath, |
| 34 | + string inputComponentPath, |
| 35 | + string inputConfig) |
| 36 | + { |
| 37 | + Environment.SetEnvironmentVariable(DaprStarterConstant.DEFAULT_DAPR_APPID, inputDaprAppIdByEnvironment); |
| 38 | + var daprOptions = new DaprOptions() |
| 39 | + { |
| 40 | + AppPort = 5000, |
| 41 | + AppIdSuffix = "AppIdSuffix", |
| 42 | + AppProtocol = Protocol.GRpc, |
| 43 | + EnableSsl = true, |
| 44 | + DaprGrpcPort = 5001, |
| 45 | + DaprHttpPort = 3501, |
| 46 | + EnableHeartBeat = false, |
| 47 | + HeartBeatInterval = 30, |
| 48 | + CreateNoWindow = true, |
| 49 | + MaxConcurrency = 1, |
| 50 | + Config = inputConfig, |
| 51 | + ComponentPath = inputComponentPath, |
| 52 | + RootPath = inputRootPath, |
| 53 | + DaprRootPath = inputDaprRootPath, |
| 54 | + EnableProfiling = true, |
| 55 | + LogLevel = LogLevel.Error, |
| 56 | + SentryAddress = "SentryAddress", |
| 57 | + MetricsPort = 9000, |
| 58 | + ProfilePort = 1, |
| 59 | + UnixDomainSocket = "UnixDomainSocket", |
| 60 | + AppId = "AppId", |
| 61 | + AppIdDelimiter = "-", |
| 62 | + DisableAppIdSuffix = disableAppIdSuffix, |
| 63 | + DaprHttpMaxRequestSize = 1, |
| 64 | + EnableDefaultPlacementHostAddress = inputEnableDefaultPlacementHostAddress, |
| 65 | + PlacementHostAddress = inputPlacementHostAddress, |
| 66 | + AllowedOrigins = "AllowedOrigins", |
| 67 | + ControlPlaneAddress = "ControlPlaneAddress", |
| 68 | + DaprHttpReadBufferSize = 1, |
| 69 | + DaprInternalGrpcPort = 1, |
| 70 | + EnableApiLogging = true, |
| 71 | + EnableMetrics = true, |
| 72 | + Mode = "Mode", |
| 73 | + ExtendedParameter = "ExtendedParameter" |
| 74 | + }; |
| 75 | + Mock<IOptionsMonitor<DaprOptions>> optionsMonitor = new(); |
| 76 | + |
| 77 | + var customDaprProcess = new CustomDaprProcess(new DaprEnvironmentProvider(), new DefaultDaprProvider(), optionsMonitor.Object); |
| 78 | + var sidecarOptions = customDaprProcess.ConvertToSidecarOptions(daprOptions); |
| 79 | + Assert.AreEqual(expectedDaprAppId, sidecarOptions.AppId); |
| 80 | + Assert.AreEqual(daprOptions.AppPort, sidecarOptions.AppPort); |
| 81 | + Assert.AreEqual(daprOptions.AppProtocol, sidecarOptions.AppProtocol); |
| 82 | + Assert.AreEqual(daprOptions.EnableSsl, sidecarOptions.EnableSsl); |
| 83 | + Assert.AreEqual(daprOptions.DaprGrpcPort, sidecarOptions.DaprGrpcPort); |
| 84 | + Assert.AreEqual(daprOptions.DaprHttpPort, sidecarOptions.DaprHttpPort); |
| 85 | + Assert.AreEqual(daprOptions.EnableHeartBeat, sidecarOptions.EnableHeartBeat); |
| 86 | + Assert.AreEqual(daprOptions.HeartBeatInterval, sidecarOptions.HeartBeatInterval); |
| 87 | + Assert.AreEqual(daprOptions.CreateNoWindow, sidecarOptions.CreateNoWindow); |
| 88 | + Assert.AreEqual(daprOptions.MaxConcurrency, sidecarOptions.MaxConcurrency); |
| 89 | + Assert.AreEqual(GetConfig(inputConfig), sidecarOptions.Config); |
| 90 | + Assert.AreEqual(GetComponentPath(inputComponentPath), sidecarOptions.ComponentPath); |
| 91 | + Assert.AreEqual(GetRootPath(inputRootPath), sidecarOptions.RootPath); |
| 92 | + |
| 93 | + Assert.AreEqual(GetDaprRootPath(inputDaprRootPath), sidecarOptions.DaprRootPath); |
| 94 | + Assert.AreEqual(daprOptions.EnableProfiling, sidecarOptions.EnableProfiling); |
| 95 | + Assert.AreEqual(daprOptions.LogLevel, sidecarOptions.LogLevel); |
| 96 | + Assert.AreEqual(daprOptions.SentryAddress, sidecarOptions.SentryAddress); |
| 97 | + Assert.AreEqual(daprOptions.MetricsPort, sidecarOptions.MetricsPort); |
| 98 | + Assert.AreEqual(daprOptions.ProfilePort, sidecarOptions.ProfilePort); |
| 99 | + Assert.AreEqual(daprOptions.UnixDomainSocket, sidecarOptions.UnixDomainSocket); |
| 100 | + Assert.AreEqual(daprOptions.EnableDefaultPlacementHostAddress, sidecarOptions.EnableDefaultPlacementHostAddress); |
| 101 | + |
| 102 | + Assert.AreEqual(GetPlacementHostAddress(inputEnableDefaultPlacementHostAddress, inputPlacementHostAddress), |
| 103 | + sidecarOptions.PlacementHostAddress); |
| 104 | + Assert.AreEqual(daprOptions.AllowedOrigins, sidecarOptions.AllowedOrigins); |
| 105 | + Assert.AreEqual(daprOptions.ControlPlaneAddress, sidecarOptions.ControlPlaneAddress); |
| 106 | + Assert.AreEqual(daprOptions.DaprHttpMaxRequestSize, sidecarOptions.DaprHttpMaxRequestSize); |
| 107 | + Assert.AreEqual(daprOptions.DaprHttpReadBufferSize, sidecarOptions.DaprHttpReadBufferSize); |
| 108 | + Assert.AreEqual(daprOptions.DaprInternalGrpcPort, sidecarOptions.DaprInternalGrpcPort); |
| 109 | + Assert.AreEqual(daprOptions.EnableApiLogging, sidecarOptions.EnableApiLogging); |
| 110 | + Assert.AreEqual(daprOptions.EnableMetrics, sidecarOptions.EnableMetrics); |
| 111 | + Assert.AreEqual(daprOptions.Mode, sidecarOptions.Mode); |
| 112 | + Assert.AreEqual(daprOptions.ExtendedParameter, sidecarOptions.ExtendedParameter); |
| 113 | + |
| 114 | + Environment.SetEnvironmentVariable(DaprStarterConstant.DEFAULT_DAPR_APPID, ""); |
| 115 | + |
| 116 | + string GetPlacementHostAddress(bool enableDefaultPlacementHostAddress, string placementHostAddress) |
| 117 | + { |
| 118 | + if (enableDefaultPlacementHostAddress && placementHostAddress.IsNullOrWhiteSpace()) |
| 119 | + { |
| 120 | + var port = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 6050 : 50005; |
| 121 | + return $"127.0.0.1:{port}"; |
| 122 | + } |
| 123 | + return placementHostAddress; |
| 124 | + } |
| 125 | + |
| 126 | + string GetRootPath(string rootPath) |
| 127 | + { |
| 128 | + if (rootPath.IsNullOrWhiteSpace()) |
| 129 | + return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".dapr"); |
| 130 | + |
| 131 | + return rootPath; |
| 132 | + } |
| 133 | + |
| 134 | + string GetDaprRootPath(string daprRootPath) |
| 135 | + { |
| 136 | + if (daprRootPath.IsNullOrWhiteSpace()) |
| 137 | + return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "C:\\dapr" : "/usr/local/bin"; |
| 138 | + |
| 139 | + return daprRootPath; |
| 140 | + } |
| 141 | + |
| 142 | + string GetComponentPath(string componentPath) |
| 143 | + { |
| 144 | + if (!componentPath.IsNullOrWhiteSpace()) |
| 145 | + return componentPath; |
| 146 | + |
| 147 | + return "components"; |
| 148 | + } |
| 149 | + |
| 150 | + string GetConfig(string config) |
| 151 | + { |
| 152 | + if (!config.IsNullOrWhiteSpace()) |
| 153 | + return config; |
| 154 | + |
| 155 | + return "config.yaml"; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments