From f66f0fb7a0671f0ad9d18cecac3d04ad38b05841 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Wed, 23 Aug 2017 14:11:41 -0400 Subject: [PATCH] Update windows SpecOpts in tests Signed-off-by: Michael Crosby --- cmd/ctr/run_windows.go | 14 ++++---------- helpers_windows_test.go | 21 ++++++--------------- spec_opts_windows.go | 26 +++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/cmd/ctr/run_windows.go b/cmd/ctr/run_windows.go index f48b50542..2bfda8f8a 100644 --- a/cmd/ctr/run_windows.go +++ b/cmd/ctr/run_windows.go @@ -6,6 +6,7 @@ import ( "github.com/containerd/console" "github.com/containerd/containerd" + "github.com/containerd/containerd/containers" "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/log" digest "github.com/opencontainers/go-digest" @@ -25,7 +26,7 @@ func init() { } func withLayers(context *cli.Context) containerd.SpecOpts { - return func(s *specs.Spec) error { + return func(ctx gocontext.Context, client *containerd.Client, c *containers.Container, s *specs.Spec) error { l := context.StringSlice("layer") if l == nil { return errors.Wrap(errdefs.ErrInvalidArgument, "base layers must be specified with `--layer`") @@ -65,7 +66,7 @@ func handleConsoleResize(ctx gocontext.Context, task resizer, con console.Consol func withTTY(terminal bool) containerd.SpecOpts { if !terminal { - return func(s *specs.Spec) error { + return func(ctx gocontext.Context, client *containerd.Client, c *containers.Container, s *specs.Spec) error { s.Process.Terminal = false return nil } @@ -85,8 +86,6 @@ func setHostNetworking() containerd.SpecOpts { func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli.Context) (containerd.Container, error) { var ( - err error - // ref = context.Args().First() id = context.Args().Get(1) args = context.Args()[2:] @@ -109,13 +108,8 @@ func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli opts = append(opts, containerd.WithProcessArgs(args...)) } - spec, err := containerd.GenerateSpec(opts...) - if err != nil { - return nil, err - } - return client.NewContainer(ctx, id, - containerd.WithNewSpec(spec), + containerd.WithNewSpec(opts...), containerd.WithContainerLabels(labels), containerd.WithRuntime(context.String("runtime")), // TODO(mlaventure): containerd.WithImage(image), diff --git a/helpers_windows_test.go b/helpers_windows_test.go index b93c06a16..e5f1ee4e7 100644 --- a/helpers_windows_test.go +++ b/helpers_windows_test.go @@ -12,19 +12,8 @@ import ( const newLine = "\r\n" -func generateSpec(ctx context.Context, client *Client, opts ...SpecOpts) (*specs.Spec, error) { - spec, err := GenerateSpec(ctx, client, opts...) - if err != nil { - return nil, err - } - - spec.Windows.LayerFolders = dockerLayerFolders - - return spec, nil -} - func withExitStatus(es int) SpecOpts { - return func(_ context.Context, _ *Client, s *specs.Spec) error { + return func(_ context.Context, _ *Client, _ *containers.Container, s *specs.Spec) error { s.Process.Args = []string{"powershell", "-noprofile", "exit", strconv.Itoa(es)} return nil } @@ -51,8 +40,10 @@ func withExecArgs(s *specs.Process, args ...string) { } func withImageConfig(i Image) SpecOpts { - // TODO: when windows has a snapshotter remove the withImageConfig helper - return withNoop + return func(_ context.Context, _ *Client, _ *containers.Container, s *specs.Spec) error { + s.Windows.LayerFolders = dockerLayerFolders + return nil + } } func withNewSnapshot(id string, i Image) NewContainerOpts { @@ -72,6 +63,6 @@ func withRemappedSnapshot(id string, i Image, u, g uint32) NewContainerOpts { } } -func withNoop(_ context.Context, _ *Client, _ *specs.Spec) error { +func withNoop(_ context.Context, _ *Client, _ *containers.Container, _ *specs.Spec) error { return nil } diff --git a/spec_opts_windows.go b/spec_opts_windows.go index 8b7eff8e9..7881ad28c 100644 --- a/spec_opts_windows.go +++ b/spec_opts_windows.go @@ -16,7 +16,7 @@ import ( ) func WithImageConfig(i Image) SpecOpts { - return func(ctx context.Context, client *Client, s *specs.Spec) error { + return func(ctx context.Context, client *Client, _ *containers.Container, s *specs.Spec) error { var ( image = i.(*image) store = client.ContentStore() @@ -52,7 +52,7 @@ func WithImageConfig(i Image) SpecOpts { } func WithTTY(width, height int) SpecOpts { - return func(_ context.Context, _ *Client, s *specs.Spec) error { + return func(_ context.Context, _ *Client, _ *containers.Container, s *specs.Spec) error { s.Process.Terminal = true s.Process.ConsoleSize.Width = uint(width) s.Process.ConsoleSize.Height = uint(height) @@ -60,7 +60,27 @@ func WithTTY(width, height int) SpecOpts { } } -func WithNewSpec(spec *specs.Spec) NewContainerOpts { +func WithNewSpec(opts ...SpecOpts) NewContainerOpts { + return func(ctx context.Context, client *Client, c *containers.Container) error { + s, err := createDefaultSpec() + if err != nil { + return err + } + for _, o := range opts { + if err := o(ctx, client, c, s); err != nil { + return err + } + } + any, err := typeurl.MarshalAny(s) + if err != nil { + return err + } + c.Spec = any + return nil + } +} + +func WithSpec(spec *specs.Spec) NewContainerOpts { return func(ctx context.Context, client *Client, c *containers.Container) error { any, err := typeurl.MarshalAny(spec) if err != nil {