This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git


The following commit(s) were added to refs/heads/master by this push:
     new 6118e6bbd4 Test and fix for persisting after importing
6118e6bbd4 is described below

commit 6118e6bbd413cd1ba6391af726b56f0262c47250
Author: Alex Heneveld <[email protected]>
AuthorDate: Fri Aug 30 14:09:26 2024 +0100

    Test and fix for persisting after importing
---
 .../brooklyn/core/mgmt/rebind/RebindTestUtils.java |  8 +++
 .../brooklyn/rest/resources/ServerResource.java    |  3 +
 .../resources/ServerExportImportResourceTest.java  | 76 +++++++++++++---------
 3 files changed, 57 insertions(+), 30 deletions(-)

diff --git 
a/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestUtils.java 
b/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestUtils.java
index 11e4786738..984cf842df 100644
--- 
a/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestUtils.java
+++ 
b/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestUtils.java
@@ -472,6 +472,14 @@ public class RebindTestUtils {
         return managementContext.getRebindManager().hasPending();
     }
 
+    public static File getLocalPersistenceDir(ManagementContext manager) {
+        BrooklynMementoPersister persister = 
manager.getRebindManager().getPersister();
+        if (!(persister instanceof BrooklynMementoPersisterToObjectStore)) 
return null;
+        PersistenceObjectStore store = 
((BrooklynMementoPersisterToObjectStore) persister).getObjectStore();
+        if (!(store instanceof FileBasedObjectStore)) return null;
+        return ((FileBasedObjectStore) store).getBaseDir();
+    }
+
     public static void stopPersistence(Application origApp) throws 
InterruptedException, TimeoutException {
         stopPersistence(origApp.getManagementContext());
     }
diff --git 
a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ServerResource.java
 
b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ServerResource.java
index 00c3cf029f..deb1d7d75f 100644
--- 
a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ServerResource.java
+++ 
b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ServerResource.java
@@ -662,6 +662,9 @@ public class ServerResource extends 
AbstractBrooklynRestResource implements Serv
             persister.stop(true);
             tempPersistenceStore.close();
 
