Skip to content

Commit ed2d7b2

Browse files
Mpdreamzrusscam
authored andcommitted
build.bat cluster now picks up the seed from test(.default).yaml, this prevents unexpected results when running tests from the IDE which is the primary usecase for
(cherry picked from commit 47997f3)
1 parent 9cbf143 commit ed2d7b2

File tree

5 files changed

+86
-45
lines changed

5 files changed

+86
-45
lines changed

build/scripts/Targets.fsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ open Commandline
3333
open Differ
3434
open Differ.Differ
3535
open Fake.IO
36+
open Octokit
3637

3738
Commandline.parse()
3839

@@ -107,14 +108,22 @@ Target "Cluster" <| fun _ ->
107108
let clusterVersion = getBuildParam "clusterVersion"
108109
let testsProjectDirectory = Path.Combine(Path.GetFullPath(Paths.Output("Tests.ClusterLauncher")), "netcoreapp2.1")
109110
let tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
111+
112+
let sourceDir = Paths.Source("Tests/Tests.Configuration");
113+
let defaultYaml = Path.Combine(sourceDir, "tests.default.yaml");
114+
let userYaml = Path.Combine(sourceDir, "tests.yaml");
115+
let e f = File.Exists f;
116+
match ((e userYaml), (e defaultYaml)) with
117+
| (true, _) -> setProcessEnvironVar "NEST_YAML_FILE" (Path.GetFullPath(userYaml))
118+
| (_, true) -> setProcessEnvironVar "NEST_YAML_FILE" (Path.GetFullPath(defaultYaml))
119+
| _ -> ignore()
120+
110121
Shell.copyDir tempDir testsProjectDirectory (fun s -> true)
111-
trace testsProjectDirectory
112-
trace tempDir
113122
let command = sprintf "%s %s" clusterName clusterVersion
114123
DotNetCli.RunCommand(fun p ->
115124
{ p with
116125
WorkingDir = tempDir;
117-
TimeOut = TimeSpan.FromMinutes(60.)
126+
TimeOut = TimeSpan.FromMinutes(120.)
118127
}) (sprintf "%s %s" (Path.Combine(tempDir, "Tests.ClusterLauncher.dll")) command)
119128

120129
Shell.deleteDir tempDir

src/Tests/Tests.ClusterLauncher/ClusterLaunchProgram.cs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5+
using System.Runtime.CompilerServices;
6+
using Elastic.Managed;
57
using Elastic.Managed.Ephemeral;
68
using FluentAssertions.Common;
9+
using Tests.Configuration;
710
using Tests.Core.ManagedElasticsearch.Clusters;
811

912
namespace Tests.ClusterLauncher
@@ -23,48 +26,62 @@ public static int Main(string[] arguments)
2326
Environment.SetEnvironmentVariable("NEST_INTEGRATION_VERSION", arguments[1], EnvironmentVariableTarget.Process);
2427
Environment.SetEnvironmentVariable("NEST_INTEGRATION_SHOW_OUTPUT_AFTER_START", "1", EnvironmentVariableTarget.Process);
2528

29+
2630
var cluster = GetClusters().FirstOrDefault(c => c.Name.StartsWith(clusterName, StringComparison.OrdinalIgnoreCase));
2731
if (cluster == null)
2832
{
2933
Console.Error.WriteLine($"No cluster found that starts with '{clusterName}");
3034
return 4;
3135
}
3236

