1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.asuite.clearcut.junit.listener; 18 19 import java.io.BufferedReader; 20 import java.io.IOException; 21 import java.io.InputStreamReader; 22 import java.net.InetAddress; 23 import java.net.UnknownHostException; 24 import java.util.Arrays; 25 import java.util.List; 26 import java.util.Optional; 27 import java.util.UUID; 28 import java.util.concurrent.TimeUnit; 29 30 public class EnvironmentInformation { 31 32 // LINT.IfChange 33 private static final List<String> GOOGLE_HOSTNAMES = Arrays.asList(".google.com", "c.googlers.com"); 34 // LINT.ThenChange(/tools/asuite/atest/constants_default.py) 35 private static String ROBOLECTRIC_SYSUI_EXTENSION_CLASS = "com.google.android.sysui.ToTSdkProvider"; 36 private static String GRADLE = "worker.org.gradle.process.internal.worker.GradleWorkerMain"; 37 private static String ROBOLECTRIC_CLASS = "org.robolectric.Robolectric"; 38 private static final String GOOGLE_EMAIL = "@google.com"; 39 40 static { 41 System.out.println("ENVIRONMENT:"); 42 System.getenv().forEach( (k,v) -> System.out.println(k + " : "+v)); 43 } 44 hasClassInLoader(String className)45 private static boolean hasClassInLoader(String className) { 46 try { 47 Thread.currentThread().getContextClassLoader().loadClass( 48 className); 49 return true; 50 } catch (ClassNotFoundException ex) { 51 return false; 52 } 53 } 54 isSysUIRoboTest()55 public static boolean isSysUIRoboTest() { 56 return hasClassInLoader(ROBOLECTRIC_SYSUI_EXTENSION_CLASS); 57 } 58 isGradleTest()59 public static boolean isGradleTest() { 60 return hasClassInLoader(GRADLE); 61 } 62 isRoboTest()63 public static boolean isRoboTest() { 64 return hasClassInLoader(ROBOLECTRIC_CLASS); 65 } 66 isDebugging()67 public static boolean isDebugging() { 68 try { 69 return java.lang.management.ManagementFactory.getRuntimeMXBean(). 70 getInputArguments().toString().contains("-agentlib:jdwp"); 71 } catch (Throwable t) { 72 return false; 73 } 74 } 75 76 getGitEmail()77 public static Optional<String> getGitEmail() { 78 return executeCommand("git", "config", "--get", "user.email"); 79 } 80 81 /** 82 * @return Optional of git username, if email and ends in @google.com 83 */ getGitUserIfGoogleEmail(Optional<String> email)84 public static Optional<String> getGitUserIfGoogleEmail(Optional<String> email) { 85 if (email.isPresent()) { 86 String emailStr = email.get(); 87 if (emailStr.trim().endsWith(GOOGLE_EMAIL)) { 88 return Optional.of(emailStr.trim().replace(GOOGLE_EMAIL, "")); 89 } 90 } 91 return Optional.empty(); 92 } 93 executeCommand(String... command)94 static Optional<String> executeCommand(String... command) { 95 try { 96 ProcessBuilder pb = new ProcessBuilder(command); 97 Process process = pb.start(); 98 BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 99 String line; 100 StringBuilder output = new StringBuilder(); 101 while ((line = reader.readLine()) != null) { 102 output.append(line).append("\n"); 103 } 104 if (process.waitFor(60, TimeUnit.SECONDS)) { 105 int exitCode = process.exitValue(); 106 if (exitCode == 0) { 107 return Optional.of(output.toString()); 108 } 109 } 110 } catch (IOException | InterruptedException e) { 111 System.out.println(Client.class.getName() + " could not execute command:" + 112 String.join(" ", command)); 113 e.printStackTrace(); 114 } 115 return Optional.empty(); 116 } 117 isGoogleDomain()118 public static boolean isGoogleDomain() { 119 try { 120 String hostname = InetAddress.getLocalHost().getHostName(); 121 if (GOOGLE_HOSTNAMES.stream().anyMatch(hostname::endsWith)) { 122 return true; 123 } 124 } catch (UnknownHostException e) { 125 System.err.println("Could not determine if google host: " + e.getMessage()); 126 } 127 return false; 128 } 129 } 130