-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConfigurationBuilderTests.cs
More file actions
86 lines (69 loc) · 2.89 KB
/
ConfigurationBuilderTests.cs
File metadata and controls
86 lines (69 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using System.Linq;
using Etcd.Microsoft.Extensions.Configuration.Auth;
using Etcd.Microsoft.Extensions.Configuration.Settings;
using Microsoft.Extensions.Configuration;
using NUnit.Framework;
namespace Etcd.Microsoft.Extensions.Configuration.IntegrationTests;
[TestFixture]
[Category("Integration")]
public class ConfigurationBuilderTests
{
[Test]
public void Build_WithSettingsFromEtcd_ValuesLoaded()
{
// Arrange
var credentials = new Credentials("MyUserName", "passw");
var etcdSettings = new EtcdSettings("http://localhost:2379");
var config = new ConfigurationBuilder()
.AddEtcd(credentials, etcdSettings)
.AddEtcd(credentials, etcdSettings, "MyPrefix")
.AddEtcd(credentials, etcdSettings, "MYCOMPLEX/prefix", "/")
.Build();
// Act
PerformTest(config);
}
[Test]
public void Build_WithSettingsFromEtcdAndCredentialsFromEnvironment_ValuesLoaded()
{
// Arrange
Environment.SetEnvironmentVariable("ETCD_TEST_USERNAME", "MyUserName");
Environment.SetEnvironmentVariable("ETCD_TEST_PASSWORD", "passw");
var credentials = new Credentials("MyUserName", "passw");
var envCredentials = Credentials.WithOverrideFromEnvironmentVariables("foo", "bar", "ETCD_TEST_USERNAME", "ETCD_TEST_PASSWORD");
var envCredentials2 = Credentials.WithOverrideFromEnvironmentVariables("MyUserName", "bar", "ETCD_TEST_PASSWORD");
var etcdSettings = new EtcdSettings("http://localhost:2379");
var config = new ConfigurationBuilder()
.AddEtcd(credentials, etcdSettings)
.AddEtcd(envCredentials, etcdSettings, "MyPrefix")
.AddEtcd(envCredentials2, etcdSettings, "MYCOMPLEX/prefix", "/")
.Build();
// Act
PerformTest(config);
// Assert
Assert.Pass("Credentials info: " + envCredentials.ToString());
}
private static void PerformTest(IConfigurationRoot config)
{
var testSection = config.GetSection("TestSection");
var testSubSection = testSection.GetSection("SubSection");
var list = testSection.GetSection("ArraySection").Get<List<string>>();
var testAppSection = config.GetSection("TestAppSection");
var complexPrefixSection = config.GetSection("Settings");
// Assert
Assert.That(config, Is.Not.Null);
Assert.That(config.GetChildren().Any());
Assert.That(testAppSection.GetChildren().Any());
Assert.That(complexPrefixSection.GetChildren().Any());
Assert.That(testSection["Item1"], Is.EqualTo("Item 1 value"));
Assert.That(testSection["iTem2"], Is.EqualTo("Item 2 value")); // Case insensitive key access
Assert.That(testSubSection["Item1"], Is.EqualTo("Sub section value 1"));
Assert.That(testSubSection["Item2"], Is.EqualTo("Sub section value 2"));
Assert.That(list.Count, Is.EqualTo(2));
Assert.That(list[0], Is.EqualTo("Item 1"));
Assert.That(list[1], Is.EqualTo("Item 2"));
Assert.That(testAppSection["Item1"], Is.EqualTo("1234321"));
Assert.That(complexPrefixSection["TestKey"], Is.EqualTo("Test value"));
}
}