Environment Tests Edit on GitHub


Jasper supports the idea of registerable environment tests that you can use to verify an application is in a usable state as part of bootstrapping. The goal here is to fail fast with environmental errors diagnosed as part of deployment.

What kind of things might you assert?

  • Can your system connect to a designated database or external system?
  • Is all the required configuration available?
  • Are expected files where the system expects them to be?

Environment checks can be registered as lambda's or as implementations of IEnvironmentCheck:


public class AppWithEnvironmentChecks : JasperRegistry
{
    public AppWithEnvironmentChecks()
    {
        // Register an IEnvironmentCheck object
        Services.EnvironmentCheck(new FileExistsCheck("settings.json"));

        // or declaratively say a file should exist (this is just syntactic sugar)
        Services.CheckFileExists("settings.json");

        // or do it manually w/ a lambda
        Services.EnvironmentCheck("settings.json can be found", () =>
        {
            if (!File.Exists("settings.json")) throw new Exception("File cannot be found");
        });

        // Or register a check type
        Services.EnvironmentCheck<CustomCheck>();

        // The concrete Store class exposes IEnvironmentCheck
        Services.AddTransient<IStore, Store>();
    }
}

public interface IStore
{
}

// Jasper will still use this as an environment check
public class Store : IStore, IEnvironmentCheck
{
    public void Assert(JasperRuntime runtime)
    {
        // do the assertion of valid state
    }

    public string Description { get; } = "Fake Store Environment Check";



}

Do note that Jasper is just finding every possible service registered in the system's IoC container that implements the IEnvironmentCheck interface and executes each check. All environment checks will be executed when a Jasper system is bootstrapped, and will throw a single aggregate exception with any failures.

Custom Environment Checks

Environment checks just need to implement this interface:


public interface IEnvironmentCheck
{
    void Assert(JasperRuntime runtime);

    string Description { get; }
}

Like this sample from Jasper itself:


public class FileExistsCheck : IEnvironmentCheck
{
    private readonly string _file;

    public FileExistsCheck(string file)
    {
        _file = file;
    }

    public void Assert(JasperRuntime runtime)
    {
        if (!File.Exists(_file))
        {
            throw new Exception($"File {_file} cannot be found!");
        }
    }

    public string Description => ToString();

    public override string ToString()
    {
        return $"File {_file} exists";
    }
}

Ignoring Startup Issues

If you so choose, you can allow a Jasper application to successfully start up even with detected environment test failures with this configuration:


public class IgnoreValidationErrors : JasperRegistry
{
    public IgnoreValidationErrors()
    {
        Advanced.ThrowOnValidationErrors = false;
    }
}