github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/compose_start_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 TestComposeStart(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      command: "sleep infinity"
    35    svc1:
    36      image: %s
    37      command: "sleep infinity"
    38  `, testutil.CommonImage, testutil.CommonImage)
    39  
    40  	comp := testutil.NewComposeDir(t, dockerComposeYAML)
    41  	defer comp.CleanUp()
    42  	projectName := comp.ProjectName()
    43  	t.Logf("projectName=%q", projectName)
    44  
    45  	base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
    46  	defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK()
    47  
    48  	// calling `compose start` after all services up has no effect.
    49  	base.ComposeCmd("-f", comp.YAMLFullPath(), "start").AssertOK()
    50  
    51  	// `compose start`` can start a stopped/killed service container
    52  	base.ComposeCmd("-f", comp.YAMLFullPath(), "stop", "--timeout", "1", "svc0").AssertOK()
    53  	base.ComposeCmd("-f", comp.YAMLFullPath(), "kill", "svc1").AssertOK()
    54  	base.ComposeCmd("-f", comp.YAMLFullPath(), "start").AssertOK()
    55  	base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc0").AssertOutContainsAny("Up", "running")
    56  	base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc1").AssertOutContainsAny("Up", "running")
    57  }
    58  
    59  func TestComposeStartFailWhenServicePause(t *testing.T) {
    60  	base := testutil.NewBase(t)
    61  	switch base.Info().CgroupDriver {
    62  	case "none", "":
    63  		t.Skip("requires cgroup (for pausing)")
    64  	}
    65  
    66  	var dockerComposeYAML = fmt.Sprintf(`
    67  version: '3.1'
    68  
    69  services:
    70    svc0:
    71      image: %s
    72      command: "sleep infinity"
    73  `, testutil.CommonImage)
    74  
    75  	comp := testutil.NewComposeDir(t, dockerComposeYAML)
    76  	defer comp.CleanUp()
    77  	projectName := comp.ProjectName()
    78  	t.Logf("projectName=%q", projectName)
    79  
    80  	base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
    81  	defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK()
    82  
    83  	// `compose start` cannot start a paused service container
    84  	base.ComposeCmd("-f", comp.YAMLFullPath(), "pause", "svc0").AssertOK()
    85  	base.ComposeCmd("-f", comp.YAMLFullPath(), "start").AssertFail()
    86  }