Example #1
0
    public async Task BuildConfig_TwoDistinctConfigs_Works()
    {
        const string TestAddress = "https://localhost:123/";

        var cluster1 = new ClusterConfig
        {
            ClusterId    = "cluster1",
            Destinations = new Dictionary <string, DestinationConfig>(StringComparer.OrdinalIgnoreCase)
            {
                { "d1", new DestinationConfig {
                      Address = TestAddress
                  } }
            }
        };
        var route1 = new RouteConfig
        {
            RouteId   = "route1",
            ClusterId = "cluster1",
            Match     = new RouteMatch {
                Path = "/"
            }
        };

        var cluster2 = new ClusterConfig
        {
            ClusterId    = "cluster2",
            Destinations = new Dictionary <string, DestinationConfig>(StringComparer.OrdinalIgnoreCase)
            {
                { "d2", new DestinationConfig {
                      Address = TestAddress
                  } }
            }
        };
        var route2 = new RouteConfig
        {
            RouteId   = "route2",
            ClusterId = "cluster2",
            Match     = new RouteMatch {
                Path = "/"
            }
        };

        var config1 = new InMemoryConfigProvider(new List <RouteConfig>()
        {
            route1
        }, new List <ClusterConfig>()
        {
            cluster1
        });
        var config2 = new InMemoryConfigProvider(new List <RouteConfig>()
        {
            route2
        }, new List <ClusterConfig>()
        {
            cluster2
        });

        var services = CreateServices(new[] { config1, config2 });

        var manager    = services.GetRequiredService <ProxyConfigManager>();
        var dataSource = await manager.InitialLoadAsync();

        Assert.NotNull(dataSource);
        var endpoints = dataSource.Endpoints;

        Assert.Equal(2, endpoints.Count);

        // The order is unstable because routes are stored in a dictionary.
        var routeConfig = endpoints.Single(e => string.Equals(e.DisplayName, "route1")).Metadata.GetMetadata <RouteModel>();

        Assert.NotNull(routeConfig);
        Assert.Equal("route1", routeConfig.Config.RouteId);

        var clusterState = routeConfig.Cluster;

        Assert.NotNull(clusterState);

        Assert.Equal("cluster1", clusterState.ClusterId);
        Assert.NotNull(clusterState.Destinations);
        Assert.NotNull(clusterState.Model);
        Assert.NotNull(clusterState.Model.HttpClient);
        Assert.Same(clusterState, routeConfig.Cluster);

        var actualDestinations = clusterState.Destinations.Values;
        var destination        = Assert.Single(actualDestinations);

        Assert.Equal("d1", destination.DestinationId);
        Assert.NotNull(destination.Model);
        Assert.Equal(TestAddress, destination.Model.Config.Address);

        routeConfig = endpoints.Single(e => string.Equals(e.DisplayName, "route2")).Metadata.GetMetadata <RouteModel>();
        Assert.NotNull(routeConfig);
        Assert.Equal("route2", routeConfig.Config.RouteId);

        clusterState = routeConfig.Cluster;
        Assert.NotNull(clusterState);

        Assert.Equal("cluster2", clusterState.ClusterId);
        Assert.NotNull(clusterState.Destinations);
        Assert.NotNull(clusterState.Model);
        Assert.NotNull(clusterState.Model.HttpClient);
        Assert.Same(clusterState, routeConfig.Cluster);

        actualDestinations = clusterState.Destinations.Values;
        destination        = Assert.Single(actualDestinations);
        Assert.Equal("d2", destination.DestinationId);
        Assert.NotNull(destination.Model);
        Assert.Equal(TestAddress, destination.Model.Config.Address);
    }