mirror of
https://github.com/coder/coder.git
synced 2026-09-22 05:05:20 +08:00
feat: add customizable upgrade message on client/server version mismatch (#11587)
This commit is contained in:
@@ -1,12 +1,24 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/goleak"
|
||||
|
||||
"github.com/coder/coder/v2/buildinfo"
|
||||
"github.com/coder/coder/v2/cli/cliui"
|
||||
"github.com/coder/coder/v2/coderd"
|
||||
"github.com/coder/coder/v2/coderd/httpapi"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
"github.com/coder/pretty"
|
||||
)
|
||||
|
||||
func Test_formatExamples(t *testing.T) {
|
||||
@@ -84,3 +96,85 @@ func TestMain(m *testing.M) {
|
||||
goleak.IgnoreTopFunction("github.com/lib/pq.NewDialListener"),
|
||||
)
|
||||
}
|
||||
|
||||
func Test_checkVersions(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("CustomUpgradeMessage", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
expectedUpgradeMessage := "My custom upgrade message"
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
httpapi.Write(r.Context(), rw, http.StatusOK, codersdk.BuildInfoResponse{
|
||||
ExternalURL: buildinfo.ExternalURL(),
|
||||
// Provide a version that will not match
|
||||
Version: "v1.0.0",
|
||||
AgentAPIVersion: coderd.AgentAPIVersionREST,
|
||||
// does not matter what the url is
|
||||
DashboardURL: "https://example.com",
|
||||
WorkspaceProxy: false,
|
||||
UpgradeMessage: expectedUpgradeMessage,
|
||||
})
|
||||
}))
|
||||
defer srv.Close()
|
||||
surl, err := url.Parse(srv.URL)
|
||||
require.NoError(t, err)
|
||||
|
||||
client := codersdk.New(surl)
|
||||
|
||||
r := &RootCmd{}
|
||||
|
||||
cmd, err := r.Command(nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
var buf bytes.Buffer
|
||||
inv := cmd.Invoke()
|
||||
inv.Stderr = &buf
|
||||
|
||||
err = r.checkVersions(inv, client, "v2.0.0")
|
||||
require.NoError(t, err)
|
||||
|
||||
fmtOutput := fmt.Sprintf("version mismatch: client v2.0.0, server v1.0.0\n%s", expectedUpgradeMessage)
|
||||
expectedOutput := fmt.Sprintln(pretty.Sprint(cliui.DefaultStyles.Warn, fmtOutput))
|
||||
require.Equal(t, expectedOutput, buf.String())
|
||||
})
|
||||
|
||||
t.Run("DefaultUpgradeMessage", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
httpapi.Write(r.Context(), rw, http.StatusOK, codersdk.BuildInfoResponse{
|
||||
ExternalURL: buildinfo.ExternalURL(),
|
||||
// Provide a version that will not match
|
||||
Version: "v1.0.0",
|
||||
AgentAPIVersion: coderd.AgentAPIVersionREST,
|
||||
// does not matter what the url is
|
||||
DashboardURL: "https://example.com",
|
||||
WorkspaceProxy: false,
|
||||
UpgradeMessage: "",
|
||||
})
|
||||
}))
|
||||
defer srv.Close()
|
||||
surl, err := url.Parse(srv.URL)
|
||||
require.NoError(t, err)
|
||||
|
||||
client := codersdk.New(surl)
|
||||
|
||||
r := &RootCmd{}
|
||||
|
||||
cmd, err := r.Command(nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
var buf bytes.Buffer
|
||||
inv := cmd.Invoke()
|
||||
inv.Stderr = &buf
|
||||
|
||||
err = r.checkVersions(inv, client, "v2.0.0")
|
||||
require.NoError(t, err)
|
||||
|
||||
fmtOutput := fmt.Sprintf("version mismatch: client v2.0.0, server v1.0.0\n%s", defaultUpgradeMessage("v1.0.0"))
|
||||
expectedOutput := fmt.Sprintln(pretty.Sprint(cliui.DefaultStyles.Warn, fmtOutput))
|
||||
require.Equal(t, expectedOutput, buf.String())
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user