From 53806d6ed949e3749f9cd178d506343ef30d0cd1 Mon Sep 17 00:00:00 2001 From: Philip Lin <41589463+philipktlin@users.noreply.github.com> Date: Mon, 6 Aug 2018 14:01:46 -0700 Subject: [PATCH] Fix/Improve existing c# tests (#115) update docker image for AgentTests.AgentStartsUpModules test; and increase timeouts for some tests to make it more stable. --- .../test-config-windows-x64.json | 4 +- .../RuntimeInfoProviderTest.cs | 2 +- .../commands/CreateCommandTest.cs | 136 +++++++++++------- 3 files changed, 90 insertions(+), 52 deletions(-) diff --git a/edge-agent/test/Microsoft.Azure.Devices.Edge.Agent.Docker.E2E.Test/test-config-windows-x64.json b/edge-agent/test/Microsoft.Azure.Devices.Edge.Agent.Docker.E2E.Test/test-config-windows-x64.json index d933616f60c..43c55722e75 100644 --- a/edge-agent/test/Microsoft.Azure.Devices.Edge.Agent.Docker.E2E.Test/test-config-windows-x64.json +++ b/edge-agent/test/Microsoft.Azure.Devices.Edge.Agent.Docker.E2E.Test/test-config-windows-x64.json @@ -6,8 +6,8 @@ "validator": { "$type": "RunCommandValidator", "command": "docker", - "args": "run --rm --link dotnet:dotnet microsoft/dotnet:2.0.6-sdk-2.1.101-nanoserver-1709 dotnet --version", - "outputEquals": "2.1.101" + "args": "run --rm --link dotnet:dotnet microsoft/dotnet:2.1.302-sdk-nanoserver-1709 dotnet --version", + "outputEquals": "2.1.302" } } ] \ No newline at end of file diff --git a/edge-agent/test/Microsoft.Azure.Devices.Edge.Agent.Docker.Test/RuntimeInfoProviderTest.cs b/edge-agent/test/Microsoft.Azure.Devices.Edge.Agent.Docker.Test/RuntimeInfoProviderTest.cs index 809298d6c76..79d0b34dfb4 100644 --- a/edge-agent/test/Microsoft.Azure.Devices.Edge.Agent.Docker.Test/RuntimeInfoProviderTest.cs +++ b/edge-agent/test/Microsoft.Azure.Devices.Edge.Agent.Docker.Test/RuntimeInfoProviderTest.cs @@ -21,7 +21,7 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Docker.Test public class RuntimeInfoProviderTest { - static readonly TimeSpan Timeout = TimeSpan.FromSeconds(30); + static readonly TimeSpan Timeout = TimeSpan.FromSeconds(45); static readonly IDockerClient Client = DockerHelper.Client; static readonly IEntityStore RestartStateStore = new Mock>().Object; static readonly IRestartPolicyManager RestartManager = new Mock().Object; diff --git a/edge-agent/test/Microsoft.Azure.Devices.Edge.Agent.Docker.Test/commands/CreateCommandTest.cs b/edge-agent/test/Microsoft.Azure.Devices.Edge.Agent.Docker.Test/commands/CreateCommandTest.cs index 9cbc3851f56..fa87ca2b073 100644 --- a/edge-agent/test/Microsoft.Azure.Devices.Edge.Agent.Docker.Test/commands/CreateCommandTest.cs +++ b/edge-agent/test/Microsoft.Azure.Devices.Edge.Agent.Docker.Test/commands/CreateCommandTest.cs @@ -27,6 +27,8 @@ public class CreateCommandTest { static readonly IDictionary EnvVars = new Dictionary(); + readonly TimeSpan defaultTimeout = TimeSpan.FromSeconds(30); + [Fact] [Integration] public async Task SmokeTest() @@ -38,7 +40,7 @@ public async Task SmokeTest() try { - using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15))) + using (var cts = new CancellationTokenSource(this.defaultTimeout)) { await DockerHelper.Client.CleanupContainerAsync(Name, Image); @@ -78,7 +80,7 @@ public async Task SmokeTest() // verify container is created and has correct settings ContainerInspectResponse container = await DockerHelper.Client.Containers.InspectContainerAsync(Name); - Assert.Equal(Name, container.Name.Substring(1)); // for whatever reason the container name is returned with a starting "/" + Assert.Equal(Name, container.Name.Substring(1)); // for whatever reason the container name is returned with a starting "/" Assert.Equal("1.0", container.Config.Labels.GetOrElse(Constants.Labels.Version, "missing")); // port mapping Assert.Equal("8080/tcp", container.HostConfig.PortBindings.First().Key); @@ -114,7 +116,7 @@ public async Task TestUdpModuleConfig() try { - using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15))) + using (var cts = new CancellationTokenSource(this.defaultTimeout)) { await DockerHelper.Client.CleanupContainerAsync(Name, Image); @@ -125,10 +127,11 @@ public async Task TestUdpModuleConfig() var config = new DockerConfig(Image, @"{""HostConfig"": {""PortBindings"": {""42/udp"": [{""HostPort"": ""42""}]}}}"); var module = new DockerModule(Name, "1.0", ModuleStatus.Running, Core.RestartPolicy.OnUnhealthy, config, null, EnvVars); - IConfigurationRoot configRoot = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary - { - { "EdgeHubConnectionString", fakeConnectionString } - }).Build(); + IConfigurationRoot configRoot = new ConfigurationBuilder().AddInMemoryCollection( + new Dictionary + { + { "EdgeHubConnectionString", fakeConnectionString } + }).Build(); // Logging options will be derived from application level configuration var modules = new Dictionary { [Name] = module }; @@ -149,7 +152,7 @@ public async Task TestUdpModuleConfig() // verify container is created with correct settings. ContainerInspectResponse container = await DockerHelper.Client.Containers.InspectContainerAsync(Name); - Assert.Equal(Name, container.Name.Substring(1)); // for whatever reason the container name is returned with a starting "/" + Assert.Equal(Name, container.Name.Substring(1)); // for whatever reason the container name is returned with a starting "/" Assert.Equal("1.0", container.Config.Labels.GetOrElse(Constants.Labels.Version, "missing")); // port bindings Assert.Equal(1, container.HostConfig.PortBindings.Count); @@ -178,13 +181,13 @@ public async Task EdgeHubLaunch() try { - using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15))) + using (var cts = new CancellationTokenSource(this.defaultTimeout)) { var mountMap = new Dictionary() { - {Constants.EdgeModuleCaCertificateFileKey, "/module.ca.cert"}, - {Constants.EdgeModuleHubServerCaChainCertificateFileKey, "/module.ca.chain.cert"}, - {Constants.EdgeModuleHubServerCertificateFileKey, "/module.server.cert"} + { Constants.EdgeModuleCaCertificateFileKey, "/module.ca.cert" }, + { Constants.EdgeModuleHubServerCaChainCertificateFileKey, "/module.ca.chain.cert" }, + { Constants.EdgeModuleHubServerCertificateFileKey, "/module.server.cert" } }; await DockerHelper.Client.CleanupContainerAsync(Name, Image); @@ -205,10 +208,10 @@ public async Task EdgeHubLaunch() new Dictionary { { "EdgeHubConnectionString", fakeConnectionString }, - {Constants.NetworkIdKey, "testnetwork" }, - {Constants.EdgeDeviceHostNameKey, "testdevice" }, - {Constants.EdgeModuleHubServerCaChainCertificateFileKey,mountMap[Constants.EdgeModuleHubServerCaChainCertificateFileKey] }, - {Constants.EdgeModuleHubServerCertificateFileKey, mountMap[Constants.EdgeModuleHubServerCertificateFileKey] }, + { Constants.NetworkIdKey, "testnetwork" }, + { Constants.EdgeDeviceHostNameKey, "testdevice" }, + { Constants.EdgeModuleHubServerCaChainCertificateFileKey, mountMap[Constants.EdgeModuleHubServerCaChainCertificateFileKey] }, + { Constants.EdgeModuleHubServerCertificateFileKey, mountMap[Constants.EdgeModuleHubServerCertificateFileKey] }, }).Build(); var modules = new Dictionary { [Name] = module }; var systemModules = new SystemModules(null, null); @@ -227,7 +230,7 @@ public async Task EdgeHubLaunch() // verify container is created with correct settings ContainerInspectResponse container = await DockerHelper.Client.Containers.InspectContainerAsync(Name); - Assert.Equal(Name, container.Name.Substring(1)); // for whatever reason the container name is returned with a starting "/" + Assert.Equal(Name, container.Name.Substring(1)); // for whatever reason the container name is returned with a starting "/" // edgeHub doesn't have a version Assert.Equal("missing", container.Config.Labels.GetOrElse(Constants.Labels.Version, "missing")); // port bindings - added default bindings for edgeHub @@ -272,7 +275,7 @@ public async Task EdgeHubLaunchWithBadLogOptions() try { - using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15))) + using (var cts = new CancellationTokenSource(this.defaultTimeout)) { await DockerHelper.Client.CleanupContainerAsync(Name, Image); @@ -310,7 +313,7 @@ public async Task EdgeHubLaunchWithBadLogOptions() // verify container is created with correct settings ContainerInspectResponse container = await DockerHelper.Client.Containers.InspectContainerAsync(Name); - Assert.Equal(Name, container.Name.Substring(1)); // for whatever reason the container name is returned with a starting "/" + Assert.Equal(Name, container.Name.Substring(1)); // for whatever reason the container name is returned with a starting "/" // labels - edgeHub doesn's have a version Assert.Equal("missing", container.Config.Labels.GetOrElse(Constants.Labels.Version, "missing")); Assert.Equal("43", container.Config.Labels.GetOrElse(Constants.Labels.ConfigurationId, "missing")); @@ -340,14 +343,21 @@ public async Task EdgeHubLaunchWithBadLogOptions() } IEdgeAgentModule CreateMockEdgeAgentModule() => new TestAgentModule( - Constants.EdgeAgentModuleName, "docker", - new TestConfig("EdgeAgentImage"), new Core.ConfigurationInfo(), EnvVars + Constants.EdgeAgentModuleName, + "docker", + new TestConfig("EdgeAgentImage"), + new ConfigurationInfo(), + EnvVars ); IEdgeHubModule CreateMockEdgeHubModule() => new TestHubModule( - Constants.EdgeHubModuleName, "docker", ModuleStatus.Running, - new TestConfig("EdgeAgentImage"), Core.RestartPolicy.Always, - new Core.ConfigurationInfo(), EnvVars + Constants.EdgeHubModuleName, + "docker", + ModuleStatus.Running, + new TestConfig("EdgeAgentImage"), + Core.RestartPolicy.Always, + new ConfigurationInfo(), + EnvVars ); [Fact] @@ -391,7 +401,9 @@ public async Task TestMountEdgeHubVolume() var configSource = new Mock(); var deploymentConfig = new DeploymentConfig( - "1.0", runtimeInfo.Object, systemModules, + "1.0", + runtimeInfo.Object, + systemModules, ImmutableDictionary.Empty ); var deploymentConfigInfo = new DeploymentConfigInfo(10, deploymentConfig); @@ -404,8 +416,13 @@ public async Task TestMountEdgeHubVolume() ICommand createCommand = await CreateCommand.BuildAsync( dockerClient.Object, new DockerModule( - "mod1", "1.0", ModuleStatus.Running, Core.RestartPolicy.OnUnhealthy, - new DockerConfig("image1"), new ConfigurationInfo("1234"), EnvVars + "mod1", + "1.0", + ModuleStatus.Running, + Core.RestartPolicy.OnUnhealthy, + new DockerConfig("image1"), + new ConfigurationInfo("1234"), + EnvVars ), moduleIdentity.Object, new DockerLoggingConfig("json"), @@ -459,7 +476,9 @@ public async Task TestMountModuleVolume() var configSource = new Mock(); var deploymentConfig = new DeploymentConfig( - "1.0", runtimeInfo.Object, systemModules, + "1.0", + runtimeInfo.Object, + systemModules, ImmutableDictionary.Empty ); var deploymentConfigInfo = new DeploymentConfigInfo(10, deploymentConfig); @@ -472,8 +491,13 @@ public async Task TestMountModuleVolume() ICommand createCommand = await CreateCommand.BuildAsync( dockerClient.Object, new DockerModule( - "mod1", "1.0", ModuleStatus.Running, Core.RestartPolicy.OnUnhealthy, - new DockerConfig("image1"), new ConfigurationInfo("1234"), EnvVars + "mod1", + "1.0", + ModuleStatus.Running, + Core.RestartPolicy.OnUnhealthy, + new DockerConfig("image1"), + new ConfigurationInfo("1234"), + EnvVars ), moduleIdentity.Object, new DockerLoggingConfig("json"), @@ -527,16 +551,22 @@ public void InjectPortBindingsTest() PortBindings = new Dictionary> { { - "8883/tcp", new List{new PortBinding + "8883/tcp", new List { - HostPort = "8883" - }} + new PortBinding + { + HostPort = "8883" + } + } }, { - "443/tcp", new List{new PortBinding + "443/tcp", new List { - HostPort = "443" - }} + new PortBinding + { + HostPort = "443" + } + } } } } @@ -566,17 +596,23 @@ public void InjectPortBindingsTest() PortBindings = new Dictionary> { { - "8883/tcp", new List{new PortBinding + "8883/tcp", new List { - HostPort = "8883", - HostIP = "1.2.3.4" - }} + new PortBinding + { + HostPort = "8883", + HostIP = "1.2.3.4" + } + } }, { - "443/tcp", new List{new PortBinding + "443/tcp", new List { - HostPort = "443" - }} + new PortBinding + { + HostPort = "443" + } + } } } } @@ -597,7 +633,6 @@ public void InjectPortBindingsTest() Assert.Equal(createContainerParameters.HostConfig.PortBindings["443/tcp"].First().HostPort, "443"); Assert.Equal(createContainerParameters.HostConfig.PortBindings["443/tcp"].First().HostIP, null); - // If EdgeHub port mappings are already in the create options, then they are not injected twice // Arrange createContainerParameters = new CreateContainerParameters @@ -607,10 +642,13 @@ public void InjectPortBindingsTest() PortBindings = new Dictionary> { { - "1234/tcp", new List{new PortBinding + "1234/tcp", new List { - HostPort = "1234" - }} + new PortBinding + { + HostPort = "1234" + } + } } } } @@ -646,7 +684,7 @@ public async Task UpstreamProtocolTest() try { - using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15))) + using (var cts = new CancellationTokenSource(this.defaultTimeout)) { // Arrange await DockerHelper.Client.CleanupContainerAsync(Name, Image); @@ -690,7 +728,7 @@ public async Task UpstreamProtocolTest() // Assert // verify container is created and has correct settings ContainerInspectResponse container = await DockerHelper.Client.Containers.InspectContainerAsync(Name); - Assert.Equal(Name, container.Name.Substring(1)); // for whatever reason the container name is returned with a starting "/" + Assert.Equal(Name, container.Name.Substring(1)); // for whatever reason the container name is returned with a starting "/" Assert.Equal("1.0", container.Config.Labels.GetOrElse(Constants.Labels.Version, "missing")); // port mapping Assert.Equal("8080/tcp", container.HostConfig.PortBindings.First().Key);