2018-03-19 18:56:51 -04:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
registryclient "github.com/docker/cli/cli/registry/client"
|
|
|
|
"github.com/docker/cli/internal/test"
|
2018-09-11 08:46:30 -04:00
|
|
|
clitypes "github.com/docker/cli/types"
|
2018-03-19 18:56:51 -04:00
|
|
|
"github.com/docker/docker/client"
|
|
|
|
ver "github.com/hashicorp/go-version"
|
|
|
|
"gotest.tools/assert"
|
|
|
|
"gotest.tools/golden"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testCli = test.NewFakeCli(&client.Client{})
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCheckForUpdatesNoContainerd(t *testing.T) {
|
|
|
|
testCli.SetContainerizedEngineClient(
|
2018-09-11 08:46:30 -04:00
|
|
|
func(string) (clitypes.ContainerizedClient, error) {
|
2018-03-19 18:56:51 -04:00
|
|
|
return nil, fmt.Errorf("some error")
|
|
|
|
},
|
|
|
|
)
|
|
|
|
cmd := newCheckForUpdatesCommand(testCli)
|
|
|
|
cmd.SilenceUsage = true
|
|
|
|
cmd.SilenceErrors = true
|
|
|
|
err := cmd.Execute()
|
|
|
|
assert.ErrorContains(t, err, "unable to access local containerd")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckForUpdatesNoCurrentVersion(t *testing.T) {
|
|
|
|
retErr := fmt.Errorf("some failure")
|
2018-09-11 08:46:30 -04:00
|
|
|
getCurrentEngineVersionFunc := func(ctx context.Context) (clitypes.EngineInitOptions, error) {
|
|
|
|
return clitypes.EngineInitOptions{}, retErr
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
testCli.SetContainerizedEngineClient(
|
2018-09-11 08:46:30 -04:00
|
|
|
func(string) (clitypes.ContainerizedClient, error) {
|
2018-03-19 18:56:51 -04:00
|
|
|
return &fakeContainerizedEngineClient{
|
|
|
|
getCurrentEngineVersionFunc: getCurrentEngineVersionFunc,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
cmd := newCheckForUpdatesCommand(testCli)
|
|
|
|
cmd.SilenceUsage = true
|
|
|
|
cmd.SilenceErrors = true
|
|
|
|
err := cmd.Execute()
|
|
|
|
assert.Assert(t, err == retErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckForUpdatesGetEngineVersionsFail(t *testing.T) {
|
|
|
|
retErr := fmt.Errorf("some failure")
|
|
|
|
getEngineVersionsFunc := func(ctx context.Context,
|
|
|
|
registryClient registryclient.RegistryClient,
|
2018-09-11 08:46:30 -04:00
|
|
|
currentVersion, imageName string) (clitypes.AvailableVersions, error) {
|
|
|
|
return clitypes.AvailableVersions{}, retErr
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
testCli.SetContainerizedEngineClient(
|
2018-09-11 08:46:30 -04:00
|
|
|
func(string) (clitypes.ContainerizedClient, error) {
|
2018-03-19 18:56:51 -04:00
|
|
|
return &fakeContainerizedEngineClient{
|
|
|
|
getEngineVersionsFunc: getEngineVersionsFunc,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
cmd := newCheckForUpdatesCommand(testCli)
|
|
|
|
cmd.SilenceUsage = true
|
|
|
|
cmd.SilenceErrors = true
|
|
|
|
err := cmd.Execute()
|
|
|
|
assert.Assert(t, err == retErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckForUpdatesGetEngineVersionsHappy(t *testing.T) {
|
2018-09-11 08:46:30 -04:00
|
|
|
getCurrentEngineVersionFunc := func(ctx context.Context) (clitypes.EngineInitOptions, error) {
|
|
|
|
return clitypes.EngineInitOptions{
|
2018-03-19 18:56:51 -04:00
|
|
|
EngineImage: "current engine",
|
|
|
|
EngineVersion: "1.1.0",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
getEngineVersionsFunc := func(ctx context.Context,
|
|
|
|
registryClient registryclient.RegistryClient,
|
2018-09-11 08:46:30 -04:00
|
|
|
currentVersion, imageName string) (clitypes.AvailableVersions, error) {
|
|
|
|
return clitypes.AvailableVersions{
|
2018-03-19 18:56:51 -04:00
|
|
|
Downgrades: parseVersions(t, "1.0.1", "1.0.2", "1.0.3-beta1"),
|
|
|
|
Patches: parseVersions(t, "1.1.1", "1.1.2", "1.1.3-beta1"),
|
|
|
|
Upgrades: parseVersions(t, "1.2.0", "2.0.0", "2.1.0-beta1"),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
testCli.SetContainerizedEngineClient(
|
2018-09-11 08:46:30 -04:00
|
|
|
func(string) (clitypes.ContainerizedClient, error) {
|
2018-03-19 18:56:51 -04:00
|
|
|
return &fakeContainerizedEngineClient{
|
|
|
|
getEngineVersionsFunc: getEngineVersionsFunc,
|
|
|
|
getCurrentEngineVersionFunc: getCurrentEngineVersionFunc,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
cmd := newCheckForUpdatesCommand(testCli)
|
|
|
|
cmd.Flags().Set("pre-releases", "true")
|
|
|
|
cmd.Flags().Set("downgrades", "true")
|
|
|
|
err := cmd.Execute()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
golden.Assert(t, testCli.OutBuffer().String(), "check-all.golden")
|
|
|
|
|
|
|
|
testCli.OutBuffer().Reset()
|
|
|
|
cmd.Flags().Set("pre-releases", "false")
|
|
|
|
cmd.Flags().Set("downgrades", "true")
|
|
|
|
err = cmd.Execute()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
fmt.Println(testCli.OutBuffer().String())
|
|
|
|
golden.Assert(t, testCli.OutBuffer().String(), "check-no-prerelease.golden")
|
|
|
|
|
|
|
|
testCli.OutBuffer().Reset()
|
|
|
|
cmd.Flags().Set("pre-releases", "false")
|
|
|
|
cmd.Flags().Set("downgrades", "false")
|
|
|
|
err = cmd.Execute()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
fmt.Println(testCli.OutBuffer().String())
|
|
|
|
golden.Assert(t, testCli.OutBuffer().String(), "check-no-downgrades.golden")
|
|
|
|
|
|
|
|
testCli.OutBuffer().Reset()
|
|
|
|
cmd.Flags().Set("pre-releases", "false")
|
|
|
|
cmd.Flags().Set("downgrades", "false")
|
|
|
|
cmd.Flags().Set("upgrades", "false")
|
|
|
|
err = cmd.Execute()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
fmt.Println(testCli.OutBuffer().String())
|
|
|
|
golden.Assert(t, testCli.OutBuffer().String(), "check-patches-only.golden")
|
|
|
|
}
|
|
|
|
|
2018-09-11 08:46:30 -04:00
|
|
|
func makeVersion(t *testing.T, tag string) clitypes.DockerVersion {
|
2018-03-19 18:56:51 -04:00
|
|
|
v, err := ver.NewVersion(tag)
|
|
|
|
assert.NilError(t, err)
|
2018-09-11 08:46:30 -04:00
|
|
|
return clitypes.DockerVersion{Version: *v, Tag: tag}
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
2018-09-11 08:46:30 -04:00
|
|
|
func parseVersions(t *testing.T, tags ...string) []clitypes.DockerVersion {
|
|
|
|
ret := make([]clitypes.DockerVersion, len(tags))
|
2018-03-19 18:56:51 -04:00
|
|
|
for i, tag := range tags {
|
|
|
|
ret[i] = makeVersion(t, tag)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|