github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/compose_build_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 TestComposeBuild(t *testing.T) {
    27  	const imageSvc0 = "composebuild_svc0"
    28  	const imageSvc1 = "composebuild_svc1"
    29  
    30  	dockerComposeYAML := fmt.Sprintf(`
    31  services:
    32    svc0:
    33      build: .
    34      image: %s
    35      ports:
    36      - 8080:80
    37      depends_on:
    38      - svc1
    39    svc1:
    40      build: .
    41      image: %s
    42      ports:
    43      - 8081:80
    44  `, imageSvc0, imageSvc1)
    45  
    46  	dockerfile := fmt.Sprintf(`FROM %s`, testutil.AlpineImage)
    47  
    48  	testutil.RequiresBuild(t)
    49  	base := testutil.NewBase(t)
    50  	defer base.Cmd("builder", "prune").Run()
    51  
    52  	comp := testutil.NewComposeDir(t, dockerComposeYAML)
    53  	defer comp.CleanUp()
    54  	comp.WriteFile("Dockerfile", dockerfile)
    55  	projectName := comp.ProjectName()
    56  	t.Logf("projectName=%q", projectName)
    57  
    58  	defer base.Cmd("rmi", imageSvc0).Run()
    59  	defer base.Cmd("rmi", imageSvc1).Run()
    60  
    61  	// 1. build only 1 service without triggering the dependency service build
    62  	base.ComposeCmd("-f", comp.YAMLFullPath(), "build", "svc0").AssertOK()
    63  	base.Cmd("images").AssertOutContains(imageSvc0)
    64  	base.Cmd("images").AssertOutNotContains(imageSvc1)
    65  	// 2. build multiple services
    66  	base.ComposeCmd("-f", comp.YAMLFullPath(), "build", "svc0", "svc1").AssertOK()
    67  	base.Cmd("images").AssertOutContains(imageSvc0)
    68  	base.Cmd("images").AssertOutContains(imageSvc1)
    69  	// 3. build all if no args are given
    70  	base.ComposeCmd("-f", comp.YAMLFullPath(), "build").AssertOK()
    71  	// 4. fail if some services args not exist in compose.yml
    72  	base.ComposeCmd("-f", comp.YAMLFullPath(), "build", "svc0", "svc100").AssertFail()
    73  }