Files
velero/pkg/cmd/cli/version/version_test.go
Harshil-1603 b02f684ece Clarify client and server version mismatch warnings (#10655)
* Clarify client and server version mismatch warnings

Signed-off-by: Harshil-1603 <harshil.agrawal098@gmail.com>

Signed-off-by: Harshil-1603 <harshil.agrawal098@gmail.com>

* Test both client and server version mismatch warnings

Signed-off-by: Harshil-1603 <harshil.agrawal098@gmail.com>

Signed-off-by: Harshil-1603 <harshil.agrawal098@gmail.com>

* Add changelog for PR 10655

Signed-off-by: Harshil-1603 <harshil.agrawal098@gmail.com>

Signed-off-by: Harshil-1603 <harshil.agrawal098@gmail.com>

---------

Signed-off-by: Harshil-1603 <harshil.agrawal098@gmail.com>
2026-10-22 14:22:07 +08:00

147 lines
4.9 KiB
Go

/*
Copyright the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package version
import (
"bytes"
"fmt"
"testing"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
kbclient "sigs.k8s.io/controller-runtime/pkg/client"
velerov1 "github.com/velero-io/velero/pkg/apis/velero/v1"
"github.com/velero-io/velero/pkg/builder"
"github.com/velero-io/velero/pkg/buildinfo"
velerotest "github.com/velero-io/velero/pkg/test"
)
func TestPrintVersion(t *testing.T) {
// set up some non-empty buildinfo values, but put them back to their
// defaults at the end of the test
var (
origVersion = buildinfo.Version
origGitSHA = buildinfo.GitSHA
origGitTreeState = buildinfo.GitTreeState
)
defer func() {
buildinfo.Version = origVersion
buildinfo.GitSHA = origGitSHA
buildinfo.GitTreeState = origGitTreeState
}()
buildinfo.Version = "v1.0.0"
buildinfo.GitSHA = "somegitsha"
buildinfo.GitTreeState = "dirty"
clientVersion := fmt.Sprintf("Client:\n\tVersion: %s\n\tGit commit: %s\n", buildinfo.Version, buildinfo.FormattedGitSHA())
tests := []struct {
name string
clientOnly bool
serverStatusRequest *velerov1.ServerStatusRequest
getterError error
clientVersion string
want string
}{
{
name: "client-only",
clientOnly: true,
want: clientVersion,
},
{
name: "server status getter error",
clientOnly: false,
serverStatusRequest: nil,
getterError: errors.New("an error"),
want: clientVersion + "<error getting server version: an error>\n",
},
{
name: "server status getter returns normally",
clientOnly: false,
serverStatusRequest: builder.ForServerStatusRequest("velero", "ssr-1", "0").ServerVersion("v1.0.1").Result(),
getterError: nil,
want: clientVersion + "Server:\n\tVersion: v1.0.1\n",
},
{
name: "older client warns to update the client",
serverStatusRequest: builder.ForServerStatusRequest("velero", "ssr-1", "0").ServerVersion("v1.1.0").Result(),
want: clientVersion + "Server:\n\tVersion: v1.1.0\n# WARNING: client version v1.0.0 and server version v1.1.0 have different major or minor versions. Compatibility is not guaranteed. Please update client to match the server's major and minor version.\n",
},
{
name: "newer client warns to update the server",
clientVersion: "v1.1.0",
serverStatusRequest: builder.ForServerStatusRequest("velero", "ssr-1", "0").ServerVersion("v1.0.0").Result(),
want: "Client:\n\tVersion: v1.1.0\n\tGit commit: somegitsha-dirty\nServer:\n\tVersion: v1.0.0\n# WARNING: client version v1.1.0 and server version v1.0.0 have different major or minor versions. Compatibility is not guaranteed. Please update server to match the client's major and minor version.\n",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
buildinfo.Version = "v1.0.0"
if tc.clientVersion != "" {
buildinfo.Version = tc.clientVersion
}
var (
kbClient = velerotest.NewFakeControllerRuntimeClient(t)
serverStatusGetter = new(mockServerStatusGetter)
buf = new(bytes.Buffer)
)
defer serverStatusGetter.AssertExpectations(t)
// GetServerStatus should only be called when clientOnly = false
if !tc.clientOnly {
serverStatusGetter.On("GetServerStatus", kbClient).Return(tc.serverStatusRequest, tc.getterError)
}
printVersion(buf, tc.clientOnly, kbClient, serverStatusGetter)
assert.Equal(t, tc.want, buf.String())
})
}
}
// mockServerStatusGetter is an autogenerated mock type for the serverStatusGetter type
// Code generated by mockery v2.2.1.
type mockServerStatusGetter struct {
mock.Mock
}
// GetServerStatus provides a mock function with given fields: mgr
func (_m *mockServerStatusGetter) GetServerStatus(kbClient kbclient.Client) (*velerov1.ServerStatusRequest, error) {
ret := _m.Called(kbClient)
var r0 *velerov1.ServerStatusRequest
if rf, ok := ret.Get(0).(func(kbclient.Client) *velerov1.ServerStatusRequest); ok {
r0 = rf(kbClient)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*velerov1.ServerStatusRequest)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(kbclient.Client) error); ok {
r1 = rf(kbClient)
} else {
r1 = ret.Error(1)
}
return r0, r1
}