Question

Jonathan M Fishbein on Mon, 28 Aug 2017 18:33:10


Specifically I would like to pass cosmosdb properties into my precompiled Run azure function from my function.json file for unit testing purposes.  My azure function is an http triggered function and does (for clarity, not yelling) NOT have a cosmosdb binding.  I had a comment convo conversation here 

https://blogs.msdn.microsoft.com/appserviceteam/2017/03/16/publishing-a-net-class-library-as-a-function-app/#comment-135665 

where I was able to hone into exactly what I was looking for.  Any help on this is greatly appreciated.  Thanks!


Sponsored



Replies

AhmedElSayed on Mon, 28 Aug 2017 21:08:29


You can use app settings for that.

Locally, you can put those in local.settings.json in the Values collection, and you'll have access to those values in your functions either through environment variables or ConfigurationManager.AppSettings.

Example:

// local.settings.json
{
  "IsEncrypted": false,
  "Values": {
    "myCosmosDbKey": "this is the value of the key"
  }
}

in your C# function you can now do

[FunctionName("Function1")]
public static HttpResponseMessage Run([HttpTrigger]HttpRequestMessage req, TraceWriter log)
{
    log.Info(Environment.GetEnvironmentVariable("myCosmosDbKey"));
    // you can also use this
    log.Info(ConfigurationManager.AppSettings["myCosmosDbKey"]);
    return req.CreateResponse(HttpStatusCode.OK, "Hello");
}

Note that local.settings.json is only for your local development, on Azure you should use the Azure App Settings feature which you can find in your platform features tab for your function app. Those app settings will be available to your function in exactly the same way as those from local.settings.json. but they are stored encrypted.

if you want to encrypt settings locally as well you can do "func settings encrypt" using the azure-functions-core-tools command line interface.


Software Engineer - Azure Functions




Jonathan M Fishbein on Mon, 28 Aug 2017 22:24:27


Thanks for this detailed response.  

Sorry I left out some details from the other post.  

I am currently using the local.settings.json in conjunction with ConfigurationManager.AppSettings.[""].  When I am attempting to unit test my pre-compiled function, I cannot access the appsettings because the function is Not executing within the azure functions runtime.

Edit:

Been thinking more on how to explain.  I'd like a way to pass properties from azure appsettings -> function.json -> Run function when in production.  But when I'm unit testing I'd like to skip the appsettings part but have function parameters that will accept the properties directly because the function will Not be executing within the azure functions runtime



AhmedElSayed on Mon, 28 Aug 2017 22:28:53


I see. I don't think there is a way to inject these settings into the unit test context. I think you'll need to pull them down in some test fixture before all tests run.

Jonathan M Fishbein on Mon, 28 Aug 2017 22:47:20


I'm having some difficulty articulating what my need is, I hope it makes sense.  It's a pre-compiled Function so when I want to unit test it, it's just an isolated function in a .dll.  Adding an App.config to the library project wouldn't make sense because I'd be undermining appsettings that should be secrets, and also I'd like to use different values when testing and when in production and I think adding an App.config would override the Azure app settings.  I've submitted a github issue here

https://github.com/Azure/Azure-Functions/issues/463

Addition

So I can unit test my function within the azure functions runtime.  I do host start and then I unit test it by doing a 

WebRequest.Create("localhost.....")

and thinking more about it I could alter the local.settings.json file to point to local databases.  But I was hoping I could have a lower layer unit test that excludes the azure functions runtime as a dependency