Пример #1
0
        public async Task CanEvaluateConstraint()
        {
            var engineEnvironmentSettings = _environmentSettingsHelper.CreateEnvironment(virtualize: true);

            engineEnvironmentSettings.Components.AddComponent(typeof(ITemplateConstraintFactory), new TestConstraintFactory("test-1"));
            engineEnvironmentSettings.Components.AddComponent(typeof(ITemplateConstraintFactory), new TestConstraintFactory("test-2"));

            var constraintManager = new TemplateConstraintManager(engineEnvironmentSettings);

            var success1 = await constraintManager.EvaluateConstraintAsync("test-1", "yes", default).ConfigureAwait(false);

            var failure1 = await constraintManager.EvaluateConstraintAsync("test-1", "no", default).ConfigureAwait(false);

            var notEvaluated1 = await constraintManager.EvaluateConstraintAsync("test-1", "not-valid", default).ConfigureAwait(false);

            var success2 = await constraintManager.EvaluateConstraintAsync("test-2", "yes", default).ConfigureAwait(false);

            Assert.Equal(TemplateConstraintResult.Status.Allowed, success1.EvaluationStatus);
            Assert.Null(success1.LocalizedErrorMessage);
            Assert.Null(success1.CallToAction);

            Assert.Equal(TemplateConstraintResult.Status.Restricted, failure1.EvaluationStatus);
            Assert.Equal("cannot run", failure1.LocalizedErrorMessage);
            Assert.Equal("do smth", failure1.CallToAction);

            Assert.Equal(TemplateConstraintResult.Status.NotEvaluated, notEvaluated1.EvaluationStatus);
            Assert.Equal("bad params", notEvaluated1.LocalizedErrorMessage);
            Assert.Null(notEvaluated1.CallToAction);

            Assert.Equal(TemplateConstraintResult.Status.Allowed, success2.EvaluationStatus);
            Assert.Null(success2.LocalizedErrorMessage);
            Assert.Null(success2.CallToAction);
        }
Пример #2
0
        public async Task CanReadArrayConfiguration()
        {
            var config = new
            {
                identity    = "test",
                constraints = new
                {
                    winOnly = new
                    {
                        type = "os",
                        args = new[] { "Windows", "Linux" }
                    }
                }
            };

            var configModel       = SimpleConfigModel.FromJObject(JObject.FromObject(config));
            var constraintManager = new TemplateConstraintManager(_sharedSettings);
            var evaluateResult    = await constraintManager.EvaluateConstraintAsync(configModel.Constraints.Single().Type, configModel.Constraints.Single().Args, default).ConfigureAwait(false);

            var pass = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux);

            Assert.Equal(pass, evaluateResult.EvaluationStatus == TemplateConstraintResult.Status.Allowed);

            if (!pass)
            {
                Assert.Equal($"Running template on {RuntimeInformation.OSDescription} is not supported, supported OS is/are: {OSPlatform.Windows}, {OSPlatform.Linux}.", evaluateResult.LocalizedErrorMessage);
            }
            else
            {
                Assert.Null(evaluateResult.LocalizedErrorMessage);
            }
            Assert.Null(evaluateResult.CallToAction);
        }
Пример #3
0
        public async Task FailsOnWrongConfiguration()
        {
            var config = new
            {
                identity    = "test",
                constraints = new
                {
                    host = new
                    {
                        type = "host",
                        args =
                            new
                        {
                            hostName = "host2",
                            version  = "1.0.0"
                        }
                    }
                }
            };

            var configModel = SimpleConfigModel.FromJObject(JObject.FromObject(config));

            IEngineEnvironmentSettings settings = A.Fake <IEngineEnvironmentSettings>();

            A.CallTo(() => settings.Host.HostIdentifier).Returns("host3");
            A.CallTo(() => settings.Host.Version).Returns("2.0.0");
            A.CallTo(() => settings.Components.OfType <ITemplateConstraintFactory>()).Returns(new[] { new HostConstraintFactory() });

            var constraintManager = new TemplateConstraintManager(settings);
            var evaluateResult    = await constraintManager.EvaluateConstraintAsync(configModel.Constraints.Single().Type, configModel.Constraints.Single().Args, default).ConfigureAwait(false);

            Assert.Equal(TemplateConstraintResult.Status.NotEvaluated, evaluateResult.EvaluationStatus);
            Assert.Equal("'{\"hostName\":\"host2\",\"version\":\"1.0.0\"}' is not a valid JSON array.", evaluateResult.LocalizedErrorMessage);
            Assert.Equal("Check the constraint configuration in template.json.", evaluateResult.CallToAction);
        }
