brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bostko <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request #498: Use SIGTERM in stop script inside Abstrac...
Date Wed, 21 Dec 2016 15:58:23 GMT
Github user bostko commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/498#discussion_r93465004
  
    --- Diff: software/base/src/test/java/org/apache/brooklyn/entity/software/base/lifecycle/ScriptHelperIntegrationTest.java
---
    @@ -0,0 +1,176 @@
    +/*
    + * 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.entity.software.base.lifecycle;
    +
    +import com.google.common.base.Joiner;
    +import com.google.common.collect.ImmutableList;
    +import com.google.common.collect.ImmutableMap;
    +import com.google.common.collect.Iterables;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.entity.EntitySpec;
    +import org.apache.brooklyn.api.location.Location;
    +import org.apache.brooklyn.core.test.BrooklynAppLiveTestSupport;
    +import org.apache.brooklyn.entity.software.base.AbstractSoftwareProcessSshDriver;
    +import org.apache.brooklyn.entity.software.base.SoftwareProcessDriver;
    +import org.apache.brooklyn.entity.software.base.VanillaSoftwareProcess;
    +import org.apache.brooklyn.entity.software.base.VanillaSoftwareProcessImpl;
    +import org.apache.brooklyn.location.ssh.SshMachineLocation;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.core.task.system.internal.ExecWithLoggingHelpers;
    +import org.apache.brooklyn.util.stream.ReaderInputStream;
    +import org.apache.brooklyn.util.text.Strings;
    +import org.apache.commons.io.output.ByteArrayOutputStream;
    +import org.apache.commons.lang3.StringUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.testng.Assert;
    +import org.testng.annotations.BeforeMethod;
    +import org.testng.annotations.Test;
    +
    +import java.io.StringReader;
    +
    +import static java.lang.String.format;
    +
    +public class ScriptHelperIntegrationTest extends BrooklynAppLiveTestSupport
    +{
    +    private static final Logger log = LoggerFactory.getLogger(ScriptHelperIntegrationTest.class);
    +    
    +    private Location loc;
    +
    +    @BeforeMethod(alwaysRun=true)
    +    @SuppressWarnings("unchecked")
    +    @Override
    +    public void setUp() throws Exception {
    +        super.setUp();
    +        loc = app.getManagementContext().getLocationRegistry().getLocationManaged("localhost");
    +    }
    +
    +    @Test(groups = "Integration")
    +    public void testStopCommandWaitsToStopWithSigTerm() {
    +        StopCommandSoftwareProcess entity = app.createAndManageChild(EntitySpec.create(StopCommandSoftwareProcess.class,
StopCommandSoftwareProcessImpl.class));
    +        entity.start(ImmutableList.of(loc));
    +        VanillaSoftwareProcessSshDriver driver = (VanillaSoftwareProcessSshDriver) entity.getDriver();
    +        String launchContents = Joiner.on('\n')
    +                .join("#!/usr/bin/env bash",
    +                        "function trap_handler_command {",
    +                        "  echo stopping...",
    +                        "  exit 13",
    +                        "}",
    +                        "trap \"trap_handler_command\" SIGTERM",
    +                        "while true; do",
    +                        "  sleep 1",
    +                        "done");
    +        driver.copyResource(ImmutableMap.of(), new ReaderInputStream(new StringReader(launchContents),
"UTF-8"), "launch.sh", true);
    +        driver.executeLaunchCommand("nohup bash launch.sh > /dev/null &");
    +        ScriptHelper stopCommandScriptHelper = driver.stopCommandScriptHelper();
    +        stopCommandScriptHelper.execute();
    +        Assert.assertEquals(StringUtils.countMatches(stopCommandScriptHelper.getResultStdout(),
"is still running"), 1, "SIGTERM should be tried one time");
    +        Assert.assertFalse(stopCommandScriptHelper.getResultStdout().contains("stopped
with SIGKILL"), "SIGKILL should not be sent after SIGTERM fails.");
    +    }
    +
    +
    +    @Test(groups = "Integration")
    +    public void testStopWithSigtermIsKilledWithSigKill() {
    +        StopCommandSoftwareProcess entity = app.createAndManageChild(EntitySpec.create(StopCommandSoftwareProcess.class,
StopCommandSoftwareProcessImpl.class));
    +        entity.start(ImmutableList.of(loc));
    +        VanillaSoftwareProcessSshDriver driver = (VanillaSoftwareProcessSshDriver) entity.getDriver();
    +        String launchContents = Joiner.on('\n')
    +                .join("#!/usr/bin/env bash",
    +                        "function trap_handler_command {",
    +                        "  while true; do",
    +                        "    echo \"Do nothing.\"",
    +                        "    sleep 1",
    +                        "  done",
    +                        "}",
    +                        "trap \"trap_handler_command\" SIGTERM",
    +                        "while true; do",
    +                        "  sleep 1",
    +                        "done");
    +        driver.copyResource(ImmutableMap.of(), new ReaderInputStream(new StringReader(launchContents),
"UTF-8"), "launch.sh", true);
    +        driver.executeLaunchCommand("nohup bash launch.sh > /dev/null &");
    +        ByteArrayOutputStream stdOut = new ByteArrayOutputStream(10);
    +        SshMachineLocation machineLocation = (SshMachineLocation) Iterables.getFirst(entity.getLocations(),
null);
    +        machineLocation.execCommands(
    +                ImmutableMap.<String, Object>of(ExecWithLoggingHelpers.STDOUT.getName(),
stdOut),
    +                "check process is stopped",
    +                ImmutableList.of("cat "+driver.getRunDir() + '/' + VanillaSoftwareProcessSshDriver.PID_FILENAME),
    --- End diff --
    
    I use the response from that command to get the pid of the command and checking that that
pid is not running anymore after the stop command.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message