Skip to content

Commit

Permalink
Add atomic operation to migrate to a new Elm app
Browse files Browse the repository at this point in the history
+ Automate test for this new functionality: Migrate from counter app to string builder app.
+ Also, make paths for the HTTP admin interface more consistent.
  • Loading branch information
Viir committed Apr 9, 2020
1 parent 316ed4f commit 0e0ecb0
Show file tree
Hide file tree
Showing 9 changed files with 684 additions and 60 deletions.
95 changes: 90 additions & 5 deletions implement/PersistentProcess/PersistentProcess.Test/TestWebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ public void Web_host_supporting_migrations_supports_set_app_config()
using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var setAppConfigResponse = adminClient.PostAsync(
StartupSupportingMigrations.PathApiSetAppConfigAndInitState,
StartupSupportingMigrations.PathApiSetAppConfigAndInitElmState,
new ByteArrayContent(webAppConfigZipArchive)).Result;

Assert.IsTrue(setAppConfigResponse.IsSuccessStatusCode, "set-app response IsSuccessStatusCode");
Expand Down Expand Up @@ -854,7 +854,7 @@ public void Web_host_supporting_migrations_supports_set_app_config()
using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var setAppHttpResponse = adminClient.PostAsync(
StartupSupportingMigrations.PathApiSetAppConfigAndInitState,
StartupSupportingMigrations.PathApiSetAppConfigAndInitElmState,
new ByteArrayContent(webAppConfigZipArchive)).Result;
}
}
Expand All @@ -866,7 +866,7 @@ public void Web_host_supporting_migrations_supports_set_app_config()
using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var setAppHttpResponse = adminClient.PostAsync(
StartupSupportingMigrations.PathApiSetAppConfigAndContinueState,
StartupSupportingMigrations.PathApiSetAppConfigAndContinueElmState,
new ByteArrayContent(webAppConfigZipArchive)).Result;

Assert.IsTrue(setAppHttpResponse.IsSuccessStatusCode, "set-app response IsSuccessStatusCode");
Expand Down Expand Up @@ -906,7 +906,7 @@ public void Web_host_supporting_migrations_supports_migrate_elm_state()
using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var setAppConfigResponse = adminClient.PostAsync(
StartupSupportingMigrations.PathApiSetAppConfigAndInitState,
StartupSupportingMigrations.PathApiSetAppConfigAndInitElmState,
new ByteArrayContent(webAppConfigZipArchive)).Result;

Assert.IsTrue(setAppConfigResponse.IsSuccessStatusCode, "set-app response IsSuccessStatusCode");
Expand Down Expand Up @@ -972,7 +972,7 @@ public void Web_host_supporting_migrations_prevents_damaging_backend_state_with_
using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var setAppConfigResponse = adminClient.PostAsync(
StartupSupportingMigrations.PathApiSetAppConfigAndInitState,
StartupSupportingMigrations.PathApiSetAppConfigAndInitElmState,
new ByteArrayContent(webAppConfigZipArchive)).Result;

Assert.IsTrue(
Expand Down Expand Up @@ -1066,6 +1066,91 @@ public void Web_host_supporting_migrations_prevents_damaging_backend_state_with_
}
}

[TestMethod]
public void Web_host_supporting_migrations_supports_set_app_config_and_migrate_elm_state()
{
var initialWebAppConfig = TestElmWebAppHttpServer.CounterWebApp;

var migrateAndSecondElmApp =
TestSetup.GetElmAppFromExampleName("migrate-from-int-to-string-builder-web-app");

var migrateAndSecondElmAppWebAppConfig =
initialWebAppConfig.WithElmApp(migrateAndSecondElmApp);

var initialWebAppConfigZipArchive =
ZipArchive.ZipArchiveFromEntries(initialWebAppConfig.AsFiles());

var migrateAndSecondElmAppWebAppConfigZipArchive =
ZipArchive.ZipArchiveFromEntries(migrateAndSecondElmAppWebAppConfig.AsFiles());

using (var testSetup = WebHostSupportingMigrationsTestSetup.Setup(adminRootPassword: "Root-Password_1234567"))
{
using (var server = testSetup.BuildServer())
{
using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var setAppConfigResponse = adminClient.PostAsync(
StartupSupportingMigrations.PathApiSetAppConfigAndInitElmState,
new ByteArrayContent(initialWebAppConfigZipArchive)).Result;

Assert.IsTrue(setAppConfigResponse.IsSuccessStatusCode, "set-app response IsSuccessStatusCode");
}

var counterEventsAndExpectedResponses =
TestSetup.CounterProcessTestEventsAndExpectedResponses(
new (int addition, int expectedResponse)[]
{
(0, 0),
(1, 1),
(13, 14),
}).ToList();

foreach (var (serializedEvent, expectedResponse) in counterEventsAndExpectedResponses)
{
using (var client = testSetup.BuildPublicAppHttpClient())
{
var httpResponse =
client.PostAsync("", new StringContent(serializedEvent, System.Text.Encoding.UTF8)).Result;

var httpResponseContent = httpResponse.Content.ReadAsStringAsync().Result;

Assert.AreEqual(expectedResponse, httpResponseContent, false, "server response matches " + expectedResponse);
}
}

using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var setAppConfigAndMigrateElmStateResponse = adminClient.PostAsync(
StartupSupportingMigrations.PathApiSetAppConfigAndMigrateElmState,
new ByteArrayContent(migrateAndSecondElmAppWebAppConfigZipArchive)).Result;

Assert.IsTrue(
setAppConfigAndMigrateElmStateResponse.IsSuccessStatusCode,
"setAppConfigAndMigrateElmStateResponse IsSuccessStatusCode (" +
setAppConfigAndMigrateElmStateResponse.Content.ReadAsStringAsync().Result + ")");
}

using (var client = testSetup.BuildPublicAppHttpClient())
{
Assert.AreEqual(
"14",
client.GetAsync("").Result.Content.ReadAsStringAsync().Result,
"State migrated from counter app");

Assert.AreEqual(HttpStatusCode.OK, client.PostAsync("", new StringContent("-part-a")).Result.StatusCode);

Assert.AreEqual(HttpStatusCode.OK, client.PostAsync("", new StringContent("-part-b")).Result.StatusCode);

Assert.AreEqual(
"14-part-a-part-b",
client.GetAsync("").Result.Content.ReadAsStringAsync().Result,
"State after multiple posts");
}
}
}
}


class FileStoreFromDelegates : IFileStore
{
readonly Action<IImmutableList<string>, byte[]> setFileContent;
Expand Down
Loading

0 comments on commit 0e0ecb0

Please sign in to comment.