Skip to content

Commit

Permalink
Merge #12660
Browse files Browse the repository at this point in the history
12660: Only warn about version upgrade once a day r=abhinav a=Frassle

<!--- 
Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation.
-->

# Description

<!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. -->

Fixes #12659.

## Checklist

<!--- Please provide details if the checkbox below is to be left unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud,
then the service should honor older versions of the CLI where this change would not exist.
You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version
  <!-- `@Pulumi` employees: If yes, you must submit corresponding changes in the service repo. -->


Co-authored-by: Fraser Waters <fraser@pulumi.com>
Co-authored-by: Abhinav Gupta <abhinav@pulumi.com>
  • Loading branch information
3 people committed Jun 14, 2023
2 parents 7228fd2 + 5164286 commit 5d7785e
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- type: feat
scope: cli
description: Don't warn about the CLI version being out of date on every run. The CLI will now only warn once a day, when it queries for the latest version.
34 changes: 23 additions & 11 deletions pkg/cmd/pulumi/pulumi.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,31 @@ func checkForUpdate(ctx context.Context) *diag.Diag {
return nil
}

latestVer, oldestAllowedVer, err := getCLIVersionInfo(ctx)
if err != nil {
logging.V(3).Infof("error fetching latest version information "+
"(set `%s=true` to skip update checks): %s", env.SkipUpdateCheck.Var().Name(), err)
var skipUpdateCheck bool
latestVer, oldestAllowedVer, err := getCachedVersionInfo()
if err == nil {
// If we have a cached version, we already warned the user once
// in the last 24 hours--the cache is considered stale after that.
// So we don't need to warn again.
skipUpdateCheck = true
} else {
latestVer, oldestAllowedVer, err = getCLIVersionInfo(ctx)
if err != nil {
logging.V(3).Infof("error fetching latest version information "+
"(set `%s=true` to skip update checks): %s", env.SkipUpdateCheck.Var().Name(), err)
}
}

if oldestAllowedVer.GT(curVer) {
return diag.RawMessage("", getUpgradeMessage(latestVer, curVer))
msg := getUpgradeMessage(latestVer, curVer)
if skipUpdateCheck {
// If we're skipping the check,
// still log this to the internal logging system
// that users don't see by default.
logging.Warningf(msg)
return nil
}
return diag.RawMessage("", msg)
}

return nil
Expand All @@ -412,13 +429,8 @@ func checkForUpdate(ctx context.Context) *diag.Diag {
// getCLIVersionInfo returns information about the latest version of the CLI and the oldest version that should be
// allowed without warning. It caches data from the server for a day.
func getCLIVersionInfo(ctx context.Context) (semver.Version, semver.Version, error) {
latest, oldest, err := getCachedVersionInfo()
if err == nil {
return latest, oldest, err
}

client := client.NewClient(httpstate.DefaultURL(), "", false, cmdutil.Diag())
latest, oldest, err = client.GetCLIVersionInfo(ctx)
latest, oldest, err := client.GetCLIVersionInfo(ctx)
if err != nil {
return semver.Version{}, semver.Version{}, err
}
Expand Down
80 changes: 80 additions & 0 deletions pkg/cmd/pulumi/pulumi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@
package main

import (
"context"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

"github.com/blang/semver"
"github.com/pulumi/pulumi/pkg/v3/version"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestIsDevVersion(t *testing.T) {
Expand All @@ -37,3 +45,75 @@ func TestIsDevVersion(t *testing.T) {
assert.True(t, isDevVersion(betaVer))
assert.True(t, isDevVersion(rcVer))
}

//nolint:paralleltest // changes environment variables and globals
func TestCheckForUpdate(t *testing.T) {
realVersion := version.Version
t.Cleanup(func() {
version.Version = realVersion
})
version.Version = "v1.0.0"

// Cached version information is stored in PULUMI_HOME.
pulumiHome := t.TempDir()
t.Setenv("PULUMI_HOME", pulumiHome)

// If the cached version is missing or outdated,
// the HTTP server receives a request.
var requestCounter int // number of requests
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/cli/version":
requestCounter++
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{
"latestVersion": "v1.2.3",
"oldestWithoutWarning": "v1.2.0"
}`))
if !assert.NoError(t, err) {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

default:
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
http.NotFound(w, r)
}
}))
t.Cleanup(srv.Close)
t.Setenv("PULUMI_API", srv.URL)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

msg := checkForUpdate(ctx)
require.NotNil(t, msg)
assert.Contains(t, msg.Message, "A new version of Pulumi is available")
assert.Contains(t, msg.Message, "upgrade from version '1.0.0' to '1.2.3'")
assert.Equal(t, 1, requestCounter,
"expected exactly one request to the HTTP server")

t.Run("cached", func(t *testing.T) {
// Once we have cached version information,
// we will not warn the user again until the cache expires.
requestCounter = 0
require.Nil(t, checkForUpdate(ctx))
assert.Equal(t, 0, requestCounter,
"no requests are expected to the HTTP server")
})

t.Run("cache expired", func(t *testing.T) {
// Expire the cached version information
// and verify that we query the server again.

versionCachePath, err := workspace.GetCachedVersionFilePath()
require.NoError(t, err)

expiredTime := time.Now().Add(-25 * time.Hour)
require.NoError(t,
os.Chtimes(versionCachePath, expiredTime, expiredTime))

requestCounter = 0
require.NotNil(t, checkForUpdate(ctx))
assert.Equal(t, 1, requestCounter)
})
}

0 comments on commit 5d7785e

Please sign in to comment.