2017-02-22 15:43:13 -05:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/compose/convert"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-07-26 09:33:57 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-02-22 15:43:13 -05:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPruneServices(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
namespace := convert.NewNamespace("foo")
|
|
|
|
services := map[string]struct{}{
|
|
|
|
"new": {},
|
|
|
|
"keep": {},
|
|
|
|
}
|
2017-03-26 02:23:24 -04:00
|
|
|
client := &fakeClient{services: []string{objectName("foo", "keep"), objectName("foo", "remove")}}
|
2017-07-05 14:43:39 -04:00
|
|
|
dockerCli := test.NewFakeCli(client)
|
2017-02-22 15:43:13 -05:00
|
|
|
|
|
|
|
pruneServices(ctx, dockerCli, namespace, services)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.Equal(t, buildObjectIDs([]string{objectName("foo", "remove")}), client.removedServices)
|
2017-02-22 15:43:13 -05:00
|
|
|
}
|
2017-07-26 09:33:57 -04:00
|
|
|
|
|
|
|
// TestServiceUpdateResolveImageChanged tests that the service's
|
|
|
|
// image digest is preserved if the image did not change in the compose file
|
|
|
|
func TestServiceUpdateResolveImageChanged(t *testing.T) {
|
|
|
|
namespace := convert.NewNamespace("mystack")
|
|
|
|
|
|
|
|
var (
|
|
|
|
receivedOptions types.ServiceUpdateOptions
|
|
|
|
receivedService swarm.ServiceSpec
|
|
|
|
)
|
|
|
|
|
|
|
|
client := test.NewFakeCli(&fakeClient{
|
|
|
|
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
|
|
|
return []swarm.Service{
|
|
|
|
{
|
|
|
|
Spec: swarm.ServiceSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: namespace.Name() + "_myservice",
|
|
|
|
Labels: map[string]string{"com.docker.stack.image": "foobar:1.2.3"},
|
|
|
|
},
|
|
|
|
TaskTemplate: swarm.TaskSpec{
|
2017-08-07 05:52:40 -04:00
|
|
|
ContainerSpec: &swarm.ContainerSpec{
|
2017-07-26 09:33:57 -04:00
|
|
|
Image: "foobar:1.2.3@sha256:deadbeef",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
serviceUpdateFunc: func(serviceID string, version swarm.Version, service swarm.ServiceSpec, options types.ServiceUpdateOptions) (types.ServiceUpdateResponse, error) {
|
|
|
|
receivedOptions = options
|
|
|
|
receivedService = service
|
|
|
|
return types.ServiceUpdateResponse{}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
var testcases = []struct {
|
|
|
|
image string
|
|
|
|
expectedQueryRegistry bool
|
|
|
|
expectedImage string
|
|
|
|
}{
|
|
|
|
// Image not changed
|
|
|
|
{
|
|
|
|
image: "foobar:1.2.3",
|
|
|
|
expectedQueryRegistry: false,
|
|
|
|
expectedImage: "foobar:1.2.3@sha256:deadbeef",
|
|
|
|
},
|
|
|
|
// Image changed
|
|
|
|
{
|
|
|
|
image: "foobar:1.2.4",
|
|
|
|
expectedQueryRegistry: true,
|
|
|
|
expectedImage: "foobar:1.2.4",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
t.Logf("Testing image %q", testcase.image)
|
|
|
|
spec := map[string]swarm.ServiceSpec{
|
|
|
|
"myservice": {
|
|
|
|
TaskTemplate: swarm.TaskSpec{
|
2017-08-07 05:52:40 -04:00
|
|
|
ContainerSpec: &swarm.ContainerSpec{
|
2017-07-26 09:33:57 -04:00
|
|
|
Image: testcase.image,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := deployServices(ctx, client, spec, namespace, false, resolveImageChanged)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, testcase.expectedQueryRegistry, receivedOptions.QueryRegistry)
|
|
|
|
assert.Equal(t, testcase.expectedImage, receivedService.TaskTemplate.ContainerSpec.Image)
|
|
|
|
|
|
|
|
receivedService = swarm.ServiceSpec{}
|
|
|
|
receivedOptions = types.ServiceUpdateOptions{}
|
|
|
|
}
|
|
|
|
}
|