github.com/mponton/terratest@v0.44.0/modules/version-checker/version_checker_test.go (about)

     1  package version_checker
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestParamValidation(t *testing.T) {
    10  	t.Parallel()
    11  
    12  	tests := []struct {
    13  		name                 string
    14  		param                CheckVersionParams
    15  		containError         bool
    16  		expectedErrorMessage string
    17  	}{
    18  		{
    19  			name:                 "Empty Params",
    20  			param:                CheckVersionParams{},
    21  			containError:         true,
    22  			expectedErrorMessage: "set WorkingDir in params",
    23  		},
    24  		{
    25  			name: "Missing VersionConstraint",
    26  			param: CheckVersionParams{
    27  				Binary:            Docker,
    28  				VersionConstraint: "",
    29  				WorkingDir:        ".",
    30  			},
    31  			containError:         true,
    32  			expectedErrorMessage: "set VersionConstraint in params",
    33  		},
    34  		{
    35  			name: "Invalid Version Constraint Format",
    36  			param: CheckVersionParams{
    37  				Binary:            Docker,
    38  				VersionConstraint: "abc",
    39  				WorkingDir:        ".",
    40  			},
    41  			containError:         true,
    42  			expectedErrorMessage: "invalid version constraint format found {abc}",
    43  		},
    44  		{
    45  			name: "Success",
    46  			param: CheckVersionParams{
    47  				Binary:            Docker,
    48  				VersionConstraint: ">1.2.3",
    49  				WorkingDir:        ".",
    50  			},
    51  			containError:         false,
    52  			expectedErrorMessage: "",
    53  		},
    54  	}
    55  
    56  	for _, tc := range tests {
    57  		err := validateParams(tc.param)
    58  		if tc.containError {
    59  			require.EqualError(t, err, tc.expectedErrorMessage, tc.name)
    60  		} else {
    61  			require.NoError(t, err, tc.name)
    62  		}
    63  	}
    64  }
    65  
    66  func TestExtractVersionFromShellCommandOutput(t *testing.T) {
    67  	t.Parallel()
    68  
    69  	tests := []struct {
    70  		name                 string
    71  		outputStr            string
    72  		expectedVersionStr   string
    73  		containError         bool
    74  		expectedErrorMessage string
    75  	}{
    76  		{
    77  			name:                 "Stand-alone version string",
    78  			outputStr:            "version is 1.2.3",
    79  			expectedVersionStr:   "1.2.3",
    80  			containError:         false,
    81  			expectedErrorMessage: "",
    82  		},
    83  		{
    84  			name:                 "version string with v prefix",
    85  			outputStr:            "version is v1.0.0",
    86  			expectedVersionStr:   "1.0.0",
    87  			containError:         false,
    88  			expectedErrorMessage: "",
    89  		},
    90  		{
    91  			name:                 "2 digit version string",
    92  			outputStr:            "version is v1.0",
    93  			expectedVersionStr:   "1.0",
    94  			containError:         false,
    95  			expectedErrorMessage: "",
    96  		},
    97  		{
    98  			name:                 "invalid output string",
    99  			outputStr:            "version is vabc",
   100  			expectedVersionStr:   "",
   101  			containError:         true,
   102  			expectedErrorMessage: "failed to find version using regex matcher",
   103  		},
   104  		{
   105  			name:                 "empty output string",
   106  			outputStr:            "",
   107  			expectedVersionStr:   "",
   108  			containError:         true,
   109  			expectedErrorMessage: "failed to find version using regex matcher",
   110  		},
   111  	}
   112  
   113  	for _, tc := range tests {
   114  		versionStr, err := extractVersionFromShellCommandOutput(tc.outputStr)
   115  		if tc.containError {
   116  			require.EqualError(t, err, tc.expectedErrorMessage, tc.name)
   117  		} else {
   118  			require.NoError(t, err, tc.name)
   119  			require.Equal(t, tc.expectedVersionStr, versionStr, tc.name)
   120  		}
   121  	}
   122  }
   123  
   124  func TestCheckVersionConstraint(t *testing.T) {
   125  	t.Parallel()
   126  
   127  	tests := []struct {
   128  		name                 string
   129  		actualVersionStr     string
   130  		versionConstraint    string
   131  		containError         bool
   132  		expectedErrorMessage string
   133  	}{
   134  		{
   135  			name:                 "invalid actualVersionStr",
   136  			actualVersionStr:     "",
   137  			versionConstraint:    "1.2.3",
   138  			containError:         true,
   139  			expectedErrorMessage: "invalid version format found for actualVersionStr: ",
   140  		},
   141  		{
   142  			name:                 "invalid versionConstraint",
   143  			actualVersionStr:     "1.2.3",
   144  			versionConstraint:    "",
   145  			containError:         true,
   146  			expectedErrorMessage: "invalid version format found for versionConstraint: ",
   147  		},
   148  		{
   149  			name:                 "pass version constraint",
   150  			actualVersionStr:     "1.2.3",
   151  			versionConstraint:    "1.2.3",
   152  			containError:         false,
   153  			expectedErrorMessage: "",
   154  		},
   155  		{
   156  			name:                 "fail version constraint",
   157  			actualVersionStr:     "1.2.3",
   158  			versionConstraint:    "1.2.4",
   159  			containError:         true,
   160  			expectedErrorMessage: "actual version {1.2.3} failed the version constraint {1.2.4}",
   161  		},
   162  		{
   163  			name:                 "special syntax version constraint",
   164  			actualVersionStr:     "1.0.5",
   165  			versionConstraint:    "~> 1.0.4",
   166  			containError:         false,
   167  			expectedErrorMessage: "",
   168  		},
   169  		{
   170  			name:                 "version constraint w/ operators",
   171  			actualVersionStr:     "1.2.7",
   172  			versionConstraint:    ">= 1.2.0, < 2.0.0",
   173  			containError:         false,
   174  			expectedErrorMessage: ""},
   175  	}
   176  
   177  	for _, tc := range tests {
   178  		err := checkVersionConstraint(tc.actualVersionStr, tc.versionConstraint)
   179  		if tc.containError {
   180  			require.EqualError(t, err, tc.expectedErrorMessage, tc.name)
   181  		} else {
   182  			require.NoError(t, err, tc.name)
   183  		}
   184  	}
   185  }
   186  
   187  // Note: with the current implementation of running shell command, it's not easy to
   188  // mock the output of running a shell command. So we assume a certain Binary is installed in the working
   189  // directory and it's greater than 0.0.1 version.
   190  func TestCheckVersionEndToEnd(t *testing.T) {
   191  	t.Parallel()
   192  	tests := []struct {
   193  		name  string
   194  		param CheckVersionParams
   195  	}{
   196  		{name: "Docker", param: CheckVersionParams{
   197  			Binary:            Docker,
   198  			VersionConstraint: ">= 0.0.1",
   199  			WorkingDir:        ".",
   200  		}},
   201  		{name: "Terraform", param: CheckVersionParams{
   202  			BinaryPath:        "",
   203  			Binary:            Terraform,
   204  			VersionConstraint: ">= 0.0.1",
   205  			WorkingDir:        ".",
   206  		}},
   207  		{name: "Packer", param: CheckVersionParams{
   208  			BinaryPath:        "/usr/local/bin/packer",
   209  			Binary:            Packer,
   210  			VersionConstraint: ">= 0.0.1",
   211  			WorkingDir:        ".",
   212  		}},
   213  	}
   214  
   215  	for _, tc := range tests {
   216  		err := CheckVersionE(t, tc.param)
   217  		require.NoError(t, err, tc.name)
   218  	}
   219  }