From 894c752c877291f1520d53e0e69b30fc4928fff6 Mon Sep 17 00:00:00 2001 From: "a.sreekumar" Date: Fri, 7 Aug 2020 15:24:10 +0100 Subject: Changes to make PAP container crash with non zero exitCode Make the application to return exitCode as 1 when the main process terminates due to issues such as: 1) Any basic issue in the startup config file causing error and terminates the main process, for e.g. a wrong name in parameterGroup. 2) Wrong info specified for database connectivity, say wrong username. This doesn't impact a case where database isn't ready yet and PAP container comes up first, such waiting is already handled by initContainer in the oom chart. PAP pod waits for policy-mariadb pod to be ready first, then only the main process is started and comes to this point. 3) An invalid port number is specified and the rest server is not able to start. These are the some of the scenarios which already makes the main process of PAP to terminate, and in these cases, an exception is thrown now which returns the exitCode as 1. Change-Id: Ie48726bcb36d7304eb60807fbaabbee8245e9c70 Issue-ID: POLICY-2754 Signed-off-by: a.sreekumar --- .../org/onap/policy/pap/main/startstop/Main.java | 38 ++++++++-------------- .../onap/policy/pap/main/startstop/TestMain.java | 24 +++++++++----- 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java b/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java index 1f478d77..74bb9f69 100644 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java +++ b/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. + * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +26,7 @@ import java.util.Arrays; import org.onap.policy.common.utils.services.Registry; import org.onap.policy.pap.main.PapConstants; import org.onap.policy.pap.main.PolicyPapException; +import org.onap.policy.pap.main.PolicyPapRuntimeException; import org.onap.policy.pap.main.parameters.PapParameterGroup; import org.onap.policy.pap.main.parameters.PapParameterHandler; import org.slf4j.Logger; @@ -64,38 +66,24 @@ public class Main { } // Validate that the arguments are sane arguments.validate(); - } catch (final PolicyPapException e) { - LOGGER.error(START_FAILED, e); - return; - } - // Read the parameters - try { + // Read the parameters parameterGroup = new PapParameterHandler().getParameters(arguments); - } catch (final Exception e) { - LOGGER.error(START_FAILED, e); - return; - } - // Initialize database - try { + // Initialize database new PapDatabaseInitializer().initializePapDatabase(parameterGroup.getDatabaseProviderParameters()); - } catch (final PolicyPapException exp) { - LOGGER.error(START_FAILED + ", used parameters are {}", Arrays.toString(args), exp); - return; - } - // Now, create the activator for the policy pap service - activator = new PapActivator(parameterGroup); - Registry.register(PapConstants.REG_PAP_ACTIVATOR, activator); + // Now, create the activator for the policy pap service + activator = new PapActivator(parameterGroup); + Registry.register(PapConstants.REG_PAP_ACTIVATOR, activator); - // Start the activator - try { + // Start the activator activator.start(); - } catch (final RuntimeException e) { - LOGGER.error("start of policy pap service failed, used parameters are {}", Arrays.toString(args), e); - Registry.unregister(PapConstants.REG_PAP_ACTIVATOR); - return; + } catch (Exception exp) { + if (null != activator) { + Registry.unregister(PapConstants.REG_PAP_ACTIVATOR); + } + throw new PolicyPapRuntimeException(START_FAILED + ", used parameters are " + Arrays.toString(args), exp); } // Add a shutdown hook to shut everything down in an orderly manner diff --git a/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java b/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java index 1efa9e14..ef410412 100644 --- a/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java +++ b/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java @@ -1,13 +1,14 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2019-2020 Nordix Foundation. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. + * Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. + * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. * ================================================================================ * Licensed 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 + * 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, @@ -21,6 +22,7 @@ package org.onap.policy.pap.main.startstop; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -33,6 +35,7 @@ import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInst import org.onap.policy.common.utils.services.Registry; import org.onap.policy.pap.main.PapConstants; import org.onap.policy.pap.main.PolicyPapException; +import org.onap.policy.pap.main.PolicyPapRuntimeException; import org.onap.policy.pap.main.parameters.CommonTestData; /** @@ -54,6 +57,7 @@ public class TestMain { /** * Shuts "main" down. + * * @throws Exception if an error occurs */ @After @@ -81,15 +85,16 @@ public class TestMain { @Test public void testMain_NoArguments() { final String[] papConfigParameters = {}; - main = new Main(papConfigParameters); - assertNull(main.getParameters()); + assertThatThrownBy(() -> new Main(papConfigParameters)).isInstanceOf(PolicyPapRuntimeException.class) + .hasMessage("start of policy pap service failed, used parameters are []"); } @Test public void testMain_InvalidArguments() { final String[] papConfigParameters = {"parameters/PapConfigParameters.json"}; - main = new Main(papConfigParameters); - assertNull(main.getParameters()); + assertThatThrownBy(() -> new Main(papConfigParameters)).isInstanceOf(PolicyPapRuntimeException.class) + .hasMessage( + "start of policy pap service failed, used parameters are [parameters/PapConfigParameters.json]"); } @Test @@ -102,7 +107,8 @@ public class TestMain { @Test public void testMain_InvalidParameters() { final String[] papConfigParameters = {"-c", "parameters/PapConfigParameters_InvalidName.json"}; - main = new Main(papConfigParameters); - assertNull(main.getParameters()); + assertThatThrownBy(() -> new Main(papConfigParameters)).isInstanceOf(PolicyPapRuntimeException.class) + .hasMessage("start of policy pap service failed, " + + "used parameters are [-c, parameters/PapConfigParameters_InvalidName.json]"); } } -- cgit 1.2.3-korg