Created
November 8, 2016 11:09
-
-
Save pauldambra/aef5f2e6694c1dc4b41b2f02fee56412 to your computer and use it in GitHub Desktop.
Setting up for ElasticSearch Integration tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace NCrunch.Framework | |
{ | |
public abstract class ResourceUsageAttribute : Attribute | |
{ | |
private readonly string[] _resourceNames; | |
public ResourceUsageAttribute(params string[] resourceName) | |
{ | |
_resourceNames = resourceName; | |
} | |
public string[] ResourceNames | |
{ | |
get { return _resourceNames; } | |
} | |
} | |
public class ExclusivelyUsesAttribute : ResourceUsageAttribute | |
{ | |
public ExclusivelyUsesAttribute(params string[] resourceName) | |
: base(resourceName) { } | |
} | |
} | |
namespace ElasticSearch.Integration.Tests | |
{ | |
using System; | |
using System.Diagnostics; | |
using System.Net.Http; | |
using System.Threading; | |
using Newtonsoft.Json; | |
using NUnit.Framework; | |
[SetUpFixture] | |
public class ElasticSearchIntegration : IDisposable | |
{ | |
private Process _process; | |
private static readonly HttpClient HttpClient = new HttpClient(); | |
private readonly string _elasticSearchDir = (Environment.GetEnvironmentVariable("ELASTIC_HOME") ?? @"C:\elasticsearch\elasticsearch-local-1.4.2").TrimEnd('\\'); | |
[SetUp] | |
public void RunBeforeAnyTests() | |
{ | |
string batchFile = string.Format(@"{0}\bin\elasticsearch.bat", _elasticSearchDir); | |
try | |
{ | |
_process = new Process | |
{ | |
StartInfo = | |
{ | |
FileName = batchFile, | |
WindowStyle = ProcessWindowStyle.Hidden, | |
RedirectStandardInput = true, | |
UseShellExecute = false | |
} | |
}; | |
// Configure the process using the StartInfo properties. | |
_process.Start(); | |
PingClusterTillGreen(); | |
} | |
catch (Exception ex) | |
{ | |
var m = string.Format("exception while loading elastic search from {0} :( --- {1}", batchFile, ex.Message); | |
var wrappedException = new Exception(m, ex); | |
throw wrappedException; | |
} | |
} | |
public static void DeleteAllElasticData(string index = "_all") | |
{ | |
//curl -XDELETE 'http://localhost:9200/_all' and wait | |
HttpClient.DeleteAsync("http://localhost:9200/" + index).Wait(); | |
} | |
private void PingClusterTillGreen() | |
{ | |
var clusterGreen = false; | |
var attempts = 0; | |
while (!clusterGreen && attempts < 10) | |
{ | |
Thread.Sleep(1000); | |
string c = GetClusterStatus(); | |
Console.WriteLine(c); | |
clusterGreen = JsonConvert.DeserializeObject<dynamic>(c)["status"] == "green"; | |
attempts++; | |
} | |
} | |
private string GetClusterStatus() | |
{ | |
try | |
{ | |
var response = HttpClient.GetAsync("http://localhost:9200/_cluster/health?pretty=true").Result; | |
var c = response.Content.ReadAsStringAsync().Result; | |
return c; | |
} | |
catch (Exception) | |
{ | |
//swallow | |
return "{\"status\": \"red\"}"; | |
} | |
} | |
[TearDown] | |
public void RunAfterAnyTests() | |
{ | |
Debug.WriteLine("Running After Tests to dispose of Elastic Search"); | |
_process.StandardInput.WriteLine("\x3"); //send ctrl + c | |
_process.StandardInput.Close(); | |
Dispose(); | |
} | |
public void Dispose() | |
{ | |
if (_process == null) | |
{ | |
return; | |
} | |
//this may just need a call to dispose | |
if (!_process.HasExited) | |
{ | |
_process.CloseMainWindow(); | |
_process.Close(); | |
} | |
_process.Dispose(); | |
_process = null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ElasticSearch.Integration.Tests | |
{ | |
using FluentAssertions; | |
using NCrunch.Framework; | |
using Nest; | |
using NUnit.Framework; | |
//stops ncrunch running these elastic tests in parallel and trying to start many instances of the server | |
[ExclusivelyUses("ElasticSearch")] | |
public class ExampleTest | |
{ | |
private ElasticClient _client; | |
private class TestThing | |
{ | |
public Guid Uniqueness { get; private set; } | |
public TestThing(Guid uniqueness) | |
{ | |
Uniqueness = uniqueness; | |
} | |
} | |
[SetUp] | |
public void Before() | |
{ | |
//defaults to http://localhost:9200 | |
ConnectionSettings connectionSettings = new ConnectionSettings(); | |
//each test could write to its own index for isolation | |
connectionSettings.SetDefaultIndex("test-index"); | |
ElasticSearchIntegration.DeleteAllElasticData("test-index"); | |
_client = new ElasticClient(connectionSettings); | |
} | |
[Test] | |
public void Hello() | |
{ | |
//arrange | |
var guid = Guid.NewGuid(); | |
_client.Index(new TestThing(guid), id => id.Refresh()); | |
//act | |
var results = _client.Search<TestThing>(sd => sd); | |
//assert | |
results.ConnectionStatus.Success.Should().BeTrue(); | |
results.Documents.Single().Uniqueness.Should().Be(guid); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment