2018-03-19 18:56:51 -04:00
|
|
|
package containerizedengine
|
|
|
|
|
|
|
|
import (
|
2018-09-11 08:46:30 -04:00
|
|
|
"bytes"
|
2018-03-19 18:56:51 -04:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-09-14 13:53:56 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-03-19 18:56:51 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd"
|
|
|
|
"github.com/containerd/containerd/cio"
|
|
|
|
"github.com/containerd/containerd/errdefs"
|
2019-01-28 08:30:31 -05:00
|
|
|
"github.com/docker/cli/cli/streams"
|
2018-09-28 17:06:28 -04:00
|
|
|
"github.com/docker/cli/internal/versions"
|
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/api/types"
|
2018-09-14 13:53:56 -04:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2018-03-19 18:56:51 -04:00
|
|
|
)
|
|
|
|
|
2018-09-28 17:06:28 -04:00
|
|
|
func TestActivateImagePermutations(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
lookedup := "not called yet"
|
|
|
|
expectedError := fmt.Errorf("expected error")
|
|
|
|
client := baseClient{
|
|
|
|
cclient: &fakeContainerdClient{
|
|
|
|
getImageFunc: func(ctx context.Context, ref string) (containerd.Image, error) {
|
|
|
|
lookedup = ref
|
|
|
|
return nil, expectedError
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tmpdir, err := ioutil.TempDir("", "enginedir")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
metadata := clitypes.RuntimeMetadata{EngineImage: clitypes.EnterpriseEngineImage}
|
|
|
|
err = versions.WriteRuntimeMetadata(tmpdir, &metadata)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
opts := clitypes.EngineInitOptions{
|
|
|
|
EngineVersion: "engineversiongoeshere",
|
|
|
|
RegistryPrefix: "registryprefixgoeshere",
|
|
|
|
ConfigFile: "/tmp/configfilegoeshere",
|
|
|
|
RuntimeMetadataDir: tmpdir,
|
|
|
|
}
|
|
|
|
|
2019-01-28 08:30:31 -05:00
|
|
|
err = client.ActivateEngine(ctx, opts, streams.NewOut(&bytes.Buffer{}), &types.AuthConfig{})
|
2018-09-28 17:06:28 -04:00
|
|
|
assert.ErrorContains(t, err, expectedError.Error())
|
|
|
|
assert.Equal(t, lookedup, fmt.Sprintf("%s/%s:%s", opts.RegistryPrefix, clitypes.EnterpriseEngineImage, opts.EngineVersion))
|
|
|
|
|
|
|
|
metadata = clitypes.RuntimeMetadata{EngineImage: clitypes.CommunityEngineImage}
|
|
|
|
err = versions.WriteRuntimeMetadata(tmpdir, &metadata)
|
|
|
|
assert.NilError(t, err)
|
2019-01-28 08:30:31 -05:00
|
|
|
err = client.ActivateEngine(ctx, opts, streams.NewOut(&bytes.Buffer{}), &types.AuthConfig{})
|
2018-09-28 17:06:28 -04:00
|
|
|
assert.ErrorContains(t, err, expectedError.Error())
|
|
|
|
assert.Equal(t, lookedup, fmt.Sprintf("%s/%s:%s", opts.RegistryPrefix, clitypes.EnterpriseEngineImage, opts.EngineVersion))
|
|
|
|
|
|
|
|
metadata = clitypes.RuntimeMetadata{EngineImage: clitypes.CommunityEngineImage + "-dm"}
|
|
|
|
err = versions.WriteRuntimeMetadata(tmpdir, &metadata)
|
|
|
|
assert.NilError(t, err)
|
2019-01-28 08:30:31 -05:00
|
|
|
err = client.ActivateEngine(ctx, opts, streams.NewOut(&bytes.Buffer{}), &types.AuthConfig{})
|
2018-09-28 17:06:28 -04:00
|
|
|
assert.ErrorContains(t, err, expectedError.Error())
|
|
|
|
assert.Equal(t, lookedup, fmt.Sprintf("%s/%s:%s", opts.RegistryPrefix, clitypes.EnterpriseEngineImage+"-dm", opts.EngineVersion))
|
|
|
|
}
|
|
|
|
|
2018-09-14 13:53:56 -04:00
|
|
|
func TestActivateConfigFailure(t *testing.T) {
|
2018-03-19 18:56:51 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
registryPrefix := "registryprefixgoeshere"
|
|
|
|
image := &fakeImage{
|
|
|
|
nameFunc: func() string {
|
2018-09-11 08:46:30 -04:00
|
|
|
return registryPrefix + "/" + clitypes.EnterpriseEngineImage + ":engineversion"
|
2018-03-19 18:56:51 -04:00
|
|
|
},
|
2018-09-14 13:53:56 -04:00
|
|
|
configFunc: func(ctx context.Context) (ocispec.Descriptor, error) {
|
|
|
|
return ocispec.Descriptor{}, fmt.Errorf("config lookup failure")
|
|
|
|
},
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
container := &fakeContainer{
|
|
|
|
imageFunc: func(context.Context) (containerd.Image, error) {
|
|
|
|
return image, nil
|
|
|
|
},
|
|
|
|
taskFunc: func(context.Context, cio.Attach) (containerd.Task, error) {
|
|
|
|
return nil, errdefs.ErrNotFound
|
|
|
|
},
|
|
|
|
labelsFunc: func(context.Context) (map[string]string, error) {
|
|
|
|
return map[string]string{}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
client := baseClient{
|
|
|
|
cclient: &fakeContainerdClient{
|
|
|
|
containersFunc: func(ctx context.Context, filters ...string) ([]containerd.Container, error) {
|
|
|
|
return []containerd.Container{container}, nil
|
|
|
|
},
|
2018-09-14 13:53:56 -04:00
|
|
|
getImageFunc: func(ctx context.Context, ref string) (containerd.Image, error) {
|
|
|
|
return image, nil
|
|
|
|
},
|
2018-03-19 18:56:51 -04:00
|
|
|
},
|
|
|
|
}
|
2018-09-28 17:06:28 -04:00
|
|
|
tmpdir, err := ioutil.TempDir("", "engindir")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
metadata := clitypes.RuntimeMetadata{EngineImage: clitypes.CommunityEngineImage}
|
|
|
|
err = versions.WriteRuntimeMetadata(tmpdir, &metadata)
|
|
|
|
assert.NilError(t, err)
|
2018-09-11 08:46:30 -04:00
|
|
|
opts := clitypes.EngineInitOptions{
|
2018-09-28 17:06:28 -04:00
|
|
|
EngineVersion: "engineversiongoeshere",
|
|
|
|
RegistryPrefix: "registryprefixgoeshere",
|
|
|
|
ConfigFile: "/tmp/configfilegoeshere",
|
|
|
|
EngineImage: clitypes.EnterpriseEngineImage,
|
|
|
|
RuntimeMetadataDir: tmpdir,
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
2019-01-28 08:30:31 -05:00
|
|
|
err = client.ActivateEngine(ctx, opts, streams.NewOut(&bytes.Buffer{}), &types.AuthConfig{})
|
2018-09-14 13:53:56 -04:00
|
|
|
assert.ErrorContains(t, err, "config lookup failure")
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestActivateDoUpdateFail(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
registryPrefix := "registryprefixgoeshere"
|
|
|
|
image := &fakeImage{
|
|
|
|
nameFunc: func() string {
|
|
|
|
return registryPrefix + "/ce-engine:engineversion"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
container := &fakeContainer{
|
|
|
|
imageFunc: func(context.Context) (containerd.Image, error) {
|
|
|
|
return image, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
client := baseClient{
|
|
|
|
cclient: &fakeContainerdClient{
|
|
|
|
containersFunc: func(ctx context.Context, filters ...string) ([]containerd.Container, error) {
|
|
|
|
return []containerd.Container{container}, nil
|
|
|
|
},
|
|
|
|
getImageFunc: func(ctx context.Context, ref string) (containerd.Image, error) {
|
|
|
|
return nil, fmt.Errorf("something went wrong")
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-09-28 17:06:28 -04:00
|
|
|
tmpdir, err := ioutil.TempDir("", "enginedir")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
metadata := clitypes.RuntimeMetadata{EngineImage: clitypes.CommunityEngineImage}
|
|
|
|
err = versions.WriteRuntimeMetadata(tmpdir, &metadata)
|
|
|
|
assert.NilError(t, err)
|
2018-09-11 08:46:30 -04:00
|
|
|
opts := clitypes.EngineInitOptions{
|
2018-09-28 17:06:28 -04:00
|
|
|
EngineVersion: "engineversiongoeshere",
|
|
|
|
RegistryPrefix: "registryprefixgoeshere",
|
|
|
|
ConfigFile: "/tmp/configfilegoeshere",
|
|
|
|
EngineImage: clitypes.EnterpriseEngineImage,
|
|
|
|
RuntimeMetadataDir: tmpdir,
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
2019-01-28 08:30:31 -05:00
|
|
|
err = client.ActivateEngine(ctx, opts, streams.NewOut(&bytes.Buffer{}), &types.AuthConfig{})
|
2018-03-19 18:56:51 -04:00
|
|
|
assert.ErrorContains(t, err, "check for image")
|
|
|
|
assert.ErrorContains(t, err, "something went wrong")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDoUpdateNoVersion(t *testing.T) {
|
2018-09-28 17:06:28 -04:00
|
|
|
tmpdir, err := ioutil.TempDir("", "enginedir")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
metadata := clitypes.RuntimeMetadata{EngineImage: clitypes.EnterpriseEngineImage}
|
|
|
|
err = versions.WriteRuntimeMetadata(tmpdir, &metadata)
|
|
|
|
assert.NilError(t, err)
|
2018-03-19 18:56:51 -04:00
|
|
|
ctx := context.Background()
|
2018-09-11 08:46:30 -04:00
|
|
|
opts := clitypes.EngineInitOptions{
|
2018-09-28 17:06:28 -04:00
|
|
|
EngineVersion: "",
|
|
|
|
RegistryPrefix: "registryprefixgoeshere",
|
|
|
|
ConfigFile: "/tmp/configfilegoeshere",
|
|
|
|
EngineImage: clitypes.EnterpriseEngineImage,
|
|
|
|
RuntimeMetadataDir: tmpdir,
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
2018-09-28 17:06:28 -04:00
|
|
|
|
2018-03-19 18:56:51 -04:00
|
|
|
client := baseClient{}
|
2019-01-28 08:30:31 -05:00
|
|
|
err = client.DoUpdate(ctx, opts, streams.NewOut(&bytes.Buffer{}), &types.AuthConfig{})
|
2018-09-20 14:02:11 -04:00
|
|
|
assert.ErrorContains(t, err, "pick the version you")
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDoUpdateImageMiscError(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2018-09-28 17:06:28 -04:00
|
|
|
tmpdir, err := ioutil.TempDir("", "enginedir")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
metadata := clitypes.RuntimeMetadata{EngineImage: clitypes.EnterpriseEngineImage}
|
|
|
|
err = versions.WriteRuntimeMetadata(tmpdir, &metadata)
|
|
|
|
assert.NilError(t, err)
|
2018-09-11 08:46:30 -04:00
|
|
|
opts := clitypes.EngineInitOptions{
|
2018-09-28 17:06:28 -04:00
|
|
|
EngineVersion: "engineversiongoeshere",
|
|
|
|
RegistryPrefix: "registryprefixgoeshere",
|
|
|
|
ConfigFile: "/tmp/configfilegoeshere",
|
|
|
|
EngineImage: "testnamegoeshere",
|
|
|
|
RuntimeMetadataDir: tmpdir,
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
client := baseClient{
|
|
|
|
cclient: &fakeContainerdClient{
|
|
|
|
getImageFunc: func(ctx context.Context, ref string) (containerd.Image, error) {
|
|
|
|
return nil, fmt.Errorf("something went wrong")
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-09-28 17:06:28 -04:00
|
|
|
|
2019-01-28 08:30:31 -05:00
|
|
|
err = client.DoUpdate(ctx, opts, streams.NewOut(&bytes.Buffer{}), &types.AuthConfig{})
|
2018-03-19 18:56:51 -04:00
|
|
|
assert.ErrorContains(t, err, "check for image")
|
|
|
|
assert.ErrorContains(t, err, "something went wrong")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDoUpdatePullFail(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2018-09-28 17:06:28 -04:00
|
|
|
tmpdir, err := ioutil.TempDir("", "enginedir")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
metadata := clitypes.RuntimeMetadata{EngineImage: clitypes.EnterpriseEngineImage}
|
|
|
|
err = versions.WriteRuntimeMetadata(tmpdir, &metadata)
|
|
|
|
assert.NilError(t, err)
|
2018-09-11 08:46:30 -04:00
|
|
|
opts := clitypes.EngineInitOptions{
|
2018-09-28 17:06:28 -04:00
|
|
|
EngineVersion: "engineversiongoeshere",
|
|
|
|
RegistryPrefix: "registryprefixgoeshere",
|
|
|
|
ConfigFile: "/tmp/configfilegoeshere",
|
|
|
|
EngineImage: "testnamegoeshere",
|
|
|
|
RuntimeMetadataDir: tmpdir,
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
client := baseClient{
|
|
|
|
cclient: &fakeContainerdClient{
|
|
|
|
getImageFunc: func(ctx context.Context, ref string) (containerd.Image, error) {
|
|
|
|
return nil, errdefs.ErrNotFound
|
|
|
|
|
|
|
|
},
|
|
|
|
pullFunc: func(ctx context.Context, ref string, opts ...containerd.RemoteOpt) (containerd.Image, error) {
|
|
|
|
return nil, fmt.Errorf("pull failure")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-09-28 17:06:28 -04:00
|
|
|
|
2019-01-28 08:30:31 -05:00
|
|
|
err = client.DoUpdate(ctx, opts, streams.NewOut(&bytes.Buffer{}), &types.AuthConfig{})
|
2018-03-19 18:56:51 -04:00
|
|
|
assert.ErrorContains(t, err, "unable to pull")
|
|
|
|
assert.ErrorContains(t, err, "pull failure")
|
|
|
|
}
|
|
|
|
|
2018-09-14 13:53:56 -04:00
|
|
|
func TestActivateDoUpdateVerifyImageName(t *testing.T) {
|
2018-03-19 18:56:51 -04:00
|
|
|
ctx := context.Background()
|
2018-09-14 13:53:56 -04:00
|
|
|
registryPrefix := "registryprefixgoeshere"
|
2018-03-19 18:56:51 -04:00
|
|
|
image := &fakeImage{
|
|
|
|
nameFunc: func() string {
|
2018-09-14 13:53:56 -04:00
|
|
|
return registryPrefix + "/ce-engine:engineversion"
|
2018-03-19 18:56:51 -04:00
|
|
|
},
|
|
|
|
}
|
2018-09-14 13:53:56 -04:00
|
|
|
container := &fakeContainer{
|
|
|
|
imageFunc: func(context.Context) (containerd.Image, error) {
|
|
|
|
return image, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
requestedImage := "unset"
|
2018-03-19 18:56:51 -04:00
|
|
|
client := baseClient{
|
|
|
|
cclient: &fakeContainerdClient{
|
2018-09-14 13:53:56 -04:00
|
|
|
containersFunc: func(ctx context.Context, filters ...string) ([]containerd.Container, error) {
|
|
|
|
return []containerd.Container{container}, nil
|
|
|
|
},
|
2018-03-19 18:56:51 -04:00
|
|
|
getImageFunc: func(ctx context.Context, ref string) (containerd.Image, error) {
|
2018-09-14 13:53:56 -04:00
|
|
|
requestedImage = ref
|
|
|
|
return nil, fmt.Errorf("something went wrong")
|
2018-03-19 18:56:51 -04:00
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-09-28 17:06:28 -04:00
|
|
|
tmpdir, err := ioutil.TempDir("", "enginedir")
|
2018-09-14 13:53:56 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
2018-09-28 17:06:28 -04:00
|
|
|
metadata := clitypes.RuntimeMetadata{EngineImage: clitypes.EnterpriseEngineImage}
|
|
|
|
err = versions.WriteRuntimeMetadata(tmpdir, &metadata)
|
2018-09-14 13:53:56 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2018-09-28 17:06:28 -04:00
|
|
|
opts := clitypes.EngineInitOptions{
|
|
|
|
EngineVersion: "engineversiongoeshere",
|
|
|
|
RegistryPrefix: "registryprefixgoeshere",
|
|
|
|
EngineImage: "testnamegoeshere",
|
|
|
|
ConfigFile: "/tmp/configfilegoeshere",
|
|
|
|
RuntimeMetadataDir: tmpdir,
|
|
|
|
}
|
2018-09-14 13:53:56 -04:00
|
|
|
|
2019-01-28 08:30:31 -05:00
|
|
|
err = client.ActivateEngine(ctx, opts, streams.NewOut(&bytes.Buffer{}), &types.AuthConfig{})
|
2018-09-14 13:53:56 -04:00
|
|
|
assert.ErrorContains(t, err, "check for image")
|
|
|
|
assert.ErrorContains(t, err, "something went wrong")
|
2018-09-28 17:06:28 -04:00
|
|
|
expectedImage := fmt.Sprintf("%s/%s:%s", opts.RegistryPrefix, opts.EngineImage, opts.EngineVersion)
|
2018-09-14 13:53:56 -04:00
|
|
|
assert.Assert(t, requestedImage == expectedImage, "%s != %s", requestedImage, expectedImage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetReleaseNotesURL(t *testing.T) {
|
|
|
|
imageName := "bogus image name #$%&@!"
|
2018-09-20 14:02:11 -04:00
|
|
|
url := getReleaseNotesURL(imageName)
|
|
|
|
assert.Equal(t, url, clitypes.ReleaseNotePrefix+"/")
|
2018-09-14 13:53:56 -04:00
|
|
|
imageName = "foo.bar/valid/repowithouttag"
|
2018-09-20 14:02:11 -04:00
|
|
|
url = getReleaseNotesURL(imageName)
|
|
|
|
assert.Equal(t, url, clitypes.ReleaseNotePrefix+"/")
|
2018-09-14 13:53:56 -04:00
|
|
|
imageName = "foo.bar/valid/repowithouttag:tag123"
|
2018-09-20 14:02:11 -04:00
|
|
|
url = getReleaseNotesURL(imageName)
|
|
|
|
assert.Equal(t, url, clitypes.ReleaseNotePrefix+"/tag123")
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|