Line data Source code
1 : // Copyright (C) 2016 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 : 19 : import com.google.common.base.Enums; 20 : import com.google.common.base.Strings; 21 : 22 : /** 23 : * Whether to enable/disable tests using SSH by inspecting the global environment. 24 : * 25 : * <p>Acceptance tests should generally not inspect this directly, since SSH may also be disabled on 26 : * a per-class or per-method basis. Inject {@code @SshEnabled boolean} instead. 27 : */ 28 17 : public enum SshMode { 29 : /** Tests annotated with UseSsh will be disabled. */ 30 17 : NO, 31 : 32 : /** Tests annotated with UseSsh will be enabled. */ 33 17 : YES; 34 : 35 : private static final String ENV_VAR = "GERRIT_USE_SSH"; 36 : private static final String SYS_PROP = "gerrit.use.ssh"; 37 : 38 : public static SshMode get() { 39 17 : String value = System.getenv(ENV_VAR); 40 17 : if (Strings.isNullOrEmpty(value)) { 41 17 : value = System.getProperty(SYS_PROP); 42 : } 43 17 : if (Strings.isNullOrEmpty(value)) { 44 17 : return YES; 45 : } 46 0 : value = value.toUpperCase(); 47 0 : SshMode mode = Enums.getIfPresent(SshMode.class, value).orNull(); 48 0 : if (!Strings.isNullOrEmpty(System.getenv(ENV_VAR))) { 49 0 : checkArgument( 50 0 : mode != null, "Invalid value for env variable %s: %s", ENV_VAR, System.getenv(ENV_VAR)); 51 : } else { 52 0 : checkArgument( 53 : mode != null, 54 : "Invalid value for system property %s: %s", 55 : SYS_PROP, 56 0 : System.getProperty(SYS_PROP)); 57 : } 58 0 : return mode; 59 : } 60 : 61 : public static boolean useSsh() { 62 17 : return get() == YES; 63 : } 64 : }