Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/test version config #3649

Merged
merged 4 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions build/scripts/Building.fs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
namespace Scripts

open System
open System.IO

open FSharp.Data

open Paths
open Projects
open Tooling
Expand Down
10 changes: 0 additions & 10 deletions build/scripts/Cluster.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

open System
open System.IO
open Fake.Core
open Fake.IO
open Commandline

Expand All @@ -15,15 +14,6 @@ module Cluster =
let testsProjectDirectory = Path.Combine(Path.GetFullPath(Paths.Output("Tests.ClusterLauncher")), "netcoreapp2.1")
let tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

let sourceDir = Paths.Source("Tests/Tests.Configuration");
let defaultYaml = Path.Combine(sourceDir, "tests.default.yaml");
let userYaml = Path.Combine(sourceDir, "tests.yaml");
let e f = File.Exists f;
match ((e userYaml), (e defaultYaml)) with
| (true, _) -> Environment.setEnvironVar "NEST_YAML_FILE" (Path.GetFullPath(userYaml))
| (_, true) -> Environment.setEnvironVar "NEST_YAML_FILE" (Path.GetFullPath(defaultYaml))
| _ -> failwithf "Expected to find a tests.default.yaml or tests.yaml in %s" sourceDir

printfn "%s" testsProjectDirectory

Shell.copyDir tempDir testsProjectDirectory (fun s -> true)
Expand Down
1 change: 0 additions & 1 deletion build/scripts/Paths.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ module Paths =
let Output(folder) = sprintf "%s/%s" BuildOutput folder
let Source(folder) = sprintf "%s/%s" SourceFolder folder
let TestsSource(folder) = sprintf "%s/Tests/%s" SourceFolder folder
let Build(folder) = sprintf "%s/%s" BuildFolder folder

let ProjFile(project:DotNetProject) =
match project with
Expand Down
25 changes: 22 additions & 3 deletions build/scripts/Targets.fs
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
namespace Scripts

open System
open System.IO

open Paths
open Build
open Commandline
open Bullseye
open ProcNet
open Fake.Core

module Main =

let private target name action = Targets.Target(name, new Action(action))
let private skip name = printfn "SKIPPED target '%s' evaluated not to run" name |> ignore
let private conditional optional name action = target name (if optional then action else (fun _ -> skip name))
let private command name dependencies action = Targets.Target(name, dependencies, new Action(action))

/// <summary>Sets command line environments indicating we are building from the command line</summary>
let setCommandLineEnvVars () =
Environment.setEnvironVar"NEST_COMMAND_LINE_BUILD" "1"

let sourceDir = Paths.Source("Tests/Tests.Configuration");
let defaultYaml = Path.Combine(sourceDir, "tests.default.yaml");
let userYaml = Path.Combine(sourceDir, "tests.yaml");
let e f = File.Exists f;
match ((e userYaml), (e defaultYaml)) with
| (true, _) -> Environment.setEnvironVar "NEST_YAML_FILE" (Path.GetFullPath(userYaml))
| (_, true) -> Environment.setEnvironVar "NEST_YAML_FILE" (Path.GetFullPath(defaultYaml))
| _ -> failwithf "Expected to find a tests.default.yaml or tests.yaml in %s" sourceDir


let [<EntryPoint>] main args =


setCommandLineEnvVars ()

let parsed = Commandline.parse (args |> Array.toList)

let buildVersions = Versioning.BuildVersioning parsed
Expand All @@ -39,7 +58,7 @@ module Main =

conditional (not parsed.SkipDocs) "documentation" <| fun _ -> Documentation.Generate parsed

conditional (not parsed.SkipTests) "test" <| fun _ -> Tests.RunUnitTests parsed
conditional (not parsed.SkipTests) "test" <| fun _ -> Tests.RunUnitTests parsed |> ignore

target "version" <| fun _ -> printfn "Artifacts Version: %O" artifactsVersion

Expand All @@ -50,7 +69,7 @@ module Main =
target "test-nuget-package" <| fun _ ->
//run release unit tests puts packages in the system cache prevent this from happening locally
if not Commandline.runningOnCi then ignore ()
else Tests.RunReleaseUnitTests artifactsVersion
else Tests.RunReleaseUnitTests artifactsVersion |> ignore

target "nuget-pack" <| fun _ -> Release.NugetPack artifactsVersion

Expand Down
7 changes: 3 additions & 4 deletions build/scripts/Testing.fs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace Scripts

open System
open Tooling
open Commandline
open Versioning
open Fake.Core
open System.IO
open Commandline
open Versioning

module Tests =

Expand All @@ -24,7 +23,6 @@ module Tests =
env "NEST_INTEGRATION_CLUSTER" clusterFilter
env "NEST_TEST_FILTER" testFilter
env "NEST_TEST_SEED" (Some <| args.Seed)
env "NEST_COMMAND_LINE_BUILD" <| Some "1"

