open-nomad/drivers/java/utils_test.go
Seth Hoenig 8f493cfa89 client/fingerprint/java: improve java version string regex matching
This PR improves the regular expression used for matching the java
version string, which varies a lot depending on the java vendor and
version.

These are the example strings we now test for:

java version "1.7.0_80"
openjdk version "11.0.1" 2018-10-16
openjdk version "11.0.1" 2018-10-16
java version "1.6.0_36"
openjdk version "1.8.0_192"
openjdk 11.0.11 2021-04-20 LTS

The last one is a new test added on behalf of #6081, which is
still broken on today's CentOS 7 default JDK package.

openjdk 11.0.11 2021-04-20 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.11+9-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.11+9-LTS, mixed mode, sharing)

==> Evaluation "21c6caf7" finished with status "complete" but failed to place all allocations:
    Task Group "example" (failed to place 1 allocation):
      * Constraint "${driver.java.version} >= 11.0.0": 1 nodes excluded by filter
    Evaluation "2b737d48" waiting for additional capacity to place remainder

Fixes #6081
2021-06-15 14:15:01 -05:00

161 lines
4.4 KiB
Go

package java
import (
"fmt"
"runtime"
"testing"
"github.com/stretchr/testify/require"
)
func TestDriver_parseJavaVersionOutput(t *testing.T) {
cases := []struct {
name string
output string
version string
runtime string
vm string
}{
{
"OracleJDK",
`java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)`,
"1.7.0_80",
"Java(TM) SE Runtime Environment (build 1.7.0_80-b15)",
"Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)",
},
{
"OpenJDK",
`openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)`,
"11.0.1",
"OpenJDK Runtime Environment 18.9 (build 11.0.1+13)",
"OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)",
},
{
"OpenJDK",
`Picked up _JAVA_OPTIONS: -Xmx2048m -Xms512m
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)`,
"11.0.1",
"OpenJDK Runtime Environment 18.9 (build 11.0.1+13)",
"OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)",
},
{
"IcedTea",
`java version "1.6.0_36"
OpenJDK Runtime Environment (IcedTea6 1.13.8) (6b36-1.13.8-0ubuntu1~12.04)
OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)`,
"1.6.0_36",
"OpenJDK Runtime Environment (IcedTea6 1.13.8) (6b36-1.13.8-0ubuntu1~12.04)",
"OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)",
},
{
"Eclipse OpenJ9",
`openjdk version "1.8.0_192"
OpenJDK Runtime Environment (build 1.8.0_192-b12_openj9)
Eclipse OpenJ9 VM (build openj9-0.11.0, JRE 1.8.0 Linux amd64-64-Bit Compressed References
20181107_95 (JIT enabled, AOT enabled)
OpenJ9 - 090ff9dcd
OMR - ea548a66
JCL - b5a3affe73 based on jdk8u192-b12)`,
"1.8.0_192",
"OpenJDK Runtime Environment (build 1.8.0_192-b12_openj9)",
"Eclipse OpenJ9 VM (build openj9-0.11.0, JRE 1.8.0 Linux amd64-64-Bit Compressed References",
},
{
"OpenJDK on CentOS 7",
`openjdk 11.0.11 2021-04-20 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.11+9-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.11+9-LTS, mixed mode, sharing)`,
`11.0.11`,
`OpenJDK Runtime Environment 18.9 (build 11.0.11+9-LTS)`,
`OpenJDK 64-Bit Server VM 18.9 (build 11.0.11+9-LTS, mixed mode, sharing)`,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
jdkVersion, jdkJRE, vm := parseJavaVersionOutput(c.output)
require.Equal(t, c.version, jdkVersion)
require.Equal(t, c.runtime, jdkJRE)
require.Equal(t, c.vm, vm)
})
}
}
func TestDriver_javaVersionInfo(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("test requires bash to run")
}
initCmd := javaVersionCommand
defer func() {
javaVersionCommand = initCmd
}()
javaVersionCommand = []string{
"/bin/sh", "-c",
fmt.Sprintf("printf '%%s\n' '%s' >/dev/stderr",
`java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)`),
}
version, jdkJRE, vm, err := javaVersionInfo()
require.NoError(t, err)
require.Equal(t, "1.7.0_80", version)
require.Equal(t, "Java(TM) SE Runtime Environment (build 1.7.0_80-b15)", jdkJRE)
require.Equal(t, "Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)", vm)
}
func TestDriver_javaVersionInfo_UnexpectedOutput(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("test requires bash to run")
}
initCmd := javaVersionCommand
defer func() {
javaVersionCommand = initCmd
}()
javaVersionCommand = []string{
"/bin/sh", "-c",
fmt.Sprintf("printf '%%s\n' '%s' >/dev/stderr", "unexpected java -version output"),
}
version, jdkJRE, vm, err := javaVersionInfo()
require.NoError(t, err)
require.Equal(t, "", version)
require.Equal(t, "", jdkJRE)
require.Equal(t, "", vm)
}
func TestDriver_javaVersionInfo_JavaVersionFails(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("test requires bash to run")
}
initCmd := javaVersionCommand
defer func() {
javaVersionCommand = initCmd
}()
javaVersionCommand = []string{
"/bin/sh", "-c",
"exit 127",
}
version, jdkJRE, vm, err := javaVersionInfo()
require.Error(t, err)
require.Contains(t, err.Error(), "failed to check java version")
require.Equal(t, "", version)
require.Equal(t, "", jdkJRE)
require.Equal(t, "", vm)
}