Skip to content

Commit

Permalink
EdgeAgent: Fill null ref in IotHubReporter.ReportShutdown (#1091)
Browse files Browse the repository at this point in the history
* Fix EdgeAgent reporter null ref

* Add test
  • Loading branch information
varunpuranik authored Apr 13, 2019
1 parent 715b698 commit 81065db
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,13 @@ AgentState BuildCurrentState(ModuleSet moduleSet, IRuntimeInfo runtimeInfo, long
Task ReportShutdownInternal(AgentState agentState, DeploymentStatus status)
{
Option<IEdgeAgentModule> edgeAgentModule = agentState.SystemModules.EdgeAgent
.Map(ea => ea as IRuntimeStatusModule)
.Filter(ea => ea != null)
.Map(ea => (IEdgeAgentModule)ea.WithRuntimeStatus(ModuleStatus.Unknown))
.Filter(ea => ea is IRuntimeStatusModule)
.Map(ea => ((IRuntimeStatusModule)ea).WithRuntimeStatus(ModuleStatus.Unknown) as IEdgeAgentModule)
.Else(agentState.SystemModules.EdgeAgent);

Option<IEdgeHubModule> edgeHubModule = agentState.SystemModules.EdgeHub
.Map(eh => eh as IRuntimeStatusModule)
.Filter(eh => eh != null)
.Map(eh => (IEdgeHubModule)eh.WithRuntimeStatus(ModuleStatus.Unknown))
.Filter(eh => eh is IRuntimeStatusModule)
.Map(eh => ((IRuntimeStatusModule)eh).WithRuntimeStatus(ModuleStatus.Unknown) as IEdgeHubModule)
.Else(agentState.SystemModules.EdgeHub);

IDictionary<string, IModule> updateUserModules = (agentState.Modules ?? ImmutableDictionary<string, IModule>.Empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,96 @@ public async void ReportShutdown()
}
}

[Fact]
[Unit]
public async void ReportShutdownWithOnlyEdgeAgentInState()
{
using (var cts = new CancellationTokenSource(Timeout))
{
// Arrange
const long DesiredVersion = 10;
const string RuntimeType = "docker";
const string MinDockerVersion = "1.25";
const string LoggingOptions = "logging options";
const string OperatingSystemType = "linux";
const string Architecture = "x86_x64";
const string Version = "17.11.0-ce";
var versionInfo = new VersionInfo("v1", "b1", "c1");
DateTime lastStartTimeUtc = DateTime.Parse("2017-11-13T23:44:35.127381Z", null, DateTimeStyles.RoundtripKind);

IEdgeAgentModule edgeAgent = new EdgeAgentDockerRuntimeModule(new DockerReportedConfig("image", string.Empty, "hash"), ModuleStatus.Running, 0, string.Empty, lastStartTimeUtc, DateTime.MinValue, new ConfigurationInfo("id"), new Dictionary<string, EnvVal>());

// prepare IEdgeAgentConnection mock
var edgeAgentConnection = new Mock<IEdgeAgentConnection>();
var reportedState = new AgentState(
0,
DeploymentStatus.Unknown,
null,
new SystemModules(edgeAgent, null),
ModuleSet.Empty.Modules.ToImmutableDictionary(),
string.Empty,
versionInfo);
edgeAgentConnection.SetupGet(c => c.ReportedProperties).Returns(Option.Some(new TwinCollection(JsonConvert.SerializeObject(reportedState))));

TwinCollection patch = null;
edgeAgentConnection.Setup(c => c.UpdateReportedPropertiesAsync(It.IsAny<TwinCollection>()))
.Callback<TwinCollection>(tc => patch = tc)
.Returns(Task.CompletedTask);

// prepare AgentConfig
var deploymentConfig = new DeploymentConfig(
"1.0",
new DockerRuntimeInfo(RuntimeType, new DockerRuntimeConfig(MinDockerVersion, LoggingOptions)),
new SystemModules(edgeAgent, null),
new Dictionary<string, IModule>());
var deploymentConfigInfo = new DeploymentConfigInfo(
DesiredVersion,
deploymentConfig);

IRuntimeInfo runtimeInfo = new DockerReportedRuntimeInfo(
RuntimeType,
(deploymentConfigInfo.DeploymentConfig.Runtime as DockerRuntimeInfo)?.Config,
new DockerPlatformInfo(OperatingSystemType, Architecture, Version));

// build current module set
ModuleSet currentModuleSet = ModuleSet.Create(edgeAgent);

var agentStateSerde = new Mock<ISerde<AgentState>>();
agentStateSerde.Setup(s => s.Deserialize(It.IsAny<string>()))
.Returns(reportedState);

// Act
var reporter = new IoTHubReporter(edgeAgentConnection.Object, agentStateSerde.Object, versionInfo);

// this should cause all modules to be updated.
await reporter.ReportAsync(cts.Token, currentModuleSet, runtimeInfo, DesiredVersion, Option.Some(DeploymentStatus.Success));

// Now use the last reported configuration to report a shutdown
await reporter.ReportShutdown(DeploymentStatus.Success, cts.Token);

// Assert
Assert.NotNull(patch);

var patchJson = JsonConvert.DeserializeObject(patch.ToJson()) as JObject;
JObject expectedPatchJson = JObject.FromObject(
new
{
systemModules = new Dictionary<string, object>
{
{
edgeAgent.Name,
new
{
runtimeStatus = "unknown"
}
}
}
});

Assert.True(JToken.DeepEquals(expectedPatchJson, patchJson));
}
}

[Fact]
[Unit]
public async void ReportedPatchWithEnvVarsTest()
Expand Down

0 comments on commit 81065db

Please sign in to comment.