test/lib/jdk/test/lib/SA/SATestUtils.java
author lmesnik
Fri, 27 Sep 2019 10:48:23 -0700
changeset 58384 9a3a700ca571
parent 53267 0b6d6db878b6
permissions -rw-r--r--
8230942: Support compressed cores in SA tests Reviewed-by: dholmes, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53267
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
     1
/*
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
     4
 *
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
     8
 *
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    13
 * accompanied this code).
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    14
 *
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    18
 *
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    21
 * questions.
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    22
 */
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    23
package jdk.test.lib.SA;
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    24
58384
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    25
import java.io.File;
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    26
import java.io.FileInputStream;
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    27
import java.io.FileOutputStream;
53267
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    28
import java.io.IOException;
58384
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    29
import java.util.ArrayList;
53267
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    30
import java.util.List;
58384
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    31
import java.util.concurrent.TimeUnit;
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    32
import java.util.zip.GZIPInputStream;
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    33
53267
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    34
import jdk.test.lib.Asserts;
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    35
import jdk.test.lib.Platform;
58384
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    36
import jtreg.SkippedException;
53267
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    37
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    38
public class SATestUtils {
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    39
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    40
    public static boolean canAddPrivileges()
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    41
       throws IOException, InterruptedException {
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    42
       List<String> echoList = new ArrayList<String>();
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    43
       echoList.add("sudo");
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    44
       echoList.add("-E");
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    45
       echoList.add("/bin/echo");
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    46
       echoList.add("'Checking for sudo'");
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    47
       ProcessBuilder pb = new ProcessBuilder(echoList);
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    48
       Process echoProcess = pb.start();
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    49
       if (echoProcess.waitFor(60, TimeUnit.SECONDS) == false) {
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    50
           // 'sudo' has been added but we don't have a no-password
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    51
           // entry for the user in the /etc/sudoers list. Could
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    52
           // have timed out waiting for the password. Skip the
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    53
           // test if there is a timeout here.
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    54
           System.out.println("Timed out waiting for the password to be entered.");
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    55
           echoProcess.destroyForcibly();
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    56
           return false;
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    57
       }
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    58
       if (echoProcess.exitValue() == 0) {
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    59
           return true;
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    60
       }
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    61
       java.io.InputStream is = echoProcess.getErrorStream();
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    62
       String err = new String(is.readAllBytes());
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    63
       System.out.println(err);
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    64
       // 'sudo' has been added but we don't have a no-password
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    65
       // entry for the user in the /etc/sudoers list. Check for
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    66
       // the sudo error message and skip the test.
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    67
       if (err.contains("no tty present") ||
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    68
           err.contains("a password is required")) {
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    69
           return false;
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    70
       } else {
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    71
           throw new Error("Unknown Error from 'sudo'");
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    72
       }
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    73
    }
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    74
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    75
    public static List<String> addPrivileges(List<String> cmdStringList)
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    76
        throws IOException {
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    77
        Asserts.assertTrue(Platform.isOSX());
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    78
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    79
        System.out.println("Adding 'sudo -E' to the command.");
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    80
        List<String> outStringList = new ArrayList<String>();
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    81
        outStringList.add("sudo");
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    82
        outStringList.add("-E");
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    83
        outStringList.addAll(cmdStringList);
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    84
        return outStringList;
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
    85
    }
58384
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    86
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    87
    public static void unzipCores(File dir) {
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    88
        File[] gzCores = dir.listFiles((directory, name) -> name.matches("core(\\.\\d+)?\\.gz"));
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    89
        for (File gzCore : gzCores) {
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    90
            String coreFileName = gzCore.getName().replace(".gz", "");
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    91
            System.out.println("Unzipping core into " + coreFileName);
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    92
            try (GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(gzCore));
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    93
                 FileOutputStream fos = new FileOutputStream(coreFileName)) {
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    94
                byte[] buffer = new byte[1024];
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    95
                int length;
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    96
                while ((length = gzis.read(buffer)) > 0) {
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    97
                    fos.write(buffer, 0, length);
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    98
                }
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
    99
            } catch (IOException e) {
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
   100
                throw new SkippedException("Not able to unzip file: " + gzCore.getAbsolutePath(), e);
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
   101
            }
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
   102
        }
9a3a700ca571 8230942: Support compressed cores in SA tests
lmesnik
parents: 53267
diff changeset
   103
    }
53267
0b6d6db878b6 8215544: SA: Modify ClhsdbLauncher to add sudo privileges to enable MacOS tests on Mach5
jgeorge
parents:
diff changeset
   104
}