2018-03-19 18:56:51 -04:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2018-09-14 13:53:56 -04:00
|
|
|
manifesttypes "github.com/docker/cli/cli/manifest/types"
|
2018-03-19 18:56:51 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2018-09-14 13:53:56 -04:00
|
|
|
"github.com/docker/distribution"
|
|
|
|
"github.com/docker/distribution/reference"
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-03-19 18:56:51 -04:00
|
|
|
"github.com/docker/docker/client"
|
2018-09-14 13:53:56 -04:00
|
|
|
"github.com/opencontainers/go-digest"
|
2018-03-19 18:56:51 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
"gotest.tools/golden"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testCli = test.NewFakeCli(&client.Client{})
|
|
|
|
)
|
|
|
|
|
2018-09-14 13:53:56 -04:00
|
|
|
type verClient struct {
|
|
|
|
client.Client
|
2018-09-28 17:06:28 -04:00
|
|
|
ver types.Version
|
|
|
|
verErr error
|
|
|
|
info types.Info
|
|
|
|
infoErr error
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
2018-09-14 13:53:56 -04:00
|
|
|
func (c *verClient) ServerVersion(ctx context.Context) (types.Version, error) {
|
|
|
|
return c.ver, c.verErr
|
|
|
|
}
|
|
|
|
|
2018-09-28 17:06:28 -04:00
|
|
|
func (c *verClient) Info(ctx context.Context) (types.Info, error) {
|
|
|
|
return c.info, c.infoErr
|
|
|
|
}
|
|
|
|
|
2018-09-14 13:53:56 -04:00
|
|
|
type testRegistryClient struct {
|
|
|
|
tags []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c testRegistryClient) GetManifest(ctx context.Context, ref reference.Named) (manifesttypes.ImageManifest, error) {
|
|
|
|
return manifesttypes.ImageManifest{}, nil
|
|
|
|
}
|
|
|
|
func (c testRegistryClient) GetManifestList(ctx context.Context, ref reference.Named) ([]manifesttypes.ImageManifest, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func (c testRegistryClient) MountBlob(ctx context.Context, source reference.Canonical, target reference.Named) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c testRegistryClient) PutManifest(ctx context.Context, ref reference.Named, manifest distribution.Manifest) (digest.Digest, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
func (c testRegistryClient) GetTags(ctx context.Context, ref reference.Named) ([]string, error) {
|
|
|
|
return c.tags, nil
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
2018-09-14 13:53:56 -04:00
|
|
|
func TestCheckForUpdatesNoCurrentVersion(t *testing.T) {
|
|
|
|
isRoot = func() bool { return true }
|
2018-09-28 17:06:28 -04:00
|
|
|
c := test.NewFakeCli(&verClient{client.Client{}, types.Version{}, nil, types.Info{}, nil})
|
2018-09-14 13:53:56 -04:00
|
|
|
c.SetRegistryClient(testRegistryClient{})
|
|
|
|
cmd := newCheckForUpdatesCommand(c)
|
2018-03-19 18:56:51 -04:00
|
|
|
cmd.SilenceUsage = true
|
|
|
|
cmd.SilenceErrors = true
|
|
|
|
err := cmd.Execute()
|
2018-09-28 17:06:28 -04:00
|
|
|
assert.ErrorContains(t, err, "no such file or directory")
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckForUpdatesGetEngineVersionsHappy(t *testing.T) {
|
2018-09-28 17:06:28 -04:00
|
|
|
c := test.NewFakeCli(&verClient{client.Client{}, types.Version{Version: "1.1.0"}, nil, types.Info{ServerVersion: "1.1.0"}, nil})
|
2018-09-14 13:53:56 -04:00
|
|
|
c.SetRegistryClient(testRegistryClient{[]string{
|
|
|
|
"1.0.1", "1.0.2", "1.0.3-beta1",
|
|
|
|
"1.1.1", "1.1.2", "1.1.3-beta1",
|
|
|
|
"1.2.0", "2.0.0", "2.1.0-beta1",
|
|
|
|
}})
|
2018-09-28 17:06:28 -04:00
|
|
|
|
2018-09-14 13:53:56 -04:00
|
|
|
isRoot = func() bool { return true }
|
|
|
|
cmd := newCheckForUpdatesCommand(c)
|
2018-03-19 18:56:51 -04:00
|
|
|
cmd.Flags().Set("pre-releases", "true")
|
|
|
|
cmd.Flags().Set("downgrades", "true")
|
2018-09-28 17:06:28 -04:00
|
|
|
cmd.Flags().Set("engine-image", "engine-community")
|
2018-09-14 13:53:56 -04:00
|
|
|
cmd.SilenceUsage = true
|
|
|
|
cmd.SilenceErrors = true
|
2018-03-19 18:56:51 -04:00
|
|
|
err := cmd.Execute()
|
|
|
|
assert.NilError(t, err)
|
2018-09-14 13:53:56 -04:00
|
|
|
golden.Assert(t, c.OutBuffer().String(), "check-all.golden")
|
2018-03-19 18:56:51 -04:00
|
|
|
|
2018-09-14 13:53:56 -04:00
|
|
|
c.OutBuffer().Reset()
|
2018-03-19 18:56:51 -04:00
|
|
|
cmd.Flags().Set("pre-releases", "false")
|
|
|
|
cmd.Flags().Set("downgrades", "true")
|
|
|
|
err = cmd.Execute()
|
|
|
|
assert.NilError(t, err)
|
2018-09-14 13:53:56 -04:00
|
|
|
fmt.Println(c.OutBuffer().String())
|
|
|
|
golden.Assert(t, c.OutBuffer().String(), "check-no-prerelease.golden")
|
2018-03-19 18:56:51 -04:00
|
|
|
|
2018-09-14 13:53:56 -04:00
|
|
|
c.OutBuffer().Reset()
|
2018-03-19 18:56:51 -04:00
|
|
|
cmd.Flags().Set("pre-releases", "false")
|
|
|
|
cmd.Flags().Set("downgrades", "false")
|
|
|
|
err = cmd.Execute()
|
|
|
|
assert.NilError(t, err)
|
2018-09-14 13:53:56 -04:00
|
|
|
fmt.Println(c.OutBuffer().String())
|
|
|
|
golden.Assert(t, c.OutBuffer().String(), "check-no-downgrades.golden")
|
2018-03-19 18:56:51 -04:00
|
|
|
|
2018-09-14 13:53:56 -04:00
|
|
|
c.OutBuffer().Reset()
|
2018-03-19 18:56:51 -04:00
|
|
|
cmd.Flags().Set("pre-releases", "false")
|
|
|
|
cmd.Flags().Set("downgrades", "false")
|
|
|
|
cmd.Flags().Set("upgrades", "false")
|
|
|
|
err = cmd.Execute()
|
|
|
|
assert.NilError(t, err)
|
2018-09-14 13:53:56 -04:00
|
|
|
fmt.Println(c.OutBuffer().String())
|
|
|
|
golden.Assert(t, c.OutBuffer().String(), "check-patches-only.golden")
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|