github.com/prysmaticlabs/prysm@v1.4.4/shared/prereq/prereq_test.go (about)

     1  package prereq
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
     9  	logTest "github.com/sirupsen/logrus/hooks/test"
    10  )
    11  
    12  func TestMeetsMinPlatformReqs(t *testing.T) {
    13  	// Linux
    14  	runtimeOS = "linux"
    15  	runtimeArch = "amd64"
    16  	meetsReqs, err := meetsMinPlatformReqs(context.Background())
    17  	require.Equal(t, true, meetsReqs)
    18  	require.NoError(t, err)
    19  	runtimeArch = "arm64"
    20  	meetsReqs, err = meetsMinPlatformReqs(context.Background())
    21  	require.Equal(t, true, meetsReqs)
    22  	require.NoError(t, err)
    23  	// mips64 is not supported
    24  	runtimeArch = "mips64"
    25  	meetsReqs, err = meetsMinPlatformReqs(context.Background())
    26  	require.Equal(t, false, meetsReqs)
    27  	require.NoError(t, err)
    28  
    29  	// Mac OS X
    30  	// In this function we'll set the execShellOutput package variable to another function that will 'mock' the shell
    31  	execShellOutput = func(ctx context.Context, command string, args ...string) (string, error) {
    32  		return "", errors.New("error while running command")
    33  	}
    34  	runtimeOS = "darwin"
    35  	runtimeArch = "amd64"
    36  	meetsReqs, err = meetsMinPlatformReqs(context.Background())
    37  	require.Equal(t, false, meetsReqs)
    38  	require.ErrorContains(t, "error obtaining MacOS version", err)
    39  
    40  	// Insufficient version
    41  	execShellOutput = func(ctx context.Context, command string, args ...string) (string, error) {
    42  		return "10.4", nil
    43  	}
    44  	meetsReqs, err = meetsMinPlatformReqs(context.Background())
    45  	require.Equal(t, false, meetsReqs)
    46  	require.NoError(t, err)
    47  
    48  	// Just-sufficient older version
    49  	execShellOutput = func(ctx context.Context, command string, args ...string) (string, error) {
    50  		return "10.14", nil
    51  	}
    52  	meetsReqs, err = meetsMinPlatformReqs(context.Background())
    53  	require.Equal(t, true, meetsReqs)
    54  	require.NoError(t, err)
    55  
    56  	// Sufficient newer version
    57  	execShellOutput = func(ctx context.Context, command string, args ...string) (string, error) {
    58  		return "10.15.7", nil
    59  	}
    60  	meetsReqs, err = meetsMinPlatformReqs(context.Background())
    61  	require.Equal(t, true, meetsReqs)
    62  	require.NoError(t, err)
    63  
    64  	// Handling abnormal response
    65  	execShellOutput = func(ctx context.Context, command string, args ...string) (string, error) {
    66  		return "tiger.lion", nil
    67  	}
    68  	meetsReqs, err = meetsMinPlatformReqs(context.Background())
    69  	require.Equal(t, false, meetsReqs)
    70  	require.ErrorContains(t, "error parsing version", err)
    71  
    72  	// Windows
    73  	runtimeOS = "windows"
    74  	runtimeArch = "amd64"
    75  	meetsReqs, err = meetsMinPlatformReqs(context.Background())
    76  	require.Equal(t, true, meetsReqs)
    77  	require.NoError(t, err)
    78  	runtimeArch = "arm64"
    79  	meetsReqs, err = meetsMinPlatformReqs(context.Background())
    80  	require.Equal(t, false, meetsReqs)
    81  	require.NoError(t, err)
    82  }
    83  
    84  func TestParseVersion(t *testing.T) {
    85  	version, err := parseVersion("1.2.3", 3, ".")
    86  	require.DeepEqual(t, version, []int{1, 2, 3})
    87  	require.NoError(t, err)
    88  
    89  	version, err = parseVersion("6 .7 . 8  ", 3, ".")
    90  	require.DeepEqual(t, version, []int{6, 7, 8})
    91  	require.NoError(t, err)
    92  
    93  	version, err = parseVersion("10,3,5,6", 4, ",")
    94  	require.DeepEqual(t, version, []int{10, 3, 5, 6})
    95  	require.NoError(t, err)
    96  
    97  	version, err = parseVersion("4;6;8;10;11", 3, ";")
    98  	require.DeepEqual(t, version, []int{4, 6, 8})
    99  	require.NoError(t, err)
   100  
   101  	_, err = parseVersion("10.11", 3, ".")
   102  	require.ErrorContains(t, "insufficient information about version", err)
   103  }
   104  
   105  func TestWarnIfNotSupported(t *testing.T) {
   106  	runtimeOS = "linux"
   107  	runtimeArch = "amd64"
   108  	hook := logTest.NewGlobal()
   109  	WarnIfPlatformNotSupported(context.Background())
   110  	require.LogsDoNotContain(t, hook, "Failed to detect host platform")
   111  	require.LogsDoNotContain(t, hook, "platform is not supported")
   112  
   113  	execShellOutput = func(ctx context.Context, command string, args ...string) (string, error) {
   114  		return "tiger.lion", nil
   115  	}
   116  	runtimeOS = "darwin"
   117  	runtimeArch = "amd64"
   118  	hook = logTest.NewGlobal()
   119  	WarnIfPlatformNotSupported(context.Background())
   120  	require.LogsContain(t, hook, "Failed to detect host platform")
   121  	require.LogsContain(t, hook, "error parsing version")
   122  
   123  	runtimeOS = "falseOs"
   124  	runtimeArch = "falseArch"
   125  	hook = logTest.NewGlobal()
   126  	WarnIfPlatformNotSupported(context.Background())
   127  	require.LogsContain(t, hook, "platform is not supported")
   128  }