Skip to content

Commit

Permalink
Fixing ManualTrigger input (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewc committed Aug 23, 2016
1 parent 6c1f574 commit ac763b1
Show file tree
Hide file tree
Showing 23 changed files with 243 additions and 30 deletions.
3 changes: 3 additions & 0 deletions src/WebJobs.Script/Binding/FunctionBinding.cs
Expand Up @@ -57,6 +57,9 @@ internal static Collection<FunctionBinding> GetBindings(ScriptHostConfiguration
}
bindings.Add(new HttpBinding(config, bindingMetadata, FileAccess.Write));
break;
case "manualtrigger":
bindings.Add(new ManualBinding(config, bindingMetadata, FileAccess.Read));
break;
default:
FunctionBinding binding = null;
if (TryParseFunctionBinding(config, bindingMetadata.Raw, out binding))
Expand Down
29 changes: 29 additions & 0 deletions src/WebJobs.Script/Binding/ManualBinding.cs
@@ -0,0 +1,29 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Reflection.Emit;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Script.Description;

namespace Microsoft.Azure.WebJobs.Script.Binding
{
public class ManualBinding : FunctionBinding
{
public ManualBinding(ScriptHostConfiguration config, BindingMetadata metadata, FileAccess access) : base (config, metadata, access)
{
}

public override Task BindAsync(BindingContext context)
{
throw new NotImplementedException();
}

public override Collection<CustomAttributeBuilder> GetCustomAttributes(Type parameterType)
{
return new Collection<CustomAttributeBuilder>();
}
}
}
1 change: 1 addition & 0 deletions src/WebJobs.Script/WebJobs.Script.csproj
Expand Up @@ -303,6 +303,7 @@
<Compile Include="Binding\Http\HttpTriggerAttribute.cs" />
<Compile Include="Binding\Http\HttpTriggerAttributeBindingProvider.cs" />
<Compile Include="Binding\IResultProcessingBinding.cs" />
<Compile Include="Binding\ManualBinding.cs" />
<Compile Include="Binding\ServiceBusScriptBindingProvider.cs" />
<Compile Include="Binding\TableBinding.cs" />
<Compile Include="Binding\WebJobsCoreScriptBindingProvider.cs" />
Expand Down
22 changes: 22 additions & 0 deletions test/WebJobs.Script.Tests/EndToEndTestsBase.cs
Expand Up @@ -111,6 +111,28 @@ protected async Task TableOutputTest()
}
}

[Fact]
public async Task ManualTrigger_Invoke_Succeeds()
{
TestHelpers.ClearFunctionLogs("ManualTrigger");

string testData = Guid.NewGuid().ToString();
string inputName = "input";
if (Fixture.FixtureId == "powershell")
{
inputName = "triggerInput";
}
Dictionary<string, object> arguments = new Dictionary<string, object>
{
{ inputName, testData }
};
await Fixture.Host.CallAsync("ManualTrigger", arguments);

// make sure the input string made it all the way through
var logs = await TestHelpers.GetFunctionLogsAsync("ManualTrigger");
Assert.True(logs.Any(p => p.Contains(testData)));
}

[Fact]
public async Task QueueTriggerToBlobTest()
{
Expand Down
45 changes: 27 additions & 18 deletions test/WebJobs.Script.Tests/NodeEndToEndTests.cs
Expand Up @@ -186,24 +186,6 @@ public async Task EventHub()
Assert.Equal(testData, payload.Id);
}

[Fact]
public async Task ManualTrigger()
{
string testData = Guid.NewGuid().ToString();
Dictionary<string, object> arguments = new Dictionary<string, object>
{
{ "input", testData }
};
await Fixture.Host.CallAsync("ManualTrigger", arguments);

// verify use of context.log to log complex objects
TraceEvent scriptTrace = Fixture.TraceWriter.Traces.Single(p => p.Message.Contains(testData));
Assert.Equal(TraceLevel.Info, scriptTrace.Level);
JObject logEntry = JObject.Parse(scriptTrace.Message);
Assert.Equal("Node.js manually triggered function called!", logEntry["message"]);
Assert.Equal(testData, logEntry["input"]);
}

[Fact]
public async Task Scenario_DoneCalledMultipleTimes_ErrorIsLogged()
{
Expand Down Expand Up @@ -232,6 +214,33 @@ public async Task Scenario_DoneCalledMultipleTimes_ErrorIsLogged()
Assert.True(logs.Any(p => p.Contains("Function completed (Success")));
}

