001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright (c) 2005-2018 The ConQAT Project                               |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016|                                                                          |
017+-------------------------------------------------------------------------*/
018package org.conqat.lib.commons.test;
019
020/**
021 * Utilities for resolving the hostnames of servers that takes the
022 * build-environment into account.
023 */
024public final class HostnameUtils {
025
026        /** Class not meant to be instantiated. */
027        private HostnameUtils() {
028        }
029
030        /**
031         * Resolves the name of the given server, which may be different when running in
032         * a build environment or locally.
033         */
034        public static String resolveServerHostname(String serverName) {
035                // CI environment is determined by the CI variable (set by, e.g. gitlab)
036                if (System.getenv("CI") != null) {
037                        String runnerTags = System.getenv("CI_RUNNER_TAGS");
038
039                        // Additionally, when using kubernetes, the logical name is the
040                        // localhost IP address
041                        if (runnerTags != null && runnerTags.toLowerCase().contains("k8s")) {
042                                return "127.0.0.1";
043                        }
044
045                        return serverName;
046                }
047                return "localhost";
048        }
049}