Skip to content

Commit

Permalink
Specify credentials for multiple container registries in the tests (A…
Browse files Browse the repository at this point in the history
…zure#1411)

* Specify credentials for multiple container registries in the tests
* Add test project back into SLN
  • Loading branch information
damonbarry authored Jul 3, 2019
1 parent 3ea68a9 commit 9704bb7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 34 deletions.
13 changes: 11 additions & 2 deletions Microsoft.Azure.Devices.Edge.sln
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{2300ED4C-1
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.Devices.Edge.Test.Common", "test\Microsoft.Azure.Devices.Edge.Test.Common\Microsoft.Azure.Devices.Edge.Test.Common.csproj", "{950DACB0-B011-41AF-B0FB-245F749B01AB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.Devices.Edge.Test.TempSensor", "test\Microsoft.Azure.Devices.Edge.Test.TempSensor\Microsoft.Azure.Devices.Edge.Test.TempSensor.csproj", "{FCF94FF4-A608-4CC1-8D1D-215B6F88F203}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.Devices.Edge.Agent.Kubernetes", "edge-agent\src\Microsoft.Azure.Devices.Edge.Agent.Kubernetes\Microsoft.Azure.Devices.Edge.Agent.Kubernetes.csproj", "{B63C506E-5CB5-4DE8-8275-F7BA8354799E}"
ProjectSection(ProjectDependencies) = postProject
{B0F37919-0D16-490F-B7A1-665BB93B9A62} = {B0F37919-0D16-490F-B7A1-665BB93B9A62}
Expand All @@ -186,6 +184,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.Devices.Edg
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.Devices.Edge.Agent.Kubernetes.Test", "edge-agent\test\Microsoft.Azure.Devices.Edge.Agent.Kubernetes.Test\Microsoft.Azure.Devices.Edge.Agent.Kubernetes.Test.csproj", "{A5DFFB52-D8EA-4E1A-BF4F-8B9665C7DAFE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.Devices.Edge.Test", "test\Microsoft.Azure.Devices.Edge.Test\Microsoft.Azure.Devices.Edge.Test.csproj", "{08986FED-9283-47F1-B938-86D9C6E753E1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
CheckInBuild|Any CPU = CheckInBuild|Any CPU
Expand Down Expand Up @@ -586,6 +586,14 @@ Global
{A5DFFB52-D8EA-4E1A-BF4F-8B9665C7DAFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5DFFB52-D8EA-4E1A-BF4F-8B9665C7DAFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5DFFB52-D8EA-4E1A-BF4F-8B9665C7DAFE}.Release|Any CPU.Build.0 = Release|Any CPU
{08986FED-9283-47F1-B938-86D9C6E753E1}.CheckInBuild|Any CPU.ActiveCfg = CheckInBuild|Any CPU
{08986FED-9283-47F1-B938-86D9C6E753E1}.CheckInBuild|Any CPU.Build.0 = CheckInBuild|Any CPU
{08986FED-9283-47F1-B938-86D9C6E753E1}.CodeCoverage|Any CPU.ActiveCfg = CodeCoverage|Any CPU
{08986FED-9283-47F1-B938-86D9C6E753E1}.CodeCoverage|Any CPU.Build.0 = CodeCoverage|Any CPU
{08986FED-9283-47F1-B938-86D9C6E753E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{08986FED-9283-47F1-B938-86D9C6E753E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{08986FED-9283-47F1-B938-86D9C6E753E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{08986FED-9283-47F1-B938-86D9C6E753E1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -657,6 +665,7 @@ Global
{950DACB0-B011-41AF-B0FB-245F749B01AB} = {2300ED4C-1D5A-460F-8691-7C85E1162E0C}
{B63C506E-5CB5-4DE8-8275-F7BA8354799E} = {54351E51-19CB-4DE3-8302-99846AB216CF}
{A5DFFB52-D8EA-4E1A-BF4F-8B9665C7DAFE} = {F5E37327-3AA9-4CC2-9FE3-B28271ADB5E3}
{08986FED-9283-47F1-B938-86D9C6E753E1} = {2300ED4C-1D5A-460F-8691-7C85E1162E0C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D71830F5-3AF5-46B4-8A9E-1DCE4F2253AC}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ public class EdgeConfigBuilder
{
readonly string deviceId;
readonly Dictionary<string, IModuleConfigBuilder> moduleBuilders;
Option<(string address, string username, string password)> registry;
readonly List<(string address, string username, string password)> registries;

public EdgeConfigBuilder(string deviceId)
{
this.deviceId = deviceId;
this.moduleBuilders = new Dictionary<string, IModuleConfigBuilder>();
this.registry = Option.None<(string, string, string)>();
this.registries = new List<(string, string, string)>();
}

public void AddRegistryCredentials(string address, string username, string password)
{
this.registry = Option.Some((address, username, password));
this.registries.Add((address, username, password));
}

public IModuleConfigBuilder AddEdgeAgent(string image = null)
Expand Down Expand Up @@ -95,19 +95,22 @@ ModuleConfiguration BuildEdgeAgent(IEnumerable<ModuleConfiguration> configs)
};

// Registry credentials under settings
this.registry.ForEach(
r =>
if (this.registries.Count != 0)
{
var credentials = new Dictionary<string, object>();
for (int i = 0; i < this.registries.Count; ++i)
{
settings["registryCredentials"] = new
(string address, string username, string password) = this.registries[i];
credentials.Add($"reg{i}", new
{
reg1 = new
{
r.username,
r.password,
r.address
}
};
});
username,
password,
address
});
}

settings["registryCredentials"] = credentials;
}

// Deployment info for edge agent
var systemModules = new Dictionary<string, object>()
Expand Down
37 changes: 23 additions & 14 deletions test/Microsoft.Azure.Devices.Edge.Test/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
namespace Microsoft.Azure.Devices.Edge.Test
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using Microsoft.Azure.Devices.Edge.Util;
using Microsoft.Extensions.Configuration;
Expand All @@ -21,25 +23,32 @@ public Context()

string GetOrDefault(string name, string defaultValue) => context.GetValue(name, defaultValue);

Option<(string, string, string)> GetAndValidateRegistry()
IEnumerable<(string, string, string)> GetAndValidateRegistries()
{
// If any container registry arguments (server, username, password)
// are given, then they must *all* be given, otherwise throw an error.
string registry = Get("registry");
string username = Get("user");
string password = Get("CONTAINER_REGISTRY_PASSWORD");

if (!string.IsNullOrWhiteSpace(registry) ||
!string.IsNullOrWhiteSpace(username) ||
!string.IsNullOrWhiteSpace(password))
var result = new List<(string, string, string)>();

var registries = context.GetSection("registries").GetChildren().ToArray();
foreach (var reg in registries)
{
string registry = reg.GetValue<string>("address");
string username = reg.GetValue<string>("username");
// To specify a password as an environment variable instead of in the
// JSON file (so it's not stored in the clear on the filesystem), name
// the variable like this: E2E_REGISTRIES__<index>__PASSWORD, where
// <index> is the 0-based number corresponding to an element in the
// "registries" JSON array.
string password = reg.GetValue<string>("PASSWORD");

// If any container registry arguments (server, username, password)
// are given, then they must *all* be given, otherwise throw an error.
Preconditions.CheckNonWhiteSpace(registry, nameof(registry));
Preconditions.CheckNonWhiteSpace(username, nameof(username));
Preconditions.CheckNonWhiteSpace(password, nameof(password));
return Option.Some((registry, username, password));

result.Add((registry, username, password));
}

return Option.None<(string, string, string)>();
return result;
}

this.ConnectionString = Get("IOT_HUB_CONNECTION_STRING");
Expand All @@ -48,7 +57,7 @@ public Context()
this.InstallerPath = Option.Maybe(Get("installerPath"));
this.PackagePath = Option.Maybe(Get("packagePath"));
this.Proxy = Option.Maybe(context.GetValue<Uri>("proxy"));
this.Registry = GetAndValidateRegistry();
this.Registries = GetAndValidateRegistries();
this.EdgeAgentImage = Option.Maybe(Get("edgeAgentImage"));
this.EdgeHubImage = Option.Maybe(Get("edgeHubImage"));
this.TempSensorImage = Option.Maybe(Get("tempSensorImage"));
Expand Down Expand Up @@ -78,7 +87,7 @@ public Context()

public Option<Uri> Proxy { get; }

public Option<(string address, string username, string password)> Registry { get; }
public IEnumerable<(string address, string username, string password)> Registries { get; }

public Option<string> EdgeAgentImage { get; }

Expand Down
12 changes: 8 additions & 4 deletions test/Microsoft.Azure.Devices.Edge.Test/EndToEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ await Profiler.Run(
token)).Expect(() => new Exception("Device should have already been created in setup fixture"));

var builder = new EdgeConfigBuilder(device.Id);
Context.Current.Registry.ForEach(
r => builder.AddRegistryCredentials(r.address, r.username, r.password));
foreach ((string address, string username, string password) in Context.Current.Registries)
{
builder.AddRegistryCredentials(address, username, password);
}
builder.AddEdgeAgent(agentImage).WithProxy(proxy);
builder.AddEdgeHub(hubImage, optimizeForPerformance).WithProxy(proxy);
builder.AddModule("tempSensor", sensorImage);
Expand Down Expand Up @@ -160,8 +162,10 @@ await Profiler.Run(
string clientTransport = protocol.ToTransportType().ToString();

var builder = new EdgeConfigBuilder(device.Id);
Context.Current.Registry.ForEach(
r => builder.AddRegistryCredentials(r.address, r.username, r.password));
foreach ((string address, string username, string password) in Context.Current.Registries)
{
builder.AddRegistryCredentials(address, username, password);
}
builder.AddEdgeAgent(agentImage).WithProxy(proxy);
builder.AddEdgeHub(hubImage, optimizeForPerformance).WithProxy(proxy);
builder.AddModule(methodSender, senderImage)
Expand Down

0 comments on commit 9704bb7

Please sign in to comment.