37+
//best effort, wont catch all the things
38+
//https://github.com/dotnet/coreclr/issues/8565
39+
//Don't want to make this windows only by registering a SetConsoleCtrlHandler though P/Invoke.
40+
AppDomain.CurrentDomain.ProcessExit += (s, ev) => Instance?.Dispose();
41+
Console.CancelKeyPress += (s, ev) => Instance?.Dispose();
42+
3343
if (!TryStartClientTestClusterBaseImplementation(cluster) && !TryStartXPackClusterImplementation(cluster))
3444
{
3545
Console.Error.WriteLine($"Could not create an instance of '{cluster.FullName}");
3646
return 1;
3747
}
3848
return 0;
3949
}
50+
51+
private static ICluster<EphemeralClusterConfiguration> Instance { get; set; }
52+
4053
private static bool TryStartXPackClusterImplementation(Type cluster)
4154
{
4255
if (!(Activator.CreateInstance(cluster) is XPackCluster instance)) return false;
56+
Instance = instance;
4357
using (instance)
44-
{
45-
instance.Start();
46-
Console.WriteLine("Press any key to shutdown the running cluster");
47-
Console.ReadKey();
48-
instance.Dispose();
49-
}
50-
51-
return true;
58+
return Run(instance);
5259
}
5360

61+
5462
private static bool TryStartClientTestClusterBaseImplementation(Type cluster)
5563
{
5664
if (!(Activator.CreateInstance(cluster) is ClientTestClusterBase instance)) return false;
65+
Instance = instance;
5766
using (instance)
67+
return Run(instance);
68+
}
69+
70+
private static bool Run(ICluster<EphemeralClusterConfiguration> instance)
71+
{
72+
TestConfiguration.Instance.DumpConfiguration();
73+
instance.Start();
74+
if (!instance.Started)
5875
{
59-
instance.Start();
60-
Console.WriteLine("Press any key to shutdown the running cluster");
61-
Console.ReadKey();
62-
instance.Dispose();
76+
Console.Error.WriteLine($"Failed to start cluster: '{instance.GetType().FullName}");
77+
return false;
6378
}
79+
Console.WriteLine("Press any key to shutdown the running cluster");
80+
var c = default(ConsoleKeyInfo);
81+
while (c.Key != ConsoleKey.Q) c = Console.ReadKey();
6482
return true;
6583
}
6684

67-
6885
private static Type[] GetClusters()
6986
{
7087
IEnumerable<Type> types;

src/Tests/Tests.Configuration/ConfigurationLoader.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.IO;
34
using System.Threading;
45

@@ -16,7 +17,18 @@ private static ITestConfiguration LoadConfiguration()
1617
// The build script sets a FAKEBUILD env variable, so if it exists then
1718
// we must be running tests from the build script
1819
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("FAKEBUILD")))
20+
{
21+
var yamlFile = Environment.GetEnvironmentVariable("NEST_YAML_FILE");
22+
if (!string.IsNullOrWhiteSpace(yamlFile) && File.Exists(yamlFile))
23+
{
24+
//load the test seed from the explicitly passed yaml file when running from FAKE
25+
var tempYamlConfiguration = new YamlConfiguration(yamlFile);
26+
Environment.SetEnvironmentVariable("NEST_TEST_SEED", tempYamlConfiguration.Seed.ToString(CultureInfo.InvariantCulture), EnvironmentVariableTarget.Process);
27+
Console.WriteLine("--->" + tempYamlConfiguration.Seed);
28+
}
1929
return new EnvironmentConfiguration();
30+
}
31+
2032

2133
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());
2234
var testsConfigurationFolder = FindTestsConfigurationFolder(directory);

src/Tests/Tests.Configuration/ITestConfiguration.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace Tests.Configuration
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Tests.Configuration
25
{
36
public interface ITestConfiguration
47
{
@@ -23,4 +26,30 @@ public class RandomConfiguration
2326
public bool SourceSerializer { get; set; }
2427
public bool TypedKeys { get; set; }
2528
}
29+
30+
public static class TestConfigurationExtensions
31+
{
32+
public static void DumpConfiguration(this ITestConfiguration config)
33+
{
34+
Console.WriteLine(new string('-', 20));
35+
Console.WriteLine("Starting tests using config:");
36+
Console.WriteLine($" - {nameof(config.TestAgainstAlreadyRunningElasticsearch)}: {config.TestAgainstAlreadyRunningElasticsearch}");
37+
Console.WriteLine($" - {nameof(config.ElasticsearchVersion)}: {config.ElasticsearchVersion}");
38+
Console.WriteLine($" - {nameof(config.ForceReseed)}: {config.ForceReseed}");
39+
Console.WriteLine($" - {nameof(config.Mode)}: {config.Mode}");
40+
Console.WriteLine($" - {nameof(config.Seed)}: {config.Seed}");
41+
if (config.Mode == TestMode.Integration)
42+
{
43+
Console.WriteLine($" - {nameof(config.ClusterFilter)}: {config.ClusterFilter}");
44+
Console.WriteLine($" - {nameof(config.TestFilter)}: {config.TestFilter}");
45+
46+
}
47+
Console.WriteLine($" - {nameof(config.RunIntegrationTests)}: {config.RunIntegrationTests}");
48+
Console.WriteLine($" - {nameof(config.RunUnitTests)}: {config.RunUnitTests}");
49+
Console.WriteLine($" - Random:");
50+
Console.WriteLine($" \t- {nameof(config.Random.SourceSerializer)}: {config.Random.SourceSerializer}");
51+
Console.WriteLine($" \t- {nameof(config.Random.TypedKeys)}: {config.Random.TypedKeys}");
52+
Console.WriteLine(new string('-', 20));
53+
}
54+
}
2655
}

src/Tests/Tests.Core/Xunit/NestXunitRunOptions.cs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,7 @@ public NestXunitRunOptions()
2424
Generators.Initialize();
2525
}
2626

27-
public override void OnBeforeTestsRun() => DumpConfiguration();
28-
29-
private static void DumpConfiguration()
30-
{
31-
var config = TestConfiguration.Instance;
32-
33-
Console.WriteLine(new string('-', 20));
34-
Console.WriteLine("Starting tests using config:");
35-
Console.WriteLine($" - {nameof(config.TestAgainstAlreadyRunningElasticsearch)}: {config.TestAgainstAlreadyRunningElasticsearch}");
36-
Console.WriteLine($" - {nameof(config.ElasticsearchVersion)}: {config.ElasticsearchVersion}");
37-
Console.WriteLine($" - {nameof(config.ForceReseed)}: {config.ForceReseed}");
38-
Console.WriteLine($" - {nameof(config.Mode)}: {config.Mode}");
39-
Console.WriteLine($" - {nameof(config.Seed)}: {config.Seed}");
40-
if (config.Mode == TestMode.Integration)
41-
{
42-
Console.WriteLine($" - {nameof(config.ClusterFilter)}: {config.ClusterFilter}");
43-
Console.WriteLine($" - {nameof(config.TestFilter)}: {config.TestFilter}");
44-
45-
}
46-
Console.WriteLine($" - {nameof(config.RunIntegrationTests)}: {config.RunIntegrationTests}");
47-
Console.WriteLine($" - {nameof(config.RunUnitTests)}: {config.RunUnitTests}");
48-
Console.WriteLine($" - Random:");
49-
Console.WriteLine($" \t- {nameof(config.Random.SourceSerializer)}: {config.Random.SourceSerializer}");
50-
Console.WriteLine($" \t- {nameof(config.Random.TypedKeys)}: {config.Random.TypedKeys}");
51-
Console.WriteLine(new string('-', 20));
52-
53-
}
27+
public override void OnBeforeTestsRun() => TestConfiguration.Instance.DumpConfiguration();
5428

5529
public override void OnTestsFinished(Dictionary<string, Stopwatch> clusterTotals, ConcurrentBag<Tuple<string, string>> failedCollections)
5630
{
@@ -109,7 +83,7 @@ private static void DumpReproduceFilters(ConcurrentBag<Tuple<string, string>> fa
10983

11084
private static string ReproduceCommandLine(ConcurrentBag<Tuple<string, string>> failedCollections, ITestConfiguration config, bool runningIntegrations)
11185
{
112-
var sb = new StringBuilder("build ")
86+
var sb = new StringBuilder("build.bat ")
11387
.Append($"seed:{config.Seed} ");
11488

11589
AppendExplictConfig(nameof(RandomConfiguration.SourceSerializer), sb);

0 commit comments

Comments
 (0)