|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | +using System.Linq.Expressions; |
| 7 | +using System.Text; |
| 8 | +using Xunit; |
| 9 | +using Xunit.Abstractions; |
| 10 | +using Xunit.Sdk; |
| 11 | + |
| 12 | +namespace JsonLD.Test |
| 13 | +{ |
| 14 | + public class DockerTests |
| 15 | + { |
| 16 | + private readonly ITestOutputHelper output; |
| 17 | + |
| 18 | + public DockerTests(ITestOutputHelper output) |
| 19 | + { |
| 20 | + this.output = output; |
| 21 | + } |
| 22 | + |
| 23 | + [SkippableFact] |
| 24 | + public void DockerBuild() |
| 25 | + { |
| 26 | + try |
| 27 | + { |
| 28 | + var exit = ExecDocker("build -t json-ld.net ."); |
| 29 | + Assert.Equal(0, exit); |
| 30 | + } |
| 31 | + catch (Win32Exception ex) when (ex.Message == "The system cannot find the file specified") |
| 32 | + { |
| 33 | + Skip.If(true, "Couldn't find docker in your path.\nThis is only required for building/testing inside a docker container."); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + [SkippableFact] |
| 38 | + public void DockerTest() |
| 39 | + { |
| 40 | + try |
| 41 | + { |
| 42 | + // --filter option required to avoid going full inception and |
| 43 | + // testing a container inside a container inside a conta... |
| 44 | + var exit = ExecDocker($"run --rm json-ld.net dotnet test --filter FullyQualifiedName!~{nameof(DockerTests)}"); |
| 45 | + Assert.Equal(0, exit); |
| 46 | + } |
| 47 | + catch (Win32Exception ex) when (ex.Message == "The system cannot find the file specified") |
| 48 | + { |
| 49 | + Skip.If(true, "Couldn't find docker in your path.\nThis is only required for building/testing inside a docker container."); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private int ExecDocker(string args) |
| 54 | + { |
| 55 | + var workspace = Environment.GetEnvironmentVariable("GITHUB_WORKSPACE"); |
| 56 | + if (string.IsNullOrEmpty(workspace)) |
| 57 | + { |
| 58 | + // Current directory for test context is usually REPO_HOME/test/json-ld.net.tests/bin/Release/netcoreapp2.1, |
| 59 | + // and we want just REPO_HOME. |
| 60 | + workspace = Path.Combine(Directory.GetCurrentDirectory(), "../../../../.."); |
| 61 | + } |
| 62 | + |
| 63 | + var process = Process.Start(new ProcessStartInfo |
| 64 | + { |
| 65 | + UseShellExecute = false, |
| 66 | + RedirectStandardOutput = true, |
| 67 | + RedirectStandardError = true, |
| 68 | + WorkingDirectory = workspace, |
| 69 | + FileName = "docker", |
| 70 | + Arguments = args |
| 71 | + }); |
| 72 | + |
| 73 | + process.WaitForExit(); |
| 74 | + |
| 75 | + output.WriteLine("=======STDOUT======="); |
| 76 | + output.WriteLine(process.StandardOutput.ReadToEnd()); |
| 77 | + output.WriteLine("\n\n\n\n=======STDERR======="); |
| 78 | + output.WriteLine(process.StandardError.ReadToEnd()); |
| 79 | + |
| 80 | + return process.ExitCode; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments