From 61b37f87dcf9429d0d25238bad0227c95e9bcd57 Mon Sep 17 00:00:00 2001 From: NicolasDorier Date: Tue, 3 Oct 2017 00:41:03 +0900 Subject: [PATCH] Rely on docker for tests, move everything to the root --- .dockerignore | 5 +- BTCPayServer.Tests/BTCPayServer.Tests.csproj | 10 +- BTCPayServer.Tests/BTCPayServerTester.cs | 8 +- BTCPayServer.Tests/Dockerfile | 12 ++ BTCPayServer.Tests/NBXplorerTester.cs | 112 ---------------- BTCPayServer.Tests/ServerTester.cs | 45 ++++--- BTCPayServer.Tests/TestAccount.cs | 2 +- BTCPayServer.Tests/UnitTest1.cs | 6 +- .../docker-compose.yml | 34 ++--- BTCPayServer/.dockerignore | 123 ------------------ .../Configuration/BTCPayServerRuntime.cs | 2 +- BTCPayServer/DockerFile | 18 --- BTCPayServer/run-docker.cmd | 2 - Dockerfile | 8 +- docker-compose.regtest.yml | 48 ------- 15 files changed, 79 insertions(+), 356 deletions(-) create mode 100644 BTCPayServer.Tests/Dockerfile delete mode 100644 BTCPayServer.Tests/NBXplorerTester.cs rename BTCPayServer/docker-compose.regtest.yml => BTCPayServer.Tests/docker-compose.yml (54%) delete mode 100644 BTCPayServer/.dockerignore delete mode 100644 BTCPayServer/DockerFile delete mode 100644 BTCPayServer/run-docker.cmd delete mode 100644 docker-compose.regtest.yml diff --git a/.dockerignore b/.dockerignore index d4c645c26..c1a6ffd0c 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,6 @@ # Build Folders (you can keep bin if you'd like, to store dlls and pdbs) -[Bb]in/ -[Oo]bj/ +**/[Bb]in/ +**/[Oo]bj/ node_modules/ dist/ @@ -121,4 +121,3 @@ bower_components output .vs -BTCPayServer.Tests/ \ No newline at end of file diff --git a/BTCPayServer.Tests/BTCPayServer.Tests.csproj b/BTCPayServer.Tests/BTCPayServer.Tests.csproj index 45261c07f..40cc3549a 100644 --- a/BTCPayServer.Tests/BTCPayServer.Tests.csproj +++ b/BTCPayServer.Tests/BTCPayServer.Tests.csproj @@ -8,7 +8,6 @@ - @@ -17,4 +16,13 @@ + + + Dockerfile + + + Always + + + diff --git a/BTCPayServer.Tests/BTCPayServerTester.cs b/BTCPayServer.Tests/BTCPayServerTester.cs index 522e4cbc6..6246fe0a2 100644 --- a/BTCPayServer.Tests/BTCPayServerTester.cs +++ b/BTCPayServer.Tests/BTCPayServerTester.cs @@ -13,7 +13,6 @@ using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NBitcoin; -using NBitcoin.Tests; using NBXplorer; using NBXplorer.DerivationStrategy; using System; @@ -59,6 +58,11 @@ namespace BTCPayServer.Tests get; set; } + public string Postgres + { + get; set; + } + IWebHost _Host; public void Start() { @@ -73,6 +77,8 @@ namespace BTCPayServer.Tests config.AppendLine($"explorer.url={NBXplorerUri.AbsoluteUri}"); config.AppendLine($"explorer.cookiefile={CookieFile}"); config.AppendLine($"hdpubkey={HDPrivateKey.Neuter().ToString(Network.RegTest)}"); + if(Postgres != null) + config.AppendLine($"postgres=" + Postgres); File.WriteAllText(Path.Combine(_Directory, "settings.config"), config.ToString()); ServerUri = new Uri("http://127.0.0.1:" + port + "/"); diff --git a/BTCPayServer.Tests/Dockerfile b/BTCPayServer.Tests/Dockerfile new file mode 100644 index 000000000..75b7a952f --- /dev/null +++ b/BTCPayServer.Tests/Dockerfile @@ -0,0 +1,12 @@ +FROM microsoft/dotnet:2.0.0-sdk +WORKDIR /app + # caches restore result by copying csproj file separately +COPY BTCPayServer.Tests/BTCPayServer.Tests.csproj BTCPayServer.Tests/BTCPayServer.Tests.csproj +COPY BTCPayServer/BTCPayServer.csproj BTCPayServer/BTCPayServer.csproj + +WORKDIR /app/BTCPayServer.Tests +RUN dotnet restore +# copies the rest of your code +COPY . ../. + +ENTRYPOINT ["dotnet", "test"] \ No newline at end of file diff --git a/BTCPayServer.Tests/NBXplorerTester.cs b/BTCPayServer.Tests/NBXplorerTester.cs deleted file mode 100644 index 7a7cfaa50..000000000 --- a/BTCPayServer.Tests/NBXplorerTester.cs +++ /dev/null @@ -1,112 +0,0 @@ -using NBitcoin; -using NBitcoin.Tests; -using NBXplorer.DerivationStrategy; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Net; -using System.Net.Sockets; -using System.Runtime.CompilerServices; -using System.Text; -using System.Threading; -using Xunit; - -namespace BTCPayServer.Tests -{ - public class NBXplorerTester : IDisposable - { - private string _Directory; - - public NBXplorerTester(string scope) - { - if(scope == null) - throw new ArgumentNullException(nameof(scope)); - this._Directory = scope; - } - - Process _Process; - - public CoreNode Node - { - get; set; - } - - public void Start() - { - ProcessLauncher launcher = new ProcessLauncher(); - launcher.GoTo("Repositories", true); - if(!launcher.GoTo(new[] { "nxbplorer", "NBXplorer" })) - { - launcher.Run("git", "clone https://github.com/dgarage/NBXplorer nxbplorer"); - Assert.True(launcher.GoTo(new[] { "nxbplorer", "NBXplorer" }), "Could not clone nxbplorer"); - } - launcher.PushDirectory(); - if(!launcher.GoTo(new[] { "bin", "Release", "netcoreapp2.0" }) || !launcher.Exists("NBXplorer.dll")) - { - launcher.PopDirectory(); - launcher.Run("git", "pull"); - launcher.Run("git", "checkout master"); - try - { - //Need to do that or VS insist in adding this repo as submodules :/ - Utils.DeleteDirectory(Path.GetFullPath(Path.Combine(launcher.CurrentDirectory, "..", ".git"))); - } - catch { } - - launcher.Run("dotnet", "build /p:Configuration=Release"); - Assert.True(launcher.GoTo(new[] { "bin", "Release", "netcoreapp2.0" }), "Could not build NBXplorer"); - - launcher.AssertExists("NBXplorer.dll"); - } - - var port = Utils.FreeTcpPort(); - var launcher2 = new ProcessLauncher(); - launcher2.GoTo(_Directory, true); - launcher2.GoTo("nbxplorer-datadir", true); - StringBuilder config = new StringBuilder(); - config.AppendLine($"regtest=1"); - config.AppendLine($"port={port}"); - config.AppendLine($"rpc.url={Node.RPCUri.AbsoluteUri}"); - config.AppendLine($"rpc.auth={Node.AuthenticationString}"); - config.AppendLine($"node.endpoint={Node.NodeEndpoint.Address}:{Node.NodeEndpoint.Port}"); - File.WriteAllText(Path.Combine(launcher2.CurrentDirectory, "settings.config"), config.ToString()); - _Process = launcher.Start("dotnet", $"NBXplorer.dll --datadir \"{launcher2.CurrentDirectory}\""); - ExplorerClient = new NBXplorer.ExplorerClient(Node.Network, new Uri($"http://127.0.0.1:{port}/")); - CookieFile = Path.Combine(launcher2.CurrentDirectory, ".cookie"); - File.Create(CookieFile).Close(); //Will be wipedout when the client starts - ExplorerClient.SetCookieAuth(CookieFile); - try - { - var cancellationSource = new CancellationTokenSource(10000); - ExplorerClient.WaitServerStarted(cancellationSource.Token); - } - catch(OperationCanceledException) - { - Assert.False(_Process.HasExited, "NBXplorer failed to launch"); - throw; - } - } - - public NBXplorer.ExplorerClient ExplorerClient - { - get; set; - } - public string CookieFile - { - get; - set; - } - - public static NBXplorerTester Create([CallerMemberNameAttribute] string scope = null) - { - return new NBXplorerTester(scope); - } - - public void Dispose() - { - if(_Process != null && !_Process.HasExited) - _Process.Kill(); - } - } -} diff --git a/BTCPayServer.Tests/ServerTester.cs b/BTCPayServer.Tests/ServerTester.cs index 10824ca03..18870ac75 100644 --- a/BTCPayServer.Tests/ServerTester.cs +++ b/BTCPayServer.Tests/ServerTester.cs @@ -2,10 +2,12 @@ using BTCPayServer.Models.AccountViewModels; using Microsoft.AspNetCore.Mvc; using NBitcoin; -using NBitcoin.Tests; +using NBitcoin.RPC; using NBitpayClient; +using NBXplorer; using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Runtime.CompilerServices; using System.Text; @@ -21,8 +23,6 @@ namespace BTCPayServer.Tests } string _Directory; - NodeBuilder _Builder; - public ServerTester(string scope) { _Directory = scope; @@ -34,42 +34,45 @@ namespace BTCPayServer.Tests Utils.DeleteDirectory(_Directory); if(!Directory.Exists(_Directory)) Directory.CreateDirectory(_Directory); - _Builder = NodeBuilder.Create(_Directory, "0.14.2"); - ExplorerNode = _Builder.CreateNode(false); - ExplorerNode.WhiteBind = true; - ExplorerNode.Start(); - ExplorerNode.CreateRPCClient().Generate(101); - ExplorerTester = NBXplorerTester.Create(Path.Combine(_Directory, "explorer")); - ExplorerTester.Node = ExplorerNode; - ExplorerTester.Start(); + ExplorerNode = new RPCClient(RPCCredentialString.Parse(GetEnvironment("TESTS_RPCCONNECTION", "server=http://127.0.0.1:43782;ceiwHEbqWI83:DwubwWsoo3")), Network); + ExplorerClient = new ExplorerClient(Network, new Uri(GetEnvironment("TESTS_NBXPLORERURL", "http://127.0.0.1:32838/"))); PayTester = new BTCPayServerTester(Path.Combine(_Directory, "pay")) { - NBXplorerUri = ExplorerTester.ExplorerClient.Address, - CookieFile = ExplorerTester.CookieFile + NBXplorerUri = ExplorerClient.Address, + Postgres = GetEnvironment("TESTS_POSTGRES", "User ID=postgres;Host=127.0.0.1;Port=39372;Database=btcpayserver") }; PayTester.Start(); } + private string GetEnvironment(string variable, string defaultValue) + { + var var = Environment.GetEnvironmentVariable(variable); + return String.IsNullOrEmpty(var) ? defaultValue : var; + } + public TestAccount CreateAccount() { return new TestAccount(this); } - public CoreNode ExplorerNode + public RPCClient ExplorerNode { get; set; } + public ExplorerClient ExplorerClient + { + get; set; + } + + + public BTCPayServerTester PayTester { get; set; } - - public NBXplorerTester ExplorerTester - { - get; set; - } + public Network Network { get; @@ -80,10 +83,6 @@ namespace BTCPayServer.Tests { if(PayTester != null) PayTester.Dispose(); - if(ExplorerTester != null) - ExplorerTester.Dispose(); - if(_Builder != null) - _Builder.Dispose(); } } } diff --git a/BTCPayServer.Tests/TestAccount.cs b/BTCPayServer.Tests/TestAccount.cs index 0bc34dcdb..99b2c454f 100644 --- a/BTCPayServer.Tests/TestAccount.cs +++ b/BTCPayServer.Tests/TestAccount.cs @@ -33,7 +33,7 @@ namespace BTCPayServer.Tests var account = parent.PayTester.GetController(); await account.Register(new RegisterViewModel() { - Email = "Bob@toto.com", + Email = Guid.NewGuid() + "@toto.com", ConfirmPassword = "Kitten0@", Password = "Kitten0@", }); diff --git a/BTCPayServer.Tests/UnitTest1.cs b/BTCPayServer.Tests/UnitTest1.cs index 4b520fc41..46d2384dd 100644 --- a/BTCPayServer.Tests/UnitTest1.cs +++ b/BTCPayServer.Tests/UnitTest1.cs @@ -85,7 +85,7 @@ namespace BTCPayServer.Tests Transaction tx = new Transaction(); tx.Outputs.AddRange(request.Details.Outputs.Select(o => new TxOut(o.Amount, o.Script))); - var cashCow = tester.ExplorerNode.CreateRPCClient(); + var cashCow = tester.ExplorerNode; tx = cashCow.FundRawTransaction(tx).Transaction; tx = cashCow.SignRawTransaction(tx); @@ -125,7 +125,7 @@ namespace BTCPayServer.Tests FullNotifications = true }); BitcoinUrlBuilder url = new BitcoinUrlBuilder(invoice.PaymentUrls.BIP21); - tester.ExplorerNode.CreateRPCClient().SendToAddress(url.Address, url.Amount); + tester.ExplorerNode.SendToAddress(url.Address, url.Amount); callbackServer.ProcessNextRequest((ctx) => { var ipn = new StreamReader(ctx.Request.Body).ReadToEnd(); @@ -193,7 +193,7 @@ namespace BTCPayServer.Tests var rate = user.BitPay.GetRates(); - var cashCow = tester.ExplorerNode.CreateRPCClient(); + var cashCow = tester.ExplorerNode; var invoiceAddress = BitcoinAddress.Create(invoice.BitcoinAddress, cashCow.Network); cashCow.SendToAddress(invoiceAddress, firstPayment); diff --git a/BTCPayServer/docker-compose.regtest.yml b/BTCPayServer.Tests/docker-compose.yml similarity index 54% rename from BTCPayServer/docker-compose.regtest.yml rename to BTCPayServer.Tests/docker-compose.yml index 413af4e3c..055b6a800 100644 --- a/BTCPayServer/docker-compose.regtest.yml +++ b/BTCPayServer.Tests/docker-compose.yml @@ -2,25 +2,21 @@ version: "3" services: - btcpayserver: - ports: - - 8080:49392 - expose: - - "49392" + tests: build: - context: . - dockerfile: DockerFile + context: .. + dockerfile: BTCPayServer.Tests/Dockerfile environment: - BTCPAY_POSTGRES: "User ID=postgres;Host=postgres;Port=5432;Database=btcpayserver" - BTCPAY_NETWORK: regtest - BTCPAY_EXPLORERURL: http://nbxplorer:32838/ - BTCPAY_BIND: 0.0.0.0:49392 + TESTS_RPCCONNECTION: server=http://bitcoind:43782;ceiwHEbqWI83:DwubwWsoo3 + TESTS_NBXPLORERURL: http://nbxplorer:32838/ + TESTS_POSTGRES: User ID=postgres;Host=postgres;Port=5432;Database=btcpayserver links: - nbxplorer - - postgres nbxplorer: image: nicolasdorier/nbxplorer:1.0.0.12 + ports: + - "32838:32838" expose: - "32838" environment: @@ -28,21 +24,27 @@ services: NBXPLORER_RPCURL: http://bitcoind:43782/ NBXPLORER_RPCUSER: ceiwHEbqWI83 NBXPLORER_RPCPASSWORD: DwubwWsoo3 - NBXPLORER_NODEENDPOINT: bitcoind:8332 + NBXPLORER_NODEENDPOINT: bitcoind:39388 NBXPLORER_BIND: 0.0.0.0:32838 NBXPLORER_NOAUTH: 1 links: - bitcoind + - postgres bitcoind: image: nicolasdorier/docker-bitcoin:0.15.0.1 + ports: + - "43782:43782" + - "39388:39388" environment: - BITCOIN_EXTRA_ARGS: "regtest=1\nrpcport=43782\nport=8332" + BITCOIN_EXTRA_ARGS: "regtest=1\nrpcport=43782\nport=39388\nwhitelist=0.0.0.0/0" BITCOIN_RPC_USER: ceiwHEbqWI83 BITCOIN_RPC_PASSWORD: DwubwWsoo3 expose: - "43782" - - "8332" - + - "39388" + postgres: image: postgres:9.6.5 + ports: + - "39372:5432" \ No newline at end of file diff --git a/BTCPayServer/.dockerignore b/BTCPayServer/.dockerignore deleted file mode 100644 index fe6a78660..000000000 --- a/BTCPayServer/.dockerignore +++ /dev/null @@ -1,123 +0,0 @@ -# Build Folders (you can keep bin if you'd like, to store dlls and pdbs) -[Bb]in/ -[Oo]bj/ -node_modules/ -dist/ - -# mstest test results -TestResults - -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. - -# User-specific files -*.suo -*.user -*.sln.docstates - -# Build results -[Dd]ebug/ -[Rr]elease/ -x64/ -*_i.c -*_p.c -*.ilk -*.meta -*.obj -*.pch -*.pdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.log -*.vspscc -*.vssscc -.builds - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opensdf -*.sdf - -# Visual Studio profiler -*.psess -*.vsp -*.vspx - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper* - -# NCrunch -*.ncrunch* -.*crunch*.local.xml - -# Installshield output folder -[Ee]xpress - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish - -# Publish Web Output -*.Publish.xml - -# NuGet Packages Directory -packages - -# Windows Azure Build Output -csx -*.build.csdef - -# Windows Store app package directory -AppPackages/ - -# Others -[Bb]in -[Oo]bj -sql -TestResults -[Tt]est[Rr]esult* -*.Cache -ClientBin -[Ss]tyle[Cc]op.* -~$* -*.dbmdl -Generated_Code #added for RIA/Silverlight projects - -# Backup & report files from converting an old project file to a newer -# Visual Studio version. Backup files are not needed, because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML - -src/Rapporteringsregisteret.Web/assets/less/*.css - -MetricResults/ -*.sln.ide/ - -_configs/ - -# vnext stuff -bower_components -output - -.vs \ No newline at end of file diff --git a/BTCPayServer/Configuration/BTCPayServerRuntime.cs b/BTCPayServer/Configuration/BTCPayServerRuntime.cs index 04c7fe5e5..57cc748ac 100644 --- a/BTCPayServer/Configuration/BTCPayServerRuntime.cs +++ b/BTCPayServer/Configuration/BTCPayServerRuntime.cs @@ -38,7 +38,7 @@ namespace BTCPayServer.Configuration if(!Explorer.SetCookieAuth(opts.CookieFile)) Explorer.SetNoAuth(); - CancellationTokenSource cts = new CancellationTokenSource(10000); + CancellationTokenSource cts = new CancellationTokenSource(30000); try { Logs.Configuration.LogInformation("Trying to connect to explorer " + Explorer.Address.AbsoluteUri); diff --git a/BTCPayServer/DockerFile b/BTCPayServer/DockerFile deleted file mode 100644 index d339677e1..000000000 --- a/BTCPayServer/DockerFile +++ /dev/null @@ -1,18 +0,0 @@ -FROM microsoft/aspnetcore-build AS builder -WORKDIR /source - # caches restore result by copying csproj file separately -COPY *.csproj . -RUN dotnet restore -# copies the rest of your code -COPY . . -RUN dotnet publish --output /app/ --configuration Release - -FROM microsoft/aspnetcore:2.0.0 -WORKDIR /app - -RUN mkdir /datadir -ENV BTCPAY_DATADIR=/datadir -VOLUME /datadir - -COPY --from=builder "/app" . -ENTRYPOINT ["dotnet", "BTCPayServer.dll"] \ No newline at end of file diff --git a/BTCPayServer/run-docker.cmd b/BTCPayServer/run-docker.cmd deleted file mode 100644 index 44a1c4559..000000000 --- a/BTCPayServer/run-docker.cmd +++ /dev/null @@ -1,2 +0,0 @@ -docker-compose -f docker-compose.regtest.yml down -docker-compose -f docker-compose.regtest.yml up --force-recreate --build \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 043867070..edbafde5a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,10 @@ FROM microsoft/aspnetcore-build AS builder WORKDIR /source -COPY BTCPayServer/BTCPayServer.csproj BTCPayServer/BTCPayServer.csproj +COPY BTCPayServer/BTCPayServer.csproj BTCPayServer.csproj # Cache some dependencies -RUN cd BTCPayServer && dotnet restore && cd .. -COPY . . -RUN cd BTCPayServer && dotnet publish --output /app/ --configuration Release +RUN dotnet restore +COPY BTCPayServer/. . +RUN dotnet publish --output /app/ --configuration Release FROM microsoft/aspnetcore:2.0.0 WORKDIR /app diff --git a/docker-compose.regtest.yml b/docker-compose.regtest.yml deleted file mode 100644 index b3fe1de25..000000000 --- a/docker-compose.regtest.yml +++ /dev/null @@ -1,48 +0,0 @@ -version: "3" - -services: - - btcpayserver: - ports: - - 8080:49392 - expose: - - "49392" - build: - context: . - dockerfile: DockerFile - environment: - BTCPAY_POSTGRES: "User ID=postgres;Host=postgres;Port=5432;Database=btcpayserver" - BTCPAY_NETWORK: regtest - BTCPAY_EXPLORERURL: http://nbxplorer:32838/ - BTCPAY_BIND: 0.0.0.0:49392 - links: - - nbxplorer - - postgres - - nbxplorer: - image: nicolasdorier/nbxplorer:1.0.0.12 - expose: - - "32838" - environment: - NBXPLORER_NETWORK: regtest - NBXPLORER_RPCURL: http://bitcoind:43782/ - NBXPLORER_RPCUSER: ceiwHEbqWI83 - NBXPLORER_RPCPASSWORD: DwubwWsoo3 - NBXPLORER_NODEENDPOINT: bitcoind:8332 - NBXPLORER_BIND: 0.0.0.0:32838 - NBXPLORER_NOAUTH: 1 - links: - - bitcoind - - bitcoind: - image: nicolasdorier/bitcoin:0.15.0.1 - environment: - BITCOIN_EXTRA_ARGS: "regtest=1\nrpcport=43782\nport=8332" - BITCOIN_RPC_USER: ceiwHEbqWI83 - BITCOIN_RPC_PASSWORD: DwubwWsoo3 - expose: - - "43782" - - "8332" - - postgres: - image: postgres:9.6.5