Skip to content

Commit

Permalink
Fix/Improve existing c# tests (#115)
Browse files Browse the repository at this point in the history
update docker image for AgentTests.AgentStartsUpModules test; and increase timeouts for some tests to make it more stable.
  • Loading branch information
philipktlin committed Aug 6, 2018
1 parent e854f9d commit 53806d6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ModuleState> RestartStateStore = new Mock<IEntityStore<string, ModuleState>>().Object;
static readonly IRestartPolicyManager RestartManager = new Mock<IRestartPolicyManager>().Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class CreateCommandTest
{
static readonly IDictionary<string, EnvVal> EnvVars = new Dictionary<string, EnvVal>();

readonly TimeSpan defaultTimeout = TimeSpan.FromSeconds(30);

[Fact]
[Integration]
public async Task SmokeTest()
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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<string, string>
{
{ "EdgeHubConnectionString", fakeConnectionString }
}).Build();
IConfigurationRoot configRoot = new ConfigurationBuilder().AddInMemoryCollection(
new Dictionary<string, string>
{
{ "EdgeHubConnectionString", fakeConnectionString }
}).Build();

// Logging options will be derived from application level configuration
var modules = new Dictionary<string, IModule> { [Name] = module };
Expand All @@ -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);
Expand Down Expand Up @@ -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<string, string>()
{
{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);

Expand All @@ -205,10 +208,10 @@ public async Task EdgeHubLaunch()
new Dictionary<string, string>
{
{ "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<string, IModule> { [Name] = module };
var systemModules = new SystemModules(null, null);
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -391,7 +401,9 @@ public async Task TestMountEdgeHubVolume()

var configSource = new Mock<IConfigSource>();
var deploymentConfig = new DeploymentConfig(
"1.0", runtimeInfo.Object, systemModules,
"1.0",
runtimeInfo.Object,
systemModules,
ImmutableDictionary<string, IModule>.Empty
);
var deploymentConfigInfo = new DeploymentConfigInfo(10, deploymentConfig);
Expand All @@ -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"),
Expand Down Expand Up @@ -459,7 +476,9 @@ public async Task TestMountModuleVolume()

var configSource = new Mock<IConfigSource>();
var deploymentConfig = new DeploymentConfig(
"1.0", runtimeInfo.Object, systemModules,
"1.0",
runtimeInfo.Object,
systemModules,
ImmutableDictionary<string, IModule>.Empty
);
var deploymentConfigInfo = new DeploymentConfigInfo(10, deploymentConfig);
Expand All @@ -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"),
Expand Down Expand Up @@ -527,16 +551,22 @@ public void InjectPortBindingsTest()
PortBindings = new Dictionary<string, IList<PortBinding>>
{
{
"8883/tcp", new List<PortBinding>{new PortBinding
"8883/tcp", new List<PortBinding>
{
HostPort = "8883"
}}
new PortBinding
{
HostPort = "8883"
}
}
},
{
"443/tcp", new List<PortBinding>{new PortBinding
"443/tcp", new List<PortBinding>
{
HostPort = "443"
}}
new PortBinding
{
HostPort = "443"
}
}
}
}
}
Expand Down Expand Up @@ -566,17 +596,23 @@ public void InjectPortBindingsTest()
PortBindings = new Dictionary<string, IList<PortBinding>>
{
{
"8883/tcp", new List<PortBinding>{new PortBinding
"8883/tcp", new List<PortBinding>
{
HostPort = "8883",
HostIP = "1.2.3.4"
}}
new PortBinding
{
HostPort = "8883",
HostIP = "1.2.3.4"
}
}
},
{
"443/tcp", new List<PortBinding>{new PortBinding
"443/tcp", new List<PortBinding>
{
HostPort = "443"
}}
new PortBinding
{
HostPort = "443"
}
}
}
}
}
Expand All @@ -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
Expand All @@ -607,10 +642,13 @@ public void InjectPortBindingsTest()
PortBindings = new Dictionary<string, IList<PortBinding>>
{
{
"1234/tcp", new List<PortBinding>{new PortBinding
"1234/tcp", new List<PortBinding>
{
HostPort = "1234"
}}
new PortBinding
{
HostPort = "1234"
}
}
}
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 53806d6

Please sign in to comment.