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"
|
|
|
|
"path/filepath"
|
2018-03-19 18:56:51 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd"
|
|
|
|
"github.com/containerd/containerd/cio"
|
|
|
|
"github.com/containerd/containerd/errdefs"
|
2018-09-11 08:46:30 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
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"
|
2018-03-19 18:56:51 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
)
|
|
|
|
|
2018-09-20 14:02:11 -04:00
|
|
|
func healthfnHappy(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-11 08:46:30 -04:00
|
|
|
opts := clitypes.EngineInitOptions{
|
2018-03-19 18:56:51 -04:00
|
|
|
EngineVersion: "engineversiongoeshere",
|
|
|
|
RegistryPrefix: "registryprefixgoeshere",
|
|
|
|
ConfigFile: "/tmp/configfilegoeshere",
|
2018-09-11 08:46:30 -04:00
|
|
|
EngineImage: clitypes.EnterpriseEngineImage,
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
2018-09-11 08:46:30 -04:00
|
|
|
err := client.ActivateEngine(ctx, opts, command.NewOutStream(&bytes.Buffer{}), &types.AuthConfig{}, healthfnHappy)
|
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-11 08:46:30 -04:00
|
|
|
opts := clitypes.EngineInitOptions{
|
2018-03-19 18:56:51 -04:00
|
|
|
EngineVersion: "engineversiongoeshere",
|
|
|
|
RegistryPrefix: "registryprefixgoeshere",
|
|
|
|
ConfigFile: "/tmp/configfilegoeshere",
|
2018-09-11 08:46:30 -04:00
|
|
|
EngineImage: clitypes.EnterpriseEngineImage,
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
2018-09-11 08:46:30 -04:00
|
|
|
err := client.ActivateEngine(ctx, opts, command.NewOutStream(&bytes.Buffer{}), &types.AuthConfig{}, healthfnHappy)
|
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) {
|
|
|
|
ctx := context.Background()
|
2018-09-11 08:46:30 -04:00
|
|
|
opts := clitypes.EngineInitOptions{
|
2018-03-19 18:56:51 -04:00
|
|
|
EngineVersion: "",
|
|
|
|
RegistryPrefix: "registryprefixgoeshere",
|
|
|
|
ConfigFile: "/tmp/configfilegoeshere",
|
2018-09-11 08:46:30 -04:00
|
|
|
EngineImage: clitypes.EnterpriseEngineImage,
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
client := baseClient{}
|
2018-09-11 08:46:30 -04:00
|
|
|
err := client.DoUpdate(ctx, opts, command.NewOutStream(&bytes.Buffer{}), &types.AuthConfig{}, healthfnHappy)
|
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-11 08:46:30 -04:00
|
|
|
opts := clitypes.EngineInitOptions{
|
2018-03-19 18:56:51 -04:00
|
|
|
EngineVersion: "engineversiongoeshere",
|
|
|
|
RegistryPrefix: "registryprefixgoeshere",
|
|
|
|
ConfigFile: "/tmp/configfilegoeshere",
|
|
|
|
EngineImage: "testnamegoeshere",
|
|
|
|
}
|
|
|
|
client := baseClient{
|
|
|
|
cclient: &fakeContainerdClient{
|
|
|
|
getImageFunc: func(ctx context.Context, ref string) (containerd.Image, error) {
|
|
|
|
return nil, fmt.Errorf("something went wrong")
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-09-11 08:46:30 -04:00
|
|
|
err := client.DoUpdate(ctx, opts, command.NewOutStream(&bytes.Buffer{}), &types.AuthConfig{}, healthfnHappy)
|
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-11 08:46:30 -04:00
|
|
|
opts := clitypes.EngineInitOptions{
|
2018-03-19 18:56:51 -04:00
|
|
|
EngineVersion: "engineversiongoeshere",
|
|
|
|
RegistryPrefix: "registryprefixgoeshere",
|
|
|
|
ConfigFile: "/tmp/configfilegoeshere",
|
|
|
|
EngineImage: "testnamegoeshere",
|
|
|
|
}
|
|
|
|
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-11 08:46:30 -04:00
|
|
|
err := client.DoUpdate(ctx, opts, command.NewOutStream(&bytes.Buffer{}), &types.AuthConfig{}, healthfnHappy)
|
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-14 13:53:56 -04:00
|
|
|
opts := clitypes.EngineInitOptions{
|
|
|
|
EngineVersion: "engineversiongoeshere",
|
|
|
|
RegistryPrefix: "registryprefixgoeshere",
|
|
|
|
ConfigFile: "/tmp/configfilegoeshere",
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-root")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
2018-09-20 14:02:11 -04:00
|
|
|
tmpDockerRoot := defaultDockerRoot
|
2018-09-14 13:53:56 -04:00
|
|
|
defaultDockerRoot = tmpdir
|
2018-09-20 14:02:11 -04:00
|
|
|
defer func() {
|
|
|
|
defaultDockerRoot = tmpDockerRoot
|
|
|
|
}()
|
2018-09-14 13:53:56 -04:00
|
|
|
metadata := RuntimeMetadata{Platform: "platformgoeshere"}
|
2018-09-20 14:02:11 -04:00
|
|
|
err = client.WriteRuntimeMetadata(tmpdir, &metadata)
|
2018-09-14 13:53:56 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
err = client.ActivateEngine(ctx, opts, command.NewOutStream(&bytes.Buffer{}), &types.AuthConfig{}, healthfnHappy)
|
|
|
|
assert.ErrorContains(t, err, "check for image")
|
|
|
|
assert.ErrorContains(t, err, "something went wrong")
|
2018-09-20 14:02:11 -04:00
|
|
|
expectedImage := fmt.Sprintf("%s/%s:%s", opts.RegistryPrefix, "engine-enterprise", opts.EngineVersion)
|
2018-09-14 13:53:56 -04:00
|
|
|
assert.Assert(t, requestedImage == expectedImage, "%s != %s", requestedImage, expectedImage)
|
|
|
|
|
|
|
|
// Redo with enterprise set
|
|
|
|
metadata = RuntimeMetadata{Platform: "Docker Engine - Enterprise"}
|
2018-09-20 14:02:11 -04:00
|
|
|
err = client.WriteRuntimeMetadata(tmpdir, &metadata)
|
2018-09-14 13:53:56 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
err = client.ActivateEngine(ctx, opts, command.NewOutStream(&bytes.Buffer{}), &types.AuthConfig{}, healthfnHappy)
|
|
|
|
assert.ErrorContains(t, err, "check for image")
|
|
|
|
assert.ErrorContains(t, err, "something went wrong")
|
|
|
|
expectedImage = fmt.Sprintf("%s/%s:%s", opts.RegistryPrefix, "engine-enterprise", opts.EngineVersion)
|
|
|
|
assert.Assert(t, requestedImage == expectedImage, "%s != %s", requestedImage, expectedImage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetCurrentRuntimeMetadataNotPresent(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-root")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
client := baseClient{}
|
|
|
|
_, err = client.GetCurrentRuntimeMetadata(ctx, tmpdir)
|
|
|
|
assert.ErrorType(t, err, os.IsNotExist)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetCurrentRuntimeMetadataBadJson(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-root")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
2018-09-20 14:02:11 -04:00
|
|
|
filename := filepath.Join(tmpdir, runtimeMetadataName+".json")
|
2018-09-14 13:53:56 -04:00
|
|
|
err = ioutil.WriteFile(filename, []byte("not json"), 0644)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
client := baseClient{}
|
|
|
|
_, err = client.GetCurrentRuntimeMetadata(ctx, tmpdir)
|
|
|
|
assert.ErrorContains(t, err, "malformed runtime metadata file")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetCurrentRuntimeMetadataHappyPath(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-root")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
client := baseClient{}
|
|
|
|
metadata := RuntimeMetadata{Platform: "platformgoeshere"}
|
2018-09-20 14:02:11 -04:00
|
|
|
err = client.WriteRuntimeMetadata(tmpdir, &metadata)
|
2018-09-14 13:53:56 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
res, err := client.GetCurrentRuntimeMetadata(ctx, tmpdir)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, res.Platform, "platformgoeshere")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|