github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/upgrade/upgrade_cli_test.go (about)

     1  // +build unit
     2  
     3  package upgrade
     4  
     5  import (
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/blang/semver"
    13  	v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
    14  	"github.com/jenkins-x/jx-api/pkg/client/clientset/versioned/fake"
    15  	"github.com/jenkins-x/jx-logging/pkg/log"
    16  	"github.com/olli-ai/jx/v2/pkg/brew"
    17  	"github.com/olli-ai/jx/v2/pkg/cmd/create/options"
    18  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    19  	"github.com/olli-ai/jx/v2/pkg/extensions"
    20  	"github.com/olli-ai/jx/v2/pkg/version"
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  )
    25  
    26  var sampleBrewInfo = `[  
    27     {  
    28        "name":"jx",
    29        "full_name":"jenkins-x/jx/jx",
    30        "oldname":null,
    31        "aliases":[  
    32  
    33        ],
    34        "versioned_formulae":[  
    35  
    36        ],
    37        "desc":"A tool to install and interact with Jenkins X on your Kubernetes cluster.",
    38        "homepage":"https://jenkins-x.github.io/jenkins-x-website/",
    39        "versions":{  
    40           "stable":"2.0.181",
    41           "devel":null,
    42           "head":null,
    43           "bottle":false
    44        },
    45        "revision":0,
    46        "version_scheme":0,
    47        "bottle":{  
    48  
    49        },
    50        "keg_only":false,
    51        "bottle_disabled":false,
    52        "options":[  
    53  
    54        ],
    55        "build_dependencies":[  
    56  
    57        ],
    58        "dependencies":[  
    59  
    60        ],
    61        "recommended_dependencies":[  
    62  
    63        ],
    64        "optional_dependencies":[  
    65  
    66        ],
    67        "requirements":[  
    68  
    69        ],
    70        "conflicts_with":[  
    71  
    72        ],
    73        "caveats":null,
    74        "installed":[  
    75           {  
    76              "version":"2.0.181",
    77              "used_options":[  
    78  
    79              ],
    80              "built_as_bottle":false,
    81              "poured_from_bottle":false,
    82              "runtime_dependencies":[  
    83  
    84              ],
    85              "installed_as_dependency":false,
    86              "installed_on_request":true
    87           }
    88        ],
    89        "linked_keg":"2.0.181",
    90        "pinned":false,
    91        "outdated":false
    92     }
    93  ]`
    94  
    95  func TestLatestJxBrewVersion(t *testing.T) {
    96  	version, err := brew.LatestJxBrewVersion(sampleBrewInfo)
    97  	assert.NoError(t, err)
    98  	assert.Equal(t, "2.0.181", version)
    99  }
   100  
   101  func TestNeedsUpgrade(t *testing.T) {
   102  	type testData struct {
   103  		current               string
   104  		latest                string
   105  		expectedUpgradeNeeded bool
   106  		expectedMessage       string
   107  	}
   108  
   109  	testCases := []testData{
   110  		{
   111  			"1.0.0", "1.0.0", false, "You are already on the latest version of jx 1.0.0\n",
   112  		},
   113  		{
   114  			"1.0.0", "1.0.1", true, "",
   115  		},
   116  		{
   117  			"1.0.0", "0.0.99", true, "",
   118  		},
   119  	}
   120  
   121  	o := UpgradeCLIOptions{}
   122  	for _, data := range testCases {
   123  		currentVersion, _ := semver.New(data.current)
   124  		latestVersion, _ := semver.New(data.latest)
   125  		actualMessage := log.CaptureOutput(func() {
   126  			actualUpgradeNeeded := o.needsUpgrade(*currentVersion, *latestVersion)
   127  			assert.Equal(t, data.expectedUpgradeNeeded, actualUpgradeNeeded, fmt.Sprintf("Unexpected upgrade flag for %v", data))
   128  		})
   129  		assert.Equal(t, data.expectedMessage, actualMessage, fmt.Sprintf("Unexpected message for %v", data))
   130  	}
   131  }
   132  
   133  func TestVersionCheckWhenCurrentVersionIsGreaterThanReleaseVersion(t *testing.T) {
   134  	jxVersion := semver.Version{Major: 1, Minor: 3, Patch: 153}
   135  	version.Map["version"] = "1.4.0"
   136  	opts := &UpgradeCLIOptions{
   137  		CreateOptions: options.CreateOptions{
   138  			CommonOptions: &opts.CommonOptions{},
   139  		},
   140  	}
   141  	update, err := opts.ShouldUpdate(jxVersion)
   142  	assert.NoError(t, err, "should check version without failure")
   143  	assert.False(t, update, "should not update")
   144  }
   145  
   146  func TestVersionCheckWhenCurrentVersionIsEqualToReleaseVersion(t *testing.T) {
   147  	jxVersion := semver.Version{Major: 1, Minor: 2, Patch: 3}
   148  	version.Map["version"] = "1.2.3"
   149  	opts := &UpgradeCLIOptions{
   150  		CreateOptions: options.CreateOptions{
   151  			CommonOptions: &opts.CommonOptions{},
   152  		},
   153  	}
   154  	update, err := opts.ShouldUpdate(jxVersion)
   155  	assert.NoError(t, err, "should check version without failure")
   156  	assert.False(t, update, "should not update")
   157  }
   158  
   159  func TestVersionCheckWhenCurrentVersionIsLessThanReleaseVersion(t *testing.T) {
   160  	jxVersion := semver.Version{Major: 1, Minor: 3, Patch: 153}
   161  	version.Map["version"] = "1.0.0"
   162  	opts := &UpgradeCLIOptions{
   163  		CreateOptions: options.CreateOptions{
   164  			CommonOptions: &opts.CommonOptions{},
   165  		},
   166  	}
   167  	update, err := opts.ShouldUpdate(jxVersion)
   168  	assert.NoError(t, err, "should check version without failure")
   169  	assert.True(t, update, "should update")
   170  }
   171  
   172  func TestVersionCheckWhenCurrentVersionIsEqualToReleaseVersionWithPatch(t *testing.T) {
   173  	prVersions := []semver.PRVersion{}
   174  	prVersions = append(prVersions, semver.PRVersion{VersionStr: "dev"})
   175  	jxVersion := semver.Version{Major: 1, Minor: 2, Patch: 3, Pre: prVersions, Build: []string(nil)}
   176  	version.Map["version"] = "1.2.3"
   177  	opts := &UpgradeCLIOptions{
   178  		CreateOptions: options.CreateOptions{
   179  			CommonOptions: &opts.CommonOptions{},
   180  		},
   181  	}
   182  	update, err := opts.ShouldUpdate(jxVersion)
   183  	assert.NoError(t, err, "should check version without failure")
   184  	assert.False(t, update, "should not update")
   185  }
   186  
   187  func TestVersionCheckWhenCurrentVersionWithPatchIsEqualToReleaseVersion(t *testing.T) {
   188  	jxVersion := semver.Version{Major: 1, Minor: 2, Patch: 3}
   189  	version.Map["version"] = "1.2.3-dev+6a8285f4"
   190  	opts := &UpgradeCLIOptions{
   191  		CreateOptions: options.CreateOptions{
   192  			CommonOptions: &opts.CommonOptions{},
   193  		},
   194  	}
   195  	update, err := opts.ShouldUpdate(jxVersion)
   196  	assert.NoError(t, err, "should check version without failure")
   197  	assert.False(t, update, "should not update")
   198  }
   199  
   200  func TestVersionCheckWhenCurrentVersionWithPatchIsLessThanReleaseVersion(t *testing.T) {
   201  	jxVersion := semver.Version{Major: 1, Minor: 2, Patch: 3}
   202  	version.Map["version"] = "1.2.2-dev+6a8285f4"
   203  	opts := &UpgradeCLIOptions{
   204  		CreateOptions: options.CreateOptions{
   205  			CommonOptions: &opts.CommonOptions{},
   206  		},
   207  	}
   208  	update, err := opts.ShouldUpdate(jxVersion)
   209  	assert.NoError(t, err, "should check version without failure")
   210  	assert.False(t, update, "should not update")
   211  }
   212  
   213  func TestUpgradeBinaryPlugins(t *testing.T) {
   214  	tmpDir, err := ioutil.TempDir("", "")
   215  	require.NoError(t, err, "failed to create tmp dir")
   216  
   217  	opts := &UpgradeCLIOptions{
   218  		CreateOptions: options.CreateOptions{
   219  			CommonOptions: &opts.CommonOptions{},
   220  		},
   221  	}
   222  	dummyPluginURL := "https://raw.githubusercontent.com/jenkins-x/jx/master/hack/gofmt.sh"
   223  	ns := "jx"
   224  	pluginName := "jx-my-plugin"
   225  	pluginVersion := "1.2.3"
   226  	jxClient := fake.NewSimpleClientset(
   227  		&v1.Plugin{
   228  			ObjectMeta: metav1.ObjectMeta{
   229  				Name:      pluginName,
   230  				Namespace: ns,
   231  				Labels: map[string]string{
   232  					extensions.PluginCommandLabel: pluginName,
   233  				},
   234  			},
   235  			Spec: v1.PluginSpec{
   236  				Name:       pluginName,
   237  				SubCommand: "my plugin",
   238  				Group:      "",
   239  				Binaries: []v1.Binary{
   240  					{
   241  						URL:    dummyPluginURL,
   242  						Goarch: "amd64",
   243  						Goos:   "Windows",
   244  					},
   245  					{
   246  						URL:    dummyPluginURL,
   247  						Goarch: "amd64",
   248  						Goos:   "Darwin",
   249  					},
   250  					{
   251  						URL:    dummyPluginURL,
   252  						Goarch: "amd64",
   253  						Goos:   "Linux",
   254  					},
   255  				},
   256  				Description: "my awesome plugin extension",
   257  				Version:     pluginVersion,
   258  			},
   259  		})
   260  	opts.SetJxClient(jxClient)
   261  	opts.SetDevNamespace(ns)
   262  
   263  	oldJXHome := os.Getenv("JX_HOME")
   264  	os.Setenv("JX_HOME", tmpDir)
   265  	defer os.Setenv("JX_HOME", oldJXHome)
   266  
   267  	t.Logf("downloading plugins to JX_HOME %s\n", tmpDir)
   268  
   269  	err = opts.UpgradeBinaryPlugins()
   270  	require.NoError(t, err, "should not fail upgrading the binary plugins")
   271  	assert.FileExists(t, filepath.Join(tmpDir, "plugins", ns, "bin", pluginName+"-"+pluginVersion))
   272  }