Ejemplo n.º 1
0
        private TestHostContext CreateTestContext([CallerMemberName] String testName = "")
        {
            var hc = new TestHostContext(this, testName);
            var expressionManager = new ExpressionManager();

            expressionManager.Initialize(hc);
            hc.SetSingleton <IExpressionManager>(expressionManager);
            Dictionary <string, VariableValue> variablesToCopy = new Dictionary <string, VariableValue>();

            _variables = new Variables(
                hostContext: hc,
                copy: variablesToCopy);
            _env = new Dictionary <string, string>()
            {
                { "env1", "1" },
                { "test", "github_actions" }
            };
            _ec = new Mock <IExecutionContext>();
            _ec.SetupAllProperties();
            _ec.Setup(x => x.Variables).Returns(_variables);

            _contexts           = new DictionaryContextData();
            _jobContext         = new JobContext();
            _contexts["github"] = new DictionaryContextData();
            _contexts["runner"] = new DictionaryContextData();
            _contexts["job"]    = _jobContext;
            _ec.Setup(x => x.ExpressionValues).Returns(_contexts);
            _ec.Setup(x => x.JobContext).Returns(_jobContext);

            _stepContext = new StepsContext();
            _ec.Setup(x => x.StepsContext).Returns(_stepContext);

            _ec.Setup(x => x.PostJobSteps).Returns(new Stack <IStep>());

            var trace = hc.GetTrace();

            _ec.Setup(x => x.Write(It.IsAny <string>(), It.IsAny <string>())).Callback((string tag, string message) => { trace.Info($"[{tag}]{message}"); });

            _stepsRunner = new StepsRunner();
            _stepsRunner.Initialize(hc);
            return(hc);
        }
Ejemplo n.º 2
0
        private TestHostContext CreateTestContext([CallerMemberName] String testName = "")
        {
            var hc = new TestHostContext(this, testName);
            var expressionManager = new ExpressionManager();

            expressionManager.Initialize(hc);
            hc.SetSingleton <IExpressionManager>(expressionManager);
            List <string> warnings;

            _variables = new Variables(
                hostContext: hc,
                copy: new Dictionary <string, VariableValue>(),
                warnings: out warnings);
            _ec = new Mock <IExecutionContext>();
            _ec.SetupAllProperties();
            _ec.Setup(x => x.Variables).Returns(_variables);
            _stepsRunner = new StepsRunner();
            _stepsRunner.Initialize(hc);
            return(hc);
        }