Пример #4
0
        public async Task Evaluate_ArrayOfVersions(IReadOnlyList <string> workloads, bool allowed)
        {
            var config = new
            {
                identity    = "test-constraint-01",
                constraints = new
                {
                    specVersions = new
                    {
                        type = "workload",
                        args = new[] { "workloadA", "workloadB" }
                    }
                }
            };

            var configModel = SimpleConfigModel.FromJObject(JObject.FromObject(config));
            IWorkloadsInfoProvider     workloadInfoProvider = new WorkloadsInfoProviderMock(workloads); //A.Fake<IWorkloadsInfoProvider>();
            IEngineEnvironmentSettings settings             = A.Fake <IEngineEnvironmentSettings>();

            A.CallTo(() => settings.Components.OfType <IWorkloadsInfoProvider>()).Returns(new[] { workloadInfoProvider });
            A.CallTo(() => settings.Components.OfType <ITemplateConstraintFactory>()).Returns(new[] { new WorkloadConstraintFactory() });

            var constraintManager = new TemplateConstraintManager(settings);

            //A.CallTo(() => workloadInfoProvider
            //    .GetInstalledWorkloadsAsync(A<CancellationToken>._))
            //    .Returns(Task.FromResult(workloads.Select(s => new WorkloadInfo(s, $"D:{s}"))));

            var evaluateResult = await constraintManager.EvaluateConstraintAsync(configModel.Constraints.Single().Type, configModel.Constraints.Single().Args, default).ConfigureAwait(false);

            Assert.Equal(allowed ? TemplateConstraintResult.Status.Allowed : TemplateConstraintResult.Status.Restricted, evaluateResult.EvaluationStatus);
        }
        public void Evaluate_SingleVersionRange(string sdkVersion, bool allowed)
        {
            var config = new
            {
                identity    = "test-constraint-01",
                constraints = new
                {
                    specVersions = new
                    {
                        type = "sdk-version",
                        args = "(1.2.3-*, 4.5]"
                    }
                }
            };

            var configModel = SimpleConfigModel.FromJObject(JObject.FromObject(config));
            ISdkInfoProvider           sdkInfoProvider = new SdkInfoProviderMock(sdkVersion); //A.Fake<ISdkInfoProvider>();
            IEngineEnvironmentSettings settings        = A.Fake <IEngineEnvironmentSettings>();

            A.CallTo(() => settings.Components.OfType <ISdkInfoProvider>()).Returns(new[] { sdkInfoProvider });
            A.CallTo(() => settings.Components.OfType <ITemplateConstraintFactory>()).Returns(new[] { new SdkVersionConstraintFactory() });

            var constraintManager = new TemplateConstraintManager(settings);

            //Workaround needed
            //A.CallTo(() => sdkInfoProvider.GetVersionAsync(A<CancellationToken>._)).Returns(t);

            var evaluateResult = constraintManager.EvaluateConstraintAsync(configModel.Constraints.Single().Type, configModel.Constraints.Single().Args, default).Result;

            Assert.Equal(allowed ? TemplateConstraintResult.Status.Allowed : TemplateConstraintResult.Status.Restricted, evaluateResult.EvaluationStatus);
        }
        public async Task Evaluate_AlternativeInstalledVersions(string sdkVersion, IReadOnlyList <string> installedVersions, bool hasAlternativeInstalled)
        {
            var config = new
            {
                identity    = "test-constraint-01",
                constraints = new
                {
                    specVersions = new
                    {
                        type = "sdk-version",
                        args = new[] { "1.2.3-*", "4.5.*" }
                    }
                }
            };

            var configModel = SimpleConfigModel.FromJObject(JObject.FromObject(config));
            ISdkInfoProvider           sdkInfoProvider = new SdkInfoProviderMock(sdkVersion, installedVersions); //A.Fake<ISdkInfoProvider>();
            IEngineEnvironmentSettings settings        = A.Fake <IEngineEnvironmentSettings>();

            A.CallTo(() => settings.Components.OfType <ISdkInfoProvider>()).Returns(new[] { sdkInfoProvider });
            A.CallTo(() => settings.Components.OfType <ITemplateConstraintFactory>()).Returns(new[] { new SdkVersionConstraintFactory() });

            var constraintManager = new TemplateConstraintManager(settings);

            var evaluateResult = await constraintManager.EvaluateConstraintAsync(configModel.Constraints.Single().Type, configModel.Constraints.Single().Args, default).ConfigureAwait(false);

            Assert.Equal(TemplateConstraintResult.Status.Restricted, evaluateResult.EvaluationStatus);
            Assert.StartsWith(
                hasAlternativeInstalled
                    ? "Sample CTA with alternatives"
                    : "Sample CTA without alternatives",
                evaluateResult.CallToAction);
        }
