Beispiel #1
0
        public void CustomHandlerConfig_ExpandEnvVars()
        {
            string hostJsonContent = @"{
                    'version': '2.0',
                    'customHandler': {
                            'description': {
                                'defaultExecutablePath': '%TestEnv%',
                                'defaultWorkerPath': '%TestEnv%'
                            }
                        }
                    }";

            try
            {
                Environment.SetEnvironmentVariable("TestEnv", "TestVal");
                File.WriteAllText(_hostJsonFile, hostJsonContent);
                var configuration              = BuildHostJsonConfiguration();
                HttpWorkerOptionsSetup setup   = new HttpWorkerOptionsSetup(new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions), configuration, _testLoggerFactory, _metricsLogger);
                HttpWorkerOptions      options = new HttpWorkerOptions();
                setup.Configure(options);
                Assert.Equal("TestVal", options.Description.DefaultExecutablePath);
                Assert.Contains("TestVal", options.Description.DefaultWorkerPath);
            }
            finally
            {
                Environment.SetEnvironmentVariable("TestEnv", string.Empty);
            }
        }
Beispiel #2
0
        public void GetUnusedTcpPort_Succeeds()
        {
            int         unusedPort  = HttpWorkerOptionsSetup.GetUnusedTcpPort();
            TcpListener tcpListener = null;

            try
            {
                tcpListener = new TcpListener(IPAddress.Loopback, unusedPort);
                tcpListener.Start();
            }
            finally
            {
                tcpListener?.Stop();
            }
        }
Beispiel #3
0
        public void MissingOrValid_HttpInvokerConfig_DoesNotThrowException(string hostJsonContent)
        {
            File.WriteAllText(_hostJsonFile, hostJsonContent);
            var configuration              = BuildHostJsonConfiguration();
            HttpWorkerOptionsSetup setup   = new HttpWorkerOptionsSetup(configuration, _testLoggerFactory);
            HttpWorkerOptions      options = new HttpWorkerOptions();
            var ex = Record.Exception(() => setup.Configure(options));

            Assert.Null(ex);
            if (options.Description != null && !string.IsNullOrEmpty(options.Description.DefaultExecutablePath))
            {
                string expectedDefaultExecutablePath = Path.Combine(Directory.GetCurrentDirectory(), "testExe");
                Assert.Equal(expectedDefaultExecutablePath, options.Description.DefaultExecutablePath);
            }
        }
Beispiel #4
0
        public void CustomHandler_Config_ExpectedValues_WorkerDirectory_WorkingDirectory(string hostJsonContent, bool appendCurrentDirToDefaultExe, bool appendCurrentDirToWorkingDir, bool appendCurrentDirToWorkerDir)
        {
            File.WriteAllText(_hostJsonFile, hostJsonContent);
            var configuration              = BuildHostJsonConfiguration();
            HttpWorkerOptionsSetup setup   = new HttpWorkerOptionsSetup(new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions), configuration, _testLoggerFactory, _metricsLogger);
            HttpWorkerOptions      options = new HttpWorkerOptions();

            options.Description            = new HttpWorkerDescription();
            options.Description.FileExists = path =>
            {
                return(appendCurrentDirToDefaultExe);
            };
            setup.Configure(options);

            Assert.True(_metricsLogger.LoggedEvents.Contains(MetricEventNames.CustomHandlerConfiguration));

            //Verify worker exe path is expected
            if (appendCurrentDirToDefaultExe)
            {
                Assert.Equal(Path.Combine(_scriptJobHostOptions.RootScriptPath, "myWorkerDir", "node"), options.Description.DefaultExecutablePath);
            }
            else
            {
                Assert.Equal("node", options.Description.DefaultExecutablePath);
            }

            // Verify worker dir is expected
            if (appendCurrentDirToWorkerDir)
            {
                Assert.Equal(Path.Combine(_scriptJobHostOptions.RootScriptPath, "myWorkerDir"), options.Description.WorkerDirectory);
            }
            else
            {
                Assert.Equal(@"c:/myWorkerDir", options.Description.WorkerDirectory);
            }

            //Verify workering Dir is expected
            if (appendCurrentDirToWorkingDir)
            {
                Assert.Equal(Path.Combine(_scriptJobHostOptions.RootScriptPath, "myWorkingDir"), options.Description.WorkingDirectory);
            }
            else
            {
                Assert.Equal(@"c:/myWorkingDir", options.Description.WorkingDirectory);
            }
        }
