Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.KeyVault.Models;
using Microsoft.Extensions.Configuration.Test;
using Microsoft.Rest.Azure;
using Moq;

namespace Microsoft.Extensions.Configuration.AzureKeyVault.Test
{
public class ConfigurationProviderKeyVaultTest : ConfigurationProviderTestBase
{
protected override (IConfigurationProvider Provider, System.Action Initializer) LoadThroughProvider(
TestSection testConfig)
{
var values = new List<KeyValuePair<string, string>>();
SectionToValues(testConfig, "", values);

return (FromKeyVault(values), () => {});
}

private IConfigurationProvider FromKeyVault(IEnumerable<KeyValuePair<string, string>> data)
{
const string vaultUri = "https://vault";

var client = new Mock<IKeyVaultClient>(MockBehavior.Strict);
var rawData = data.ToList();
var nextPageLink = vaultUri;

for (var i = 0; i < rawData.Count; i++)
{
var currentPageLink = nextPageLink;
nextPageLink = i < (rawData.Count - 1) ? $"next{i}" : null;

var secretId = new SecretIdentifier(vaultUri, rawData[i].Key).Identifier;

var pageMock = new PageMock
{
NextPageLink = nextPageLink,
Value = new[] { new SecretItem { Id = secretId, Attributes = new SecretAttributes { Enabled = true } } }
};

if (i == 0)
{
client.Setup(c => c.GetSecretsAsync(currentPageLink)).ReturnsAsync(pageMock);
}
else
{
client.Setup(c => c.GetSecretsNextAsync(currentPageLink)).ReturnsAsync(pageMock);
}

client.Setup(c => c.GetSecretAsync(secretId)).ReturnsAsync(new SecretBundle() { Value = rawData[i].Value, Id = secretId });
}

return new AzureKeyVaultConfigurationProvider(
client.Object,
vaultUri,
new DefaultKeyVaultSecretManager());
}

private class PageMock : IPage<SecretItem>
{
public IEnumerable<SecretItem> Value { get; set; }
public IEnumerator<SecretItem> GetEnumerator() => Value.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public string NextPageLink { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<TargetFramework>net461</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Config\test\Microsoft.Extensions.Configuration.Tests.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.Configuration.AzureKeyVault" />
<Reference Include="Microsoft.Extensions.Configuration.Test.Common" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration.Test;
using Xunit;

namespace Microsoft.Extensions.Configuration.CommandLine.Test
{
public class ConfigurationProviderCommandLineTest : ConfigurationProviderTestBase
{
protected override (IConfigurationProvider Provider, Action Initializer) LoadThroughProvider(
TestSection testConfig)
{
var args = new List<string>();
SectionToArgs(args, "", testConfig);

var provider = new CommandLineConfigurationProvider(args);

return (provider, () => { });
}

private void SectionToArgs(List<string> args, string sectionName, TestSection section)
{
foreach (var tuple in section.Values.SelectMany(e => e.Value.Expand(e.Key)))
{
args.Add($"--{sectionName}{tuple.Key}={tuple.Value}");
}

foreach (var tuple in section.Sections)
{
SectionToArgs(args, sectionName + tuple.Key + ":", tuple.Section);
}
}

[Fact]
public override void Load_from_single_provider_with_duplicates_throws()
{
AssertConfig(BuildConfigRoot(LoadThroughProvider(TestSection.DuplicatesTestConfig)));
}

[Fact]
public override void Load_from_single_provider_with_differing_case_duplicates_throws()
{
AssertConfig(BuildConfigRoot(LoadThroughProvider(TestSection.DuplicatesDifferentCaseTestConfig)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<TargetFrameworks>$(StandardTestTfms)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Config\test\Microsoft.Extensions.Configuration.Tests.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.Configuration.CommandLine" />
<Reference Include="Microsoft.Extensions.Configuration.Test.Common" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration.Test;

namespace Microsoft.Extensions.Configuration.EnvironmentVariables.Test
{
public class ConfigurationProviderEnvironmentVariablesTest : ConfigurationProviderTestBase
{
protected override (IConfigurationProvider Provider, Action Initializer) LoadThroughProvider(
TestSection testConfig)
{
var values = new List<KeyValuePair<string, string>>();
SectionToValues(testConfig, "", values);

var provider = new EnvironmentVariablesConfigurationProvider(null);

return (provider, () => provider.Load(new Hashtable(values.ToDictionary(e => e.Key, e => e.Value))));
}

public override void Load_from_single_provider_with_differing_case_duplicates_throws()
{
AssertConfig(BuildConfigRoot(LoadThroughProvider(TestSection.DuplicatesDifferentCaseTestConfig)));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using Microsoft.Extensions.Configuration.Test;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<TargetFrameworks>$(StandardTestTfms)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Config\test\Microsoft.Extensions.Configuration.Tests.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
<Reference Include="Microsoft.Extensions.Configuration.Test.Common" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using System.Text;
using Microsoft.Extensions.Configuration.Test;

namespace Microsoft.Extensions.Configuration.Ini.Test
{
public class ConfigurationProviderIniTest : ConfigurationProviderTestBase
{
protected override (IConfigurationProvider Provider, Action Initializer) LoadThroughProvider(
TestSection testConfig)
{
var iniBuilder = new StringBuilder();
SectionToIni(iniBuilder, "", testConfig);

var provider = new IniConfigurationProvider(
new IniConfigurationSource
{
Optional = true
});

var ini = iniBuilder.ToString();
return (provider, () => provider.Load(TestStreamHelpers.StringToStream(ini)));
}

private void SectionToIni(StringBuilder iniBuilder, string sectionName, TestSection section)
{
foreach (var tuple in section.Values.SelectMany(e => e.Value.Expand(e.Key)))
{
iniBuilder.AppendLine($"{tuple.Key}={tuple.Value}");
}

foreach (var tuple in section.Sections)
{
iniBuilder.AppendLine($"[{sectionName}{tuple.Key}]");
SectionToIni(iniBuilder, tuple.Key + ":", tuple.Section);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<TargetFrameworks>$(StandardTestTfms)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Config\test\Microsoft.Extensions.Configuration.Tests.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.Configuration.Ini" />
<Reference Include="Microsoft.Extensions.Configuration.Test.Common" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using System.Text;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.Configuration.Test;

namespace Microsoft.Extensions.Configuration
{
public class ConfigurationProviderJsonTest : ConfigurationProviderTestBase
{
public override void Load_from_single_provider_with_duplicates_throws()
{
// JSON provider doesn't throw for duplicate values with the same case
AssertConfig(BuildConfigRoot(LoadThroughProvider(TestSection.DuplicatesTestConfig)));
}

protected override (IConfigurationProvider Provider, Action Initializer) LoadThroughProvider(TestSection testConfig)
{
var jsonBuilder = new StringBuilder();
SectionToJson(jsonBuilder, testConfig);

var provider = new JsonConfigurationProvider(
new JsonConfigurationSource
{
Optional = true
});

var json = jsonBuilder.ToString();

return (provider, () => provider.Load(TestStreamHelpers.StringToStream(json)));
}

private void SectionToJson(StringBuilder jsonBuilder, TestSection section)
{
jsonBuilder.AppendLine("{");

foreach (var tuple in section.Values)
{
jsonBuilder.AppendLine(tuple.Value.AsArray != null
? $"'{tuple.Key}': [{string.Join(", ", tuple.Value.AsArray.Select(v => $"'{v}'"))}],"
: $"'{tuple.Key}': '{tuple.Value.AsString}',");
}

foreach (var tuple in section.Sections)
{
jsonBuilder.Append($"'{tuple.Key}': ");
SectionToJson(jsonBuilder, tuple.Section);
}

jsonBuilder.AppendLine("},");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,4 @@ public void ThrowFormatExceptionWhenFileIsEmpty()
var exception = Assert.Throws<FormatException>(() => LoadProvider(@""));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<TargetFrameworks>$(StandardTestTfms)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Config\test\Microsoft.Extensions.Configuration.Tests.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.Configuration.Json" />
<Reference Include="Microsoft.Extensions.Configuration.Test.Common" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration.Test;
using Microsoft.Extensions.FileProviders;

namespace Microsoft.Extensions.Configuration.KeyPerFile.Test
{
public class ConfigurationProviderCommandLineTest : ConfigurationProviderTestBase
{
protected override (IConfigurationProvider Provider, Action Initializer) LoadThroughProvider(
TestSection testConfig)
{
var testFiles = new List<IFileInfo>();
SectionToTestFiles(testFiles, "", testConfig);

var provider = new KeyPerFileConfigurationProvider(
new KeyPerFileConfigurationSource
{
Optional = true,
FileProvider = new TestFileProvider(testFiles.ToArray())
});

return (provider, () => { });
}

private void SectionToTestFiles(List<IFileInfo> testFiles, string sectionName, TestSection section)
{
foreach (var tuple in section.Values.SelectMany(e => e.Value.Expand(e.Key)))
{
testFiles.Add(new TestFile(sectionName + tuple.Key, tuple.Value));
}

foreach (var tuple in section.Sections)
{
SectionToTestFiles(testFiles, sectionName + tuple.Key + "__", tuple.Section);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<TargetFrameworks>$(StandardTestTfms)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Config\test\Microsoft.Extensions.Configuration.Tests.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.Configuration.KeyPerFile" />
</ItemGroup>
Expand Down
Loading