Running Integration Tests against Postgres Database (#838)
* Allow configuring postgres with environment variables (cherry picked from commit 8439df78fea25656a9a1275d2a2fe3f0df0528c7) * Fix process provider when environment variables alread exist [common] (cherry picked from commit 66e5b4025974e081c1406f01a860b1ac52949c22) * First bash at running integration tests on postgres (cherry picked from commit f950e80c7e4f9b088ec6a149386160eab83b61c3) * Postgres integration tests running as part of the build pipeline (cherry picked from commit 9ca8616f5098778e9b5e6ce09d2aa11224018fab) * Fixed: Register PostgresOptions when running in utility mode * fixup! * fixup! * fixup! * fixup! * fixup! Co-authored-by: ta264 <ta264@users.noreply.github.com> Co-authored-by: Qstick <qstick@gmail.com>pull/1051/head
parent
9b1f9abfac
commit
cac2729230
@ -0,0 +1,26 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace NzbDrone.Core.Datastore
|
||||
{
|
||||
public class PostgresOptions
|
||||
{
|
||||
public string Host { get; set; }
|
||||
public int Port { get; set; }
|
||||
public string User { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string MainDb { get; set; }
|
||||
public string LogDb { get; set; }
|
||||
|
||||
public static PostgresOptions GetOptions()
|
||||
{
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
|
||||
var postgresOptions = new PostgresOptions();
|
||||
config.GetSection("Prowlarr:Postgres").Bind(postgresOptions);
|
||||
|
||||
return postgresOptions;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using Npgsql;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Datastore.Migration.Framework;
|
||||
|
||||
namespace NzbDrone.Test.Common.Datastore
|
||||
{
|
||||
public static class PostgresDatabase
|
||||
{
|
||||
public static PostgresOptions GetTestOptions()
|
||||
{
|
||||
var options = PostgresOptions.GetOptions();
|
||||
|
||||
var uid = TestBase.GetUID();
|
||||
options.MainDb = uid + "_main";
|
||||
options.LogDb = uid + "_log";
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
public static void Create(PostgresOptions options, MigrationType migrationType)
|
||||
{
|
||||
var db = GetDatabaseName(options, migrationType);
|
||||
var connectionString = GetConnectionString(options);
|
||||
using var conn = new NpgsqlConnection(connectionString);
|
||||
conn.Open();
|
||||
|
||||
using var cmd = conn.CreateCommand();
|
||||
cmd.CommandText = $"CREATE DATABASE \"{db}\" WITH OWNER = {options.User} ENCODING = 'UTF8' CONNECTION LIMIT = -1;";
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public static void Drop(PostgresOptions options, MigrationType migrationType)
|
||||
{
|
||||
var db = GetDatabaseName(options, migrationType);
|
||||
var connectionString = GetConnectionString(options);
|
||||
using var conn = new NpgsqlConnection(connectionString);
|
||||
conn.Open();
|
||||
|
||||
using var cmd = conn.CreateCommand();
|
||||
cmd.CommandText = $"DROP DATABASE \"{db}\" WITH (FORCE);";
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
private static string GetConnectionString(PostgresOptions options)
|
||||
{
|
||||
var builder = new NpgsqlConnectionStringBuilder()
|
||||
{
|
||||
Host = options.Host,
|
||||
Port = options.Port,
|
||||
Username = options.User,
|
||||
Password = options.Password,
|
||||
Enlist = false
|
||||
};
|
||||
|
||||
return builder.ConnectionString;
|
||||
}
|
||||
|
||||
private static string GetDatabaseName(PostgresOptions options, MigrationType migrationType)
|
||||
{
|
||||
return migrationType switch
|
||||
{
|
||||
MigrationType.Main => options.MainDb,
|
||||
MigrationType.Log => options.LogDb,
|
||||
_ => throw new NotImplementedException("Unknown migration type")
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
using System.IO;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.Datastore.Migration.Framework;
|
||||
|
||||
namespace NzbDrone.Test.Common.Datastore
|
||||
{
|
||||
public static class SqliteDatabase
|
||||
{
|
||||
public static string GetCachedDb(MigrationType type)
|
||||
{
|
||||
return Path.Combine(TestContext.CurrentContext.TestDirectory, $"cached_{type}.db");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RunSettings>
|
||||
<RunConfiguration>
|
||||
<EnvironmentVariables>
|
||||
<Prowlarr__Postgres__Host>192.168.100.5</Prowlarr__Postgres__Host>
|
||||
<Prowlarr__Postgres__Port>5432</Prowlarr__Postgres__Port>
|
||||
<Prowlarr__Postgres__User>abc</Prowlarr__Postgres__User>
|
||||
<Prowlarr__Postgres__Password>abc</Prowlarr__Postgres__Password>
|
||||
</EnvironmentVariables>
|
||||
</RunConfiguration>
|
||||
</RunSettings>
|
Loading…
Reference in new issue