open-nomad/drivers/java/utils_test.go
Mahmood Ali aec9120994
drivers/java: restore 0.8.7 java version detection (#5317)
Restore 0.8.x behavior where java driver is marked as detected when
`java -version` exits with 0 but returns unexpected output.

Furthermore, we restore behavior when `java -version` where we parse the
first three lines of `java -version` but ignore rest.

If `java -version` returns less than 3 lines, Nomad 0.8.7 would panic.
In this implementation, we'd still mark java as detected but returns
empty version.

The 0.8.7 logic for detecting java version is found in
https://github.com/hashicorp/nomad/blob/v0.8.7/client/driver/java.go#L132-L172
.

I punt on revamping how we can be more resilient to java -version
syntax, and aimed for preserving existing behavior instead.
2019-02-12 13:41:26 -05:00

153 lines
4 KiB
Go

package java
import (
"fmt"
"runtime"
"testing"
"github.com/stretchr/testify/require"
)
const oracleJDKOutput = `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)
`
func TestDriver_parseJavaVersionOutput(t *testing.T) {
cases := []struct {
name string
output string
version string
runtime string
vm string
}{
{
"OracleJDK",
oracleJDKOutput,
"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",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
version, runtime, vm := parseJavaVersionOutput(c.output)
require.Equal(t, c.version, version)
require.Equal(t, c.runtime, runtime)
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", oracleJDKOutput),
}
version, runtime, 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)", runtime)
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, runtime, vm, err := javaVersionInfo()
require.NoError(t, err)
require.Equal(t, "", version)
require.Equal(t, "", runtime)
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, runtime, vm, err := javaVersionInfo()
require.Error(t, err)
require.Contains(t, err.Error(), "failed to check java version")
require.Equal(t, "", version)
require.Equal(t, "", runtime)
require.Equal(t, "", vm)
}