You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by pp...@apache.org on 2022/08/18 09:28:03 UTC

[ignite] branch master updated: IGNITE-17512 Ability to load test logger configuration and test properties from the classpath (#10193)

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

ppa pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new b000d591d71 IGNITE-17512 Ability to load test logger configuration and test properties from the classpath (#10193)
b000d591d71 is described below

commit b000d591d718feb02ccce0c73a1daeb796b7b87a
Author: Pavel Pereslegin <xx...@gmail.com>
AuthorDate: Thu Aug 18 12:27:55 2022 +0300

    IGNITE-17512 Ability to load test logger configuration and test properties from the classpath (#10193)
---
 modules/core/pom.xml                               | 11 ++++
 modules/core/src/test/config/examples.properties   |  2 +-
 modules/core/src/test/config/log4j2-test.xml       |  7 ++-
 modules/core/src/test/config/log4j2-test.xml~      | 63 -------------------
 .../apache/ignite/testframework/GridTestUtils.java | 18 ++++++
 .../testframework/config/GridTestProperties.java   | 73 +++++++++++++---------
 .../common/GridCommonAbstractTestSelfTest.java     | 15 +++++
 7 files changed, 94 insertions(+), 95 deletions(-)

diff --git a/modules/core/pom.xml b/modules/core/pom.xml
index 455a01d0127..9913700b807 100644
--- a/modules/core/pom.xml
+++ b/modules/core/pom.xml
@@ -219,6 +219,17 @@
             <testResource>
                 <directory>src/test/resources</directory>
             </testResource>
+            <testResource>
+                <directory>src/test/config</directory>
+                <includes>
+                    <include>log4j2-test.xml</include>
+                    <include>tests.properties</include>
+                </includes>
+                <!-- Hide the test logger configuration from the log4j autoconfigurator
+                     in case the user starts Ignite with test.jar on the classpath. -->
+                <targetPath>${project.build.testOutputDirectory}/test/config</targetPath>
+            </testResource>
+
         </testResources>
 
         <plugins>
diff --git a/modules/core/src/test/config/examples.properties b/modules/core/src/test/config/examples.properties
index cca6862d10d..b30280c130f 100644
--- a/modules/core/src/test/config/examples.properties
+++ b/modules/core/src/test/config/examples.properties
@@ -15,4 +15,4 @@
 # limitations under the License.
 #
 
-DataRegionExample=examples/config/example-data-regions.xml
\ No newline at end of file
+DataRegionExample=examples/config/example-data-regions.xml
diff --git a/modules/core/src/test/config/log4j2-test.xml b/modules/core/src/test/config/log4j2-test.xml
index e1056be1f58..ee2104f38d3 100644
--- a/modules/core/src/test/config/log4j2-test.xml
+++ b/modules/core/src/test/config/log4j2-test.xml
@@ -82,7 +82,12 @@
         <Logger name="org.eclipse.jetty" level="INFO"/>
 
         <Root level="INFO">
-            <AppenderRef ref="FILE" level="DEBUG"/>
+            <!--
+                Uncomment to enable log output to file.
+            -->
+            <!--
+                <AppenderRef ref="FILE" level="DEBUG"/>
+            -->
             <AppenderRef ref="CONSOLE" level="DEBUG"/>
             <AppenderRef ref="CONSOLE_ERR" level="WARN"/>
         </Root>
diff --git a/modules/core/src/test/config/log4j2-test.xml~ b/modules/core/src/test/config/log4j2-test.xml~
deleted file mode 100644
index 4222b0c1bfa..00000000000
--- a/modules/core/src/test/config/log4j2-test.xml~
+++ /dev/null
@@ -1,63 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-  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.
--->
-<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN"
-    "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
-<Configuration status="debug" strict="true" name="XMLConfigTest"
-               packages="org.apache.ignite.logger.log4j2">
-  <Filter type="ThresholdFilter" level="TRACE"/>
-  <Appenders>
-    <Appender type="Console" name="CONSOLE_ERR">
-    </Appender>
-	<Routing name="Routing">	
-	   <Routes pattern="$${ctx:ROUTINGKEY}">
-	   <Route key="$${ctx:ROUTINGKEY}">
-		  <RollingFile name="Rolling-default" fileName="work/log/default.log"
-					filePattern="work/log/${date:yyyy-MM}/default-%d{yyyy-MM-dd}-%i.log.gz">
-			<PatternLayout>
-		  <pattern>[%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n</pattern>
-			</PatternLayout>
-			<Policies>
-			  <TimeBasedTriggeringPolicy interval="6" modulate="true" />
-			  <SizeBasedTriggeringPolicy size="10 MB" />
-			</Policies>
-		  </RollingFile>
-		</Route>
-	   <Route>
-		  <RollingFile name="Rolling-${ctx:ROUTINGKEY}" fileName="work/log/ignite-${ctx:ROUTINGKEY}.log"
-					filePattern="work/log/${date:yyyy-MM}/ignite-${ctx:ROUTINGKEY}-%d{yyyy-MM-dd}-%i.log.gz">
-		<PatternLayout>
-		  <pattern>[%d{ABSOLUTE}][%-5p] $${ctx:nodeidmsg}[%t][%c{1}]%msg%n</pattern>
-		</PatternLayout>
-		<Policies>
-		  <TimeBasedTriggeringPolicy interval="6" modulate="true" />
-		  <SizeBasedTriggeringPolicy size="10 MB" />
-		</Policies>
-		  </RollingFile>
-		</Route>
-		  </Routes>
-		</Routing>
-	</Appenders>
-  <Loggers>
-    <Logger name="org.springframework" level="warn"/>
-    <Logger name="rg.eclipse.jetty" level="warn"/>
-    <Logger name="org.eclipse.jetty.util.log" level="warn"/>
-    <Logger name="org.eclipse.jetty.util.component" level="warn"/>
-    <Logger name="com.amazonaws" level="warn"/>
-    <Root level="TRACE">
-    	<AppenderRef ref="Routing"/>
-   </Root>
-  </Loggers>
-</Configuration>
diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/GridTestUtils.java b/modules/core/src/test/java/org/apache/ignite/testframework/GridTestUtils.java
index 31c4b9200e6..d6585b7224d 100644
--- a/modules/core/src/test/java/org/apache/ignite/testframework/GridTestUtils.java
+++ b/modules/core/src/test/java/org/apache/ignite/testframework/GridTestUtils.java
@@ -78,6 +78,7 @@ import org.apache.ignite.IgniteCache;
 import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.IgniteException;
 import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.IgniteSystemProperties;
 import org.apache.ignite.Ignition;
 import org.apache.ignite.cache.query.annotations.QuerySqlFunction;
 import org.apache.ignite.cluster.ClusterNode;
@@ -99,6 +100,7 @@ import org.apache.ignite.internal.processors.cache.distributed.near.GridNearCach
 import org.apache.ignite.internal.processors.odbc.ClientListenerProcessor;
 import org.apache.ignite.internal.processors.port.GridPortRecord;
 import org.apache.ignite.internal.util.GridBusyLock;
+import org.apache.ignite.internal.util.IgniteUtils;
 import org.apache.ignite.internal.util.future.GridFutureAdapter;
 import org.apache.ignite.internal.util.lang.GridAbsClosure;
 import org.apache.ignite.internal.util.lang.GridAbsPredicate;
@@ -123,6 +125,7 @@ import org.apache.ignite.testframework.junits.GridAbstractTest;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
+import static org.apache.ignite.IgniteSystemProperties.IGNITE_HOME;
 import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.DFLT_STORE_DIR;
 import static org.apache.ignite.ssl.SslContextFactory.DFLT_KEY_ALGORITHM;
 import static org.apache.ignite.ssl.SslContextFactory.DFLT_SSL_PROTOCOL;
@@ -1280,6 +1283,21 @@ public final class GridTestUtils {
         }
     }
 
+    /**
+     * Initialize {@link IgniteSystemProperties#IGNITE_HOME IGNITE_HOME} system property.
+     */
+    public static void initTestProjectHome() {
+        String igniteHome = U.getIgniteHome();
+
+        if (F.isEmpty(igniteHome)) {
+            igniteHome = new File(System.getProperty("user.dir"), "ignite").getAbsolutePath();
+
+            IgniteUtils.setIgniteHome(igniteHome);
+
+            U.warn(null, '"' + IGNITE_HOME + "\" system property was automatically set to " + igniteHome);
+        }
+    }
+
     /**
      * @return Ignite home.
      * @throws Exception If failed.
diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/config/GridTestProperties.java b/modules/core/src/test/java/org/apache/ignite/testframework/config/GridTestProperties.java
index edadc65e077..85cbca4c3fb 100644
--- a/modules/core/src/test/java/org/apache/ignite/testframework/config/GridTestProperties.java
+++ b/modules/core/src/test/java/org/apache/ignite/testframework/config/GridTestProperties.java
@@ -18,9 +18,10 @@
 package org.apache.ignite.testframework.config;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URI;
+import java.net.URL;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
@@ -30,12 +31,13 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.apache.ignite.binary.BinaryBasicNameMapper;
 import org.apache.ignite.binary.BinaryTypeConfiguration;
-import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.testframework.GridTestUtils;
 import org.apache.logging.log4j.core.config.Configurator;
 import org.apache.logging.log4j.core.config.LoggerConfig;
 import org.jetbrains.annotations.Nullable;
 
+import static org.apache.ignite.testframework.GridTestUtils.initTestProjectHome;
+
 /**
  * Loads test properties from {@code config} folder under tests.
  * The property structure is as follows:
@@ -58,11 +60,17 @@ import org.jetbrains.annotations.Nullable;
  * </ul>
  */
 public final class GridTestProperties {
+    /** */
+    public static final String DEFAULT_LOG4J_FILE = "log4j2-test.xml";
+
     /** */
     public static final String TESTS_PROP_FILE = "tests.properties";
 
     /** */
-    public static final String TESTS_CFG_PATH = "modules/core/src/test/config";
+    private static final String TEST_CONFIG_DIR = "/test/config/";
+
+    /** */
+    private static final String TESTS_CFG_PATH = "modules/core/src" + TEST_CONFIG_DIR;
 
     /** */
     private static final Pattern PROP_REGEX = Pattern.compile("[@$]\\{[^@${}]+\\}");
@@ -93,16 +101,14 @@ public final class GridTestProperties {
 
     /** */
     static {
-        // Initialize IGNITE_HOME system property.
-        U.getIgniteHome();
+        initTestProjectHome();
 
         // Load default properties.
-        File cfgFile = getTestConfigurationFile(null, TESTS_PROP_FILE);
+        URI cfgFile = getTestConfigurationFile(null, TESTS_PROP_FILE);
 
-        assert cfgFile != null && cfgFile.exists();
-        assert !cfgFile.isDirectory();
+        assert cfgFile != null;
 
-        dfltProps = Collections.unmodifiableMap(loadFromFile(new HashMap<String, String>(), cfgFile));
+        dfltProps = Collections.unmodifiableMap(loadFromResource(new HashMap<>(), cfgFile));
 
         if ("false".equals(System.getProperty("IGNITE_TEST_PROP_DISABLE_LOG4J", "false"))) {
             String user = System.getProperty("user.name");
@@ -128,14 +134,14 @@ public final class GridTestProperties {
         String cfgFile = System.getProperty("IGNITE_TEST_PROP_LOG4J_FILE");
 
         if (cfgFile == null)
-            cfgFile = "log4j2-test.xml";
+            cfgFile = DEFAULT_LOG4J_FILE;
 
-        File log4jFile = getTestConfigurationFile(user, cfgFile);
+        URI log4jFile = getTestConfigurationFile(user, cfgFile);
 
         if (log4jFile == null)
             log4jFile = getTestConfigurationFile(null, cfgFile);
 
-        Configurator.initialize(LoggerConfig.ROOT, log4jFile.getAbsolutePath());
+        Configurator.initialize(LoggerConfig.ROOT, GridTestProperties.class.getClassLoader(), log4jFile);
 
         System.out.println("Configured log4j2 from: " + log4jFile);
     }
@@ -145,6 +151,16 @@ public final class GridTestProperties {
         // No-op.
     }
 
+    /** */
+    public static URI findTestResource(String res) {
+        URL resUrl = GridTestProperties.class.getResource(TEST_CONFIG_DIR + res);
+
+        if (resUrl == null)
+            return null;
+
+        return URI.create(resUrl.toExternalForm());
+    }
+
     /**
      * @return Default properties.
      */
@@ -265,10 +281,10 @@ public final class GridTestProperties {
      * @return Loaded properties.
      */
     private static Map<String, String> loadProperties(Map<String, String> props, String dir) {
-        File cfg = getTestConfigurationFile(dir, TESTS_PROP_FILE);
+        URI cfg = getTestConfigurationFile(dir, TESTS_PROP_FILE);
 
         if (cfg != null)
-            loadFromFile(props, cfg);
+            loadFromResource(props, cfg);
 
         return props;
     }
@@ -278,7 +294,7 @@ public final class GridTestProperties {
      * @param fileName File name.
      * @return Configuration file for given user.
      */
-    @Nullable private static File getTestConfigurationFile(@Nullable String user, String fileName) {
+    @Nullable private static URI getTestConfigurationFile(@Nullable String user, String fileName) {
         String path = TESTS_CFG_PATH;
 
         if (user != null)
@@ -291,36 +307,33 @@ public final class GridTestProperties {
         if (file != null && file.exists()) {
             assert !file.isDirectory();
 
-            return file;
+            return file.toURI();
         }
 
-        return null;
+        return user == null ? findTestResource(fileName) : null;
     }
 
     /**
      * @param props Initial properties.
-     * @param file Property file.
+     * @param resource File resource location.
      * @return Loaded properties.
      */
-    private static Map<String, String> loadFromFile(Map<String, String> props, File file) {
-        try {
-
-            try (InputStream in = new FileInputStream(file)) {
-                Properties fileProps = new Properties();
+    private static Map<String, String> loadFromResource(Map<String, String> props, URI resource) {
+        try (InputStream in = resource.toURL().openStream()) {
+            Properties fileProps = new Properties();
 
-                fileProps.load(in);
+            fileProps.load(in);
 
-                for (Entry<Object, Object> prop : fileProps.entrySet())
-                    props.put((String)prop.getKey(), (String)prop.getValue());
+            for (Entry<Object, Object> prop : fileProps.entrySet())
+                props.put((String)prop.getKey(), (String)prop.getValue());
 
-                for (Entry<String, String> prop : props.entrySet())
-                    prop.setValue(substituteProperties(prop.getValue()));
-            }
+            for (Entry<String, String> prop : props.entrySet())
+                prop.setValue(substituteProperties(prop.getValue()));
         }
         catch (IOException e) {
             e.printStackTrace();
 
-            assert false : "Failed to load test configuration properties: " + file;
+            assert false : "Failed to load test configuration properties: " + resource;
         }
 
         return props;
diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/junits/common/GridCommonAbstractTestSelfTest.java b/modules/core/src/test/java/org/apache/ignite/testframework/junits/common/GridCommonAbstractTestSelfTest.java
index 5a7e9bb2c0d..4a031129df4 100644
--- a/modules/core/src/test/java/org/apache/ignite/testframework/junits/common/GridCommonAbstractTestSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/testframework/junits/common/GridCommonAbstractTestSelfTest.java
@@ -19,12 +19,27 @@ package org.apache.ignite.testframework.junits.common;
 
 import java.util.Arrays;
 import java.util.HashMap;
+import java.util.function.Consumer;
+import org.apache.ignite.testframework.config.GridTestProperties;
 import org.junit.Test;
 
+import static org.apache.ignite.testframework.config.GridTestProperties.findTestResource;
+
 /**
  *
  */
 public class GridCommonAbstractTestSelfTest extends GridCommonAbstractTest {
+    /**
+     *
+     */
+    @Test
+    public void testResourceLocation() {
+        Consumer<String> check = (res) -> assertNotNull("Required resource not found: " + res, findTestResource(res));
+
+        check.accept(GridTestProperties.TESTS_PROP_FILE);
+        check.accept(GridTestProperties.DEFAULT_LOG4J_FILE);
+    }
+
     /**
      *
      */