Beispiel #5
0
        public void InValid_HttpInvokerConfig_Throws_HostConfigurationException()
        {
            string hostJsonContent = @"{
                    'version': '2.0',
                    'httpInvoker': {
                            'invalid': {
                                'defaultExecutablePath': 'testExe'
                            }
                        }
                    }";

            File.WriteAllText(_hostJsonFile, hostJsonContent);
            var configuration              = BuildHostJsonConfiguration();
            HttpWorkerOptionsSetup setup   = new HttpWorkerOptionsSetup(configuration, _testLoggerFactory);
            HttpWorkerOptions      options = new HttpWorkerOptions();
            var ex = Assert.Throws <HostConfigurationException>(() => setup.Configure(options));

            Assert.Contains("Missing WorkerDescription for HttpInvoker", ex.Message);
        }
        public void InValid_HttpWorkerConfig_Throws_ValidationException()
        {
            string hostJsonContent = @"{
                    'version': '2.0',
                    'httpWorker': {
                            'description': {
                                'langauge': 'testExe'
                            }
                        }
                    }";

            File.WriteAllText(_hostJsonFile, hostJsonContent);
            var configuration              = BuildHostJsonConfiguration();
            HttpWorkerOptionsSetup setup   = new HttpWorkerOptionsSetup(new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions), configuration, _testLoggerFactory);
            HttpWorkerOptions      options = new HttpWorkerOptions();
            var ex = Assert.Throws <ValidationException>(() => setup.Configure(options));

            Assert.Contains("WorkerDescription DefaultExecutablePath cannot be empty", ex.Message);
        }
Beispiel #7
0
        public void HttpWorkerConfig_OverrideConfigViaEnvVars_Test()
        {
            string hostJsonContent = @"{
                    'version': '2.0',
                    'httpWorker': {
                            'description': {
                                'langauge': 'testExe',
                                'defaultExecutablePath': 'dotnet',
                                'defaultWorkerPath':'ManualTrigger/run.csx',
                                'arguments': ['--xTest1 --xTest2'],
                                'workerArguments': ['--xTest3 --xTest4']
                            }
                        }
                    }";

            try
            {
                File.WriteAllText(_hostJsonFile, hostJsonContent);
                Environment.SetEnvironmentVariable("AzureFunctionsJobHost:httpWorker:description:defaultWorkerPath", "OneSecondTimer/run.csx");
                Environment.SetEnvironmentVariable("AzureFunctionsJobHost:httpWorker:description:arguments", "[\"--xTest5\", \"--xTest6\", \"--xTest7\"]");
                var configuration              = BuildHostJsonConfiguration();
                HttpWorkerOptionsSetup setup   = new HttpWorkerOptionsSetup(new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions), configuration, _testLoggerFactory, _metricsLogger);
                HttpWorkerOptions      options = new HttpWorkerOptions();
                setup.Configure(options);
                Assert.Equal("dotnet", options.Description.DefaultExecutablePath);
                // Verify options are overridden
                Assert.Contains("OneSecondTimer/run.csx", options.Description.DefaultWorkerPath);
                Assert.Equal(3, options.Description.Arguments.Count);
                Assert.Contains("--xTest5", options.Description.Arguments);
                Assert.Contains("--xTest6", options.Description.Arguments);
                Assert.Contains("--xTest7", options.Description.Arguments);

                // Verify options not overridden
                Assert.Equal(1, options.Description.WorkerArguments.Count);
                Assert.Equal("--xTest3 --xTest4", options.Description.WorkerArguments.ElementAt(0));
            }
            finally
            {
                Environment.SetEnvironmentVariable("AzureFunctionsJobHost:httpWorker:description:defaultWorkerPath", string.Empty);
                Environment.SetEnvironmentVariable("AzureFunctionsJobHost:httpWorker:description:arguments", string.Empty);
            }
        }