[Fact]
public async Task Scenario_Logging()
{
TestHelpers.ClearFunctionLogs("Scenarios");

string testData = Guid.NewGuid().ToString();
JObject input = new JObject
{
{ "scenario", "logging" },
{ "input", testData },
};
Dictionary<string, object> arguments = new Dictionary<string, object>
{
{ "input", input.ToString() }
};
await Fixture.Host.CallAsync("Scenarios", arguments);

var logs = await TestHelpers.GetFunctionLogsAsync("Scenarios");

// verify use of context.log to log complex objects
TraceEvent scriptTrace = Fixture.TraceWriter.Traces.Single(p => p.Message.Contains(testData));
Assert.Equal(TraceLevel.Info, scriptTrace.Level);
JObject logEntry = JObject.Parse(scriptTrace.Message);
Assert.Equal("This is a test", logEntry["message"]);
Assert.Equal(testData, logEntry["input"]);
}

[Fact]
public async Task Scenario_RandGuidBinding_GeneratesRandomIDs()
{
Expand Down
Expand Up @@ -15,7 +15,7 @@
using Newtonsoft.Json.Linq;
using Xunit;

namespace Microsoft.Azure.WebJobs.Script.Tests.TestScripts
namespace Microsoft.Azure.WebJobs.Script.Tests
{
public class PowerShellFunctionGenerationTests
{
Expand Down
@@ -0,0 +1,9 @@
{
"bindings": [
{
"type": "manualTrigger",
"name": "input",
"direction": "in"
}
]
}
@@ -0,0 +1,2 @@
input=$(<$input)
echo $input
@@ -0,0 +1,9 @@
{
"bindings": [
{
"type": "manualTrigger",
"name": "input",
"direction": "in"
}
]
}
@@ -0,0 +1,7 @@
using System;
using Microsoft.Azure.WebJobs.Host;

public static void Run(string input, TraceWriter log)
{
log.Info(input);
}
@@ -0,0 +1,9 @@
{
"bindings": [
{
"type": "manualTrigger",
"name": "input",
"direction": "in"
}
]
}
17 changes: 17 additions & 0 deletions test/WebJobs.Script.Tests/TestScripts/FSharp/ManualTrigger/run.fsx
@@ -0,0 +1,17 @@
//----------------------------------------------------------------------------------------
// This prelude allows scripts to be edited in Visual Studio or another F# editing environment

#if !COMPILED
#I "../../../../../bin/Binaries/WebJobs.Script.Host"
#r "Microsoft.Azure.WebJobs.Host.dll"
#r "Microsoft.Azure.WebJobs.Extensions.dll"
#endif

//----------------------------------------------------------------------------------------
// This is the implementation of the function

open System
open Microsoft.Azure.WebJobs.Host

let Run(input: string, log: TraceWriter) =
log.Info(input)
@@ -1,13 +1,4 @@
module.exports = function (context, input) {
var logEntry = {
message: 'Node.js manually triggered function called!',
input: input
};
context.log(logEntry);
context.log('Mathew Charles');
context.log(null);
context.log(1234);
context.log(true);

context.log(input);
context.done();
}
13 changes: 13 additions & 0 deletions test/WebJobs.Script.Tests/TestScripts/Node/Scenarios/index.js
Expand Up @@ -18,6 +18,19 @@ var util = require('util');
context.bindings.blob = input.value;
context.done();
}
else if (scenario == 'logging') {
var logEntry = {
message: 'This is a test',
input: input.input
};
context.log(logEntry);
context.log('Mathew Charles');
context.log(null);
context.log(1234);
context.log(true);

context.done();
}
else {
// throw if the scenario didn't match
throw new Error(util.format("The scenario '%s' did not match any known scenario.", scenario));
Expand Down
@@ -0,0 +1,9 @@
{
"bindings": [
{
"type": "manualTrigger",
"name": "input",
"direction": "in"
}
]
}
@@ -0,0 +1,4 @@
<?php
$input = file_get_contents(getenv('input'));
fwrite(STDOUT, $input);
?>
@@ -0,0 +1,9 @@
{
"bindings": [
{
"type": "manualTrigger",
"name": "triggerInput",
"direction": "in"
}
]
}
@@ -0,0 +1,2 @@
$in = Get-Content $triggerInput
echo $in;
@@ -0,0 +1,9 @@
{
"bindings": [
{
"type": "manualTrigger",
"name": "input",
"direction": "in"
}
]
}
@@ -0,0 +1,6 @@
import os

# read the input and write to stdout
input = open(os.environ['input']).read()
message = "Python script processed queue message '{0}'".format(input)
print(input)
@@ -0,0 +1,9 @@
{
"bindings": [
{
"type": "manualTrigger",
"name": "input",
"direction": "in"
}
]
}
@@ -0,0 +1,2 @@
SET /p input=<%input%
echo %input%

0 comments on commit ac763b1

Please sign in to comment.