Skip to content

Commit

Permalink
Update unit test GenerateModulesContentShouldReturnValue (#1360)
Browse files Browse the repository at this point in the history
Update unit tests on ConfigHelper

Add info link on how to configure a container create options

Add container create options on create/edit edge module #1170

Co-authored-by: Hocine Hacherouf <hacherouf.hocine@gmail.com>
  • Loading branch information
kbeaugrand and hocinehacherouf authored Oct 16, 2022
1 parent 18aaf95 commit de5ef92
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,16 @@ public void CreateGatewayModuleShouldReturnValue()
// Arrange
var config = new Configuration("config_test");

var expectedContainerCreateOptions = /*lang=json,strict*/ "{\"HostConfig\":{\"PortBindings\":{\"5671/tcp\":[{\"HostPort\":\"5671\"}],\"8883/tcp\":[{\"HostPort\":\"8883\"}],\"443/tcp\":[{\"HostPort\":\"443\"}]}}}";

var jPropModule = new Dictionary<string, object>()
{
{ "status", "running" },
{ "version", "1.0" },
{ "settings", new Dictionary<string, object>()
{
{ "image", "image_test" }
{ "image", "image_test" },
{ "createOptions", expectedContainerCreateOptions }
}
},
{ "env", new Dictionary<string, object>()
Expand All @@ -118,6 +121,7 @@ public void CreateGatewayModuleShouldReturnValue()
Assert.IsNotNull(result);
Assert.AreEqual("running", result.Status);
Assert.AreEqual("image_test", result.ImageURI);
Assert.AreEqual(expectedContainerCreateOptions, result.ContainerCreateOptions);
Assert.AreEqual(1, result.EnvironmentVariables.Count);
}

Expand Down Expand Up @@ -151,21 +155,26 @@ public void CreateGatewayModuleWithNullModuleShouldThrowException()
public void GenerateModulesContentShouldReturnValue()
{
// Arrange
var expectedModuleName = "ModuleTest";
var expectedContainerCreateOptions = /*lang=json,strict*/
"{\"HostConfig\":{\"PortBindings\":{\"5671/tcp\":[{\"HostPort\":\"5671\"}],\"8883/tcp\":[{\"HostPort\":\"8883\"}],\"443/tcp\":[{\"HostPort\":\"443\"}]}}}";

var edgeModel = new IoTEdgeModel()
{
ModelId = Guid.NewGuid().ToString(),
Name = "Model test",
Description = "Description Test",
EdgeModules = new List<IoTEdgeModule>()
{
new IoTEdgeModule()
new()
{
ModuleName = "ModuleTest",
ModuleName = expectedModuleName,
Status = "running",
ImageURI = "image",
ContainerCreateOptions = expectedContainerCreateOptions,
EnvironmentVariables = new List<IoTEdgeModuleEnvironmentVariable>()
{
new IoTEdgeModuleEnvironmentVariable(){ Name = "envTest01", Value = "test" }
new() { Name = "envTest01", Value = "test" }
},
ModuleIdentityTwinSettings = new List<IoTEdgeModuleTwinSetting>()
}
Expand All @@ -179,6 +188,9 @@ public void GenerateModulesContentShouldReturnValue()
Assert.IsNotNull(result);
Assert.IsAssignableFrom<Dictionary<string, IDictionary<string, object>>>(result);
Assert.AreEqual(2, result.Count);
var edgeHubPropertiesDesired = (EdgeAgentPropertiesDesired)result["$edgeAgent"]["properties.desired"];
_ = edgeHubPropertiesDesired.Modules[expectedModuleName].Settings.CreateOptions.Should()
.Be(expectedContainerCreateOptions);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
@using AzureIoTHub.Portal.Models.v10
@using AzureIoTHub.Portal.Shared.Models.v10
@inject HttpClient Http
@inject IJSRuntime JS
@inject ISnackbar Snackbar

<MudDialog ContentStyle="overflow:auto;max-height:600px;">
<DialogContent >
Expand All @@ -23,6 +20,16 @@
For="@(()=> Module.ImageURI)"
Required="true" />
</MudItem>
<MudItem xs="12">
<MudTextField @bind-Value="@currentContainerCreateOptions"
id=@nameof(IoTEdgeModule.ContainerCreateOptions)
Label="Container Create Options"
Variant="Variant.Outlined"
For="@(()=> Module.ContainerCreateOptions)" />
</MudItem>
<MudItem xs="12">
<MudAlert Severity="Severity.Info"><MudLink Href="https://learn.microsoft.com/en-us/azure/iot-edge/how-to-use-create-options" Target="_blank ">How to configure container create options for IoT Edge modules</MudLink></MudAlert>
</MudItem>
</MudGrid>

<MudTabs Elevation="1" Class="mt-10" Rounded="true" PanelClass="mt-6 scrollable-tab-content">
Expand Down Expand Up @@ -55,6 +62,7 @@

private string currentModuleName;
private string currentImageUri;
private string currentContainerCreateOptions;

private List<IoTEdgeModuleEnvironmentVariable> currentEnvironmentVariables = new();
private List<IoTEdgeModuleTwinSetting> currentModuleIdentityTwinSettings = new();
Expand All @@ -69,6 +77,7 @@

currentModuleName = Module.ModuleName;
currentImageUri = Module.ImageURI;
currentContainerCreateOptions = Module.ContainerCreateOptions;
currentEnvironmentVariables = new List<IoTEdgeModuleEnvironmentVariable>(Module.EnvironmentVariables.ToArray());
currentModuleIdentityTwinSettings = new List<IoTEdgeModuleTwinSetting>(Module.ModuleIdentityTwinSettings.ToArray());
currentCommands = new List<IoTEdgeModuleCommand>(Module.Commands.ToArray());
Expand All @@ -80,6 +89,7 @@
{
Module.ModuleName = currentModuleName;
Module.ImageURI = currentImageUri;
Module.ContainerCreateOptions = currentContainerCreateOptions;
Module.EnvironmentVariables = new List<IoTEdgeModuleEnvironmentVariable>(currentEnvironmentVariables.ToArray());
Module.ModuleIdentityTwinSettings = new List<IoTEdgeModuleTwinSetting>(currentModuleIdentityTwinSettings.ToArray());
Module.Commands = new List<IoTEdgeModuleCommand>(currentCommands.ToArray());
Expand Down
4 changes: 3 additions & 1 deletion src/AzureIoTHub.Portal/Server/Helpers/ConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public static IoTEdgeModule CreateGatewayModule(Configuration config, JProperty
{
ModuleName = module.Name,
ImageURI = module.Value["settings"]["image"]?.Value<string>(),
ContainerCreateOptions = module.Value["settings"]["createOptions"]?.Value<string>(),
Status = module.Value["status"]?.Value<string>(),
};

Expand Down Expand Up @@ -248,7 +249,8 @@ public static Dictionary<string, IDictionary<string, object>> GenerateModulesCon
Status = "running",
Settings = new ModuleSettings()
{
Image = module.ImageURI
Image = module.ImageURI,
CreateOptions = module.ContainerCreateOptions
},
RestartPolicy = "always",
EnvironmentVariables = new Dictionary<string, EnvironmentVariable>()
Expand Down
2 changes: 2 additions & 0 deletions src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class IoTEdgeModule
[Required(ErrorMessage = "The device image uri is required.")]
public string ImageURI { get; set; }

public string ContainerCreateOptions { get; set; }

/// <summary>
/// The module status.
/// </summary>
Expand Down

0 comments on commit de5ef92

Please sign in to comment.