-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jin Dong <jindon@amazon.com>
- Loading branch information
Showing
5 changed files
with
479 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/containerd/nerdctl/pkg/composer" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newComposeCreateCommand() *cobra.Command { | ||
var composeCreateCommand = &cobra.Command{ | ||
Use: "create [flags] [SERVICE...]", | ||
Short: "Creates containers for one or more services", | ||
RunE: composeCreateAction, | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
} | ||
composeCreateCommand.Flags().Bool("build", false, "Build images before starting containers.") | ||
composeCreateCommand.Flags().Bool("no-build", false, "Don't build an image even if it's missing, conflict with --build.") | ||
composeCreateCommand.Flags().Bool("force-recreate", false, "Recreate containers even if their configuration and image haven't changed.") | ||
composeCreateCommand.Flags().Bool("no-recreate", false, "Don't recreate containers if they exist, conflict with --force-recreate.") | ||
composeCreateCommand.Flags().String("pull", "missing", "Pull images before running. (support always|missing|never)") | ||
return composeCreateCommand | ||
} | ||
|
||
func composeCreateAction(cmd *cobra.Command, args []string) error { | ||
build, err := cmd.Flags().GetBool("build") | ||
if err != nil { | ||
return err | ||
} | ||
noBuild, err := cmd.Flags().GetBool("no-build") | ||
if err != nil { | ||
return err | ||
} | ||
if build && noBuild { | ||
return errors.New("flag --build and --no-build cannot be specified together") | ||
} | ||
forceRecreate, err := cmd.Flags().GetBool("force-recreate") | ||
if err != nil { | ||
return err | ||
} | ||
noRecreate, err := cmd.Flags().GetBool("no-recreate") | ||
if err != nil { | ||
return nil | ||
} | ||
if forceRecreate && noRecreate { | ||
return errors.New("flag --force-recreate and --no-recreate cannot be specified together") | ||
} | ||
|
||
client, ctx, cancel, err := newClient(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
defer cancel() | ||
|
||
c, err := getComposer(cmd, client) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
opt := composer.CreateOptions{ | ||
Build: build, | ||
NoBuild: noBuild, | ||
ForceRecreate: forceRecreate, | ||
NoRecreate: noRecreate, | ||
} | ||
|
||
if cmd.Flags().Changed("pull") { | ||
pull, err := cmd.Flags().GetString("pull") | ||
if err != nil { | ||
return err | ||
} | ||
opt.Pull = &pull | ||
} | ||
|
||
return c.Create(ctx, opt, args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/containerd/nerdctl/pkg/testutil" | ||
) | ||
|
||
func TestComposeCreate(t *testing.T) { | ||
// docker-compose v1 depecreated this command | ||
// docker-compose v2 reimplemented this command | ||
testutil.DockerIncompatible(t) | ||
|
||
base := testutil.NewBase(t) | ||
var dockerComposeYAML = fmt.Sprintf(` | ||
version: '3.1' | ||
services: | ||
svc0: | ||
image: %s | ||
`, testutil.AlpineImage) | ||
|
||
comp := testutil.NewComposeDir(t, dockerComposeYAML) | ||
defer comp.CleanUp() | ||
projectName := comp.ProjectName() | ||
t.Logf("projectName=%q", projectName) | ||
|
||
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK() | ||
|
||
// 1.1 `compose create` should create service container (in `created` status) | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "create").AssertOK() | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc0").AssertOutContainsAny("Created", "created") | ||
// 1.2 created container can be started by `compose start` | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "start").AssertOK() | ||
} | ||
|
||
func TestComposeCreateDependency(t *testing.T) { | ||
// docker-compose v1 depecreated this command | ||
// docker-compose v2 reimplemented this command | ||
testutil.DockerIncompatible(t) | ||
|
||
base := testutil.NewBase(t) | ||
var dockerComposeYAML = fmt.Sprintf(` | ||
version: '3.1' | ||
services: | ||
svc0: | ||
image: %s | ||
depends_on: | ||
- "svc1" | ||
svc1: | ||
image: %s | ||
`, testutil.CommonImage, testutil.CommonImage) | ||
|
||
comp := testutil.NewComposeDir(t, dockerComposeYAML) | ||
defer comp.CleanUp() | ||
projectName := comp.ProjectName() | ||
t.Logf("projectName=%q", projectName) | ||
|
||
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK() | ||
|
||
// `compose create` should create containers for both services and their dependencies | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "create", "svc0").AssertOK() | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc0").AssertOutContainsAny("Created", "created") | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc1").AssertOutContainsAny("Created", "created") | ||
} | ||
|
||
func TestComposeCreatePull(t *testing.T) { | ||
// docker-compose v1 depecreated this command | ||
// docker-compose v2 reimplemented this command | ||
testutil.DockerIncompatible(t) | ||
|
||
base := testutil.NewBase(t) | ||
var dockerComposeYAML = fmt.Sprintf(` | ||
version: '3.1' | ||
services: | ||
svc0: | ||
image: %s | ||
`, testutil.AlpineImage) | ||
|
||
comp := testutil.NewComposeDir(t, dockerComposeYAML) | ||
defer comp.CleanUp() | ||
projectName := comp.ProjectName() | ||
t.Logf("projectName=%q", projectName) | ||
|
||
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK() | ||
|
||
// `compose create --pull never` should fail: no such image | ||
base.Cmd("rmi", "-f", testutil.AlpineImage).Run() | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "create", "--pull", "never").AssertFail() | ||
// `compose create --pull missing(default)|always` should succeed: image is pulled and container is created | ||
base.Cmd("rmi", "-f", testutil.AlpineImage).Run() | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "create").AssertOK() | ||
base.Cmd("rmi", "-f", testutil.AlpineImage).Run() | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "create", "--pull", "always").AssertOK() | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc0").AssertOutContainsAny("Created", "created") | ||
} | ||
|
||
func TestComposeCreateBuild(t *testing.T) { | ||
// docker-compose v1 depecreated this command | ||
// docker-compose v2 reimplemented this command | ||
testutil.DockerIncompatible(t) | ||
|
||
const imageSvc0 = "composebuild_svc0" | ||
|
||
dockerComposeYAML := fmt.Sprintf(` | ||
services: | ||
svc0: | ||
build: . | ||
image: %s | ||
`, imageSvc0) | ||
|
||
dockerfile := fmt.Sprintf(`FROM %s`, testutil.AlpineImage) | ||
|
||
testutil.RequiresBuild(t) | ||
base := testutil.NewBase(t) | ||
defer base.Cmd("builder", "prune").Run() | ||
|
||
comp := testutil.NewComposeDir(t, dockerComposeYAML) | ||
defer comp.CleanUp() | ||
comp.WriteFile("Dockerfile", dockerfile) | ||
projectName := comp.ProjectName() | ||
t.Logf("projectName=%q", projectName) | ||
|
||
defer base.Cmd("rmi", imageSvc0).Run() | ||
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK() | ||
|
||
// `compose create --no-build` should fail if service image needs build | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "create", "--no-build").AssertFail() | ||
// `compose create --build` should succeed: image is built and container is created | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "create", "--build").AssertOK() | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "svc0").AssertOutContains(imageSvc0) | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc0").AssertOutContainsAny("Created", "created") | ||
} |
Oops, something went wrong.