Github user geomacy commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/617#discussion_r110162824
  
    --- Diff: 
core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/ManagementPlaneIdTest.java
 ---
    @@ -0,0 +1,251 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.core.mgmt.rebind;
    +
    +import static org.testng.Assert.assertEquals;
    +import static org.testng.Assert.assertNotEquals;
    +
    +import java.io.File;
    +import java.io.FilenameFilter;
    +import java.io.IOException;
    +import java.nio.charset.StandardCharsets;
    +import java.nio.file.Files;
    +import java.util.ArrayList;
    +import java.util.Collection;
    +import java.util.concurrent.Callable;
    +
    +import org.apache.brooklyn.api.mgmt.ManagementContext;
    +import org.apache.brooklyn.api.mgmt.ha.HighAvailabilityMode;
    +import org.apache.brooklyn.api.mgmt.ha.ManagementNodeState;
    +import org.apache.brooklyn.core.entity.Entities;
    +import org.apache.brooklyn.core.internal.BrooklynProperties;
    +import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext;
    +import 
org.apache.brooklyn.core.mgmt.persist.BrooklynMementoPersisterToObjectStore;
    +import org.apache.brooklyn.core.mgmt.persist.FileBasedObjectStore;
    +import org.apache.brooklyn.core.mgmt.persist.PersistMode;
    +import org.apache.brooklyn.core.server.BrooklynServerConfig;
    +import org.apache.brooklyn.core.server.BrooklynServerPaths;
    +import org.apache.brooklyn.test.Asserts;
    +import org.apache.brooklyn.util.os.Os;
    +import org.apache.brooklyn.util.text.Strings;
    +import org.testng.annotations.AfterMethod;
    +import org.testng.annotations.BeforeMethod;
    +import org.testng.annotations.DataProvider;
    +import org.testng.annotations.Test;
    +
    +public class ManagementPlaneIdTest {
    +    private File mementoDir;
    +
    +    protected ClassLoader classLoader = getClass().getClassLoader();
    +
    +    private Collection<ManagementContext> managementContextForTermination;
    +
    +    @BeforeMethod
    +    public void setUp() {
    +        mementoDir = Os.newTempDir(getClass());
    +        managementContextForTermination = new ArrayList<>();
    +    }
    +
    +    @AfterMethod(alwaysRun=true)
    +    public void tearDown() throws Exception {
    +        if (managementContextForTermination != null) {
    +            for (ManagementContext mgmt : managementContextForTermination) 
{
    +                Entities.destroyAll(mgmt);
    +            }
    +        }
    +        if (mementoDir != null) 
FileBasedObjectStore.deleteCompletely(mementoDir);
    +    }
    +
    +    @Test
    +    public void testUninitializedThrows() {
    +        ManagementContext mgmt = new 
LocalManagementContext(BrooklynProperties.Factory.newEmpty());
    +        try {
    +            mgmt.getManagementPlaneId();
    +            Asserts.shouldHaveFailedPreviously("managementPlaneId not 
initialized");
    +        } catch (NullPointerException e) {
    +            Asserts.expectedFailureContains(e, "not initialized");
    +        }
    +    }
    +    
    +    @Test
    +    public void testPlaneIdPersists() throws Exception {
    +        final ManagementContext mgmt = 
createManagementContext(PersistMode.AUTO, HighAvailabilityMode.DISABLED);
    +        checkRebindIdPersisted(mgmt);
    +    }
    +
    +    @Test(enabled=false) //Need to wait for 1hr to verify the update will 
not overwrite
    +    public void testPlaneIdNotOverwritten() throws Exception {
    +        final LocalManagementContext mgmt = 
createManagementContext(PersistMode.AUTO, HighAvailabilityMode.DISABLED);
    +
    +        final File planeIdFile = new File(mementoDir, 
BrooklynMementoPersisterToObjectStore.PLANE_ID_FILE_NAME);
    +        checkRebindIdPersisted(mgmt);
    +        final String oldPlaneId = mgmt.getManagementPlaneId();
    +        mgmt.setManagementPlaneId(Strings.makeRandomId(8));
    +        assertNotEquals(oldPlaneId, mgmt.getManagementPlaneId());
    +        Asserts.succeedsContinually(new Callable<Void>() {
    +            @Override
    +            public Void call() throws Exception {
    +                String planeId = readFile(planeIdFile);
    +                assertEquals(oldPlaneId, planeId);
    +                return null;
    +            }
    +        });
    +    }
    +
    +    @Test
    +    public void testColdRebindInitialisesPlaneId() throws Exception {
    +        final LocalManagementContext origMgmt = 
createManagementContext(PersistMode.AUTO, HighAvailabilityMode.DISABLED);
    +        checkRebindIdPersisted(origMgmt);
    +        Entities.destroyAll(origMgmt);
    +
    +        LocalManagementContext rebindMgmt = 
createManagementContext(PersistMode.AUTO, HighAvailabilityMode.DISABLED);
    +
    +        assertEquals(origMgmt.getManagementPlaneId(), 
rebindMgmt.getManagementPlaneId());
    +    }
    +
    +
    +    @DataProvider
    +    public Object[][] haSlaveModes() {
    +        return new Object[][] {
    +            {HighAvailabilityMode.AUTO},
    +            {HighAvailabilityMode.STANDBY},
    +            {HighAvailabilityMode.HOT_STANDBY},
    +            {HighAvailabilityMode.HOT_BACKUP},
    +        };
    +    }
    +
    +    @Test(dataProvider="haSlaveModes")
    +    public void testHaRebindInitialisesPlaneId(HighAvailabilityMode 
slaveMode) throws Exception {
    +        final LocalManagementContext origMgmt = 
createManagementContext(PersistMode.AUTO, HighAvailabilityMode.AUTO);
    +        final LocalManagementContext rebindMgmt = 
createManagementContext(PersistMode.AUTO, slaveMode);
    +
    +        Asserts.succeedsEventually(new Runnable() {
    +            @Override
    +            public void run() {
    +                assertEquals(origMgmt.getManagementPlaneId(), 
rebindMgmt.getManagementPlaneId());
    +            }
    +        });
    +    }
    +
    +    @Test
    +    public void testHaFailoverKeepsPlaneId() throws Exception {
    +        final LocalManagementContext origMgmt = 
createManagementContext(PersistMode.AUTO, HighAvailabilityMode.MASTER);
    +        final LocalManagementContext rebindMgmt = 
createManagementContext(PersistMode.AUTO, HighAvailabilityMode.STANDBY);
    +
    +        Asserts.succeedsEventually(new Runnable() {
    +            @Override
    +            public void run() {
    +                assertEquals(origMgmt.getManagementPlaneId(), 
rebindMgmt.getManagementPlaneId());
    +            }
    +        });
    +
    +        Entities.destroyAll(origMgmt);
    +
    +        Asserts.succeedsEventually(new Runnable() {
    +            @Override
    +            public void run() {
    +                
assertEquals(rebindMgmt.getHighAvailabilityManager().getNodeState(), 
ManagementNodeState.MASTER);
    +                if 
(rebindMgmt.getRebindManager().isAwaitingInitialRebind()) {
    +                    throw new IllegalStateException("still rebinding");
    +                }
    +            }
    +        });
    +
    +        assertEquals(origMgmt.getManagementPlaneId(), 
rebindMgmt.getManagementPlaneId());
    +    }
    +    
    +    @Test
    +    public void testPlaneIdBackedUp() throws Exception {
    +        final LocalManagementContext origMgmt = 
createManagementContext(PersistMode.AUTO, HighAvailabilityMode.AUTO);
    +        checkRebindIdPersisted(origMgmt);
    +        Entities.destroyAll(origMgmt);
    +
    +        LocalManagementContext rebindMgmt = 
createManagementContextWithBackups(PersistMode.AUTO, HighAvailabilityMode.AUTO);
    +
    +        assertEquals(origMgmt.getManagementPlaneId(), 
rebindMgmt.getManagementPlaneId());
    +
    +        String backupContainer = 
BrooklynServerPaths.newBackupPersistencePathResolver(rebindMgmt).resolve();
    +        
    +        File[] promotionFolders = new File(backupContainer).listFiles(new 
FilenameFilter() {
    +            @Override
    +            public boolean accept(File dir, String name) {
    +                return name.contains("promotion");
    +            }
    +        });
    +        
    +        assertEquals(promotionFolders.length, 1);
    +        
    +        File planeIdFile = new File(promotionFolders[0], 
BrooklynMementoPersisterToObjectStore.PLANE_ID_FILE_NAME);
    +        String planeId = readFile(planeIdFile);
    +        assertEquals(origMgmt.getManagementPlaneId(), planeId);
    +    }
    +    
    +    @Test
    +    public void testFullPersist() throws Exception {
    +        final LocalManagementContext origMgmt = 
createManagementContext(PersistMode.DISABLED, HighAvailabilityMode.DISABLED);
    +        origMgmt.getRebindManager().getPersister().enableWriteAccess();
    +        origMgmt.getRebindManager().forcePersistNow(true, null);
    +        checkRebindIdPersisted(origMgmt);
    +    }
    +    
    +    protected LocalManagementContext createManagementContext(PersistMode 
persistMode, HighAvailabilityMode haMode) {
    +        return createManagementContext(persistMode, haMode, false);
    +    }
    +    
    +    protected LocalManagementContext 
createManagementContextWithBackups(PersistMode persistMode, 
HighAvailabilityMode haMode) {
    +        return createManagementContext(persistMode, haMode, true);
    +    }
    +    
    +    protected LocalManagementContext createManagementContext(PersistMode 
persistMode, HighAvailabilityMode haMode, boolean backedUp) {
    +        BrooklynProperties props = BrooklynProperties.Factory.newEmpty();
    +        props.put(BrooklynServerConfig.PERSISTENCE_BACKUPS_DIR, 
mementoDir);
    +        LocalManagementContext mgmt = 
RebindTestUtils.managementContextBuilder(mementoDir, classLoader)
    +                .persistPeriodMillis(1)
    +                .persistMode(persistMode)
    +                .haMode(HighAvailabilityMode.DISABLED)
    --- End diff --
    
    `.haMode(haMode)` ?   Currently `haMode` parameter is unused.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to