for random in args.RandomArguments do
let tokens = random.Split [|':'|]
Expand Down Expand Up @@ -78,5 +76,6 @@ module Tests =
| None -> failwith "No versions specified to run integration tests against"
| Some esVersions ->
for esVersion in esVersions do
Environment.setEnvironVar "NEST_INTEGRATION_TEST" "1"
Environment.setEnvironVar "NEST_INTEGRATION_VERSION" esVersion
dotnetTest args.MultiTarget |> ignore
1 change: 1 addition & 0 deletions build/scripts/Tooling.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module Tooling =
type BuildTooling(timeout, path) =
let timeout = match timeout with | Some t -> t | None -> defaultTimeout
member this.Path = path
member this.ReadInWithTimeout workingDirectory arguments timeout = readInWithTimeout timeout (Some workingDirectory) this.Path arguments
member this.ExecInWithTimeout workingDirectory arguments timeout = execInWithTimeout timeout (Some workingDirectory) this.Path arguments
member this.ExecWithTimeout arguments timeout = execInWithTimeout timeout None this.Path arguments
member this.ExecIn workingDirectory arguments = this.ExecInWithTimeout workingDirectory arguments timeout
Expand Down
1 change: 0 additions & 1 deletion src/CodeGeneration/DocGenerator/DocGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<VersionPrefix>7.0.0</VersionPrefix>
<VersionSuffix>alpha</VersionSuffix>
<NoWarn>NU1701,NU1605</NoWarn>
</PropertyGroup>
Expand Down
15 changes: 14 additions & 1 deletion src/Tests/Tests.ClusterLauncher/ClusterLaunchProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,23 @@ public static int Main(string[] arguments)
return 3;
}

// Force TestConfiguration to load as if started from the command line even if we are actually starting
// from the IDE. Also force configuration mode to integration test so the seeders run
Environment.SetEnvironmentVariable("NEST_COMMAND_LINE_BUILD", "1", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("NEST_INTEGRATION_TEST", "1", EnvironmentVariableTarget.Process);

if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("NEST_YAML_FILE")))
{
// build always sets previous argument, assume we are running from the IDE or dotnet run
var yamlFile = TestConfiguration.LocateTestYamlFile();
Environment.SetEnvironmentVariable("NEST_YAML_FILE", yamlFile, EnvironmentVariableTarget.Process);
}

// if version is passed this will take precedence over the version in the yaml file
// in the constructor of EnvironmentConfiguration
var clusterName = arguments[0];
if (arguments.Length > 1)
Environment.SetEnvironmentVariable("NEST_INTEGRATION_VERSION", arguments[1], EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("NEST_INTEGRATION_SHOW_OUTPUT_AFTER_START", "1", EnvironmentVariableTarget.Process);

var cluster = clusters.FirstOrDefault(c => c.Name.StartsWith(clusterName, StringComparison.OrdinalIgnoreCase));
if (cluster == null)
Expand Down
62 changes: 39 additions & 23 deletions src/Tests/Tests.Configuration/ConfigurationLoader.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,64 @@
using System;
using System.Globalization;
using System.IO;
using System.Threading;

namespace Tests.Configuration
{
public static class TestConfiguration
{
private static readonly Lazy<ITestConfiguration> Lazy
= new Lazy<ITestConfiguration>(LoadConfiguration, LazyThreadSafetyMode.ExecutionAndPublication);
private static readonly Lazy<TestConfigurationBase> Lazy
= new Lazy<TestConfigurationBase>(LoadConfiguration, LazyThreadSafetyMode.ExecutionAndPublication);

public static ITestConfiguration Instance => Lazy.Value;
public static TestConfigurationBase Instance => Lazy.Value;

private static ITestConfiguration LoadConfiguration()
private static TestConfigurationBase LoadConfiguration() =>
!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("NEST_COMMAND_LINE_BUILD"))
? (TestConfigurationBase)LoadCommandLineConfiguration()
: LoadYamlConfiguration();

/// <summary>
/// Loads configuration by reading from the yaml and overriding specific configuration settings through
/// environment variables set by the command line build.
/// </summary>
private static EnvironmentConfiguration LoadCommandLineConfiguration()
{
// The build script sets a NEST_COMMAND_LINE_BUILD env variable, so if it exists then
// we must be running tests from the build script
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("NEST_COMMAND_LINE_BUILD")))
{
var yamlFile = Environment.GetEnvironmentVariable("NEST_YAML_FILE");
if (!string.IsNullOrWhiteSpace(yamlFile) && File.Exists(yamlFile))
{
//load the test seed from the explicitly passed yaml file when running from FAKE
var tempYamlConfiguration = new YamlConfiguration(yamlFile);
return new EnvironmentConfiguration(tempYamlConfiguration);
}
return new EnvironmentConfiguration();
}
var yamlFile = Environment.GetEnvironmentVariable("NEST_YAML_FILE");
if (string.IsNullOrWhiteSpace(yamlFile))
throw new Exception("expected NEST_YAML_FILE to be set when calling build.bat or build.sh");
if (!File.Exists(yamlFile))
throw new Exception($"expected {yamlFile} to exist on disk NEST_YAML_FILE seems misconfigured");

//load the test seed from the explicitly passed yaml file when running from FAKE
var tempYamlConfiguration = new YamlConfiguration(yamlFile);
return new EnvironmentConfiguration(tempYamlConfiguration);
}

