github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/drivers/java/utils_test.go (about)

     1  package java
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  const oracleJDKOutput = `java version "1.7.0_80"
    12  Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
    13  Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
    14  `
    15  
    16  func TestDriver_parseJavaVersionOutput(t *testing.T) {
    17  	cases := []struct {
    18  		name    string
    19  		output  string
    20  		version string
    21  		runtime string
    22  		vm      string
    23  	}{
    24  		{
    25  			"OracleJDK",
    26  			oracleJDKOutput,
    27  			"1.7.0_80",
    28  			"Java(TM) SE Runtime Environment (build 1.7.0_80-b15)",
    29  			"Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)",
    30  		},
    31  		{
    32  			"OpenJDK",
    33  			`openjdk version "11.0.1" 2018-10-16
    34  			OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
    35  			OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)`,
    36  			"11.0.1",
    37  			"OpenJDK Runtime Environment 18.9 (build 11.0.1+13)",
    38  			"OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)",
    39  		},
    40  		{
    41  			"OpenJDK",
    42  			`Picked up _JAVA_OPTIONS: -Xmx2048m -Xms512m
    43  			openjdk version "11.0.1" 2018-10-16
    44  			OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
    45  			OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)`,
    46  			"11.0.1",
    47  			"OpenJDK Runtime Environment 18.9 (build 11.0.1+13)",
    48  			"OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)",
    49  		},
    50  		{
    51  			"IcedTea",
    52  			`java version "1.6.0_36"
    53  			 OpenJDK Runtime Environment (IcedTea6 1.13.8) (6b36-1.13.8-0ubuntu1~12.04)
    54  			 OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)`,
    55  			"1.6.0_36",
    56  			"OpenJDK Runtime Environment (IcedTea6 1.13.8) (6b36-1.13.8-0ubuntu1~12.04)",
    57  			"OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)",
    58  		},
    59  		{
    60  			"Eclipse OpenJ9",
    61  			`openjdk version "1.8.0_192"
    62  			OpenJDK Runtime Environment (build 1.8.0_192-b12_openj9)
    63  			Eclipse OpenJ9 VM (build openj9-0.11.0, JRE 1.8.0 Linux amd64-64-Bit Compressed References
    64  			20181107_95 (JIT enabled, AOT enabled)
    65  			OpenJ9 - 090ff9dcd
    66  			OMR - ea548a66
    67  			JCL - b5a3affe73 based on jdk8u192-b12)`,
    68  			"1.8.0_192",
    69  			"OpenJDK Runtime Environment (build 1.8.0_192-b12_openj9)",
    70  			"Eclipse OpenJ9 VM (build openj9-0.11.0, JRE 1.8.0 Linux amd64-64-Bit Compressed References",
    71  		},
    72  	}
    73  
    74  	for _, c := range cases {
    75  		t.Run(c.name, func(t *testing.T) {
    76  			version, runtime, vm := parseJavaVersionOutput(c.output)
    77  
    78  			require.Equal(t, c.version, version)
    79  			require.Equal(t, c.runtime, runtime)
    80  			require.Equal(t, c.vm, vm)
    81  		})
    82  	}
    83  }
    84  
    85  func TestDriver_javaVersionInfo(t *testing.T) {
    86  	if runtime.GOOS == "windows" {
    87  		t.Skip("test requires bash to run")
    88  	}
    89  
    90  	initCmd := javaVersionCommand
    91  	defer func() {
    92  		javaVersionCommand = initCmd
    93  	}()
    94  
    95  	javaVersionCommand = []string{
    96  		"/bin/sh", "-c",
    97  		fmt.Sprintf("printf '%%s\n' '%s' >/dev/stderr", oracleJDKOutput),
    98  	}
    99  
   100  	version, runtime, vm, err := javaVersionInfo()
   101  	require.NoError(t, err)
   102  	require.Equal(t, "1.7.0_80", version)
   103  	require.Equal(t, "Java(TM) SE Runtime Environment (build 1.7.0_80-b15)", runtime)
   104  	require.Equal(t, "Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)", vm)
   105  
   106  }
   107  
   108  func TestDriver_javaVersionInfo_UnexpectedOutput(t *testing.T) {
   109  	if runtime.GOOS == "windows" {
   110  		t.Skip("test requires bash to run")
   111  	}
   112  
   113  	initCmd := javaVersionCommand
   114  	defer func() {
   115  		javaVersionCommand = initCmd
   116  	}()
   117  
   118  	javaVersionCommand = []string{
   119  		"/bin/sh", "-c",
   120  		fmt.Sprintf("printf '%%s\n' '%s' >/dev/stderr", "unexpected java -version output"),
   121  	}
   122  
   123  	version, runtime, vm, err := javaVersionInfo()
   124  	require.NoError(t, err)
   125  	require.Equal(t, "", version)
   126  	require.Equal(t, "", runtime)
   127  	require.Equal(t, "", vm)
   128  }
   129  
   130  func TestDriver_javaVersionInfo_JavaVersionFails(t *testing.T) {
   131  	if runtime.GOOS == "windows" {
   132  		t.Skip("test requires bash to run")
   133  	}
   134  
   135  	initCmd := javaVersionCommand
   136  	defer func() {
   137  		javaVersionCommand = initCmd
   138  	}()
   139  
   140  	javaVersionCommand = []string{
   141  		"/bin/sh", "-c",
   142  		"exit 127",
   143  	}
   144  
   145  	version, runtime, vm, err := javaVersionInfo()
   146  	require.Error(t, err)
   147  	require.Contains(t, err.Error(), "failed to check java version")
   148  
   149  	require.Equal(t, "", version)
   150  	require.Equal(t, "", runtime)
   151  	require.Equal(t, "", vm)
   152  }