Пример #7
0
        public async Task CanEvaluateConstraint_WhenOtherCreationFailed()
        {
            var engineEnvironmentSettings = _environmentSettingsHelper.CreateEnvironment(virtualize: true);

            engineEnvironmentSettings.Components.AddComponent(typeof(ITemplateConstraintFactory), new FailingTestConstraintFactory("test-1"));
            engineEnvironmentSettings.Components.AddComponent(typeof(ITemplateConstraintFactory), new TestConstraintFactory("test-2"));

            var constraintManager = new TemplateConstraintManager(engineEnvironmentSettings);

            var result1 = await constraintManager.EvaluateConstraintAsync("test-1", "yes", default);

            Assert.Equal(TemplateConstraintResult.Status.NotEvaluated, result1.EvaluationStatus);
            Assert.Equal("The constraint 'test-1' failed to initialize: creation failed", result1.LocalizedErrorMessage);
            Assert.Null(result1.CallToAction);

            var success2 = await constraintManager.EvaluateConstraintAsync("test-2", "yes", default).ConfigureAwait(false);

            Assert.Equal(TemplateConstraintResult.Status.Allowed, success2.EvaluationStatus);
            Assert.Null(success2.LocalizedErrorMessage);
            Assert.Null(success2.CallToAction);
        }
Пример #8
0
        public async Task CanProcessDifferentHostNames(string hostName, string fallbackHostNames, string hostVersion, bool expectedResult)
        {
            var config = new
            {
                identity    = "test",
                constraints = new
                {
                    host = new
                    {
                        type = "host",
                        args = new[]
                        {
                            new
                            {
                                hostName = "host1",
                                version  = "[1.0, 2.0]"
                            },
                            new
                            {
                                hostName = "fallback",
                                version  = "[2.0, 3.0]"
                            },
                        }
                    }
                }
            };

            var configModel = SimpleConfigModel.FromJObject(JObject.FromObject(config));
            IEngineEnvironmentSettings settings = A.Fake <IEngineEnvironmentSettings>();

            A.CallTo(() => settings.Host.HostIdentifier).Returns(hostName);
            A.CallTo(() => settings.Host.Version).Returns(hostVersion);
            A.CallTo(() => settings.Host.FallbackHostTemplateConfigNames).Returns(fallbackHostNames.Split('|'));
            A.CallTo(() => settings.Components.OfType <ITemplateConstraintFactory>()).Returns(new[] { new HostConstraintFactory() });

            var constraintManager = new TemplateConstraintManager(settings);
            var evaluateResult    = await constraintManager.EvaluateConstraintAsync(configModel.Constraints.Single().Type, configModel.Constraints.Single().Args, default).ConfigureAwait(false);

            if (expectedResult)
            {
                Assert.Equal(TemplateConstraintResult.Status.Allowed, evaluateResult.EvaluationStatus);
            }
            else
            {
                Assert.Equal(TemplateConstraintResult.Status.Restricted, evaluateResult.EvaluationStatus);
            }
        }
Пример #9
0
        public async Task CanReadConfiguration_ExactVersion()
        {
            var config = new
            {
                identity    = "test",
                constraints = new
                {
                    host = new
                    {
                        type = "host",
                        args = new[]
                        {
                            new
                            {
                                hostName = "host1",
                                version  = ""
                            },
                            new
                            {
                                hostName = "host2",
                                version  = "1.0.0"
                            },
                            new
                            {
                                hostName = "host3",
                                version  = "[1.0.0-*]"
                            },
                        }
                    }
                }
            };

            var configModel = SimpleConfigModel.FromJObject(JObject.FromObject(config));
            IEngineEnvironmentSettings settings = A.Fake <IEngineEnvironmentSettings>();

            A.CallTo(() => settings.Host.HostIdentifier).Returns("host2");
            A.CallTo(() => settings.Host.Version).Returns("2.0.0");
            A.CallTo(() => settings.Components.OfType <ITemplateConstraintFactory>()).Returns(new[] { new HostConstraintFactory() });

            var constraintManager = new TemplateConstraintManager(settings);
            var evaluateResult    = await constraintManager.EvaluateConstraintAsync(configModel.Constraints.Single().Type, configModel.Constraints.Single().Args, default).ConfigureAwait(false);

            Assert.Equal(TemplateConstraintResult.Status.Restricted, evaluateResult.EvaluationStatus);
            Assert.Equal("Running template on host2 (version: 2.0.0) is not supported, supported hosts is/are: host1, host2(1.0.0), host3([1.0.0-*]).", evaluateResult.LocalizedErrorMessage);
            Assert.Null(evaluateResult.CallToAction);
        }
