All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.trino.tests.product.launcher.suite.SuiteTestRun Maven / Gradle / Ivy

There is a newer version: 448
Show newest version
/*
 * 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
 *
 * 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 io.trino.tests.product.launcher.suite;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import io.trino.tests.product.launcher.env.EnvironmentConfig;
import io.trino.tests.product.launcher.env.EnvironmentProvider;

import java.util.List;
import java.util.Map;

import static com.google.common.base.MoreObjects.toStringHelper;
import static io.trino.tests.product.TestGroups.Introspection.validateGroupIdentityReferences;
import static io.trino.tests.product.launcher.Configurations.nameForEnvironmentClass;
import static java.lang.System.getenv;
import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;

public class SuiteTestRun
{
    private static final String SKIP_TEST_ARGUMENT = "DISTRO_SKIP_TEST";
    private static final String SKIP_GROUP_ARGUMENT = "DISTRO_SKIP_GROUP";

    private static final String TEMPTO_TEST_ARG = "-t";
    private static final String TEMPTO_GROUP_ARG = "-g";
    private static final String TEMPTO_EXCLUDE_GROUP_ARG = "-x";
    private static final String TEMPTO_EXCLUDE_TEST_ARG = "-e";

    private final Class environment;
    private final Map extraOptions;
    private final List groups;
    private final List excludedGroups;
    private final List tests;
    private final List excludedTests;

    public SuiteTestRun(
            Class environment,
            Map extraOptions,
            List groups,
            List excludedGroups,
            List tests,
            List excludedTests)
    {
        this.environment = requireNonNull(environment, "environment is null");
        this.extraOptions = ImmutableMap.copyOf(requireNonNull(extraOptions, "extraOptions is null"));
        this.groups = requireNonNull(groups, "groups is null");
        this.excludedGroups = requireNonNull(excludedGroups, "excludedGroups is null");
        this.tests = requireNonNull(tests, "tests is null");
        this.excludedTests = requireNonNull(excludedTests, "excludedTests is null");
    }

    public Class getEnvironment()
    {
        return environment;
    }

    public String getEnvironmentName()
    {
        return nameForEnvironmentClass(environment);
    }

    public Map getExtraOptions()
    {
        return extraOptions;
    }

    public List getGroups()
    {
        return groups;
    }

    public List getExcludedGroups()
    {
        return ImmutableList.builder()
                .addAll(excludedGroups)
                .addAll(splitValueFromEnv(SKIP_GROUP_ARGUMENT))
                .build();
    }

    public List getTests()
    {
        return tests;
    }

    public List getExcludedTests()
    {
        return ImmutableList.builder()
                .addAll(excludedTests)
                .addAll(splitValueFromEnv(SKIP_TEST_ARGUMENT))
                .build();
    }

    public List getTemptoRunArguments()
    {
        ImmutableList.Builder arguments = ImmutableList.builder();
        Joiner joiner = Joiner.on(",");

        if (!groups.isEmpty()) {
            arguments.add(TEMPTO_GROUP_ARG, joiner.join(groups));
        }

        if (!excludedGroups.isEmpty()) {
            arguments.add(TEMPTO_EXCLUDE_GROUP_ARG, joiner.join(excludedGroups));
        }

        if (!tests.isEmpty()) {
            arguments.add(TEMPTO_TEST_ARG, joiner.join(tests));
        }

        if (!excludedTests.isEmpty()) {
            arguments.add(TEMPTO_EXCLUDE_TEST_ARG, joiner.join(excludedTests));
        }

        return arguments.build();
    }

    public SuiteTestRun withConfigApplied(EnvironmentConfig config)
    {
        return new SuiteTestRun(
                environment,
                extraOptions,
                getGroups(),
                merge(getExcludedGroups(), config.getExcludedGroups()),
                getTests(),
                merge(getExcludedTests(), config.getExcludedTests()));
    }

    private static List merge(List first, List second)
    {
        return ImmutableList.copyOf(Iterables.concat(first, second));
    }

    @Override
    public String toString()
    {
        return toStringHelper(this)
                .add("environment", getEnvironmentName())
                .add("options", getExtraOptions())
                .add("groups", getGroups())
                .add("excludedGroups", getExcludedGroups())
                .add("tests", getTests())
                .add("excludedTests", getExcludedTests())
                .toString();
    }

    private static List splitValueFromEnv(String key)
    {
        String value = getenv(key);

        if (Strings.isNullOrEmpty(value)) {
            return ImmutableList.of();
        }

        return Splitter.on(',').trimResults().omitEmptyStrings().splitToList(value);
    }

    public static Builder testOnEnvironment(Class environment)
    {
        return new Builder(environment, ImmutableMap.of());
    }

    public static Builder testOnEnvironment(Class environment, Map extraOptions)
    {
        return new Builder(environment, extraOptions);
    }

    public static class Builder
    {
        private Class environment;
        private Map extraOptions;
        private List groups = ImmutableList.of();
        private List excludedGroups = ImmutableList.of();
        private List excludedTests = ImmutableList.of();
        private List tests = ImmutableList.of();

        private Builder(Class environment, Map extraOptions)
        {
            this.environment = requireNonNull(environment, "environment is null");
            this.extraOptions = ImmutableMap.copyOf(requireNonNull(extraOptions, "extraOptions is null"));
        }

        public Builder withGroups(String... groups)
        {
            validateGroupIdentityReferences(asList(groups));
            this.groups = ImmutableList.copyOf(groups);
            return this;
        }

        public Builder withExcludedGroups(String... groups)
        {
            validateGroupIdentityReferences(asList(groups));
            this.excludedGroups = ImmutableList.copyOf(groups);
            return this;
        }

        public Builder withTests(String... tests)
        {
            this.tests = ImmutableList.copyOf(tests);
            return this;
        }

        public Builder withExcludedTests(String... tests)
        {
            this.excludedTests = ImmutableList.copyOf(tests);
            return this;
        }

        public SuiteTestRun build()
        {
            return new SuiteTestRun(environment, extraOptions, groups, excludedGroups, tests, excludedTests);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy