From 664a2a1de850cbc85a205c4f16125faa5277f3cf Mon Sep 17 00:00:00 2001 From: Michael Kirschner Date: Tue, 27 Feb 2024 08:04:33 -0500 Subject: [PATCH] DYN-6728 feature flags should be controlled by no network mode when creating a dynamo model directly. (#14975) * check network mode * align disable and networkmode * add small tests * remove todo * review comments * another test --- src/DynamoCore/Models/DynamoModel.cs | 6 +- .../Logging/AnalyticsServiceTest.cs | 67 +++++++++++++++++++ .../DynamoApplicationTests.cs | 19 ++++++ 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/DynamoCore/Models/DynamoModel.cs b/src/DynamoCore/Models/DynamoModel.cs index 04daed12487..e7aebf67b43 100644 --- a/src/DynamoCore/Models/DynamoModel.cs +++ b/src/DynamoCore/Models/DynamoModel.cs @@ -702,10 +702,14 @@ protected DynamoModel(IStartConfiguration config) // Do nothing for now } } + //If network traffic is disabled, analytics should also be disabled - this is already done in + //our other entry points(CLI,Sandbox etc) - but + //not all integrators will use those entry points, some may just create a DynamoModel directly. + Analytics.DisableAnalytics = NoNetworkMode || Analytics.DisableAnalytics; // If user skipped analytics from assembly config, do not try to launch the analytics client // or the feature flags client for web traffic reason. - if (!IsServiceMode && !areAnalyticsDisabledFromConfig && !Analytics.DisableAnalytics) + if (!IsServiceMode && !areAnalyticsDisabledFromConfig && !Analytics.DisableAnalytics && !NoNetworkMode) { HandleAnalytics(); diff --git a/test/DynamoCoreTests/Logging/AnalyticsServiceTest.cs b/test/DynamoCoreTests/Logging/AnalyticsServiceTest.cs index 4430c3dbb5d..3d77c350e6e 100644 --- a/test/DynamoCoreTests/Logging/AnalyticsServiceTest.cs +++ b/test/DynamoCoreTests/Logging/AnalyticsServiceTest.cs @@ -80,5 +80,72 @@ public void DisableAnalytics() dynamoCLI?.Kill(); } } + + [Test] + [Platform("win")]//nunit attribute for now only run on windows until we know it's useful on linux. + public void DisableAnalyticsViaNoNetWorkMode() + { + var versions = new List(){ + + new Version(230, 0,0), + }; + + var directory = new DirectoryInfo(Assembly.GetExecutingAssembly().Location); + var testDirectory = Path.Combine(directory.Parent.Parent.Parent.FullName, "test"); + string openPath = Path.Combine(testDirectory, @"core\Angle.dyn"); + //go get a valid asm path. + var locatedPath = string.Empty; + var coreDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + Process dynamoCLI = null; + //TODO an approach we could take to get this running on linux. + //unclear if this needs to be compiled with an ifdef or runtime is ok. + //related to https://jira.autodesk.com/browse/DYN-5705 + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + DynamoShapeManager.Utilities.SearchForASMInLibGFallback(versions, ref locatedPath, coreDirectory, out _); + } + else + { + DynamoShapeManager.Utilities.GetInstalledAsmVersion2(versions, ref locatedPath, coreDirectory); + } + try + { + Assert.DoesNotThrow(() => + { + + dynamoCLI = Process.Start(new ProcessStartInfo(Path.Combine(coreDirectory, "DynamoCLI.exe"), $"--GeometryPath \"{locatedPath}\" -k --NoNetworkMode -o \"{openPath}\" ") { UseShellExecute = true }); + + Thread.Sleep(5000);// Wait 5 seconds to open the dyn + Assert.IsFalse(dynamoCLI.HasExited); + var dt = DataTarget.AttachToProcess(dynamoCLI.Id, false); + var assemblies = dt + .ClrVersions + .Select(dtClrVersion => dtClrVersion.CreateRuntime()) + .SelectMany(runtime => runtime.AppDomains.SelectMany(runtimeAppDomain => runtimeAppDomain.Modules)) + .Select(clrModule => clrModule.AssemblyName) + .Distinct() + .Where(x => x != null) + .ToList(); + + var firstASMmodulePath = string.Empty; + foreach (string module in assemblies) + { + if (module.IndexOf("Analytics", StringComparison.OrdinalIgnoreCase) != -1) + { + Assert.Fail("Analytics module was loaded"); + } + if (module.IndexOf("AdpSDKCSharpWrapper", StringComparison.OrdinalIgnoreCase) != -1) + { + Assert.Fail("ADP module was loaded"); + } + } + }); + } + finally + { + + dynamoCLI?.Kill(); + } + } } } diff --git a/test/System/IntegrationTests/DynamoApplicationTests.cs b/test/System/IntegrationTests/DynamoApplicationTests.cs index 4bb21094912..5d1353bb34e 100644 --- a/test/System/IntegrationTests/DynamoApplicationTests.cs +++ b/test/System/IntegrationTests/DynamoApplicationTests.cs @@ -3,8 +3,10 @@ using System.Diagnostics; using System.IO; using Dynamo.Applications; +using Dynamo.Logging; using Dynamo.Models; using NUnit.Framework; +using static Dynamo.Models.DynamoModel; namespace IntegrationTests { @@ -64,6 +66,23 @@ public void DynamoMakeModelWithHostName() var model = Dynamo.Applications.StartupUtils.MakeModel(false, string.Empty, "DynamoFormIt"); Assert.AreEqual(DynamoModel.HostAnalyticsInfo.HostName, "DynamoFormIt"); } + [Test] + public void DynamoModelStartedWithNoNetworkMode_AlsoDisablesAnalytics() + { + var startConfig = new DefaultStartConfiguration() { NoNetworkMode = true }; + var model = DynamoModel.Start(startConfig); + Assert.AreEqual(true, Analytics.DisableAnalytics); + model.ShutDown(false); + } + [Test] + public void DynamoModelStartedWithNoNetworkModeFalse_DisablesAnalyticsCanBeTrue() + { + var startConfig = new DefaultStartConfiguration() { NoNetworkMode = false }; + Analytics.DisableAnalytics = true; + var model = DynamoModel.Start(startConfig); + Assert.AreEqual(true, Analytics.DisableAnalytics); + model.ShutDown(false); + } [Test] public void IfASMPathInvalidExceptionNotThrown()