2018-10-31 14:30:13 +00:00
|
|
|
package java
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-10-31 14:30:13 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDriver_parseJavaVersionOutput(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2018-10-31 14:30:13 +00:00
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
output string
|
|
|
|
version string
|
|
|
|
runtime string
|
|
|
|
vm string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"OracleJDK",
|
2021-06-15 19:14:58 +00:00
|
|
|
`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)`,
|
2018-10-31 14:30:13 +00:00
|
|
|
"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)",
|
|
|
|
},
|
2019-01-08 21:17:47 +00:00
|
|
|
{
|
|
|
|
"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)",
|
|
|
|
},
|
2018-10-31 14:30:13 +00:00
|
|
|
{
|
|
|
|
"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)",
|
|
|
|
},
|
2019-02-12 18:41:26 +00:00
|
|
|
{
|
|
|
|
"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",
|
|
|
|
},
|
2021-06-15 19:14:58 +00:00
|
|
|
{
|
|
|
|
"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)`,
|
|
|
|
},
|
2018-10-31 14:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
2021-06-15 19:14:58 +00:00
|
|
|
jdkVersion, jdkJRE, vm := parseJavaVersionOutput(c.output)
|
|
|
|
require.Equal(t, c.version, jdkVersion)
|
|
|
|
require.Equal(t, c.runtime, jdkJRE)
|
2018-10-31 14:30:13 +00:00
|
|
|
require.Equal(t, c.vm, vm)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDriver_javaVersionInfo(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-10-31 14:30:13 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("test requires bash to run")
|
|
|
|
}
|
|
|
|
|
|
|
|
initCmd := javaVersionCommand
|
|
|
|
defer func() {
|
|
|
|
javaVersionCommand = initCmd
|
|
|
|
}()
|
|
|
|
|
|
|
|
javaVersionCommand = []string{
|
|
|
|
"/bin/sh", "-c",
|
2021-06-15 19:14:58 +00:00
|
|
|
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)`),
|
2018-10-31 14:30:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 19:14:58 +00:00
|
|
|
version, jdkJRE, vm, err := javaVersionInfo()
|
2018-10-31 14:30:13 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "1.7.0_80", version)
|
2021-06-15 19:14:58 +00:00
|
|
|
require.Equal(t, "Java(TM) SE Runtime Environment (build 1.7.0_80-b15)", jdkJRE)
|
2018-10-31 14:30:13 +00:00
|
|
|
require.Equal(t, "Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)", vm)
|
|
|
|
|
|
|
|
}
|
2019-02-12 18:41:26 +00:00
|
|
|
|
|
|
|
func TestDriver_javaVersionInfo_UnexpectedOutput(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-02-12 18:41:26 +00:00
|
|
|
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"),
|
|
|
|
}
|
|
|
|
|
2021-06-15 19:14:58 +00:00
|
|
|
version, jdkJRE, vm, err := javaVersionInfo()
|
2019-02-12 18:41:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "", version)
|
2021-06-15 19:14:58 +00:00
|
|
|
require.Equal(t, "", jdkJRE)
|
2019-02-12 18:41:26 +00:00
|
|
|
require.Equal(t, "", vm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDriver_javaVersionInfo_JavaVersionFails(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-02-12 18:41:26 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("test requires bash to run")
|
|
|
|
}
|
|
|
|
|
|
|
|
initCmd := javaVersionCommand
|
|
|
|
defer func() {
|
|
|
|
javaVersionCommand = initCmd
|
|
|
|
}()
|
|
|
|
|
|
|
|
javaVersionCommand = []string{
|
|
|
|
"/bin/sh", "-c",
|
|
|
|
"exit 127",
|
|
|
|
}
|
|
|
|
|
2021-06-15 19:14:58 +00:00
|
|
|
version, jdkJRE, vm, err := javaVersionInfo()
|
2019-02-12 18:41:26 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "failed to check java version")
|
|
|
|
|
|
|
|
require.Equal(t, "", version)
|
2021-06-15 19:14:58 +00:00
|
|
|
require.Equal(t, "", jdkJRE)
|
2019-02-12 18:41:26 +00:00
|
|
|
require.Equal(t, "", vm)
|
|
|
|
}
|