|
| 1 | +namespace ServiceControlInstaller.Engine.UnitTests.Validation |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Diagnostics; |
| 5 | + using System.IO; |
| 6 | + using System.Net.Http; |
| 7 | + using System.Threading; |
| 8 | + using System.Threading.Tasks; |
| 9 | + using FileSystem; |
| 10 | + using NUnit.Framework; |
| 11 | + using Raven.Client.Documents; |
| 12 | + using Raven.Client.Documents.Indexes; |
| 13 | + using Raven.Client.Documents.Operations.Indexes; |
| 14 | + using Raven.Client.ServerWide; |
| 15 | + using Raven.Client.ServerWide.Operations; |
| 16 | + using TestHelper; |
| 17 | + |
| 18 | + [TestFixture] |
| 19 | + [Platform("Win")] |
| 20 | + [NonParallelizable] |
| 21 | + class PackagedRavenDBTests |
| 22 | + { |
| 23 | + [Test] |
| 24 | + public async Task CanPersistAndIndexDocumentsWithoutNativeKafkaDependencies() |
| 25 | + { |
| 26 | + var root = Path.Combine(Path.GetTempPath(), "PackagedRavenDB", Guid.NewGuid().ToString("N")); |
| 27 | + var serverDirectory = Path.Combine(root, "Server"); |
| 28 | + var dataDirectory = Path.Combine(root, "Data"); |
| 29 | + var serverUrl = $"http://127.0.0.1:{PortUtility.FindAvailablePort(33400)}"; |
| 30 | + using var timeout = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CurrentContext.CancellationToken); |
| 31 | + timeout.CancelAfter(TimeSpan.FromMinutes(3)); |
| 32 | + var cancellationToken = timeout.Token; |
| 33 | + Process server = null; |
| 34 | + Task<string> standardOutput = null; |
| 35 | + Task<string> standardError = null; |
| 36 | + |
| 37 | + try |
| 38 | + { |
| 39 | + FileUtils.UnzipToSubdirectory("RavenDBServer.zip", serverDirectory); |
| 40 | + |
| 41 | + using (Assert.EnterMultipleScope()) |
| 42 | + { |
| 43 | + Assert.That(Path.Combine(serverDirectory, "libcurl.dll"), Does.Not.Exist); |
| 44 | + Assert.That(Path.Combine(serverDirectory, "librdkafka.dll"), Does.Not.Exist); |
| 45 | + Assert.That(Path.Combine(serverDirectory, "librdkafkacpp.dll"), Does.Not.Exist); |
| 46 | + Assert.That(Path.Combine(serverDirectory, "Raven.Server.exe"), Does.Exist); |
| 47 | + } |
| 48 | + |
| 49 | + await StartServer(); |
| 50 | + |
| 51 | + const string databaseName = "PackagingSmokeTest"; |
| 52 | + using var store = new DocumentStore |
| 53 | + { |
| 54 | + Urls = [serverUrl], |
| 55 | + Database = databaseName, |
| 56 | + Conventions = { DisableTopologyUpdates = true } |
| 57 | + }.Initialize(); |
| 58 | + |
| 59 | + await store.Maintenance.Server.SendAsync(new CreateDatabaseOperation(new DatabaseRecord(databaseName)), cancellationToken); |
| 60 | + await store.Maintenance.SendAsync(new PutIndexesOperation(new IndexDefinition |
| 61 | + { |
| 62 | + Name = indexName, |
| 63 | + Maps = ["from document in docs select new { document.Name }"] |
| 64 | + }), cancellationToken); |
| 65 | + |
| 66 | + using (var session = store.OpenAsyncSession()) |
| 67 | + { |
| 68 | + await session.StoreAsync(new PackagedDocument { Name = documentName }, documentId, cancellationToken); |
| 69 | + await session.SaveChangesAsync(cancellationToken); |
| 70 | + } |
| 71 | + |
| 72 | + await AssertDocumentAndIndex(store, cancellationToken); |
| 73 | + await StopServer(); |
| 74 | + await StartServer(); |
| 75 | + await AssertDocumentAndIndex(store, cancellationToken); |
| 76 | + } |
| 77 | + finally |
| 78 | + { |
| 79 | + await StopServer(); |
| 80 | + if (Directory.Exists(root)) |
| 81 | + { |
| 82 | + FileUtils.DeleteDirectory(root, recursive: true, contentsOnly: false); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + async Task StartServer() |
| 87 | + { |
| 88 | + var startInfo = new ProcessStartInfo(Path.Combine(serverDirectory, "Raven.Server.exe")) |
| 89 | + { |
| 90 | + WorkingDirectory = serverDirectory, |
| 91 | + UseShellExecute = false, |
| 92 | + RedirectStandardOutput = true, |
| 93 | + RedirectStandardError = true, |
| 94 | + CreateNoWindow = true |
| 95 | + }; |
| 96 | + startInfo.ArgumentList.Add("--non-interactive"); |
| 97 | + startInfo.ArgumentList.Add("--log-to-console"); |
| 98 | + startInfo.ArgumentList.Add($"--ServerUrl={serverUrl}"); |
| 99 | + startInfo.ArgumentList.Add($"--DataDir={dataDirectory}"); |
| 100 | + startInfo.ArgumentList.Add($"--Logs.Path={Path.Combine(root, "Logs")}"); |
| 101 | + startInfo.ArgumentList.Add("--Setup.Mode=None"); |
| 102 | + startInfo.ArgumentList.Add("--Security.UnsecuredAccessAllowed=Local"); |
| 103 | + startInfo.ArgumentList.Add("--License.Eula.Accepted=true"); |
| 104 | + startInfo.ArgumentList.Add($"--License.Path={Path.Combine(TestContext.CurrentContext.TestDirectory, "RavenLicense.json")}"); |
| 105 | + startInfo.ArgumentList.Add("--License.DisableAutoUpdate=true"); |
| 106 | + startInfo.ArgumentList.Add("--License.DisableLicenseSupportCheck=true"); |
| 107 | + |
| 108 | + server = Process.Start(startInfo); |
| 109 | + standardOutput = server.StandardOutput.ReadToEndAsync(CancellationToken.None); |
| 110 | + standardError = server.StandardError.ReadToEndAsync(CancellationToken.None); |
| 111 | + |
| 112 | + using var client = new HttpClient { Timeout = Timeout.InfiniteTimeSpan }; |
| 113 | + while (true) |
| 114 | + { |
| 115 | + cancellationToken.ThrowIfCancellationRequested(); |
| 116 | + Assert.That(server.HasExited, Is.False, "The packaged RavenDB server exited before accepting requests."); |
| 117 | + try |
| 118 | + { |
| 119 | + using var response = await client.GetAsync($"{serverUrl}/build/version", cancellationToken); |
| 120 | + if (response.IsSuccessStatusCode) |
| 121 | + { |
| 122 | + return; |
| 123 | + } |
| 124 | + } |
| 125 | + catch (HttpRequestException) |
| 126 | + { |
| 127 | + } |
| 128 | + await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + async Task StopServer() |
| 133 | + { |
| 134 | + if (server == null) |
| 135 | + { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + try |
| 140 | + { |
| 141 | + if (!server.HasExited) |
| 142 | + { |
| 143 | + server.Kill(entireProcessTree: true); |
| 144 | + } |
| 145 | + |
| 146 | + await server.WaitForExitAsync(CancellationToken.None).WaitAsync(TimeSpan.FromSeconds(15)); |
| 147 | + TestContext.Out.WriteLine(await standardOutput.WaitAsync(TimeSpan.FromSeconds(15))); |
| 148 | + TestContext.Out.WriteLine(await standardError.WaitAsync(TimeSpan.FromSeconds(15))); |
| 149 | + } |
| 150 | + finally |
| 151 | + { |
| 152 | + server.Dispose(); |
| 153 | + server = null; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + static async Task AssertDocumentAndIndex(IDocumentStore store, CancellationToken cancellationToken) |
| 159 | + { |
| 160 | + using var session = store.OpenAsyncSession(); |
| 161 | + var document = await session.LoadAsync<PackagedDocument>(documentId, cancellationToken); |
| 162 | + Assert.That(document?.Name, Is.EqualTo(documentName)); |
| 163 | + |
| 164 | + var indexedDocument = await session.Advanced.AsyncDocumentQuery<PackagedDocument>(indexName) |
| 165 | + .WaitForNonStaleResults(TimeSpan.FromSeconds(30)) |
| 166 | + .WhereEquals(nameof(PackagedDocument.Name), documentName) |
| 167 | + .SingleAsync(cancellationToken); |
| 168 | + Assert.That(indexedDocument.Name, Is.EqualTo(documentName)); |
| 169 | + } |
| 170 | + |
| 171 | + class PackagedDocument |
| 172 | + { |
| 173 | + public string Name { get; set; } |
| 174 | + } |
| 175 | + |
| 176 | + const string documentId = "documents/1"; |
| 177 | + const string documentName = "Packaged RavenDB"; |
| 178 | + const string indexName = "Packaging/ByName"; |
| 179 | + } |
| 180 | +} |
0 commit comments