public static string LocateTestYamlFile()
{
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());
var testsConfigurationFolder = FindTestsConfigurationFolder(directory);
if (testsConfigurationFolder == null)
throw new Exception($"Tried to locate a parent test folder starting from pwd:{directory.FullName}");

var localYamlFile = Path.Combine(testsConfigurationFolder.FullName, "tests.yaml");
if (File.Exists(localYamlFile))
return new YamlConfiguration(localYamlFile);

var defaultYamlFile = Path.Combine(testsConfigurationFolder.FullName, "tests.default.yaml");
if (File.Exists(defaultYamlFile))
return new YamlConfiguration(defaultYamlFile);
if (File.Exists(localYamlFile)) return localYamlFile;

if (File.Exists(defaultYamlFile)) return defaultYamlFile;

throw new Exception($"Tried to load a yaml file from {testsConfigurationFolder.FullName}");

}

/// <summary>
/// The test configuration loaded when you run the tests
/// <para> - from the IDE </para>
/// <para> - when calling dotnet test in the tests directory </para>
/// </summary>
private static YamlConfiguration LoadYamlConfiguration()
{
var yamlFile = LocateTestYamlFile();
return new YamlConfiguration(yamlFile);
}

private static DirectoryInfo FindTestsConfigurationFolder(DirectoryInfo directoryInfo)
Expand Down
38 changes: 12 additions & 26 deletions src/Tests/Tests.Configuration/EnvironmentConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,33 @@ namespace Tests.Configuration
{
public class EnvironmentConfiguration : TestConfigurationBase
{
public EnvironmentConfiguration(YamlConfiguration tempYamlConfiguration) : this()
public EnvironmentConfiguration(YamlConfiguration yamlConfiguration)
{
ElasticsearchVersion = tempYamlConfiguration.ElasticsearchVersion;
Seed = tempYamlConfiguration.Seed;
}

public EnvironmentConfiguration()
{
//if env var NEST_INTEGRATION_VERSION is set assume integration mode used by the build script FAKE
var version = Environment.GetEnvironmentVariable("NEST_INTEGRATION_VERSION");
if (!string.IsNullOrEmpty(version)) Mode = TestMode.Integration;
Mode = Environment.GetEnvironmentVariable("NEST_INTEGRATION_TEST") != null ? TestMode.Integration : TestMode.Unit;

ElasticsearchVersion = string.IsNullOrWhiteSpace(version) ? DefaultVersion : version;
ClusterFilter = Environment.GetEnvironmentVariable("NEST_INTEGRATION_CLUSTER");
ShowElasticsearchOutputAfterStarted = Environment.GetEnvironmentVariable("NEST_INTEGRATION_SHOW_OUTPUT_AFTER_START") == "1";
TestFilter = Environment.GetEnvironmentVariable("NEST_TEST_FILTER");

var newRandom = new Random().Next(1, 100000);
var version = Environment.GetEnvironmentVariable("NEST_INTEGRATION_VERSION");
ElasticsearchVersion = string.IsNullOrWhiteSpace(version) ? yamlConfiguration.ElasticsearchVersion : version;
if (string.IsNullOrWhiteSpace(ElasticsearchVersion))
throw new Exception("Elasticsearch Version could not be determined from env var NEST_INTEGRATION_VERSION nor the test yaml configuration");

Seed = TryGetEnv("NEST_TEST_SEED", out var seed) ? int.Parse(seed) : newRandom;
var randomizer = new Random(Seed);
var externalSeed = TryGetEnv("NEST_TEST_SEED", out var seed)
? int.Parse(seed)
: yamlConfiguration.SeedProvidedExternally
? yamlConfiguration.Seed
: (int?)null;
SetExternalSeed(externalSeed, out var randomizer);

TestOnlyOne = RandomBoolConfig("TEST_ONLY_ONE", randomizer, false);

Random = new RandomConfiguration
{
SourceSerializer = RandomBoolConfig("SOURCESERIALIZER", randomizer),
TypedKeys = RandomBoolConfig("TYPEDKEYS", randomizer),
};
}

public sealed override string ClusterFilter { get; protected set; }
public sealed override string ElasticsearchVersion { get; protected set; }
public sealed override bool ForceReseed { get; protected set; } = true;
public sealed override bool TestOnlyOne { get; protected set; } = false;
public sealed override TestMode Mode { get; protected set; } = TestMode.Unit;
public sealed override int Seed { get; protected set; }
public sealed override bool ShowElasticsearchOutputAfterStarted { get; protected set; }
public sealed override bool TestAgainstAlreadyRunningElasticsearch { get; protected set; } = false;
public sealed override string TestFilter { get; protected set; }

private static bool RandomBoolConfig(string key, Random randomizer, bool? @default = null)
{
if (TryGetEnv("NEST_RANDOM_" + key, out var source) && bool.TryParse(source, out var b))
Expand Down
Loading