+            // ensure the imported state is persisted
+            mgmt().getRebindManager().forcePersistNow(true, null);
+
         } catch (Exception e){
             Exceptions.propagateIfFatal(e);
             ApiError.Builder error = 
ApiError.builder().errorCode(Response.Status.BAD_REQUEST);
diff --git 
a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/ServerExportImportResourceTest.java
 
b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/ServerExportImportResourceTest.java
index 0d07dafc34..107303d702 100644
--- 
a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/ServerExportImportResourceTest.java
+++ 
b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/ServerExportImportResourceTest.java
@@ -33,6 +33,8 @@ import org.apache.brooklyn.core.entity.Entities;
 import org.apache.brooklyn.core.internal.BrooklynProperties;
 import org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult;
 import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
+import 
org.apache.brooklyn.core.mgmt.persist.BrooklynMementoPersisterToObjectStore;
+import org.apache.brooklyn.core.mgmt.persist.FileBasedObjectStore;
 import org.apache.brooklyn.core.mgmt.rebind.RebindTestUtils;
 import org.apache.brooklyn.core.test.entity.TestEntity;
 import org.apache.brooklyn.core.typereg.BrooklynBomYamlCatalogBundleResolver;
@@ -45,6 +47,7 @@ import 
org.apache.brooklyn.rest.domain.HighAvailabilitySummary;
 import org.apache.brooklyn.rest.domain.VersionSummary;
 import org.apache.brooklyn.rest.testing.BrooklynRestResourceTest;
 import org.apache.brooklyn.test.Asserts;
+import org.apache.brooklyn.util.collections.MutableList;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.http.HttpAsserts;
 import org.apache.brooklyn.util.os.Os;
@@ -61,7 +64,9 @@ import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.nio.file.Path;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -94,7 +99,7 @@ public class ServerExportImportResourceTest extends 
BrooklynRestResourceTest {
     }
 
     @Test
-    public void testExportPersistedStateWithBundlesThenReimport() throws 
Exception {
+    public void testExportPersistedStateWithBundlesThenReimportTwice() throws 
Exception {
         // export seems to preserve install order probably to minimise 
conflicts, so use deferred start to get the problematic order
         OsgiBundleInstallationResult r2 = ((ManagementContextInternal) 
manager).getOsgiManager().get().installDeferredStart(null,
                 () -> new ByteArrayInputStream(Strings.lines(
@@ -130,38 +135,49 @@ public class ServerExportImportResourceTest extends 
BrooklynRestResourceTest {
         org.apache.brooklyn.api.entity.Entity b2 = 
Iterables.getOnlyElement(Iterables.getOnlyElement(manager.getApplications()).getChildren());
         Asserts.assertInstanceOf(b2, TestEntity.class);
 
-        byte[] zip = 
client().path("/server/ha/persist/export").get(byte[].class);
+        for (int i = 0; i < 2; i++) {
+            byte[] zip = 
client().path("/server/ha/persist/export").get(byte[].class);
 
-        // restart the server, so it has nothing, then try importing
-        destroyClass();
+            // restart the server, so it has nothing, then try importing
+            destroyClass();
 
-        File mementoDir = Os.newTempDir(getClass());
-        manager = RebindTestUtils.managementContextBuilder(mementoDir, 
getClass().getClassLoader())
-                .persistPeriodMillis(Duration.ONE_MINUTE.toMilliseconds())
-                .haMode(HighAvailabilityMode.MASTER)
-                .forLive(true)
-                .enablePersistenceBackups(false)
-                .emptyCatalog(true)
+            File mementoDir = Os.newTempDir(getClass());
+            manager = RebindTestUtils.managementContextBuilder(mementoDir, 
getClass().getClassLoader())
+                    .persistPeriodMillis(Duration.ONE_MINUTE.toMilliseconds())
+                    .haMode(HighAvailabilityMode.MASTER)
+                    .forLive(true)
+                    .enablePersistenceBackups(false)
+                    .emptyCatalog(true)
 //                .properties(false)
-                .setOsgiEnablementAndReuse(useOsgi(), true)
-                .buildStarted();
-        new BrooklynCampPlatformLauncherNoServer()
-                .useManagementContext(manager)
-                .launch();
-
-        initClass();
-
-        Asserts.assertNull(manager.getTypeRegistry().get("b2"));
-        Asserts.assertSize(manager.getApplications(), 0);
-
-        Response importResponse = 
client().path("/server/ha/persist/import").post(Entity.entity(zip, 
MediaType.APPLICATION_OCTET_STREAM_TYPE));
-        HttpAsserts.assertHealthyStatusCode(importResponse.getStatus());
-
-        Asserts.assertNotNull(manager.getTypeRegistry().get("b1"));
-        Asserts.assertNotNull(manager.getTypeRegistry().get("b2"));
-        org.apache.brooklyn.api.entity.Entity b2b = 
Iterables.getOnlyElement(Iterables.getOnlyElement(manager.getApplications()).getChildren());
-        Asserts.assertInstanceOf(b2b, TestEntity.class);
-        Asserts.assertEquals(b2b.getId(), b2.getId());
+                    .setOsgiEnablementAndReuse(useOsgi(), true)
+                    .buildStarted();
+            new BrooklynCampPlatformLauncherNoServer()
+                    .useManagementContext(manager)
+                    .launch();
+
+            initClass();
+
+            Asserts.assertNull(manager.getTypeRegistry().get("b2"));
+            Asserts.assertSize(manager.getApplications(), 0);
+
+            Response importResponse = 
client().path("/server/ha/persist/import").post(Entity.entity(zip, 
MediaType.APPLICATION_OCTET_STREAM_TYPE));
+            HttpAsserts.assertHealthyStatusCode(importResponse.getStatus());
+
+            Asserts.assertNotNull(manager.getTypeRegistry().get("b1"), "Failed 
to find type 'b1' after import (iteration " + (i + 1) + ")");
+            Asserts.assertNotNull(manager.getTypeRegistry().get("b2"));
+            org.apache.brooklyn.api.entity.Entity b2b = 
Iterables.getOnlyElement(Iterables.getOnlyElement(manager.getApplications()).getChildren());
+            Asserts.assertInstanceOf(b2b, TestEntity.class);
+            Asserts.assertEquals(b2b.getId(), b2.getId());
+
+            // assert it is persisted (export makes a copy from mgmt so might 
not be identical, but it should!)
+            RebindTestUtils.waitForPersisted(manager);
+            File bundlesDir = 
RebindTestUtils.getLocalPersistenceDir(manager).toPath().resolve("bundles").toFile();
+            // the error below _has_ been seen, when we didn't persist after 
import; unlike some of the other tests (eg looping and not finding type after 
import) which are just to be safe
+            Asserts.assertNotNull(bundlesDir, "No bundles dir after import on 
iteration "+(i+1));
+            Asserts.assertThat(Arrays.asList(bundlesDir.list()),
+                    l -> l.stream().anyMatch(f -> 
f.toLowerCase().endsWith(".jar")),
+                    "Bundles dir does not contain JAR on iteration "+(i+1));
+        }
     }
 
     private List<String> listEntryNames(byte[] zip) throws Exception {

Reply via email to