LCOV - code coverage report
Current view: top level - testing - IndexVersions.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 43 45 95.6 %
Date: 2022-11-19 15:00:39 Functions: 6 7 85.7 %

          Line data    Source code
       1             : // Copyright (C) 2017 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.testing;
      16             : 
      17             : import static com.google.common.base.Preconditions.checkArgument;
      18             : import static java.util.stream.Collectors.toMap;
      19             : 
      20             : import com.google.common.annotations.VisibleForTesting;
      21             : import com.google.common.base.Splitter;
      22             : import com.google.common.base.Strings;
      23             : import com.google.common.collect.ImmutableList;
      24             : import com.google.common.primitives.Ints;
      25             : import com.google.gerrit.index.Schema;
      26             : import com.google.gerrit.index.SchemaDefinitions;
      27             : import java.util.ArrayList;
      28             : import java.util.List;
      29             : import java.util.Map;
      30             : import java.util.NavigableMap;
      31             : import org.eclipse.jgit.lib.Config;
      32             : 
      33           0 : public class IndexVersions {
      34             :   static final String ALL = "all";
      35             :   static final String CURRENT = "current";
      36             :   static final String PREVIOUS = "previous";
      37             : 
      38             :   /**
      39             :    * Returns the index versions from {@link IndexVersions#get(SchemaDefinitions)} without the latest
      40             :    * schema version.
      41             :    *
      42             :    * @param schemaDef the schema definition
      43             :    * @return the index versions from {@link IndexVersions#get(SchemaDefinitions)} without the latest
      44             :    *     schema version
      45             :    */
      46             :   public static <V> ImmutableList<Integer> getWithoutLatest(SchemaDefinitions<V> schemaDef) {
      47           8 :     List<Integer> schemaVersions = new ArrayList<>(get(schemaDef));
      48           8 :     schemaVersions.remove(Integer.valueOf(schemaDef.getLatest().getVersion()));
      49           8 :     return ImmutableList.copyOf(schemaVersions);
      50             :   }
      51             : 
      52             :   /**
      53             :    * Returns the schema versions against which the query tests should be executed.
      54             :    *
      55             :    * <p>The schema versions are read from the '<schema-name>_INDEX_VERSIONS' env var if it is set,
      56             :    * e.g. 'ACCOUNTS_INDEX_VERSIONS', 'CHANGES_INDEX_VERSIONS', 'GROUPS_INDEX_VERSIONS'.
      57             :    *
      58             :    * <p>If schema versions were not specified by an env var, they are read from the
      59             :    * 'gerrit.index.<schema-name>.versions' system property, e.g. 'gerrit.index.accounts.version',
      60             :    * 'gerrit.index.changes.version', 'gerrit.index.groups.version'.
      61             :    *
      62             :    * <p>As value a comma-separated list of schema versions is expected. {@code current} can be used
      63             :    * for the latest schema version and {@code previous} is resolved to the second last schema
      64             :    * version. Alternatively the value can also be {@code all} for all schema versions.
      65             :    *
      66             :    * <p>If schema versions were neither specified by an env var nor by a system property, the
      67             :    * current and the second last schema versions are returned. If there is no other schema version
      68             :    * than the current schema version, only the current schema version is returned.
      69             :    *
      70             :    * @param schemaDef the schema definition
      71             :    * @return the schema versions against which the query tests should be executed
      72             :    * @throws IllegalArgumentException if the value of the env var or system property is invalid or
      73             :    *     if any of the specified schema versions doesn't exist
      74             :    */
      75             :   public static <V> ImmutableList<Integer> get(SchemaDefinitions<V> schemaDef) {
      76           8 :     String envVar = schemaDef.getName().toUpperCase() + "_INDEX_VERSIONS";
      77           8 :     String value = System.getenv(envVar);
      78           8 :     if (!Strings.isNullOrEmpty(value)) {
      79           0 :       return get(schemaDef, "env variable " + envVar, value);
      80             :     }
      81             : 
      82           8 :     String systemProperty = "gerrit.index." + schemaDef.getName().toLowerCase() + ".versions";
      83           8 :     value = System.getProperty(systemProperty);
      84           8 :     return get(schemaDef, "system property " + systemProperty, value);
      85             :   }
      86             : 
      87             :   @VisibleForTesting
      88             :   static <V> ImmutableList<Integer> get(SchemaDefinitions<V> schemaDef, String name, String value) {
      89           9 :     if (value != null) {
      90           1 :       value = value.trim();
      91             :     }
      92             : 
      93           9 :     NavigableMap<Integer, Schema<V>> schemas = schemaDef.getSchemas();
      94           9 :     if (!Strings.isNullOrEmpty(value)) {
      95           1 :       if (ALL.equals(value)) {
      96           1 :         return ImmutableList.copyOf(schemas.keySet());
      97             :       }
      98             : 
      99           1 :       ImmutableList.Builder<Integer> versions = ImmutableList.builder();
     100           1 :       for (String s : Splitter.on(',').trimResults().split(value)) {
     101           1 :         if (CURRENT.equals(s)) {
     102           1 :           versions.add(schemaDef.getLatest().getVersion());
     103           1 :         } else if (PREVIOUS.equals(s)) {
     104           1 :           checkArgument(schemaDef.getPrevious() != null, "previous version does not exist");
     105           1 :           versions.add(schemaDef.getPrevious().getVersion());
     106             :         } else {
     107           1 :           Integer version = Ints.tryParse(s);
     108           1 :           checkArgument(version != null, "Invalid value for %s: %s", name, s);
     109           1 :           checkArgument(
     110           1 :               schemas.containsKey(version),
     111             :               "Index version %s that was specified by %s not found." + " Possible versions are: %s",
     112             :               version,
     113             :               name,
     114           1 :               schemas.keySet());
     115           1 :           versions.add(version);
     116             :         }
     117           1 :       }
     118           1 :       return versions.build();
     119             :     }
     120             : 
     121           9 :     ImmutableList.Builder<Integer> schemaVersions = ImmutableList.builderWithExpectedSize(2);
     122           9 :     if (schemaDef.getPrevious() != null) {
     123           9 :       schemaVersions.add(schemaDef.getPrevious().getVersion());
     124             :     }
     125           9 :     schemaVersions.add(schemaDef.getLatest().getVersion());
     126           9 :     return schemaVersions.build();
     127             :   }
     128             : 
     129             :   public static <V> Map<String, Config> asConfigMap(
     130             :       SchemaDefinitions<V> schemaDef,
     131             :       List<Integer> schemaVersions,
     132             :       String testSuiteNamePrefix,
     133             :       Config baseConfig) {
     134           8 :     return schemaVersions.stream()
     135           8 :         .collect(
     136           8 :             toMap(
     137           8 :                 i -> testSuiteNamePrefix + i,
     138             :                 i -> {
     139           8 :                   Config cfg = baseConfig;
     140           8 :                   cfg.setInt(
     141           8 :                       "index", "lucene", schemaDef.getName().toLowerCase() + "TestVersion", i);
     142           8 :                   return cfg;
     143             :                 }));
     144             :   }
     145             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750