Beispiel #8
0
        public void MissingOrValid_HttpWorkerConfig_DoesNotThrowException(string hostJsonContent)
        {
            File.WriteAllText(_hostJsonFile, hostJsonContent);
            var configuration              = BuildHostJsonConfiguration();
            HttpWorkerOptionsSetup setup   = new HttpWorkerOptionsSetup(new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions), configuration, _testLoggerFactory, _metricsLogger);
            HttpWorkerOptions      options = new HttpWorkerOptions();

            options.Description            = new HttpWorkerDescription();
            options.Description.FileExists = path =>
            {
                return(true);
            };
            setup.Configure(options);

            if (options.Description != null && !string.IsNullOrEmpty(options.Description.DefaultExecutablePath))
            {
                string expectedDefaultExecutablePath = Path.Combine(_scriptJobHostOptions.RootScriptPath, "testExe");
                Assert.Equal(expectedDefaultExecutablePath, options.Description.DefaultExecutablePath);
            }
        }
Beispiel #9
0
        public void InValid_HttpWorkerConfig_Throws_Exception(string hostJsonContent)
        {
            File.WriteAllText(_hostJsonFile, hostJsonContent);
            var configuration              = BuildHostJsonConfiguration();
            HttpWorkerOptionsSetup setup   = new HttpWorkerOptionsSetup(new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions), configuration, _testLoggerFactory, _metricsLogger);
            HttpWorkerOptions      options = new HttpWorkerOptions();
            var ex = Record.Exception(() => setup.Configure(options));

            Assert.NotNull(ex);
            if (options.Description == null)
            {
                Assert.IsType <HostConfigurationException>(ex);
                Assert.Equal($"Missing worker Description.", ex.Message);
            }
            else
            {
                Assert.IsType <ValidationException>(ex);
                Assert.Equal($"WorkerDescription DefaultExecutablePath cannot be empty", ex.Message);
            }
        }
Beispiel #10
0
        public void HttpWorker_Config_ExpectedValues(string hostJsonContent, bool appendCurrentDirectoryToExe, bool appendCurrentDirToWorkerPath, bool workerPathSet)
        {
            File.WriteAllText(_hostJsonFile, hostJsonContent);
            var configuration              = BuildHostJsonConfiguration();
            HttpWorkerOptionsSetup setup   = new HttpWorkerOptionsSetup(new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions), configuration, _testLoggerFactory, _metricsLogger);
            HttpWorkerOptions      options = new HttpWorkerOptions();

            setup.Configure(options);

            //Verify worker exe path is expected
            if (appendCurrentDirectoryToExe)
            {
                Assert.Equal(Path.Combine(_scriptJobHostOptions.RootScriptPath, "node"), options.Description.DefaultExecutablePath);
            }
            else if (Path.IsPathRooted(options.Description.DefaultExecutablePath))
            {
                Assert.Equal(@"c:/myruntime/node", options.Description.DefaultExecutablePath);
            }
            else
            {
                Assert.Equal("node", options.Description.DefaultExecutablePath);
            }

            //Verify worker path is expected
            if (appendCurrentDirToWorkerPath)
            {
                Assert.Equal("httpWorker.js", options.Description.DefaultWorkerPath);
            }
            else if (!workerPathSet)
            {
                Assert.Null(options.Description.DefaultWorkerPath);
            }
            else
            {
                Assert.Equal(@"c:/myworkerPath/httpWorker.js", options.Description.DefaultWorkerPath);
            }

            Assert.Equal(1, options.Description.Arguments.Count);
            Assert.Equal("--xTest1 --xTest2", options.Description.Arguments[0]);
        }