Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client/fingerprint/java: improve java version string regex matching #10765

Merged
merged 1 commit into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions drivers/java/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
}
}

version, runtime, vm, err := javaVersionInfo()
version, jdkJRE, vm, err := javaVersionInfo()
if err != nil {
// return no error, as it isn't an error to not find java, it just means we
// can't use it.
Expand All @@ -367,7 +367,7 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {

fp.Attributes[driverAttr] = pstructs.NewBoolAttribute(true)
fp.Attributes[driverVersionAttr] = pstructs.NewStringAttribute(version)
fp.Attributes["driver.java.runtime"] = pstructs.NewStringAttribute(runtime)
fp.Attributes["driver.java.runtime"] = pstructs.NewStringAttribute(jdkJRE)
fp.Attributes["driver.java.vm"] = pstructs.NewStringAttribute(vm)

return fp
Expand Down
7 changes: 5 additions & 2 deletions drivers/java/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func javaVersionInfo() (version, runtime, vm string, err error) {
return
}

var (
javaVersionRe = regexp.MustCompile(`([.\d_]+)`)
)

func parseJavaVersionOutput(infoString string) (version, runtime, vm string) {
infoString = strings.TrimSpace(infoString)

Expand All @@ -65,8 +69,7 @@ func parseJavaVersionOutput(infoString string) (version, runtime, vm string) {

versionString := strings.TrimSpace(lines[0])

re := regexp.MustCompile(`version "([^"]*)"`)
if match := re.FindStringSubmatch(lines[0]); len(match) == 2 {
if match := javaVersionRe.FindStringSubmatch(versionString); len(match) == 2 {
versionString = match[1]
}

Expand Down
42 changes: 25 additions & 17 deletions drivers/java/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import (
"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
Expand All @@ -23,7 +18,9 @@ func TestDriver_parseJavaVersionOutput(t *testing.T) {
}{
{
"OracleJDK",
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)`,
"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)",
Expand Down Expand Up @@ -69,14 +66,22 @@ func TestDriver_parseJavaVersionOutput(t *testing.T) {
"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) {
version, runtime, vm := parseJavaVersionOutput(c.output)

require.Equal(t, c.version, version)
require.Equal(t, c.runtime, runtime)
jdkVersion, jdkJRE, vm := parseJavaVersionOutput(c.output)
require.Equal(t, c.version, jdkVersion)
require.Equal(t, c.runtime, jdkJRE)
require.Equal(t, c.vm, vm)
})
}
Expand All @@ -94,13 +99,16 @@ func TestDriver_javaVersionInfo(t *testing.T) {

javaVersionCommand = []string{
"/bin/sh", "-c",
fmt.Sprintf("printf '%%s\n' '%s' >/dev/stderr", oracleJDKOutput),
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, runtime, vm, err := javaVersionInfo()
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)", runtime)
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)

}
Expand All @@ -120,10 +128,10 @@ func TestDriver_javaVersionInfo_UnexpectedOutput(t *testing.T) {
fmt.Sprintf("printf '%%s\n' '%s' >/dev/stderr", "unexpected java -version output"),
}

version, runtime, vm, err := javaVersionInfo()
version, jdkJRE, vm, err := javaVersionInfo()
require.NoError(t, err)
require.Equal(t, "", version)
require.Equal(t, "", runtime)
require.Equal(t, "", jdkJRE)
require.Equal(t, "", vm)
}

Expand All @@ -142,11 +150,11 @@ func TestDriver_javaVersionInfo_JavaVersionFails(t *testing.T) {
"exit 127",
}

version, runtime, vm, err := javaVersionInfo()
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, "", runtime)
require.Equal(t, "", jdkJRE)
require.Equal(t, "", vm)
}