示例#1
0
        private async Task WhenISendACommandIntoTheCluster(UpdateFileConfiguration command)
        {
            async Task <bool> SendCommand()
            {
                try
                {
                    var p    = _peers.Peers.First();
                    var json = JsonConvert.SerializeObject(command, new JsonSerializerSettings()
                    {
                        TypeNameHandling = TypeNameHandling.All
                    });
                    var httpContent = new StringContent(json);
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    using (var httpClient = new HttpClient())
                    {
                        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
                        var response = await httpClient.PostAsync($"{p.HostAndPort}/administration/raft/command", httpContent);

                        response.EnsureSuccessStatusCode();
                        var content = await response.Content.ReadAsStringAsync();

                        var errorResult = JsonConvert.DeserializeObject <ErrorResponse <UpdateFileConfiguration> >(content);

                        if (!string.IsNullOrEmpty(errorResult.Error))
                        {
                            return(false);
                        }

                        var okResult = JsonConvert.DeserializeObject <OkResponse <UpdateFileConfiguration> >(content);

                        if (okResult.Command.Configuration.ReRoutes.Count == 2)
                        {
                            return(true);
                        }
                    }

                    return(false);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return(false);
                }
            }

            var commandSent = await Wait.WaitFor(40000).Until(async() =>
            {
                var result = await SendCommand();
                Thread.Sleep(1000);
                return(result);
            });

            commandSent.ShouldBeTrue();
        }
示例#2
0
        private async Task GivenIHaveAnOcelotToken(string adminPath)
        {
            async Task <bool> AddToken()
            {
                try
                {
                    var tokenUrl = $"{adminPath}/connect/token";
                    var formData = new List <KeyValuePair <string, string> >
                    {
                        new KeyValuePair <string, string>("client_id", "admin"),
                        new KeyValuePair <string, string>("client_secret", "secret"),
                        new KeyValuePair <string, string>("scope", "admin"),
                        new KeyValuePair <string, string>("grant_type", "client_credentials")
                    };
                    var content = new FormUrlEncodedContent(formData);

                    var response = await _httpClient.PostAsync(tokenUrl, content);

                    var responseContent = await response.Content.ReadAsStringAsync();

                    if (!response.IsSuccessStatusCode)
                    {
                        return(false);
                    }

                    _token = JsonConvert.DeserializeObject <BearerToken>(responseContent);
                    var configPath = $"{adminPath}/.well-known/openid-configuration";
                    response = await _httpClient.GetAsync(configPath);

                    return(response.IsSuccessStatusCode);
                }
                catch (Exception)
                {
                    return(false);
                }
            }

            var addToken = await Wait.WaitFor(40000).Until(async() =>
            {
                var result = await AddToken();
                Thread.Sleep(1000);
                return(result);
            });

            addToken.ShouldBeTrue();
        }
示例#3
0
        private async Task WhenIPostOnTheApiGateway(string url, FileConfiguration updatedConfiguration)
        {
            async Task <bool> SendCommand()
            {
                var json = JsonConvert.SerializeObject(updatedConfiguration);

                var content = new StringContent(json);

                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                _response = await _httpClient.PostAsync(url, content);

                var responseContent = await _response.Content.ReadAsStringAsync();

                if (responseContent == "There was a problem. This error message sucks raise an issue in GitHub.")
                {
                    return(false);
                }

                if (string.IsNullOrEmpty(responseContent))
                {
                    return(false);
                }

                return(_response.IsSuccessStatusCode);
            }

            var commandSent = await Wait.WaitFor(40000).Until(async() =>
            {
                var result = await SendCommand();
                Thread.Sleep(1000);
                return(result);
            });

            commandSent.ShouldBeTrue();
        }
示例#4
0
        private async Task ThenTheCommandIsReplicatedToAllStateMachines(UpdateFileConfiguration expecteds)
        {
            async Task <bool> CommandCalledOnAllStateMachines()
            {
                try
                {
                    var passed = 0;
                    foreach (var peer in _peers.Peers)
                    {
                        var path = $"{peer.HostAndPort.Replace("/", "").Replace(":", "")}.db";
                        using (var connection = new SqliteConnection($"Data Source={path};"))
                        {
                            connection.Open();
                            var sql = @"select count(id) from logs";
                            using (var command = new SqliteCommand(sql, connection))
                            {
                                var index = Convert.ToInt32(command.ExecuteScalar());
                                index.ShouldBe(1);
                            }
                        }

                        _httpClientForAssertions.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
                        var result = await _httpClientForAssertions.GetAsync($"{peer.HostAndPort}/administration/configuration");

                        var json = await result.Content.ReadAsStringAsync();

                        var response = JsonConvert.DeserializeObject <FileConfiguration>(json, new JsonSerializerSettings {
                            TypeNameHandling = TypeNameHandling.All
                        });
                        response.GlobalConfiguration.RequestIdKey.ShouldBe(expecteds.Configuration.GlobalConfiguration.RequestIdKey);
                        response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expecteds.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Host);
                        response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expecteds.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Port);

                        for (var i = 0; i < response.ReRoutes.Count; i++)
                        {
                            for (var j = 0; j < response.ReRoutes[i].DownstreamHostAndPorts.Count; j++)
                            {
                                var res      = response.ReRoutes[i].DownstreamHostAndPorts[j];
                                var expected = expecteds.Configuration.ReRoutes[i].DownstreamHostAndPorts[j];
                                res.Host.ShouldBe(expected.Host);
                                res.Port.ShouldBe(expected.Port);
                            }

                            response.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expecteds.Configuration.ReRoutes[i].DownstreamPathTemplate);
                            response.ReRoutes[i].DownstreamScheme.ShouldBe(expecteds.Configuration.ReRoutes[i].DownstreamScheme);
                            response.ReRoutes[i].UpstreamPathTemplate.ShouldBe(expecteds.Configuration.ReRoutes[i].UpstreamPathTemplate);
                            response.ReRoutes[i].UpstreamHttpMethod.ShouldBe(expecteds.Configuration.ReRoutes[i].UpstreamHttpMethod);
                        }

                        passed++;
                    }

                    return(passed == 5);
                }
                catch (Exception e)
                {
                    //_output.WriteLine($"{e.Message}, {e.StackTrace}");
                    Console.WriteLine(e);
                    return(false);
                }
            }

            var commandOnAllStateMachines = await Wait.WaitFor(40000).Until(async() =>
            {
                var result = await CommandCalledOnAllStateMachines();
                Thread.Sleep(1000);
                return(result);
            });

            commandOnAllStateMachines.ShouldBeTrue();
        }