github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/compose_rm_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  	"time"
    23  
    24  	"github.com/containerd/nerdctl/pkg/testutil"
    25  )
    26  
    27  func TestComposeRemove(t *testing.T) {
    28  	base := testutil.NewBase(t)
    29  	var dockerComposeYAML = fmt.Sprintf(`
    30  version: '3.1'
    31  
    32  services:
    33  
    34    wordpress:
    35      image: %s
    36      ports:
    37        - 8080:80
    38      environment:
    39        WORDPRESS_DB_HOST: db
    40        WORDPRESS_DB_USER: exampleuser
    41        WORDPRESS_DB_PASSWORD: examplepass
    42        WORDPRESS_DB_NAME: exampledb
    43      volumes:
    44        - wordpress:/var/www/html
    45  
    46    db:
    47      image: %s
    48      environment:
    49        MYSQL_DATABASE: exampledb
    50        MYSQL_USER: exampleuser
    51        MYSQL_PASSWORD: examplepass
    52        MYSQL_RANDOM_ROOT_PASSWORD: '1'
    53      volumes:
    54        - db:/var/lib/mysql
    55  
    56  volumes:
    57    wordpress:
    58    db:
    59  `, testutil.WordpressImage, testutil.MariaDBImage)
    60  
    61  	comp := testutil.NewComposeDir(t, dockerComposeYAML)
    62  	defer comp.CleanUp()
    63  	projectName := comp.ProjectName()
    64  	t.Logf("projectName=%q", projectName)
    65  
    66  	base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
    67  	defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run()
    68  
    69  	// no stopped containers
    70  	base.ComposeCmd("-f", comp.YAMLFullPath(), "rm", "-f").AssertOK()
    71  	time.Sleep(3 * time.Second)
    72  	base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "wordpress").AssertOutContainsAny("Up", "running")
    73  	base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "db").AssertOutContainsAny("Up", "running")
    74  	// remove one stopped service
    75  	base.ComposeCmd("-f", comp.YAMLFullPath(), "stop", "wordpress").AssertOK()
    76  	base.ComposeCmd("-f", comp.YAMLFullPath(), "rm", "-f", "wordpress").AssertOK()
    77  	time.Sleep(3 * time.Second)
    78  	base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "wordpress").AssertOutNotContains("wordpress")
    79  	base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "db").AssertOutContainsAny("Up", "running")
    80  	// remove all services with `--stop`
    81  	base.ComposeCmd("-f", comp.YAMLFullPath(), "rm", "-f", "-s").AssertOK()
    82  	time.Sleep(3 * time.Second)
    83  	base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "db").AssertOutNotContains("db")
    84  }