github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/compose_create_linux_test.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/containerd/nerdctl/pkg/testutil"
    24  )
    25  
    26  func TestComposeCreate(t *testing.T) {
    27  	base := testutil.NewBase(t)
    28  	var dockerComposeYAML = fmt.Sprintf(`
    29  version: '3.1'
    30  
    31  services:
    32    svc0:
    33      image: %s
    34  `, testutil.AlpineImage)
    35  
    36  	comp := testutil.NewComposeDir(t, dockerComposeYAML)
    37  	defer comp.CleanUp()
    38  	projectName := comp.ProjectName()
    39  	t.Logf("projectName=%q", projectName)
    40  
    41  	defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK()
    42  
    43  	// 1.1 `compose create` should create service container (in `created` status)
    44  	base.ComposeCmd("-f", comp.YAMLFullPath(), "create").AssertOK()
    45  	base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc0", "-a").AssertOutContainsAny("Created", "created")
    46  	// 1.2 created container can be started by `compose start`
    47  	base.ComposeCmd("-f", comp.YAMLFullPath(), "start").AssertOK()
    48  }
    49  
    50  func TestComposeCreateDependency(t *testing.T) {
    51  	base := testutil.NewBase(t)
    52  	var dockerComposeYAML = fmt.Sprintf(`
    53  version: '3.1'
    54  
    55  services:
    56    svc0:
    57      image: %s
    58      depends_on:
    59      - "svc1"
    60    svc1:
    61      image: %s
    62  `, testutil.CommonImage, testutil.CommonImage)
    63  
    64  	comp := testutil.NewComposeDir(t, dockerComposeYAML)
    65  	defer comp.CleanUp()
    66  	projectName := comp.ProjectName()
    67  	t.Logf("projectName=%q", projectName)
    68  
    69  	defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK()
    70  
    71  	// `compose create` should create containers for both services and their dependencies
    72  	base.ComposeCmd("-f", comp.YAMLFullPath(), "create", "svc0").AssertOK()
    73  	base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc0", "-a").AssertOutContainsAny("Created", "created")
    74  	base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc1", "-a").AssertOutContainsAny("Created", "created")
    75  }
    76  
    77  func TestComposeCreatePull(t *testing.T) {
    78  
    79  	base := testutil.NewBase(t)
    80  	var dockerComposeYAML = fmt.Sprintf(`
    81  version: '3.1'
    82  
    83  services:
    84    svc0:
    85      image: %s
    86  `, testutil.AlpineImage)
    87  
    88  	comp := testutil.NewComposeDir(t, dockerComposeYAML)
    89  	defer comp.CleanUp()
    90  	projectName := comp.ProjectName()
    91  	t.Logf("projectName=%q", projectName)
    92  
    93  	defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK()
    94  
    95  	// `compose create --pull never` should fail: no such image
    96  	base.Cmd("rmi", "-f", testutil.AlpineImage).Run()
    97  	base.ComposeCmd("-f", comp.YAMLFullPath(), "create", "--pull", "never").AssertFail()
    98  	// `compose create --pull missing(default)|always` should succeed: image is pulled and container is created
    99  	base.Cmd("rmi", "-f", testutil.AlpineImage).Run()
   100  	base.ComposeCmd("-f", comp.YAMLFullPath(), "create").AssertOK()
   101  	base.Cmd("rmi", "-f", testutil.AlpineImage).Run()
   102  	base.ComposeCmd("-f", comp.YAMLFullPath(), "create", "--pull", "always").AssertOK()
   103  	base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc0", "-a").AssertOutContainsAny("Created", "created")
   104  }
   105  
   106  func TestComposeCreateBuild(t *testing.T) {
   107  	const imageSvc0 = "composebuild_svc0"
   108  
   109  	dockerComposeYAML := fmt.Sprintf(`
   110  services:
   111    svc0:
   112      build: .
   113      image: %s
   114  `, imageSvc0)
   115  
   116  	dockerfile := fmt.Sprintf(`FROM %s`, testutil.AlpineImage)
   117  
   118  	testutil.RequiresBuild(t)
   119  	base := testutil.NewBase(t)
   120  	defer base.Cmd("builder", "prune").Run()
   121  
   122  	comp := testutil.NewComposeDir(t, dockerComposeYAML)
   123  	defer comp.CleanUp()
   124  	comp.WriteFile("Dockerfile", dockerfile)
   125  	projectName := comp.ProjectName()
   126  	t.Logf("projectName=%q", projectName)
   127  
   128  	defer base.Cmd("rmi", imageSvc0).Run()
   129  	defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK()
   130  
   131  	// `compose create --no-build` should fail if service image needs build
   132  	base.ComposeCmd("-f", comp.YAMLFullPath(), "create", "--no-build").AssertFail()
   133  	// `compose create --build` should succeed: image is built and container is created
   134  	base.ComposeCmd("-f", comp.YAMLFullPath(), "create", "--build").AssertOK()
   135  	base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "svc0").AssertOutContains(imageSvc0)
   136  	base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc0", "-a").AssertOutContainsAny("Created", "created")
   137  }