Пример #10
0
        public async Task CanReadConfiguration_VersionRange()
        {
            var config = new
            {
                identity    = "test",
                constraints = new
                {
                    host = new
                    {
                        type = "host",
                        args = new[]
                        {
                            new
                            {
                                hostName = "host1",
                                version  = ""
                            },
                            new
                            {
                                hostName = "host2",
                                version  = "1.0.0"
                            },
                            new
                            {
                                hostName = "host3",
                                version  = "[1.0.0-*]"
                            },
                        }
                    }
                }
            };

            var configModel = SimpleConfigModel.FromJObject(JObject.FromObject(config));

            IEngineEnvironmentSettings settings = A.Fake <IEngineEnvironmentSettings>();

            A.CallTo(() => settings.Host.HostIdentifier).Returns("host3");
            A.CallTo(() => settings.Host.Version).Returns("2.0.0");
            A.CallTo(() => settings.Components.OfType <ITemplateConstraintFactory>()).Returns(new[] { new HostConstraintFactory() });

            var constraintManager = new TemplateConstraintManager(settings);
            var evaluateResult    = await constraintManager.EvaluateConstraintAsync(configModel.Constraints.Single().Type, configModel.Constraints.Single().Args, default).ConfigureAwait(false);

            Assert.Equal(TemplateConstraintResult.Status.Allowed, evaluateResult.EvaluationStatus);
        }
Пример #11
0
        public async Task Evaluate_MultipleConflictingProviders()
        {
            var config = new
            {
                identity    = "test-constraint-01",
                constraints = new
                {
                    specVersions = new
                    {
                        type = "workload",
                        args = new[] { "workloadA", "workloadB" }
                    }
                }
            };

            var configModel = SimpleConfigModel.FromJObject(JObject.FromObject(config));
            IWorkloadsInfoProvider workloadInfoProviderA = A.Fake <IWorkloadsInfoProvider>();

            A.CallTo(() => workloadInfoProviderA
                     .GetInstalledWorkloadsAsync(A <CancellationToken> ._))
            .Returns(Task.FromResult(new[] { "workload", "workloadA" }.Select(s => new WorkloadInfo(s, $"D:{s}"))));

            IWorkloadsInfoProvider workloadInfoProviderB = A.Fake <IWorkloadsInfoProvider>();

            A.CallTo(() => workloadInfoProviderB
                     .GetInstalledWorkloadsAsync(A <CancellationToken> ._))
            .Returns(Task.FromResult(new[] { "workload", "workloadA", "workloadB" }.Select(s => new WorkloadInfo(s, $"D:{s}"))));

            IEngineEnvironmentSettings settings = A.Fake <IEngineEnvironmentSettings>();

            A.CallTo(() => settings.Components.OfType <IWorkloadsInfoProvider>()).Returns(new[] { workloadInfoProviderA, workloadInfoProviderB });
            A.CallTo(() => settings.Components.OfType <ITemplateConstraintFactory>()).Returns(new[] { new WorkloadConstraintFactory() });
            List <(LogLevel, string)> messagesCollection = new();
            ILogger logger = new InMemoryLoggerProvider(messagesCollection).CreateLogger("x");

            A.CallTo(() => settings.Host.Logger).Returns(logger);

            var constraintManager = new TemplateConstraintManager(settings);

            var evaluateResult = await constraintManager.EvaluateConstraintAsync(configModel.Constraints.Single().Type, configModel.Constraints.Single().Args, default).ConfigureAwait(false);

            Assert.Equal(TemplateConstraintResult.Status.NotEvaluated, evaluateResult.EvaluationStatus);
            Assert.Equal(0, messagesCollection.Count(t => t.Item1 >= LogLevel.Warning));
            Assert.StartsWith("The constraint 'workload' failed to initialize", evaluateResult.LocalizedErrorMessage);
        }