Search in sources :

Example 6 with LocatorState

use of org.apache.geode.distributed.LocatorLauncher.LocatorState in project geode by apache.

the class LocatorLauncherRemoteIntegrationTest method testStartUsingPortInUseFails.

@Test
public void testStartUsingPortInUseFails() throws Throwable {
    this.socket = SocketCreatorFactory.createNonDefaultInstance(false, false, null, null, System.getProperties()).createServerSocket(this.locatorPort, 50, null, -1);
    this.locatorPort = this.socket.getLocalPort();
    final List<String> jvmArguments = getJvmArguments();
    final List<String> command = new ArrayList<String>();
    command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
    for (String jvmArgument : jvmArguments) {
        command.add(jvmArgument);
    }
    command.add("-cp");
    command.add(System.getProperty("java.class.path"));
    command.add(LocatorLauncher.class.getName());
    command.add(LocatorLauncher.Command.START.getName());
    command.add(getUniqueName());
    command.add("--redirect-output");
    command.add("--port=" + this.locatorPort);
    String expectedString = "java.net.BindException";
    AtomicBoolean outputContainedExpectedString = new AtomicBoolean();
    this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
    this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).inputListener(createExpectedListener("sysout", getUniqueName() + "#sysout", expectedString, outputContainedExpectedString)).build().start();
    this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).inputListener(createExpectedListener("syserr", getUniqueName() + "#syserr", expectedString, outputContainedExpectedString)).build().start();
    // wait for locator to start and fail
    final LocatorLauncher dirLauncher = new LocatorLauncher.Builder().setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath()).build();
    try {
        int code = process.waitFor();
        assertEquals("Expected exit code 1 but was " + code, 1, code);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        // check the status
        final LocatorState locatorState = dirLauncher.status();
        assertNotNull(locatorState);
        assertEquals(Status.NOT_RESPONDING, locatorState.getStatus());
        final String logFileName = getUniqueName() + ".log";
        assertFalse("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // if the following fails, then the SHORTER_TIMEOUT is too short for slow machines
    // or this test needs to use MainLauncher in ProcessWrapper
    // validate that output contained BindException
    this.errorCollector.checkThat(outputContainedExpectedString.get(), is(equalTo(true)));
    // just in case the launcher started...
    LocatorState status = null;
    try {
        status = dirLauncher.stop();
    } catch (Throwable t) {
    // ignore
    }
    this.errorCollector.checkThat(status.getStatus(), is(equalTo(getExpectedStopStatusForNotRunning())));
}
Also used : Builder(org.apache.geode.distributed.LocatorLauncher.Builder) ArrayList(java.util.ArrayList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProcessStreamReader(org.apache.geode.internal.process.ProcessStreamReader) LocatorState(org.apache.geode.distributed.LocatorLauncher.LocatorState) File(java.io.File) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 7 with LocatorState

use of org.apache.geode.distributed.LocatorLauncher.LocatorState in project geode by apache.

the class LocatorLauncherLocalIntegrationTest method testStartUsingPort.

/*
                          * // create existing pid file final int realPid =
                          * Host.getHost(0).getVM(3).invoke(() -> ProcessUtils.identifyPid());
                          * assertFalse("Remote pid shouldn't be the same as local pid " + realPid,
                          * realPid == ProcessUtils.identifyPid());
                          * 
                          * this.pidFile = new File(ProcessType.LOCATOR.getPidFileName());
                          * writePid(this.pidFile, realPid);
                          * 
                          * // build and start the locator final Builder builder = new Builder()
                          * .setMemberName(getUniqueName()) .setPort(this.locatorPort)
                          * .setRedirectOutput(true) .set(logLevel, "config");
                          * 
                          * assertFalse(builder.getForce()); this.launcher = builder.build();
                          * assertFalse(this.launcher.isForcing());
                          * 
                          * // collect and throw the FIRST failure Throwable failure = null;
                          * RuntimeException expected = null;
                          * 
                          * try { this.launcher.start();
                          * fail("LocatorLauncher start should have thrown RuntimeException caused by FileAlreadyExistsException"
                          * ); } catch (RuntimeException e) { expected = e;
                          * assertNotNull(expected.getMessage()); assertTrue(expected.getMessage(),
                          * expected.getMessage().
                          * contains("A PID file already exists and a Locator may be running in"));
                          * assertIndexDetailsEquals(RuntimeException.class, expected.getClass()); }
                          * catch (Throwable e) { logger.error(e); if (failure == null) { failure =
                          * e; } }
                          * 
                          * // just in case the launcher started... LocatorState status = null; try
                          * { status = this.launcher.stop(); } catch (Throwable t) { // ignore }
                          * 
                          * try { assertNotNull(expected); final Throwable cause =
                          * expected.getCause(); assertNotNull(cause); assertTrue(cause instanceof
                          * FileAlreadyExistsException);
                          * assertTrue(cause.getMessage().contains("Pid file already exists: "));
                          * assertTrue(cause.getMessage().contains("vf.gf.locator.pid for process "
                          * + realPid)); } catch (Throwable e) { logger.error(e); if (failure ==
                          * null) { failure = e; } }
                          * 
                          * try { delete(this.pidFile); final Status theStatus = status.getStatus();
                          * assertFalse(theStatus == Status.STARTING); assertFalse(theStatus ==
                          * Status.ONLINE); } catch (Throwable e) { logger.error(e); if (failure ==
                          * null) { failure = e; } }
                          * 
                          * if (failure != null) { throw failure; } } //
                          * testStartWithExistingPidFileFails
                          */
@Test
public void testStartUsingPort() throws Throwable {
    // generate one free port and then use it instead of default
    final int freeTCPPort = AvailablePortHelper.getRandomAvailableTCPPort();
    assertTrue(AvailablePort.isPortAvailable(freeTCPPort, AvailablePort.SOCKET));
    this.launcher = new Builder().setMemberName(getUniqueName()).setPort(freeTCPPort).setRedirectOutput(true).setWorkingDirectory(this.workingDirectory).set(CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory).set(LOG_LEVEL, "config").build();
    int pid = 0;
    try {
        // if start succeeds without throwing exception then #47778 is fixed
        this.launcher.start();
        waitForLocatorToStart(this.launcher);
        // validate the pid file and its contents
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
        assertTrue(pidFile.exists());
        pid = readPid(pidFile);
        assertTrue(pid > 0);
        assertTrue(ProcessUtils.isProcessAlive(pid));
        assertEquals(getPid(), pid);
        // verify locator did not use default port
        assertTrue(AvailablePort.isPortAvailable(this.locatorPort, AvailablePort.SOCKET));
        final LocatorState status = this.launcher.status();
        final String portString = status.getPort();
        assertEquals("Port should be \"" + freeTCPPort + "\" instead of " + portString, String.valueOf(freeTCPPort), portString);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // stop the locator
    try {
        assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
        waitForFileToDelete(this.pidFile);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
}
Also used : Builder(org.apache.geode.distributed.LocatorLauncher.Builder) LocatorState(org.apache.geode.distributed.LocatorLauncher.LocatorState) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 8 with LocatorState

use of org.apache.geode.distributed.LocatorLauncher.LocatorState in project geode by apache.

the class LocatorLauncherLocalIntegrationTest method testStartUsingPortInUseFails.

@Test
public void testStartUsingPortInUseFails() throws Throwable {
    // Test makes no sense in this case
    if (this.locatorPort == 0) {
        return;
    }
    // generate one free port and then use it instead of default
    this.socket = SocketCreatorFactory.getSocketCreatorForComponent(SecurableCommunicationChannel.CLUSTER).createServerSocket(this.locatorPort, 50, null, -1);
    this.launcher = new Builder().setMemberName(getUniqueName()).setPort(this.locatorPort).setRedirectOutput(true).setWorkingDirectory(this.workingDirectory).set(CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory).set(LOG_LEVEL, "config").build();
    RuntimeException expected = null;
    try {
        this.launcher.start();
        fail("LocatorLauncher start should have thrown RuntimeException caused by BindException");
    } catch (RuntimeException e) {
        expected = e;
        assertNotNull(expected.getMessage());
    // BindException string varies by platform
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        assertNotNull(expected);
        final Throwable cause = expected.getCause();
        assertNotNull(cause);
        assertTrue(cause instanceof BindException);
    // BindException string varies by platform
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
        assertFalse("Pid file should not exist: " + this.pidFile, this.pidFile.exists());
        // creation of log file seems to be random -- look into why sometime
        final String logFileName = getUniqueName() + ".log";
        assertFalse("Log file should not exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // just in case the launcher started...
    LocatorState status = null;
    try {
        status = this.launcher.stop();
    } catch (Throwable t) {
    // ignore
    }
    try {
        waitForFileToDelete(this.pidFile);
        assertEquals(getExpectedStopStatusForNotRunning(), status.getStatus());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
}
Also used : Builder(org.apache.geode.distributed.LocatorLauncher.Builder) BindException(java.net.BindException) LocatorState(org.apache.geode.distributed.LocatorLauncher.LocatorState) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 9 with LocatorState

use of org.apache.geode.distributed.LocatorLauncher.LocatorState in project geode by apache.

the class LocatorLauncherLocalIntegrationTest method testStartWithDefaultPortInUseFails.

/*
   * assertTrue(getUniqueName() + " is broken if PID == Integer.MAX_VALUE",
   * ProcessUtils.identifyPid() != Integer.MAX_VALUE);
   * 
   * // create existing pid file this.pidFile = new File(ProcessType.LOCATOR.getPidFileName());
   * final int realPid = Host.getHost(0).getVM(3).invoke(() -> ProcessUtils.identifyPid());
   * assertFalse(realPid == ProcessUtils.identifyPid()); writePid(this.pidFile, realPid);
   * 
   * // build and start the locator final Builder builder = new Builder() .setForce(true)
   * .setMemberName(getUniqueName()) .setPort(this.locatorPort) .setRedirectOutput(true)
   * 
   * assertTrue(builder.getForce()); this.launcher = builder.build();
   * assertTrue(this.launcher.isForcing()); this.launcher.start();
   * 
   * // collect and throw the FIRST failure Throwable failure = null;
   * 
   * try { waitForLocatorToStart(this.launcher);
   * 
   * // validate the pid file and its contents assertTrue(this.pidFile.exists()); final int pid =
   * readPid(this.pidFile); assertTrue(pid > 0); assertTrue(ProcessUtils.isProcessAlive(pid));
   * assertIndexDetailsEquals(getPid(), pid);
   * 
   * // validate log file was created final String logFileName = getUniqueName()+".log";
   * assertTrue("Log file should exist: " + logFileName, new File(logFileName).exists());
   * 
   * } catch (Throwable e) { logger.error(e); if (failure == null) { failure = e; } }
   * 
   * try { assertIndexDetailsEquals(Status.STOPPED, this.launcher.stop().getStatus());
   * waitForFileToDelete(this.pidFile); } catch (Throwable e) { logger.error(e); if (failure ==
   * null) { failure = e; } }
   * 
   * if (failure != null) { throw failure; } } // testStartUsingForceOverwritesExistingPidFile
   */
@Test
public void testStartWithDefaultPortInUseFails() throws Throwable {
    // Test makes no sense in this case
    if (this.locatorPort == 0) {
        return;
    }
    this.socket = SocketCreatorFactory.getSocketCreatorForComponent(SecurableCommunicationChannel.CLUSTER).createServerSocket(this.locatorPort, 50, null, -1);
    assertTrue(this.socket.isBound());
    assertFalse(this.socket.isClosed());
    assertFalse(AvailablePort.isPortAvailable(this.locatorPort, AvailablePort.SOCKET));
    assertNotNull(System.getProperty(DistributionLocator.TEST_OVERRIDE_DEFAULT_PORT_PROPERTY));
    assertEquals(this.locatorPort, Integer.valueOf(System.getProperty(DistributionLocator.TEST_OVERRIDE_DEFAULT_PORT_PROPERTY)).intValue());
    assertFalse(AvailablePort.isPortAvailable(this.locatorPort, AvailablePort.SOCKET));
    this.launcher = new Builder().setMemberName(getUniqueName()).setRedirectOutput(true).setWorkingDirectory(this.workingDirectory).set(CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory).set(LOG_LEVEL, "config").build();
    assertEquals(this.locatorPort, this.launcher.getPort().intValue());
    RuntimeException expected = null;
    try {
        this.launcher.start();
        // why did it not fail like it's supposed to?
        final String property = System.getProperty(DistributionLocator.TEST_OVERRIDE_DEFAULT_PORT_PROPERTY);
        assertNotNull(property);
        assertEquals(this.locatorPort, Integer.valueOf(property).intValue());
        assertFalse(AvailablePort.isPortAvailable(this.locatorPort, AvailablePort.SOCKET));
        assertEquals(this.locatorPort, this.launcher.getPort().intValue());
        assertEquals(this.locatorPort, this.socket.getLocalPort());
        assertTrue(this.socket.isBound());
        assertFalse(this.socket.isClosed());
        fail("LocatorLauncher start should have thrown RuntimeException caused by BindException");
    } catch (RuntimeException e) {
        expected = e;
        assertNotNull(expected.getMessage());
    // BindException text varies by platform
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        assertNotNull(expected);
        final Throwable cause = expected.getCause();
        assertNotNull(cause);
        assertTrue(cause instanceof BindException);
    // BindException string varies by platform
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
        assertFalse("Pid file should not exist: " + this.pidFile, this.pidFile.exists());
        // creation of log file seems to be random -- look into why sometime
        final String logFileName = getUniqueName() + ".log";
        assertFalse("Log file should not exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // just in case the launcher started...
    LocatorState status = null;
    try {
        status = this.launcher.stop();
    } catch (Throwable t) {
    // ignore
    }
    try {
        waitForFileToDelete(this.pidFile);
        assertEquals(getExpectedStopStatusForNotRunning(), status.getStatus());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
}
Also used : Builder(org.apache.geode.distributed.LocatorLauncher.Builder) BindException(java.net.BindException) LocatorState(org.apache.geode.distributed.LocatorLauncher.LocatorState) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 10 with LocatorState

use of org.apache.geode.distributed.LocatorLauncher.LocatorState in project geode by apache.

the class LocatorLauncherLocalIntegrationTest method testStopUsingWorkingDirectory.

@Test
public void testStopUsingWorkingDirectory() throws Throwable {
    final Builder builder = new Builder().setMemberName(getUniqueName()).setPort(this.locatorPort).setRedirectOutput(true).setWorkingDirectory(this.workingDirectory).set(CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory).set(LOG_LEVEL, "config");
    assertFalse(builder.getForce());
    this.launcher = builder.build();
    assertFalse(this.launcher.isForcing());
    LocatorLauncher dirLauncher = null;
    try {
        this.launcher.start();
        waitForLocatorToStart(this.launcher);
        // validate the pid file and its contents
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
        assertTrue("Pid file " + this.pidFile.getCanonicalPath().toString() + " should exist", this.pidFile.exists());
        final int pid = readPid(this.pidFile);
        assertTrue(pid > 0);
        assertEquals(ProcessUtils.identifyPid(), pid);
        dirLauncher = new Builder().setWorkingDirectory(this.workingDirectory).build();
        assertNotNull(dirLauncher);
        assertFalse(dirLauncher.isRunning());
        // stop the locator
        final LocatorState locatorState = dirLauncher.stop();
        assertNotNull(locatorState);
        assertEquals(Status.STOPPED, locatorState.getStatus());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        this.launcher.stop();
    } catch (Throwable e) {
    // ignore
    }
    try {
        // verify the PID file was deleted
        waitForFileToDelete(this.pidFile);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
}
Also used : Builder(org.apache.geode.distributed.LocatorLauncher.Builder) LocatorState(org.apache.geode.distributed.LocatorLauncher.LocatorState) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

LocatorState (org.apache.geode.distributed.LocatorLauncher.LocatorState)24 Test (org.junit.Test)22 File (java.io.File)21 Builder (org.apache.geode.distributed.LocatorLauncher.Builder)21 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)16 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)10 LocatorLauncher (org.apache.geode.distributed.LocatorLauncher)7 ArrayList (java.util.ArrayList)6 CommandStringBuilder (org.apache.geode.management.internal.cli.util.CommandStringBuilder)6 CommandResult (org.apache.geode.management.internal.cli.result.CommandResult)5 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)5 ProcessStreamReader (org.apache.geode.internal.process.ProcessStreamReader)4 Category (org.junit.experimental.categories.Category)3 BindException (java.net.BindException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)2 InetAddress (java.net.InetAddress)1 Properties (java.util.Properties)1 Status (org.apache.geode.distributed.AbstractLauncher.Status)1 OSProcess (org.apache.geode.internal.OSProcess)1