Ejemplo n.º 3
0
        public void Expression_CustomFunction_Test()
        {
            var sut = new ExpressionManager();

            sut.Initialize(new Dictionary <string, string>()
            {
                { "expr1", "CustomFunc1(F4)" }
            }, @"
Function CustomFunc1(dt)
    CustomFunc1 = Month(dt) + Day(dt) + Year(dt)
End Function
");

            var dt = GetTestDataTable();

            sut.EvaluateExpressions(dt);

            dt.Columns.Count.Should().Be(4 + 1);
            dt.Rows[0]["expr1"].Should().Be("2021");
        }
Ejemplo n.º 4
0
        public void Expression_Test_Corner_Cases()
        {
            var sut = new ExpressionManager();

            sut.Initialize(new Dictionary <string, string>()
            {
            });


            var dt = GetTestDataTable();

            sut.EvaluateExpressions(dt);

            dt.Columns.Count.Should().Be(4);

            var    sut2    = new ExpressionManager();
            Action action1 = () => { sut.Dispose(); };  // Initialized
            Action action2 = () => { sut2.Dispose(); }; // Not Initialized

            action1.ShouldNotThrow();
            action2.ShouldNotThrow();
        }
Ejemplo n.º 5
0
        public void Expression_Test()
        {
            var sut = new ExpressionManager();

            sut.Initialize(new Dictionary <string, string>()
            {
                { "expr1", "\"Hello\" + F1 + \"World\"" },
                { "expr2", "UCase(Left(F1, 3)) + LCase(Mid(F1, 4))" },
                { "expr3", "Sqr(F2)+Log(Exp(F3))" },
                { "expr4", "Month(F4)+Day(F4)+Year(F4)" }
            });

            var dt = GetTestDataTable();

            sut.EvaluateExpressions(dt);

            dt.Columns.Count.Should().Be(4 + 4);
            dt.Rows[0]["expr1"].Should().Be("HelloabcDEFGHWorld");
            dt.Rows[0]["expr2"].Should().Be("ABCdefgh");
            dt.Rows[0]["expr3"].Should().Be("14");
            dt.Rows[0]["expr4"].Should().Be("2021");
        }
Ejemplo n.º 6
0
        private TestHostContext CreateTestContext([CallerMemberName] String testName = "")
        {
            var hc = new TestHostContext(this, testName);
            var expressionManager = new ExpressionManager();

            expressionManager.Initialize(hc);
            hc.SetSingleton <IExpressionManager>(expressionManager);
            Dictionary <string, VariableValue> variablesToCopy = new Dictionary <string, VariableValue>();

            variablesToCopy.Add(Constants.Variables.Agent.RetainDefaultEncoding, new VariableValue("true", false));
            List <string> warnings;

            _variables = new Variables(
                hostContext: hc,
                copy: variablesToCopy,
                warnings: out warnings);
            _ec = new Mock <IExecutionContext>();
            _ec.SetupAllProperties();
            _ec.Setup(x => x.Variables).Returns(_variables);
            _stepsRunner = new StepsRunner();
            _stepsRunner.Initialize(hc);
            return(hc);
        }
Ejemplo n.º 7
0
        private TestHostContext CreateTestContext([CallerMemberName] String testName = "")
        {
            var hc = new TestHostContext(this, testName);

            _jobEc                = new Agent.Worker.ExecutionContext();
            _config               = new Mock <IConfigurationStore>();
            _extensions           = new Mock <IExtensionManager>();
            _jobExtension         = new Mock <IJobExtension>();
            _jobServer            = new Mock <IJobServer>();
            _jobServerQueue       = new Mock <IJobServerQueue>();
            _proxyConfig          = new Mock <IVstsAgentWebProxy>();
            _cert                 = new Mock <IAgentCertificateManager>();
            _taskServer           = new Mock <ITaskServer>();
            _stepRunner           = new Mock <IStepsRunner>();
            _logger               = new Mock <IPagingLogger>();
            _temp                 = new Mock <ITempDirectoryManager>();
            _diagnosticLogManager = new Mock <IDiagnosticLogManager>();

            if (_tokenSource != null)
            {
                _tokenSource.Dispose();
                _tokenSource = null;
            }

            _tokenSource = new CancellationTokenSource();
            var expressionManager = new ExpressionManager();

            expressionManager.Initialize(hc);
            hc.SetSingleton <IExpressionManager>(expressionManager);

            _jobRunner = new JobRunner();
            _jobRunner.Initialize(hc);

            TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference();
            TimelineReference timeline          = new Timeline(Guid.NewGuid());
            JobEnvironment    environment       = new JobEnvironment();

            environment.Variables[Constants.Variables.System.Culture] = "en-US";
            environment.SystemConnection = new ServiceEndpoint()
            {
                Url           = new Uri("https://test.visualstudio.com"),
                Authorization = new EndpointAuthorization()
                {
                    Scheme = "Test",
                }
            };
            environment.SystemConnection.Authorization.Parameters["AccessToken"] = "token";

            List <TaskInstance> tasks = new List <TaskInstance>();
            Guid JobId = Guid.NewGuid();

            _message = new AgentJobRequestMessage(plan, timeline, JobId, testName, testName, environment, tasks);

            _extensions.Setup(x => x.GetExtensions <IJobExtension>()).
            Returns(new[] { _jobExtension.Object }.ToList());

            _initResult.PreJobSteps.Clear();
            _initResult.JobSteps.Clear();
            _initResult.PostJobStep.Clear();

            _jobExtension.Setup(x => x.InitializeJob(It.IsAny <IExecutionContext>(), It.IsAny <AgentJobRequestMessage>())).
            Returns(Task.FromResult(_initResult));
            _jobExtension.Setup(x => x.HostType)
            .Returns <string>(null);

            _proxyConfig.Setup(x => x.ProxyAddress)
            .Returns(string.Empty);

            var settings = new AgentSettings
            {
                AgentId    = 1,
                AgentName  = "agent1",
                ServerUrl  = "https://test.visualstudio.com",
                WorkFolder = "_work",
            };

            _config.Setup(x => x.GetSettings())
            .Returns(settings);

            _logger.Setup(x => x.Setup(It.IsAny <Guid>(), It.IsAny <Guid>()));

            hc.SetSingleton(_config.Object);
            hc.SetSingleton(_jobServer.Object);
            hc.SetSingleton(_jobServerQueue.Object);
            hc.SetSingleton(_proxyConfig.Object);
            hc.SetSingleton(_cert.Object);
            hc.SetSingleton(_taskServer.Object);
            hc.SetSingleton(_stepRunner.Object);
            hc.SetSingleton(_extensions.Object);
            hc.SetSingleton(_temp.Object);
            hc.SetSingleton(_diagnosticLogManager.Object);
            hc.EnqueueInstance <IExecutionContext>(_jobEc);
            hc.EnqueueInstance <IPagingLogger>(_logger.Object);
            return(hc);
        }
Ejemplo n.º 8
0
        private TestHostContext CreateTestContext([CallerMemberName] String testName = "")
        {
            var hc = new TestHostContext(this, testName);

            _jobEc                = new Runner.Worker.ExecutionContext();
            _config               = new Mock <IConfigurationStore>();
            _extensions           = new Mock <IExtensionManager>();
            _jobExtension         = new Mock <IJobExtension>();
            _jobServer            = new Mock <IJobServer>();
            _jobServerQueue       = new Mock <IJobServerQueue>();
            _stepRunner           = new Mock <IStepsRunner>();
            _logger               = new Mock <IPagingLogger>();
            _temp                 = new Mock <ITempDirectoryManager>();
            _diagnosticLogManager = new Mock <IDiagnosticLogManager>();

            if (_tokenSource != null)
            {
                _tokenSource.Dispose();
                _tokenSource = null;
            }

            _tokenSource = new CancellationTokenSource();
            var expressionManager = new ExpressionManager();

            expressionManager.Initialize(hc);
            hc.SetSingleton <IExpressionManager>(expressionManager);

            _jobRunner = new JobRunner();
            _jobRunner.Initialize(hc);

            TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference();
            TimelineReference timeline          = new Timeline(Guid.NewGuid());
            Guid jobId = Guid.NewGuid();

            _message = new Pipelines.AgentJobRequestMessage(plan, timeline, jobId, testName, testName, null, null, null, new Dictionary <string, VariableValue>(), new List <MaskHint>(), new Pipelines.JobResources(), new Pipelines.ContextData.DictionaryContextData(), new Pipelines.WorkspaceOptions(), new List <Pipelines.ActionStep>(), null, null);
            _message.Variables[Constants.Variables.System.Culture] = "en-US";
            _message.Resources.Endpoints.Add(new ServiceEndpoint()
            {
                Name          = WellKnownServiceEndpointNames.SystemVssConnection,
                Url           = new Uri("https://pipelines.actions.githubusercontent.com"),
                Authorization = new EndpointAuthorization()
                {
                    Scheme     = "Test",
                    Parameters =
                    {
                        { "AccessToken", "token" }
                    }
                },
            });

            _message.Resources.Repositories.Add(new Pipelines.RepositoryResource()
            {
                Alias   = Pipelines.PipelineConstants.SelfAlias,
                Id      = "github",
                Version = "sha1"
            });
            _message.ContextData.Add("github", new Pipelines.ContextData.DictionaryContextData());

            _initResult.Clear();

            _jobExtension.Setup(x => x.InitializeJob(It.IsAny <IExecutionContext>(), It.IsAny <Pipelines.AgentJobRequestMessage>())).
            Returns(Task.FromResult(_initResult));

            var settings = new RunnerSettings
            {
                AgentId    = 1,
                AgentName  = "agent1",
                ServerUrl  = "https://pipelines.actions.githubusercontent.com",
                WorkFolder = "_work",
            };

            _config.Setup(x => x.GetSettings())
            .Returns(settings);

            _logger.Setup(x => x.Setup(It.IsAny <Guid>(), It.IsAny <Guid>()));

            hc.SetSingleton(_config.Object);
            hc.SetSingleton(_jobServer.Object);
            hc.SetSingleton(_jobServerQueue.Object);
            hc.SetSingleton(_stepRunner.Object);
            hc.SetSingleton(_extensions.Object);
            hc.SetSingleton(_temp.Object);
            hc.SetSingleton(_diagnosticLogManager.Object);
            hc.EnqueueInstance <IExecutionContext>(_jobEc);
            hc.EnqueueInstance <IPagingLogger>(_logger.Object);
            hc.EnqueueInstance <IJobExtension>(_jobExtension.Object);
            return(hc);
        }